blob: 6b08b6beeff455c991ed8c04c7d22f20284d8c17 [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 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040028
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010029#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
31
Derek Foremanf8180982014-10-16 16:37:02 -050032#include <stdbool.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030033#include <stdint.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040034#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040035#include <string.h>
36#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030037#include <float.h>
38#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020039#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030040#include <drm_fourcc.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040041
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010042#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080043#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030044#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080045#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010046
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010048#include "shared/platform.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010049#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040050
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010051struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010052 GLuint program;
53 GLuint vertex_shader, fragment_shader;
54 GLint proj_uniform;
55 GLint tex_uniforms[3];
56 GLint alpha_uniform;
57 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030058 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010059};
60
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020061#define BUFFER_DAMAGE_COUNT 2
62
Jason Ekstrande5512d42014-02-04 21:36:38 -060063enum gl_border_status {
64 BORDER_STATUS_CLEAN = 0,
65 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
66 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
67 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
68 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
69 BORDER_ALL_DIRTY = 0xf,
70 BORDER_SIZE_CHANGED = 0x10
71};
72
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050073struct gl_border_image {
74 GLuint tex;
75 int32_t width, height;
76 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050077 void *data;
78};
79
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010080struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010081 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020082 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050083 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060084 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050085 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060086 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050087
88 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010089};
90
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030091enum buffer_type {
92 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020093 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030094 BUFFER_TYPE_SHM,
95 BUFFER_TYPE_EGL
96};
97
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030098struct gl_renderer;
99
100struct egl_image {
101 struct gl_renderer *renderer;
102 EGLImageKHR image;
103 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000104};
Pekka Paalanena3525802014-06-12 16:49:29 +0300105
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000106enum import_type {
107 IMPORT_TYPE_INVALID,
108 IMPORT_TYPE_DIRECT,
109 IMPORT_TYPE_GL_CONVERSION
110};
111
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000112struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300113 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000114 int num_images;
115 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300116 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000117
118 enum import_type import_type;
119 GLenum target;
120 struct gl_shader *shader;
121};
122
123struct yuv_plane_descriptor {
124 int width_divisor;
125 int height_divisor;
126 uint32_t format;
127 int plane_index;
128};
129
130struct yuv_format_descriptor {
131 uint32_t format;
132 int input_planes;
133 int output_planes;
134 int texture_type;
135 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300136};
137
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100138struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100139 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100140 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100141
142 GLuint textures[3];
143 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600144 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200145 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100146
Neil Roberts4d085e72014-04-07 15:01:01 +0100147 /* These are only used by SHM surfaces to detect when we need
148 * to do a full upload to specify a new internal texture
149 * format */
150 GLenum gl_format;
151 GLenum gl_pixel_type;
152
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300153 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100154 GLenum target;
155 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200156
157 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300158 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200159 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200160 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400161 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300162
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200163 /* Extension needed for SHM YUV texture */
164 int offset[3]; /* offset per plane */
165 int hvsub[3]; /* horizontal vertical subsampling per plane */
166
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300167 struct weston_surface *surface;
168
169 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300170 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100171};
172
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100173struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100174 struct weston_renderer base;
175 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400176 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300177 struct weston_binding *fragment_binding;
178 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100179
180 EGLDisplay egl_display;
181 EGLContext egl_context;
182 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100183
Armin Krezović28d240f2016-06-23 11:59:35 +0200184 EGLSurface dummy_surface;
185
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400186 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400187 struct wl_array vtxcnt;
188
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100189 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
190 PFNEGLCREATEIMAGEKHRPROC create_image;
191 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600192 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100193 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100194
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100195 int has_unpack_subimage;
196
197 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
198 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
199 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
200 int has_bind_display;
201
202 int has_egl_image_external;
203
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200204 int has_egl_buffer_age;
205
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000206 int has_configless_context;
207
Armin Krezović28d240f2016-06-23 11:59:35 +0200208 int has_surfaceless_context;
209
Pekka Paalanena3525802014-06-12 16:49:29 +0300210 int has_dmabuf_import;
211 struct wl_list dmabuf_images;
212
Vincent Abrioufdeefe42016-10-05 14:54:34 +0200213 int has_gl_texture_rg;
214
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100215 struct gl_shader texture_shader_rgba;
216 struct gl_shader texture_shader_rgbx;
217 struct gl_shader texture_shader_egl_external;
218 struct gl_shader texture_shader_y_uv;
219 struct gl_shader texture_shader_y_u_v;
220 struct gl_shader texture_shader_y_xuxv;
221 struct gl_shader invert_color_shader;
222 struct gl_shader solid_shader;
223 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300224
225 struct wl_signal destroy_signal;
Armin Krezović36d699a2016-08-05 15:28:30 +0200226
227 struct wl_listener output_destroy_listener;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100228};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100229
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100230static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100231
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000232static inline const char *
233dump_format(uint32_t format, char out[4])
234{
235#if BYTE_ORDER == BIG_ENDIAN
236 format = __builtin_bswap32(format);
237#endif
238 memcpy(out, &format, 4);
239 return out;
240}
241
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100242static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100243get_output_state(struct weston_output *output)
244{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100245 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100246}
247
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300248static int
249gl_renderer_create_surface(struct weston_surface *surface);
250
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100251static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100252get_surface_state(struct weston_surface *surface)
253{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300254 if (!surface->renderer_state)
255 gl_renderer_create_surface(surface);
256
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100257 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100258}
259
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100260static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100261get_renderer(struct weston_compositor *ec)
262{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100263 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100264}
265
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300266static struct egl_image*
267egl_image_create(struct gl_renderer *gr, EGLenum target,
268 EGLClientBuffer buffer, const EGLint *attribs)
269{
270 struct egl_image *img;
271
272 img = zalloc(sizeof *img);
273 img->renderer = gr;
274 img->refcount = 1;
275 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
276 target, buffer, attribs);
277
278 if (img->image == EGL_NO_IMAGE_KHR) {
279 free(img);
280 return NULL;
281 }
282
283 return img;
284}
285
286static struct egl_image*
287egl_image_ref(struct egl_image *image)
288{
289 image->refcount++;
290
291 return image;
292}
293
294static int
295egl_image_unref(struct egl_image *image)
296{
297 struct gl_renderer *gr = image->renderer;
298
299 assert(image->refcount > 0);
300
301 image->refcount--;
302 if (image->refcount > 0)
303 return image->refcount;
304
305 gr->destroy_image(gr->egl_display, image->image);
306 free(image);
307
308 return 0;
309}
310
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000311static struct dmabuf_image*
312dmabuf_image_create(void)
313{
314 struct dmabuf_image *img;
315
316 img = zalloc(sizeof *img);
317 wl_list_init(&img->link);
318
319 return img;
320}
321
322static void
323dmabuf_image_destroy(struct dmabuf_image *image)
324{
325 int i;
326
327 for (i = 0; i < image->num_images; ++i)
328 egl_image_unref(image->images[i]);
329
330 if (image->dmabuf)
331 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
332
333 wl_list_remove(&image->link);
334}
335
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400336static const char *
337egl_error_string(EGLint code)
338{
339#define MYERRCODE(x) case x: return #x;
340 switch (code) {
341 MYERRCODE(EGL_SUCCESS)
342 MYERRCODE(EGL_NOT_INITIALIZED)
343 MYERRCODE(EGL_BAD_ACCESS)
344 MYERRCODE(EGL_BAD_ALLOC)
345 MYERRCODE(EGL_BAD_ATTRIBUTE)
346 MYERRCODE(EGL_BAD_CONTEXT)
347 MYERRCODE(EGL_BAD_CONFIG)
348 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
349 MYERRCODE(EGL_BAD_DISPLAY)
350 MYERRCODE(EGL_BAD_SURFACE)
351 MYERRCODE(EGL_BAD_MATCH)
352 MYERRCODE(EGL_BAD_PARAMETER)
353 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
354 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
355 MYERRCODE(EGL_CONTEXT_LOST)
356 default:
357 return "unknown";
358 }
359#undef MYERRCODE
360}
361
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300362static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200363gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400364{
365 EGLint code;
366
367 code = eglGetError();
368 weston_log("EGL error state: %s (0x%04lx)\n",
369 egl_error_string(code), (long)code);
370}
371
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400372#define max(a, b) (((a) > (b)) ? (a) : (b))
373#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400374
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300375/*
376 * Compute the boundary vertices of the intersection of the global coordinate
377 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
378 * 'surf_rect' when transformed from surface coordinates into global coordinates.
379 * The vertices are written to 'ex' and 'ey', and the return value is the
380 * number of vertices. Vertices are produced in clockwise winding order.
381 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
382 * polygon area.
383 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400384static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400386 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
387{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800388
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300389 struct clip_context ctx;
390 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400391 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300392 struct polygon8 surf = {
393 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
394 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
395 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400396 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400397
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300398 ctx.clip.x1 = rect->x1;
399 ctx.clip.y1 = rect->y1;
400 ctx.clip.x2 = rect->x2;
401 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400402
403 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300404 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
406 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400407
408 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300409 min_x = max_x = surf.x[0];
410 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400411
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300412 for (i = 1; i < surf.n; i++) {
413 min_x = min(min_x, surf.x[i]);
414 max_x = max(max_x, surf.x[i]);
415 min_y = min(min_y, surf.y[i]);
416 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400417 }
418
419 /* First, simple bounding box check to discard early transformed
420 * surface rects that do not intersect with the clip region:
421 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300422 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
423 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400424 return 0;
425
426 /* Simple case, bounding box edges are parallel to surface edges,
427 * there will be only four edges. We just need to clip the surface
428 * vertices to the clip rect bounds:
429 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500430 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800431 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400432
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300433 /* Transformed case: use a general polygon clipping algorithm to
434 * clip the surface rectangle with each side of 'rect'.
435 * The algorithm is Sutherland-Hodgman, as explained in
436 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
437 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400438 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800439 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300440
441 if (n < 3)
442 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400443
444 return n;
445}
446
Derek Foremanf8180982014-10-16 16:37:02 -0500447static bool
448merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
449{
450 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
451 merge->x1 = a->x1;
452 merge->x2 = a->x2;
453 merge->y1 = b->y1;
454 merge->y2 = a->y2;
455 return true;
456 }
457 return false;
458}
459
460static int
461compress_bands(pixman_box32_t *inrects, int nrects,
462 pixman_box32_t **outrects)
463{
Quentin Glidicd84af7c2016-07-10 11:00:50 +0200464 bool merged = false;
Derek Foremanf8180982014-10-16 16:37:02 -0500465 pixman_box32_t *out, merge_rect;
466 int i, j, nout;
467
468 if (!nrects) {
469 *outrects = NULL;
470 return 0;
471 }
472
473 /* nrects is an upper bound - we're not too worried about
474 * allocating a little extra
475 */
476 out = malloc(sizeof(pixman_box32_t) * nrects);
477 out[0] = inrects[0];
478 nout = 1;
479 for (i = 1; i < nrects; i++) {
480 for (j = 0; j < nout; j++) {
481 merged = merge_down(&inrects[i], &out[j], &merge_rect);
482 if (merged) {
483 out[j] = merge_rect;
484 break;
485 }
486 }
487 if (!merged) {
488 out[nout] = inrects[i];
489 nout++;
490 }
491 }
492 *outrects = out;
493 return nout;
494}
495
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400496static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400498 pixman_region32_t *surf_region)
499{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 struct gl_surface_state *gs = get_surface_state(ev->surface);
501 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400502 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400503 GLfloat *v, inv_width, inv_height;
504 unsigned int *vtxcnt, nvtx = 0;
505 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500506 pixman_box32_t *raw_rects;
507 int i, j, k, nrects, nsurf, raw_nrects;
508 bool used_band_compression;
509 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400510 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
511
Derek Foremanf8180982014-10-16 16:37:02 -0500512 if (raw_nrects < 4) {
513 used_band_compression = false;
514 nrects = raw_nrects;
515 rects = raw_rects;
516 } else {
517 nrects = compress_bands(raw_rects, raw_nrects, &rects);
518 used_band_compression = true;
519 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400520 /* worst case we can have 8 vertices per rect (ie. clipped into
521 * an octagon):
522 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400523 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
524 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400525
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200526 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200527 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528
529 for (i = 0; i < nrects; i++) {
530 pixman_box32_t *rect = &rects[i];
531 for (j = 0; j < nsurf; j++) {
532 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200533 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400534 GLfloat ex[8], ey[8]; /* edge points in screen space */
535 int n;
536
537 /* The transformed surface, after clipping to the clip region,
538 * can have as many as eight sides, emitted as a triangle-fan.
539 * The first vertex in the triangle fan can be chosen arbitrarily,
540 * since the area is guaranteed to be convex.
541 *
542 * If a corner of the transformed surface falls outside of the
543 * clip region, instead of emitting one vertex for the corner
544 * of the surface, up to two are emitted for two corresponding
545 * intersection point(s) between the surface and the clip region.
546 *
547 * To do this, we first calculate the (up to eight) points that
548 * form the intersection of the clip rect and the transformed
549 * surface.
550 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400552 if (n < 3)
553 continue;
554
555 /* emit edge points: */
556 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_view_from_global_float(ev, ex[k], ey[k],
558 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400559 /* position: */
560 *(v++) = ex[k];
561 *(v++) = ey[k];
562 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 weston_surface_to_buffer_float(ev->surface,
564 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200565 &bx, &by);
566 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400567 if (gs->y_inverted) {
568 *(v++) = by * inv_height;
569 } else {
570 *(v++) = (gs->height - by) * inv_height;
571 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400572 }
573
574 vtxcnt[nvtx++] = n;
575 }
576 }
577
Derek Foremanf8180982014-10-16 16:37:02 -0500578 if (used_band_compression)
579 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400580 return nvtx;
581}
582
583static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100587 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400588 int i;
589 GLushort *buffer;
590 GLushort *index;
591 int nelems;
592 static int color_idx = 0;
593 static const GLfloat color[][4] = {
594 { 1.0, 0.0, 0.0, 1.0 },
595 { 0.0, 1.0, 0.0, 1.0 },
596 { 0.0, 0.0, 1.0, 1.0 },
597 { 1.0, 1.0, 1.0, 1.0 },
598 };
599
600 nelems = (count - 1 + count - 2) * 2;
601
602 buffer = malloc(sizeof(GLushort) * nelems);
603 index = buffer;
604
605 for (i = 1; i < count; i++) {
606 *index++ = first;
607 *index++ = first + i;
608 }
609
610 for (i = 2; i < count; i++) {
611 *index++ = first + i - 1;
612 *index++ = first + i;
613 }
614
John Kåre Alsaker40684142012-11-13 19:10:25 +0100615 glUseProgram(gr->solid_shader.program);
616 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400617 color[color_idx++ % ARRAY_LENGTH(color)]);
618 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100619 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400620 free(buffer);
621}
622
623static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400625 pixman_region32_t *surf_region)
626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400628 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400629 GLfloat *v;
630 unsigned int *vtxcnt;
631 int i, first, nfans;
632
633 /* The final region to be painted is the intersection of
634 * 'region' and 'surf_region'. However, 'region' is in the global
635 * coordinates, and 'surf_region' is in the surface-local
636 * coordinates. texture_region() will iterate over all pairs of
637 * rectangles from both regions, compute the intersection
638 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800639 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400640 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400642
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400643 v = gr->vertices.data;
644 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400645
646 /* position: */
647 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
648 glEnableVertexAttribArray(0);
649
650 /* texcoord: */
651 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
652 glEnableVertexAttribArray(1);
653
654 for (i = 0, first = 0; i < nfans; i++) {
655 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400656 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400658 first += vtxcnt[i];
659 }
660
661 glDisableVertexAttribArray(1);
662 glDisableVertexAttribArray(0);
663
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400664 gr->vertices.size = 0;
665 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400666}
667
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100668static int
669use_output(struct weston_output *output)
670{
671 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100672 struct gl_output_state *go = get_output_state(output);
673 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100674 EGLBoolean ret;
675
676 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
677 go->egl_surface, gr->egl_context);
678
679 if (ret == EGL_FALSE) {
680 if (errored)
681 return -1;
682 errored = 1;
683 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200684 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100685 return -1;
686 }
687
688 return 0;
689}
690
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300691static int
692shader_init(struct gl_shader *shader, struct gl_renderer *gr,
693 const char *vertex_source, const char *fragment_source);
694
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400695static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300696use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400697{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300698 if (!shader->program) {
699 int ret;
700
701 ret = shader_init(shader, gr,
702 shader->vertex_source,
703 shader->fragment_source);
704
705 if (ret < 0)
706 weston_log("warning: failed to compile shader\n");
707 }
708
John Kåre Alsaker40684142012-11-13 19:10:25 +0100709 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400710 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400711 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100712 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713}
714
715static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100716shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 struct weston_view *view,
718 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400719{
720 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500722 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400723
724 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500725 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100726 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400728
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100729 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400730 glUniform1i(shader->tex_uniforms[i], i);
731}
732
733static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500734draw_view(struct weston_view *ev, struct weston_output *output,
735 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400736{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100738 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500739 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400740 /* repaint bounding region in global coordinates: */
741 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200742 /* opaque region in surface coordinates: */
743 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400744 /* non-opaque region in surface coordinates: */
745 pixman_region32_t surface_blend;
746 GLint filter;
747 int i;
748
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200749 /* In case of a runtime switch of renderers, we may not have received
750 * an attach for this surface since the switch. In that case we don't
751 * have a valid buffer or a proper shader set up so skip rendering. */
752 if (!gs->shader)
753 return;
754
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400755 pixman_region32_init(&repaint);
756 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200757 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500758 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400759
760 if (!pixman_region32_not_empty(&repaint))
761 goto out;
762
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400763 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
764
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400765 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100766 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400768 }
769
John Kåre Alsaker40684142012-11-13 19:10:25 +0100770 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500771 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400772
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200774 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 filter = GL_LINEAR;
776 else
777 filter = GL_NEAREST;
778
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100779 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400780 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100781 glBindTexture(gs->target, gs->textures[i]);
782 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
783 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784 }
785
786 /* blended region is whole surface minus opaque region: */
787 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600788 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200789 if (ev->geometry.scissor_enabled)
790 pixman_region32_intersect(&surface_blend, &surface_blend,
791 &ev->geometry.scissor);
792 pixman_region32_subtract(&surface_blend, &surface_blend,
793 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200796 pixman_region32_init(&surface_opaque);
797 if (ev->geometry.scissor_enabled)
798 pixman_region32_intersect(&surface_opaque,
799 &ev->surface->opaque,
800 &ev->geometry.scissor);
801 else
802 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
803
804 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100805 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 /* Special case for RGBA textures with possibly
807 * bad data in alpha channel: use the shader
808 * that forces texture alpha = 1.0.
809 * Xwayland surfaces need this.
810 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100811 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500812 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400813 }
814
Jason Ekstranda7af7042013-10-12 22:38:11 -0500815 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400816 glEnable(GL_BLEND);
817 else
818 glDisable(GL_BLEND);
819
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200820 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400821 }
822
823 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100824 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400825 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400827 }
828
829 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200830 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400831
832out:
833 pixman_region32_fini(&repaint);
834}
835
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400836static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500837repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400838{
839 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400841
Jason Ekstranda7af7042013-10-12 22:38:11 -0500842 wl_list_for_each_reverse(view, &compositor->view_list, link)
843 if (view->plane == &compositor->primary_plane)
844 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400845}
846
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500847static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600848draw_output_border_texture(struct gl_output_state *go,
849 enum gl_renderer_border_side side,
850 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500851 int32_t width, int32_t height)
852{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600853 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500854 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
855
856 if (!img->data) {
857 if (img->tex) {
858 glDeleteTextures(1, &img->tex);
859 img->tex = 0;
860 }
861
862 return;
863 }
864
865 if (!img->tex) {
866 glGenTextures(1, &img->tex);
867 glBindTexture(GL_TEXTURE_2D, img->tex);
868
869 glTexParameteri(GL_TEXTURE_2D,
870 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
871 glTexParameteri(GL_TEXTURE_2D,
872 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
873 glTexParameteri(GL_TEXTURE_2D,
874 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
875 glTexParameteri(GL_TEXTURE_2D,
876 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
877 } else {
878 glBindTexture(GL_TEXTURE_2D, img->tex);
879 }
880
Jason Ekstrande5512d42014-02-04 21:36:38 -0600881 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500882 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
883 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
884 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500885 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
886 img->tex_width, img->height, 0,
887 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500888 }
889
890 GLfloat texcoord[] = {
891 0.0f, 0.0f,
892 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
893 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
894 0.0f, 1.0f,
895 };
896
897 GLfloat verts[] = {
898 x, y,
899 x + width, y,
900 x + width, y + height,
901 x, y + height
902 };
903
904 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
905 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
906 glEnableVertexAttribArray(0);
907 glEnableVertexAttribArray(1);
908
909 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
910
911 glDisableVertexAttribArray(1);
912 glDisableVertexAttribArray(0);
913}
914
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600915static int
916output_has_borders(struct weston_output *output)
917{
918 struct gl_output_state *go = get_output_state(output);
919
920 return go->borders[GL_RENDERER_BORDER_TOP].data ||
921 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
922 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
923 go->borders[GL_RENDERER_BORDER_LEFT].data;
924}
925
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500926static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600927draw_output_borders(struct weston_output *output,
928 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500929{
930 struct gl_output_state *go = get_output_state(output);
931 struct gl_renderer *gr = get_renderer(output->compositor);
932 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500933 struct gl_border_image *top, *bottom, *left, *right;
934 struct weston_matrix matrix;
935 int full_width, full_height;
936
Jason Ekstrande5512d42014-02-04 21:36:38 -0600937 if (border_status == BORDER_STATUS_CLEAN)
938 return; /* Clean. Nothing to do. */
939
Jason Ekstrand00b84282013-10-27 22:24:59 -0500940 top = &go->borders[GL_RENDERER_BORDER_TOP];
941 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
942 left = &go->borders[GL_RENDERER_BORDER_LEFT];
943 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
944
945 full_width = output->current_mode->width + left->width + right->width;
946 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500947
948 glDisable(GL_BLEND);
949 use_shader(gr, shader);
950
Jason Ekstrand00b84282013-10-27 22:24:59 -0500951 glViewport(0, 0, full_width, full_height);
952
953 weston_matrix_init(&matrix);
954 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
955 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
956 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500957
958 glUniform1i(shader->tex_uniforms[0], 0);
959 glUniform1f(shader->alpha_uniform, 1);
960 glActiveTexture(GL_TEXTURE0);
961
Jason Ekstrande5512d42014-02-04 21:36:38 -0600962 if (border_status & BORDER_TOP_DIRTY)
963 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
964 0, 0,
965 full_width, top->height);
966 if (border_status & BORDER_LEFT_DIRTY)
967 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
968 0, top->height,
969 left->width, output->current_mode->height);
970 if (border_status & BORDER_RIGHT_DIRTY)
971 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
972 full_width - right->width, top->height,
973 right->width, output->current_mode->height);
974 if (border_status & BORDER_BOTTOM_DIRTY)
975 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
976 0, full_height - bottom->height,
977 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500978}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100979
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400980static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600981output_get_border_damage(struct weston_output *output,
982 enum gl_border_status border_status,
983 pixman_region32_t *damage)
984{
985 struct gl_output_state *go = get_output_state(output);
986 struct gl_border_image *top, *bottom, *left, *right;
987 int full_width, full_height;
988
989 if (border_status == BORDER_STATUS_CLEAN)
990 return; /* Clean. Nothing to do. */
991
992 top = &go->borders[GL_RENDERER_BORDER_TOP];
993 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
994 left = &go->borders[GL_RENDERER_BORDER_LEFT];
995 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
996
997 full_width = output->current_mode->width + left->width + right->width;
998 full_height = output->current_mode->height + top->height + bottom->height;
999 if (border_status & BORDER_TOP_DIRTY)
1000 pixman_region32_union_rect(damage, damage,
1001 0, 0,
1002 full_width, top->height);
1003 if (border_status & BORDER_LEFT_DIRTY)
1004 pixman_region32_union_rect(damage, damage,
1005 0, top->height,
1006 left->width, output->current_mode->height);
1007 if (border_status & BORDER_RIGHT_DIRTY)
1008 pixman_region32_union_rect(damage, damage,
1009 full_width - right->width, top->height,
1010 right->width, output->current_mode->height);
1011 if (border_status & BORDER_BOTTOM_DIRTY)
1012 pixman_region32_union_rect(damage, damage,
1013 0, full_height - bottom->height,
1014 full_width, bottom->height);
1015}
1016
1017static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001018output_get_damage(struct weston_output *output,
1019 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001020{
1021 struct gl_output_state *go = get_output_state(output);
1022 struct gl_renderer *gr = get_renderer(output->compositor);
1023 EGLint buffer_age = 0;
1024 EGLBoolean ret;
1025 int i;
1026
1027 if (gr->has_egl_buffer_age) {
1028 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1029 EGL_BUFFER_AGE_EXT, &buffer_age);
1030 if (ret == EGL_FALSE) {
1031 weston_log("buffer age query failed.\n");
1032 gl_renderer_print_egl_error_state();
1033 }
1034 }
1035
Jason Ekstrande5512d42014-02-04 21:36:38 -06001036 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001037 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001038 *border_damage = BORDER_ALL_DIRTY;
1039 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001040 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001041 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001042
1043 if (*border_damage & BORDER_SIZE_CHANGED) {
1044 /* If we've had a resize, we have to do a full
1045 * repaint. */
1046 *border_damage |= BORDER_ALL_DIRTY;
1047 pixman_region32_copy(buffer_damage, &output->region);
1048 } else {
1049 for (i = 0; i < buffer_age - 1; i++)
1050 pixman_region32_union(buffer_damage,
1051 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001052 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001053 }
1054 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055}
1056
1057static void
1058output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001059 pixman_region32_t *output_damage,
1060 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001061{
1062 struct gl_output_state *go = get_output_state(output);
1063 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001064
1065 if (!gr->has_egl_buffer_age)
1066 return;
1067
Derek Foreman4c582662014-10-09 18:39:44 -05001068 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1069 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001070
Derek Foreman4c582662014-10-09 18:39:44 -05001071 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1072 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001073}
1074
Derek Foremanc4cfe852015-05-15 12:12:40 -05001075/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1076 * unavailable, so we're assuming the background has no transparency
1077 * and that everything with a blend, like drop shadows, will have something
1078 * opaque (like the background) drawn underneath it.
1079 *
1080 * Depending on the underlying hardware, violating that assumption could
1081 * result in seeing through to another display plane.
1082 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001083static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001084gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001085 pixman_region32_t *output_damage)
1086{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001087 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001088 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001089 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001090 EGLBoolean ret;
1091 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001092 int i, nrects, buffer_height;
1093 EGLint *egl_damage, *d;
1094 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001095 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001096 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001097
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001098 if (use_output(output) < 0)
1099 return;
1100
Jason Ekstrand00b84282013-10-27 22:24:59 -05001101 /* Calculate the viewport */
1102 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1103 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1104 output->current_mode->width,
1105 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001106
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001107 /* Calculate the global GL matrix */
1108 go->output_matrix = output->matrix;
1109 weston_matrix_translate(&go->output_matrix,
1110 -(output->current_mode->width / 2.0),
1111 -(output->current_mode->height / 2.0), 0);
1112 weston_matrix_scale(&go->output_matrix,
1113 2.0 / output->current_mode->width,
1114 -2.0 / output->current_mode->height, 1);
1115
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001116 /* if debugging, redraw everything outside the damage to clean up
1117 * debug lines from the previous draw on this buffer:
1118 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001119 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001120 pixman_region32_t undamaged;
1121 pixman_region32_init(&undamaged);
1122 pixman_region32_subtract(&undamaged, &output->region,
1123 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001124 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001126 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001127 pixman_region32_fini(&undamaged);
1128 }
1129
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001130 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001131 pixman_region32_init(&buffer_damage);
1132
Jason Ekstrande5512d42014-02-04 21:36:38 -06001133 output_get_damage(output, &buffer_damage, &border_damage);
1134 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001135
1136 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001137 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001140
1141 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001142 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001143
Jason Ekstrande5512d42014-02-04 21:36:38 -06001144 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001145
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001146 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001147 wl_signal_emit(&output->frame_signal, output);
1148
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001149 if (gr->swap_buffers_with_damage) {
1150 pixman_region32_init(&buffer_damage);
1151 weston_transformed_region(output->width, output->height,
1152 output->transform,
1153 output->current_scale,
1154 output_damage, &buffer_damage);
1155
1156 if (output_has_borders(output)) {
1157 pixman_region32_translate(&buffer_damage,
1158 go->borders[GL_RENDERER_BORDER_LEFT].width,
1159 go->borders[GL_RENDERER_BORDER_TOP].height);
1160 output_get_border_damage(output, go->border_status,
1161 &buffer_damage);
1162 }
1163
1164 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1165 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1166
1167 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1168 output->current_mode->height +
1169 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1170
1171 d = egl_damage;
1172 for (i = 0; i < nrects; ++i) {
1173 *d++ = rects[i].x1;
1174 *d++ = buffer_height - rects[i].y2;
1175 *d++ = rects[i].x2 - rects[i].x1;
1176 *d++ = rects[i].y2 - rects[i].y1;
1177 }
1178 ret = gr->swap_buffers_with_damage(gr->egl_display,
1179 go->egl_surface,
1180 egl_damage, nrects);
1181 free(egl_damage);
1182 pixman_region32_fini(&buffer_damage);
1183 } else {
1184 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1185 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001186
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001187 if (ret == EGL_FALSE && !errored) {
1188 errored = 1;
1189 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001190 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001191 }
1192
Jason Ekstrande5512d42014-02-04 21:36:38 -06001193 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001194}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001195
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001196static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001197gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001198 pixman_format_code_t format, void *pixels,
1199 uint32_t x, uint32_t y,
1200 uint32_t width, uint32_t height)
1201{
1202 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001203 struct gl_output_state *go = get_output_state(output);
1204
1205 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1206 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001207
1208 switch (format) {
1209 case PIXMAN_a8r8g8b8:
1210 gl_format = GL_BGRA_EXT;
1211 break;
1212 case PIXMAN_a8b8g8r8:
1213 gl_format = GL_RGBA;
1214 break;
1215 default:
1216 return -1;
1217 }
1218
1219 if (use_output(output) < 0)
1220 return -1;
1221
1222 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1223 glReadPixels(x, y, width, height, gl_format,
1224 GL_UNSIGNED_BYTE, pixels);
1225
1226 return 0;
1227}
1228
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001229static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001230gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001232 struct gl_renderer *gr = get_renderer(surface->compositor);
1233 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001234 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001236 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001237 pixman_box32_t *rectangles;
1238 void *data;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001239 int i, j, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001240
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001241 pixman_region32_union(&gs->texture_damage,
1242 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001243
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001244 if (!buffer)
1245 return;
1246
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001247 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001248 * We still accumulate the damage in texture_damage, and
1249 * hold the reference to the buffer, in case the surface
1250 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001251 */
Derek Foreman97746792015-11-18 16:32:28 -06001252 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 wl_list_for_each(view, &surface->views, surface_link) {
1254 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001255 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 break;
1257 }
1258 }
1259 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001260 return;
1261
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001262 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1263 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001264 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001265
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001266 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001267
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001268 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001269 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001270 for (j = 0; j < gs->num_textures; j++) {
1271 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1272 glTexImage2D(GL_TEXTURE_2D, 0,
1273 gs->gl_format,
1274 gs->pitch / gs->hvsub[j],
1275 buffer->height / gs->hvsub[j],
1276 0,
1277 gs->gl_format,
1278 gs->gl_pixel_type,
1279 data + gs->offset[j]);
1280 }
Neil Robertse5051712013-11-13 15:44:06 +00001281 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001282
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001283 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001284 }
1285
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001286 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001287
1288 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001289 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1290 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001291 wl_shm_buffer_begin_access(buffer->shm_buffer);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001292 for (j = 0; j < gs->num_textures; j++) {
1293 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1294 glTexImage2D(GL_TEXTURE_2D, 0,
1295 gs->gl_format,
1296 gs->pitch / gs->hvsub[j],
1297 buffer->height / gs->hvsub[j],
1298 0,
1299 gs->gl_format,
1300 gs->gl_pixel_type,
1301 data + gs->offset[j]);
1302 }
Neil Robertse5051712013-11-13 15:44:06 +00001303 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001304 goto done;
1305 }
1306
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001307 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001308 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001309 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001310 pixman_box32_t r;
1311
1312 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1313
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001314 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1315 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001316 for (j = 0; j < gs->num_textures; j++) {
1317 glBindTexture(GL_TEXTURE_2D, gs->textures[j]);
1318 glTexSubImage2D(GL_TEXTURE_2D, 0,
1319 r.x1 / gs->hvsub[j],
1320 r.y1 / gs->hvsub[j],
1321 (r.x2 - r.x1) / gs->hvsub[j],
1322 (r.y2 - r.y1) / gs->hvsub[j],
1323 gs->gl_format,
1324 gs->gl_pixel_type,
1325 data + gs->offset[j]);
1326 }
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001327 }
Neil Robertse5051712013-11-13 15:44:06 +00001328 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001329
1330done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001331 pixman_region32_fini(&gs->texture_damage);
1332 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001333 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001334
1335 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001336}
1337
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001338static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001339ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001340{
1341 int i;
1342
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001343 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001344 return;
1345
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001346 for (i = gs->num_textures; i < num_textures; i++) {
1347 glGenTextures(1, &gs->textures[i]);
1348 glBindTexture(gs->target, gs->textures[i]);
1349 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001350 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001351 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001352 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1353 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001354 gs->num_textures = num_textures;
1355 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001356}
1357
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001358static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001359gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1360 struct wl_shm_buffer *shm_buffer)
1361{
1362 struct weston_compositor *ec = es->compositor;
1363 struct gl_renderer *gr = get_renderer(ec);
1364 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001365 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001366 int pitch;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001367 int num_planes;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001368
1369 buffer->shm_buffer = shm_buffer;
1370 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1371 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1372
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001373 num_planes = 1;
1374 gs->offset[0] = 0;
1375 gs->hvsub[0] = 1;
1376
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001377 switch (wl_shm_buffer_get_format(shm_buffer)) {
1378 case WL_SHM_FORMAT_XRGB8888:
1379 gs->shader = &gr->texture_shader_rgbx;
1380 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001381 gl_format = GL_BGRA_EXT;
1382 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001383 break;
1384 case WL_SHM_FORMAT_ARGB8888:
1385 gs->shader = &gr->texture_shader_rgba;
1386 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001387 gl_format = GL_BGRA_EXT;
1388 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001389 break;
1390 case WL_SHM_FORMAT_RGB565:
1391 gs->shader = &gr->texture_shader_rgbx;
1392 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001393 gl_format = GL_RGB;
1394 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001395 break;
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001396 case WL_SHM_FORMAT_YUV420:
1397 gs->shader = &gr->texture_shader_y_u_v;
1398 pitch = wl_shm_buffer_get_stride(shm_buffer);
1399 if (gr->has_gl_texture_rg)
1400 gl_format = GL_R8_EXT;
1401 else
1402 gl_format = GL_LUMINANCE;
1403 gl_pixel_type = GL_UNSIGNED_BYTE;
1404 num_planes = 3;
1405 gs->offset[1] = gs->offset[0] + (pitch / gs->hvsub[0]) *
1406 (buffer->height / gs->hvsub[0]);
1407 gs->hvsub[1] = 2;
1408 gs->offset[2] = gs->offset[1] + (pitch / gs->hvsub[1]) *
1409 (buffer->height / gs->hvsub[1]);
1410 gs->hvsub[2] = 2;
1411 break;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001412 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001413 weston_log("warning: unknown shm buffer format: %08x\n",
1414 wl_shm_buffer_get_format(shm_buffer));
1415 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001416 }
1417
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001418 /* Only allocate a texture if it doesn't match existing one.
1419 * If a switch from DRM allocated buffer to a SHM buffer is
1420 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001421 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001422 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001423 gl_format != gs->gl_format ||
1424 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001425 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001426 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001427 gs->height = buffer->height;
1428 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001429 gs->gl_format = gl_format;
1430 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001431 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001432 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001433 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001434
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001435 gs->surface = es;
1436
Vincent Abrioufdeefe42016-10-05 14:54:34 +02001437 ensure_textures(gs, num_planes);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001438 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001439}
1440
1441static void
1442gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1443 uint32_t format)
1444{
1445 struct weston_compositor *ec = es->compositor;
1446 struct gl_renderer *gr = get_renderer(ec);
1447 struct gl_surface_state *gs = get_surface_state(es);
1448 EGLint attribs[3];
1449 int i, num_planes;
1450
1451 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1452 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1453 EGL_WIDTH, &buffer->width);
1454 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1455 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001456 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1457 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001458
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001459 for (i = 0; i < gs->num_images; i++) {
1460 egl_image_unref(gs->images[i]);
1461 gs->images[i] = NULL;
1462 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001463 gs->num_images = 0;
1464 gs->target = GL_TEXTURE_2D;
1465 switch (format) {
1466 case EGL_TEXTURE_RGB:
1467 case EGL_TEXTURE_RGBA:
1468 default:
1469 num_planes = 1;
1470 gs->shader = &gr->texture_shader_rgba;
1471 break;
1472 case EGL_TEXTURE_EXTERNAL_WL:
1473 num_planes = 1;
1474 gs->target = GL_TEXTURE_EXTERNAL_OES;
1475 gs->shader = &gr->texture_shader_egl_external;
1476 break;
1477 case EGL_TEXTURE_Y_UV_WL:
1478 num_planes = 2;
1479 gs->shader = &gr->texture_shader_y_uv;
1480 break;
1481 case EGL_TEXTURE_Y_U_V_WL:
1482 num_planes = 3;
1483 gs->shader = &gr->texture_shader_y_u_v;
1484 break;
1485 case EGL_TEXTURE_Y_XUXV_WL:
1486 num_planes = 2;
1487 gs->shader = &gr->texture_shader_y_xuxv;
1488 break;
1489 }
1490
1491 ensure_textures(gs, num_planes);
1492 for (i = 0; i < num_planes; i++) {
1493 attribs[0] = EGL_WAYLAND_PLANE_WL;
1494 attribs[1] = i;
1495 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001496 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001497 EGL_WAYLAND_BUFFER_WL,
1498 buffer->legacy_buffer,
1499 attribs);
1500 if (!gs->images[i]) {
1501 weston_log("failed to create img for plane %d\n", i);
1502 continue;
1503 }
1504 gs->num_images++;
1505
1506 glActiveTexture(GL_TEXTURE0 + i);
1507 glBindTexture(gs->target, gs->textures[i]);
1508 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001509 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001510 }
1511
1512 gs->pitch = buffer->width;
1513 gs->height = buffer->height;
1514 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001515 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001516}
1517
1518static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001519gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1520{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001521 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001522
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001523 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001524}
1525
1526static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001527import_simple_dmabuf(struct gl_renderer *gr,
1528 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001529{
1530 struct egl_image *image;
1531 EGLint attribs[30];
1532 int atti = 0;
1533
Pekka Paalanena3525802014-06-12 16:49:29 +03001534 /* This requires the Mesa commit in
1535 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1536 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1537 * Otherwise Mesa closes the fd behind our back and re-importing
1538 * will fail.
1539 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1540 */
1541
1542 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001543 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001544 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001545 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001546 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001547 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001548 /* XXX: Add modifier here when supported */
1549
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001550 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001551 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001552 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001553 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001554 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001555 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001556 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001557 }
1558
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001559 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001560 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001561 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001562 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001563 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001564 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001565 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001566 }
1567
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001568 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001569 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001570 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001571 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001572 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001573 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001574 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001575 }
1576
1577 attribs[atti++] = EGL_NONE;
1578
1579 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1580 attribs);
1581
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001582 return image;
1583}
Pekka Paalanena3525802014-06-12 16:49:29 +03001584
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001585/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1586 * some of the definitions here so that building Weston won't require
1587 * bleeding-edge kernel headers.
1588 */
1589#ifndef DRM_FORMAT_R8
1590#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1591#endif
1592
1593#ifndef DRM_FORMAT_GR88
1594#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1595#endif
1596
1597struct yuv_format_descriptor yuv_formats[] = {
1598 {
1599 .format = DRM_FORMAT_YUYV,
1600 .input_planes = 1,
1601 .output_planes = 2,
1602 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1603 {{
1604 .width_divisor = 1,
1605 .height_divisor = 1,
1606 .format = DRM_FORMAT_GR88,
1607 .plane_index = 0
1608 }, {
1609 .width_divisor = 2,
1610 .height_divisor = 1,
1611 .format = DRM_FORMAT_ARGB8888,
1612 .plane_index = 0
1613 }}
1614 }, {
1615 .format = DRM_FORMAT_NV12,
1616 .input_planes = 2,
1617 .output_planes = 2,
1618 .texture_type = EGL_TEXTURE_Y_UV_WL,
1619 {{
1620 .width_divisor = 1,
1621 .height_divisor = 1,
1622 .format = DRM_FORMAT_R8,
1623 .plane_index = 0
1624 }, {
1625 .width_divisor = 2,
1626 .height_divisor = 2,
1627 .format = DRM_FORMAT_GR88,
1628 .plane_index = 1
1629 }}
1630 }, {
1631 .format = DRM_FORMAT_YUV420,
1632 .input_planes = 3,
1633 .output_planes = 3,
1634 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1635 {{
1636 .width_divisor = 1,
1637 .height_divisor = 1,
1638 .format = DRM_FORMAT_R8,
1639 .plane_index = 0
1640 }, {
1641 .width_divisor = 2,
1642 .height_divisor = 2,
1643 .format = DRM_FORMAT_R8,
1644 .plane_index = 1
1645 }, {
1646 .width_divisor = 2,
1647 .height_divisor = 2,
1648 .format = DRM_FORMAT_R8,
1649 .plane_index = 2
1650 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001651 }, {
1652 .format = DRM_FORMAT_YUV444,
1653 .input_planes = 3,
1654 .output_planes = 3,
1655 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1656 {{
1657 .width_divisor = 1,
1658 .height_divisor = 1,
1659 .format = DRM_FORMAT_R8,
1660 .plane_index = 0
1661 }, {
1662 .width_divisor = 1,
1663 .height_divisor = 1,
1664 .format = DRM_FORMAT_R8,
1665 .plane_index = 1
1666 }, {
1667 .width_divisor = 1,
1668 .height_divisor = 1,
1669 .format = DRM_FORMAT_R8,
1670 .plane_index = 2
1671 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001672 }
1673};
1674
1675static struct egl_image *
1676import_dmabuf_single_plane(struct gl_renderer *gr,
1677 const struct dmabuf_attributes *attributes,
1678 struct yuv_plane_descriptor *descriptor)
1679{
1680 struct dmabuf_attributes plane;
1681 struct egl_image *image;
1682 char fmt[4];
1683
1684 plane.width = attributes->width / descriptor->width_divisor;
1685 plane.height = attributes->height / descriptor->height_divisor;
1686 plane.format = descriptor->format;
1687 plane.n_planes = 1;
1688 plane.fd[0] = attributes->fd[descriptor->plane_index];
1689 plane.offset[0] = attributes->offset[descriptor->plane_index];
1690 plane.stride[0] = attributes->stride[descriptor->plane_index];
1691 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1692
1693 image = import_simple_dmabuf(gr, &plane);
1694 if (!image) {
1695 weston_log("Failed to import plane %d as %.4s\n",
1696 descriptor->plane_index,
1697 dump_format(descriptor->format, fmt));
1698 return NULL;
1699 }
1700
1701 return image;
1702}
1703
1704static bool
1705import_yuv_dmabuf(struct gl_renderer *gr,
1706 struct dmabuf_image *image)
1707{
1708 unsigned i;
1709 int j;
1710 int ret;
1711 struct yuv_format_descriptor *format = NULL;
1712 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1713 char fmt[4];
1714
1715 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1716 if (yuv_formats[i].format == attributes->format) {
1717 format = &yuv_formats[i];
1718 break;
1719 }
1720 }
1721
1722 if (!format) {
1723 weston_log("Error during import, and no known conversion for format "
1724 "%.4s in the renderer",
1725 dump_format(attributes->format, fmt));
1726 return false;
1727 }
1728
1729 if (attributes->n_planes != format->input_planes) {
1730 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1731 dump_format(format->format, fmt),
1732 format->input_planes,
1733 (format->input_planes > 1) ? "s" : "",
1734 attributes->n_planes);
1735 return false;
1736 }
1737
1738 for (j = 0; j < format->output_planes; ++j) {
1739 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1740 &format->plane[j]);
1741 if (!image->images[j]) {
1742 while (j) {
1743 ret = egl_image_unref(image->images[--j]);
1744 assert(ret == 0);
1745 }
1746 return false;
1747 }
1748 }
1749
1750 image->num_images = format->output_planes;
1751
1752 switch (format->texture_type) {
1753 case EGL_TEXTURE_Y_XUXV_WL:
1754 image->shader = &gr->texture_shader_y_xuxv;
1755 break;
1756 case EGL_TEXTURE_Y_UV_WL:
1757 image->shader = &gr->texture_shader_y_uv;
1758 break;
1759 case EGL_TEXTURE_Y_U_V_WL:
1760 image->shader = &gr->texture_shader_y_u_v;
1761 break;
1762 default:
1763 assert(false);
1764 }
1765
1766 return true;
1767}
1768
1769static GLenum
1770choose_texture_target(struct dmabuf_attributes *attributes)
1771{
1772 if (attributes->n_planes > 1)
1773 return GL_TEXTURE_EXTERNAL_OES;
1774
1775 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1776 case DRM_FORMAT_YUYV:
1777 case DRM_FORMAT_YVYU:
1778 case DRM_FORMAT_UYVY:
1779 case DRM_FORMAT_VYUY:
1780 case DRM_FORMAT_AYUV:
1781 return GL_TEXTURE_EXTERNAL_OES;
1782 default:
1783 return GL_TEXTURE_2D;
1784 }
1785}
1786
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001787static struct dmabuf_image *
1788import_dmabuf(struct gl_renderer *gr,
1789 struct linux_dmabuf_buffer *dmabuf)
1790{
1791 struct egl_image *egl_image;
1792 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001793
1794 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001795 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001796
1797 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1798 if (egl_image) {
1799 image->num_images = 1;
1800 image->images[0] = egl_image;
1801 image->import_type = IMPORT_TYPE_DIRECT;
1802 image->target = choose_texture_target(&dmabuf->attributes);
1803
1804 switch (image->target) {
1805 case GL_TEXTURE_2D:
1806 image->shader = &gr->texture_shader_rgba;
1807 break;
1808 default:
1809 image->shader = &gr->texture_shader_egl_external;
1810 }
1811 } else {
1812 if (!import_yuv_dmabuf(gr, image)) {
1813 dmabuf_image_destroy(image);
1814 return NULL;
1815 }
1816 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1817 image->target = GL_TEXTURE_2D;
1818 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001819
1820 return image;
1821}
1822
1823static bool
1824gl_renderer_import_dmabuf(struct weston_compositor *ec,
1825 struct linux_dmabuf_buffer *dmabuf)
1826{
1827 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001828 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001829 int i;
1830
1831 assert(gr->has_dmabuf_import);
1832
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001833 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001834 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001835 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001836 return false;
1837 }
1838
1839 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001840 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001841 return false;
1842
1843 image = import_dmabuf(gr, dmabuf);
1844 if (!image)
1845 return false;
1846
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001847 wl_list_insert(&gr->dmabuf_images, &image->link);
1848 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1849 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001850
1851 return true;
1852}
1853
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001854static bool
1855import_known_dmabuf(struct gl_renderer *gr,
1856 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001857{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001858 switch (image->import_type) {
1859 case IMPORT_TYPE_DIRECT:
1860 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1861 if (!image->images[0])
1862 return false;
1863 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001864
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001865 case IMPORT_TYPE_GL_CONVERSION:
1866 if (!import_yuv_dmabuf(gr, image))
1867 return false;
1868 break;
1869
Pekka Paalanena3525802014-06-12 16:49:29 +03001870 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001871 weston_log("Invalid import type for dmabuf\n");
1872 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001873 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001874
1875 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001876}
1877
1878static void
1879gl_renderer_attach_dmabuf(struct weston_surface *surface,
1880 struct weston_buffer *buffer,
1881 struct linux_dmabuf_buffer *dmabuf)
1882{
1883 struct gl_renderer *gr = get_renderer(surface->compositor);
1884 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001885 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001886 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001887 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001888
1889 if (!gr->has_dmabuf_import) {
1890 linux_dmabuf_buffer_send_server_error(dmabuf,
1891 "EGL dmabuf import not supported");
1892 return;
1893 }
1894
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001895 buffer->width = dmabuf->attributes.width;
1896 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03001897
1898 /*
1899 * GL-renderer uses the OpenGL convention of texture coordinates, where
1900 * the origin is at bottom-left. Because dmabuf buffers have the origin
1901 * at top-left, we must invert the Y_INVERT flag to get the image right.
1902 */
Pekka Paalanena3525802014-06-12 16:49:29 +03001903 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03001904 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001905
1906 for (i = 0; i < gs->num_images; i++)
1907 egl_image_unref(gs->images[i]);
1908 gs->num_images = 0;
1909
Pekka Paalanena3525802014-06-12 16:49:29 +03001910 /*
1911 * We try to always hold an imported EGLImage from the dmabuf
1912 * to prevent the client from preventing re-imports. But, we also
1913 * need to re-import every time the contents may change because
1914 * GL driver's caching may need flushing.
1915 *
1916 * Here we release the cache reference which has to be final.
1917 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001918 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001919
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001920 /* The dmabuf_image should have been created during the import */
1921 assert(image != NULL);
1922
1923 for (i = 0; i < image->num_images; ++i) {
1924 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001925 assert(ret == 0);
1926 }
1927
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001928 if (!import_known_dmabuf(gr, image)) {
1929 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1930 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001931 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001932
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001933 gs->num_images = image->num_images;
1934 for (i = 0; i < gs->num_images; ++i)
1935 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001936
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001937 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001938 ensure_textures(gs, gs->num_images);
1939 for (i = 0; i < gs->num_images; ++i) {
1940 glActiveTexture(GL_TEXTURE0 + i);
1941 glBindTexture(gs->target, gs->textures[i]);
1942 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1943 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001944
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001945 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001946 gs->pitch = buffer->width;
1947 gs->height = buffer->height;
1948 gs->buffer_type = BUFFER_TYPE_EGL;
1949 gs->y_inverted = buffer->y_inverted;
1950}
1951
1952static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001953gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001954{
1955 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001956 struct gl_renderer *gr = get_renderer(ec);
1957 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001958 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001959 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001960 EGLint format;
1961 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001962
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001963 weston_buffer_reference(&gs->buffer_ref, buffer);
1964
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001965 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001966 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001967 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001968 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001969 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001970 gs->num_images = 0;
1971 glDeleteTextures(gs->num_textures, gs->textures);
1972 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001973 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001974 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001975 return;
1976 }
1977
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001979
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001980 if (shm_buffer)
1981 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001982 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001983 EGL_TEXTURE_FORMAT, &format))
1984 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001985 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1986 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001987 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001988 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001989 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001990 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001991 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001992 }
1993}
1994
Kristian Høgsberg42263852012-09-06 21:59:29 -04001995static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001996gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001997 float red, float green, float blue, float alpha)
1998{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001999 struct gl_surface_state *gs = get_surface_state(surface);
2000 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002001
2002 gs->color[0] = red;
2003 gs->color[1] = green;
2004 gs->color[2] = blue;
2005 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02002006 gs->buffer_type = BUFFER_TYPE_SOLID;
2007 gs->pitch = 1;
2008 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002009
John Kåre Alsaker40684142012-11-13 19:10:25 +01002010 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002011}
2012
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002013static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002014gl_renderer_surface_get_content_size(struct weston_surface *surface,
2015 int *width, int *height)
2016{
2017 struct gl_surface_state *gs = get_surface_state(surface);
2018
2019 if (gs->buffer_type == BUFFER_TYPE_NULL) {
2020 *width = 0;
2021 *height = 0;
2022 } else {
2023 *width = gs->pitch;
2024 *height = gs->height;
2025 }
2026}
2027
2028static uint32_t
2029pack_color(pixman_format_code_t format, float *c)
2030{
2031 uint8_t r = round(c[0] * 255.0f);
2032 uint8_t g = round(c[1] * 255.0f);
2033 uint8_t b = round(c[2] * 255.0f);
2034 uint8_t a = round(c[3] * 255.0f);
2035
2036 switch (format) {
2037 case PIXMAN_a8b8g8r8:
2038 return (a << 24) | (b << 16) | (g << 8) | r;
2039 default:
2040 assert(0);
2041 return 0;
2042 }
2043}
2044
2045static int
2046gl_renderer_surface_copy_content(struct weston_surface *surface,
2047 void *target, size_t size,
2048 int src_x, int src_y,
2049 int width, int height)
2050{
2051 static const GLfloat verts[4 * 2] = {
2052 0.0f, 0.0f,
2053 1.0f, 0.0f,
2054 1.0f, 1.0f,
2055 0.0f, 1.0f
2056 };
2057 static const GLfloat projmat_normal[16] = { /* transpose */
2058 2.0f, 0.0f, 0.0f, 0.0f,
2059 0.0f, 2.0f, 0.0f, 0.0f,
2060 0.0f, 0.0f, 1.0f, 0.0f,
2061 -1.0f, -1.0f, 0.0f, 1.0f
2062 };
2063 static const GLfloat projmat_yinvert[16] = { /* transpose */
2064 2.0f, 0.0f, 0.0f, 0.0f,
2065 0.0f, -2.0f, 0.0f, 0.0f,
2066 0.0f, 0.0f, 1.0f, 0.0f,
2067 -1.0f, 1.0f, 0.0f, 1.0f
2068 };
2069 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2070 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2071 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2072 struct gl_renderer *gr = get_renderer(surface->compositor);
2073 struct gl_surface_state *gs = get_surface_state(surface);
2074 int cw, ch;
2075 GLuint fbo;
2076 GLuint tex;
2077 GLenum status;
2078 const GLfloat *proj;
2079 int i;
2080
2081 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2082
2083 switch (gs->buffer_type) {
2084 case BUFFER_TYPE_NULL:
2085 return -1;
2086 case BUFFER_TYPE_SOLID:
2087 *(uint32_t *)target = pack_color(format, gs->color);
2088 return 0;
2089 case BUFFER_TYPE_SHM:
2090 gl_renderer_flush_damage(surface);
2091 /* fall through */
2092 case BUFFER_TYPE_EGL:
2093 break;
2094 }
2095
2096 glGenTextures(1, &tex);
2097 glBindTexture(GL_TEXTURE_2D, tex);
2098 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2099 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2100 glBindTexture(GL_TEXTURE_2D, 0);
2101
2102 glGenFramebuffers(1, &fbo);
2103 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2104 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2105 GL_TEXTURE_2D, tex, 0);
2106
2107 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2108 if (status != GL_FRAMEBUFFER_COMPLETE) {
2109 weston_log("%s: fbo error: %#x\n", __func__, status);
2110 glDeleteFramebuffers(1, &fbo);
2111 glDeleteTextures(1, &tex);
2112 return -1;
2113 }
2114
2115 glViewport(0, 0, cw, ch);
2116 glDisable(GL_BLEND);
2117 use_shader(gr, gs->shader);
2118 if (gs->y_inverted)
2119 proj = projmat_normal;
2120 else
2121 proj = projmat_yinvert;
2122
2123 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2124 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2125
2126 for (i = 0; i < gs->num_textures; i++) {
2127 glUniform1i(gs->shader->tex_uniforms[i], i);
2128
2129 glActiveTexture(GL_TEXTURE0 + i);
2130 glBindTexture(gs->target, gs->textures[i]);
2131 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2132 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2133 }
2134
2135 /* position: */
2136 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2137 glEnableVertexAttribArray(0);
2138
2139 /* texcoord: */
2140 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2141 glEnableVertexAttribArray(1);
2142
2143 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2144
2145 glDisableVertexAttribArray(1);
2146 glDisableVertexAttribArray(0);
2147
2148 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2149 glReadPixels(src_x, src_y, width, height, gl_format,
2150 GL_UNSIGNED_BYTE, target);
2151
2152 glDeleteFramebuffers(1, &fbo);
2153 glDeleteTextures(1, &tex);
2154
2155 return 0;
2156}
2157
2158static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002159surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002160{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002161 int i;
2162
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002163 wl_list_remove(&gs->surface_destroy_listener.link);
2164 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002165
2166 gs->surface->renderer_state = NULL;
2167
2168 glDeleteTextures(gs->num_textures, gs->textures);
2169
2170 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002171 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002172
2173 weston_buffer_reference(&gs->buffer_ref, NULL);
2174 pixman_region32_fini(&gs->texture_damage);
2175 free(gs);
2176}
2177
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002178static void
2179surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2180{
2181 struct gl_surface_state *gs;
2182 struct gl_renderer *gr;
2183
2184 gs = container_of(listener, struct gl_surface_state,
2185 surface_destroy_listener);
2186
2187 gr = get_renderer(gs->surface->compositor);
2188
2189 surface_state_destroy(gs, gr);
2190}
2191
2192static void
2193surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2194{
2195 struct gl_surface_state *gs;
2196 struct gl_renderer *gr;
2197
2198 gr = data;
2199
2200 gs = container_of(listener, struct gl_surface_state,
2201 renderer_destroy_listener);
2202
2203 surface_state_destroy(gs, gr);
2204}
2205
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002206static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002207gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002208{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002209 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002210 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002211
Bryce Harringtonde16d892014-11-20 22:21:57 -08002212 gs = zalloc(sizeof *gs);
2213 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002214 return -1;
2215
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002216 /* A buffer is never attached to solid color surfaces, yet
2217 * they still go through texcoord computations. Do not divide
2218 * by zero there.
2219 */
2220 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002221 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002222
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002223 gs->surface = surface;
2224
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002225 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002226 surface->renderer_state = gs;
2227
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002228 gs->surface_destroy_listener.notify =
2229 surface_state_handle_surface_destroy;
2230 wl_signal_add(&surface->destroy_signal,
2231 &gs->surface_destroy_listener);
2232
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002233 gs->renderer_destroy_listener.notify =
2234 surface_state_handle_renderer_destroy;
2235 wl_signal_add(&gr->destroy_signal,
2236 &gs->renderer_destroy_listener);
2237
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002238 if (surface->buffer_ref.buffer) {
2239 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2240 gl_renderer_flush_damage(surface);
2241 }
2242
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002243 return 0;
2244}
2245
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002246static const char vertex_shader[] =
2247 "uniform mat4 proj;\n"
2248 "attribute vec2 position;\n"
2249 "attribute vec2 texcoord;\n"
2250 "varying vec2 v_texcoord;\n"
2251 "void main()\n"
2252 "{\n"
2253 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2254 " v_texcoord = texcoord;\n"
2255 "}\n";
2256
2257/* Declare common fragment shader uniforms */
2258#define FRAGMENT_CONVERT_YUV \
2259 " y *= alpha;\n" \
2260 " u *= alpha;\n" \
2261 " v *= alpha;\n" \
2262 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2263 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2264 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2265 " gl_FragColor.a = alpha;\n"
2266
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002267static const char fragment_debug[] =
2268 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2269
2270static const char fragment_brace[] =
2271 "}\n";
2272
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002273static const char texture_fragment_shader_rgba[] =
2274 "precision mediump float;\n"
2275 "varying vec2 v_texcoord;\n"
2276 "uniform sampler2D tex;\n"
2277 "uniform float alpha;\n"
2278 "void main()\n"
2279 "{\n"
2280 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002281 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002282
2283static const char texture_fragment_shader_rgbx[] =
2284 "precision mediump float;\n"
2285 "varying vec2 v_texcoord;\n"
2286 "uniform sampler2D tex;\n"
2287 "uniform float alpha;\n"
2288 "void main()\n"
2289 "{\n"
2290 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2291 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002292 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002293
2294static const char texture_fragment_shader_egl_external[] =
2295 "#extension GL_OES_EGL_image_external : require\n"
2296 "precision mediump float;\n"
2297 "varying vec2 v_texcoord;\n"
2298 "uniform samplerExternalOES tex;\n"
2299 "uniform float alpha;\n"
2300 "void main()\n"
2301 "{\n"
2302 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002303 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002304
2305static const char texture_fragment_shader_y_uv[] =
2306 "precision mediump float;\n"
2307 "uniform sampler2D tex;\n"
2308 "uniform sampler2D tex1;\n"
2309 "varying vec2 v_texcoord;\n"
2310 "uniform float alpha;\n"
2311 "void main() {\n"
2312 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2313 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2314 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2315 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002316 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002317
2318static const char texture_fragment_shader_y_u_v[] =
2319 "precision mediump float;\n"
2320 "uniform sampler2D tex;\n"
2321 "uniform sampler2D tex1;\n"
2322 "uniform sampler2D tex2;\n"
2323 "varying vec2 v_texcoord;\n"
2324 "uniform float alpha;\n"
2325 "void main() {\n"
2326 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2327 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2328 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2329 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002330 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002331
2332static const char texture_fragment_shader_y_xuxv[] =
2333 "precision mediump float;\n"
2334 "uniform sampler2D tex;\n"
2335 "uniform sampler2D tex1;\n"
2336 "varying vec2 v_texcoord;\n"
2337 "uniform float alpha;\n"
2338 "void main() {\n"
2339 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2340 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2341 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2342 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002343 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002344
2345static const char solid_fragment_shader[] =
2346 "precision mediump float;\n"
2347 "uniform vec4 color;\n"
2348 "uniform float alpha;\n"
2349 "void main()\n"
2350 "{\n"
2351 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002352 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002353
2354static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002355compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002356{
2357 GLuint s;
2358 char msg[512];
2359 GLint status;
2360
2361 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002362 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002363 glCompileShader(s);
2364 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2365 if (!status) {
2366 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2367 weston_log("shader info: %s\n", msg);
2368 return GL_NONE;
2369 }
2370
2371 return s;
2372}
2373
2374static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002375shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002376 const char *vertex_source, const char *fragment_source)
2377{
2378 char msg[512];
2379 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002380 int count;
2381 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002382
2383 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002384 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2385
2386 if (renderer->fragment_shader_debug) {
2387 sources[0] = fragment_source;
2388 sources[1] = fragment_debug;
2389 sources[2] = fragment_brace;
2390 count = 3;
2391 } else {
2392 sources[0] = fragment_source;
2393 sources[1] = fragment_brace;
2394 count = 2;
2395 }
2396
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002397 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002398 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002399
2400 shader->program = glCreateProgram();
2401 glAttachShader(shader->program, shader->vertex_shader);
2402 glAttachShader(shader->program, shader->fragment_shader);
2403 glBindAttribLocation(shader->program, 0, "position");
2404 glBindAttribLocation(shader->program, 1, "texcoord");
2405
2406 glLinkProgram(shader->program);
2407 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2408 if (!status) {
2409 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2410 weston_log("link info: %s\n", msg);
2411 return -1;
2412 }
2413
2414 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2415 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2416 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2417 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2418 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2419 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2420
2421 return 0;
2422}
2423
2424static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002425shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002426{
2427 glDeleteShader(shader->vertex_shader);
2428 glDeleteShader(shader->fragment_shader);
2429 glDeleteProgram(shader->program);
2430
2431 shader->vertex_shader = 0;
2432 shader->fragment_shader = 0;
2433 shader->program = 0;
2434}
2435
2436static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002437log_extensions(const char *name, const char *extensions)
2438{
2439 const char *p, *end;
2440 int l;
2441 int len;
2442
2443 l = weston_log("%s:", name);
2444 p = extensions;
2445 while (*p) {
2446 end = strchrnul(p, ' ');
2447 len = end - p;
2448 if (l + len > 78)
2449 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2450 len, p);
2451 else
2452 l += weston_log_continue(" %.*s", len, p);
2453 for (p = end; isspace(*p); p++)
2454 ;
2455 }
2456 weston_log_continue("\n");
2457}
2458
2459static void
2460log_egl_gl_info(EGLDisplay egldpy)
2461{
2462 const char *str;
2463
2464 str = eglQueryString(egldpy, EGL_VERSION);
2465 weston_log("EGL version: %s\n", str ? str : "(null)");
2466
2467 str = eglQueryString(egldpy, EGL_VENDOR);
2468 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2469
2470 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2471 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2472
2473 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2474 log_extensions("EGL extensions", str ? str : "(null)");
2475
2476 str = (char *)glGetString(GL_VERSION);
2477 weston_log("GL version: %s\n", str ? str : "(null)");
2478
2479 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2480 weston_log("GLSL version: %s\n", str ? str : "(null)");
2481
2482 str = (char *)glGetString(GL_VENDOR);
2483 weston_log("GL vendor: %s\n", str ? str : "(null)");
2484
2485 str = (char *)glGetString(GL_RENDERER);
2486 weston_log("GL renderer: %s\n", str ? str : "(null)");
2487
2488 str = (char *)glGetString(GL_EXTENSIONS);
2489 log_extensions("GL extensions", str ? str : "(null)");
2490}
2491
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002492static void
2493log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2494{
2495 EGLint r, g, b, a;
2496
2497 weston_log("Chosen EGL config details:\n");
2498
2499 weston_log_continue(STAMP_SPACE "RGBA bits");
2500 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2501 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2502 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2503 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2504 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2505 else
2506 weston_log_continue(" unknown\n");
2507
2508 weston_log_continue(STAMP_SPACE "swap interval range");
2509 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2510 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2511 weston_log_continue(": %d - %d\n", a, b);
2512 else
2513 weston_log_continue(" unknown\n");
2514}
2515
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002516static int
Derek Foremane76f1852015-05-15 12:12:39 -05002517match_config_to_visual(EGLDisplay egl_display,
2518 EGLint visual_id,
2519 EGLConfig *configs,
2520 int count)
2521{
2522 int i;
2523
2524 for (i = 0; i < count; ++i) {
2525 EGLint id;
2526
2527 if (!eglGetConfigAttrib(egl_display,
2528 configs[i], EGL_NATIVE_VISUAL_ID,
2529 &id))
2530 continue;
2531
2532 if (id == visual_id)
2533 return i;
2534 }
2535
Derek Foremane76f1852015-05-15 12:12:39 -05002536 return -1;
2537}
2538
2539static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002540egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002541 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002542 EGLConfig *config_out)
2543{
2544 EGLint count = 0;
2545 EGLint matched = 0;
2546 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002547 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002548
Derek Foremana7e19912015-05-20 14:57:58 -05002549 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2550 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002551 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002552 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002553 configs = calloc(count, sizeof *configs);
2554 if (!configs)
2555 return -1;
2556
2557 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002558 count, &matched) || !matched) {
2559 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002560 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002561 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002562
Derek Foremane76f1852015-05-15 12:12:39 -05002563 if (!visual_id)
2564 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002565
Derek Foremane76f1852015-05-15 12:12:39 -05002566 for (i = 0; config_index == -1 && i < n_ids; i++)
2567 config_index = match_config_to_visual(gr->egl_display,
2568 visual_id[i],
2569 configs,
2570 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002571
Derek Foremane76f1852015-05-15 12:12:39 -05002572 if (config_index != -1)
2573 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002574
2575out:
2576 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002577 if (config_index == -1)
2578 return -1;
2579
Derek Foremana7e19912015-05-20 14:57:58 -05002580 if (i > 1)
2581 weston_log("Unable to use first choice EGL config with id"
2582 " 0x%x, succeeded with alternate id 0x%x.\n",
2583 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002584 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002585}
2586
John Kåre Alsaker44154502012-11-13 19:10:20 +01002587static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002588gl_renderer_output_set_border(struct weston_output *output,
2589 enum gl_renderer_border_side side,
2590 int32_t width, int32_t height,
2591 int32_t tex_width, unsigned char *data)
2592{
2593 struct gl_output_state *go = get_output_state(output);
2594
Jason Ekstrande5512d42014-02-04 21:36:38 -06002595 if (go->borders[side].width != width ||
2596 go->borders[side].height != height)
2597 /* In this case, we have to blow everything and do a full
2598 * repaint. */
2599 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2600
2601 if (data == NULL) {
2602 width = 0;
2603 height = 0;
2604 }
2605
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002606 go->borders[side].width = width;
2607 go->borders[side].height = height;
2608 go->borders[side].tex_width = tex_width;
2609 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002610 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002611}
2612
John Kåre Alsaker94659272012-11-13 19:10:18 +01002613static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002614gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002615
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002616static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002617gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002618 EGLNativeWindowType window_for_legacy,
2619 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002620 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002621 const EGLint *visual_id,
2622 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002623{
2624 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002625 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002626 struct gl_output_state *go;
2627 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002628 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002629
Derek Foremane76f1852015-05-15 12:12:39 -05002630 if (egl_choose_config(gr, attribs, visual_id,
2631 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002632 weston_log("failed to choose EGL config for output\n");
2633 return -1;
2634 }
2635
2636 if (egl_config != gr->egl_config &&
2637 !gr->has_configless_context) {
2638 weston_log("attempted to use a different EGL config for an "
2639 "output but EGL_MESA_configless_context is not "
2640 "supported\n");
2641 return -1;
2642 }
2643
Bryce Harringtonde16d892014-11-20 22:21:57 -08002644 go = zalloc(sizeof *go);
2645 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002646 return -1;
2647
Jonny Lamb671148f2015-03-20 15:26:52 +01002648 if (gr->create_platform_window) {
2649 go->egl_surface =
2650 gr->create_platform_window(gr->egl_display,
2651 egl_config,
2652 window_for_platform,
2653 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002654 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002655 go->egl_surface =
2656 eglCreateWindowSurface(gr->egl_display,
2657 egl_config,
2658 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002659 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002660
2661 if (go->egl_surface == EGL_NO_SURFACE) {
2662 weston_log("failed to create egl surface\n");
2663 free(go);
2664 return -1;
2665 }
2666
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002667 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002668 pixman_region32_init(&go->buffer_damage[i]);
2669
John Kåre Alsaker94659272012-11-13 19:10:18 +01002670 output->renderer_state = go;
2671
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002672 log_egl_config_info(gr->egl_display, egl_config);
2673
John Kåre Alsaker94659272012-11-13 19:10:18 +01002674 return 0;
2675}
2676
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002677static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002678gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002679{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002680 struct gl_renderer *gr = get_renderer(output->compositor);
2681 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002682 int i;
2683
2684 for (i = 0; i < 2; i++)
2685 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002686
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002687 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002688
2689 free(go);
2690}
2691
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002692static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002693gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002694{
2695 return get_output_state(output)->egl_surface;
2696}
2697
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002698static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002699gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002700{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002701 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002702 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002703
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002704 wl_signal_emit(&gr->destroy_signal, gr);
2705
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002706 if (gr->has_bind_display)
2707 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002708
2709 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2710 eglMakeCurrent(gr->egl_display,
2711 EGL_NO_SURFACE, EGL_NO_SURFACE,
2712 EGL_NO_CONTEXT);
2713
Pekka Paalanena3525802014-06-12 16:49:29 +03002714
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002715 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2716 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002717
Armin Krezović28d240f2016-06-23 11:59:35 +02002718 if (gr->dummy_surface != EGL_NO_SURFACE)
2719 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2720
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002721 eglTerminate(gr->egl_display);
2722 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002723
Armin Krezović36d699a2016-08-05 15:28:30 +02002724 wl_list_remove(&gr->output_destroy_listener.link);
2725
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002726 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002727 wl_array_release(&gr->vtxcnt);
2728
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002729 if (gr->fragment_binding)
2730 weston_binding_destroy(gr->fragment_binding);
2731 if (gr->fan_binding)
2732 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002733
Scott Moreau976a0502013-03-07 10:15:17 -07002734 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002735}
2736
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002737static void
2738renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2739{
2740 const char *extensions;
2741
2742 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2743 if (!extensions) {
2744 weston_log("Retrieving EGL client extension string failed.\n");
2745 return;
2746 }
2747
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002748 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002749 gr->create_platform_window =
2750 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2751 else
2752 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2753}
2754
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002755static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002756gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2757{
2758 struct gl_renderer *gr = get_renderer(ec);
2759 const char *extensions;
2760 EGLBoolean ret;
2761
2762 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2763 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2764 gr->bind_display =
2765 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2766 gr->unbind_display =
2767 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2768 gr->query_buffer =
2769 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2770
2771 extensions =
2772 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2773 if (!extensions) {
2774 weston_log("Retrieving EGL extension string failed.\n");
2775 return -1;
2776 }
2777
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002778 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002779 gr->has_bind_display = 1;
2780 if (gr->has_bind_display) {
2781 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2782 if (!ret)
2783 gr->has_bind_display = 0;
2784 }
2785
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002786 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002787 gr->has_egl_buffer_age = 1;
2788 else
2789 weston_log("warning: EGL_EXT_buffer_age not supported. "
2790 "Performance could be affected.\n");
2791
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002792 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002793 gr->swap_buffers_with_damage =
2794 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2795 else
2796 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2797 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002798
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002799 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002800 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002801
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002802 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002803 gr->has_surfaceless_context = 1;
2804
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002805 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002806 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002807
Vincent Abrioufdeefe42016-10-05 14:54:34 +02002808 if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg"))
2809 gr->has_gl_texture_rg = 1;
2810
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002811 renderer_setup_egl_client_extensions(gr);
2812
Neil Robertsb7f85332014-03-07 18:05:49 +00002813 return 0;
2814}
2815
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002816static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002817 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2818 EGL_RED_SIZE, 1,
2819 EGL_GREEN_SIZE, 1,
2820 EGL_BLUE_SIZE, 1,
2821 EGL_ALPHA_SIZE, 0,
2822 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2823 EGL_NONE
2824};
2825
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002826static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002827 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2828 EGL_RED_SIZE, 1,
2829 EGL_GREEN_SIZE, 1,
2830 EGL_BLUE_SIZE, 1,
2831 EGL_ALPHA_SIZE, 1,
2832 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2833 EGL_NONE
2834};
2835
Armin Krezović28d240f2016-06-23 11:59:35 +02002836
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002837/** Checks whether a platform EGL client extension is supported
2838 *
2839 * \param ec The weston compositor
2840 * \param extension_suffix The EGL client extension suffix
2841 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2842 *
2843 * This function checks whether a specific platform_* extension is supported
2844 * by EGL.
2845 *
2846 * The extension suffix should be the suffix of the platform extension (that
2847 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2848 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2849 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2850 *
2851 * The return value is 1:
2852 * - if the supplied EGL client extension is supported.
2853 * The return value is 0:
2854 * - if the platform_base client extension isn't supported so will
2855 * fallback to eglGetDisplay and friends.
2856 * The return value is -1:
2857 * - if the supplied EGL client extension is not supported.
2858 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002859static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002860gl_renderer_supports(struct weston_compositor *ec,
2861 const char *extension_suffix)
2862{
2863 static const char *extensions = NULL;
2864 char s[64];
2865
2866 if (!extensions) {
2867 extensions = (const char *) eglQueryString(
2868 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2869
2870 if (!extensions)
2871 return 0;
2872
2873 log_extensions("EGL client extensions",
2874 extensions);
2875 }
2876
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002877 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002878 return 0;
2879
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002880 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002881 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002882 return 1;
2883
2884 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002885 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002886 return 1;
2887
2888 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002889 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002890 return 1;
2891
Pekka Paalanenf2824542015-04-08 17:02:21 +03002892 /* at this point we definitely have some platform extensions but
2893 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002894 * not supported. */
2895
2896 return -1;
2897}
2898
Jonny Lamb74eed312015-03-24 13:12:04 +01002899static const char *
2900platform_to_extension(EGLenum platform)
2901{
2902 switch (platform) {
2903 case EGL_PLATFORM_GBM_KHR:
2904 return "gbm";
2905 case EGL_PLATFORM_WAYLAND_KHR:
2906 return "wayland";
2907 case EGL_PLATFORM_X11_KHR:
2908 return "x11";
2909 default:
2910 assert(0 && "bad EGL platform enum");
2911 }
2912}
2913
Armin Krezović36d699a2016-08-05 15:28:30 +02002914static void
2915output_handle_destroy(struct wl_listener *listener, void *data)
2916{
2917 struct gl_renderer *gr;
2918 struct weston_output *output = data;
2919
2920 gr = container_of(listener, struct gl_renderer,
2921 output_destroy_listener);
2922
2923 if (wl_list_empty(&output->compositor->output_list))
2924 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
2925 gr->dummy_surface, gr->egl_context);
2926}
2927
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002928static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002929gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2930 EGLConfig pbuffer_config;
2931
2932 static const EGLint pbuffer_config_attribs[] = {
2933 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2934 EGL_RED_SIZE, 1,
2935 EGL_GREEN_SIZE, 1,
2936 EGL_BLUE_SIZE, 1,
2937 EGL_ALPHA_SIZE, 0,
2938 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2939 EGL_NONE
2940 };
2941
2942 static const EGLint pbuffer_attribs[] = {
2943 EGL_WIDTH, 10,
2944 EGL_HEIGHT, 10,
2945 EGL_NONE
2946 };
2947
2948 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2949 weston_log("failed to choose EGL config for PbufferSurface");
2950 return -1;
2951 }
2952
2953 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2954 pbuffer_config,
2955 pbuffer_attribs);
2956
2957 if (gr->dummy_surface == EGL_NO_SURFACE) {
2958 weston_log("failed to create PbufferSurface\n");
2959 return -1;
2960 }
2961
2962 return 0;
2963}
2964
2965static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002966gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2967 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002968 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002969{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002970 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002971 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002972 int supports = 0;
2973
2974 if (platform) {
2975 supports = gl_renderer_supports(
2976 ec, platform_to_extension(platform));
2977 if (supports < 0)
2978 return -1;
2979 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002980
Bryce Harringtonde16d892014-11-20 22:21:57 -08002981 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002982 if (gr == NULL)
2983 return -1;
2984
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002985 gr->base.read_pixels = gl_renderer_read_pixels;
2986 gr->base.repaint_output = gl_renderer_repaint_output;
2987 gr->base.flush_damage = gl_renderer_flush_damage;
2988 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002989 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002990 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002991 gr->base.surface_get_content_size =
2992 gl_renderer_surface_get_content_size;
2993 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002994 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002995
Jonny Lambf1ec5062015-03-24 13:12:05 +01002996 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002997 if (supports) {
2998 if (!get_platform_display) {
2999 get_platform_display = (void *) eglGetProcAddress(
3000 "eglGetPlatformDisplayEXT");
3001 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003002
Jonny Lamb74eed312015-03-24 13:12:04 +01003003 /* also wrap this in the supports check because
3004 * eglGetProcAddress can return non-NULL and still not
3005 * support the feature at runtime, so ensure the
3006 * appropriate extension checks have been done. */
3007 if (get_platform_display && platform) {
3008 gr->egl_display = get_platform_display(platform,
3009 native_window,
3010 NULL);
3011 }
3012 }
Jonny Lamb74eed312015-03-24 13:12:04 +01003013
3014 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003015 weston_log("warning: either no EGL_EXT_platform_base "
3016 "support or specific platform support; "
3017 "falling back to eglGetDisplay.\n");
3018 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003019 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01003020
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003021 if (gr->egl_display == EGL_NO_DISPLAY) {
3022 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003023 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003024 }
3025
3026 if (!eglInitialize(gr->egl_display, &major, &minor)) {
3027 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05003028 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003029 }
3030
Derek Foremane76f1852015-05-15 12:12:39 -05003031 if (egl_choose_config(gr, attribs, visual_id,
3032 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003033 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003034 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003035 }
3036
3037 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003038 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003039 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02003040 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003041
Neil Robertsb7f85332014-03-07 18:05:49 +00003042 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05003043 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00003044
Pekka Paalanena3525802014-06-12 16:49:29 +03003045 wl_list_init(&gr->dmabuf_images);
3046 if (gr->has_dmabuf_import)
3047 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
3048
Armin Krezović28d240f2016-06-23 11:59:35 +02003049 if (gr->has_surfaceless_context) {
3050 weston_log("EGL_KHR_surfaceless_context available\n");
3051 gr->dummy_surface = EGL_NO_SURFACE;
3052 } else {
3053 weston_log("EGL_KHR_surfaceless_context unavailable. "
3054 "Trying PbufferSurface\n");
3055
3056 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3057 goto fail_with_error;
3058 }
3059
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003060 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
Vincent Abrioufdeefe42016-10-05 14:54:34 +02003061 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420);
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003062
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003063 wl_signal_init(&gr->destroy_signal);
3064
Armin Krezović28d240f2016-06-23 11:59:35 +02003065 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3066 if (gr->dummy_surface != EGL_NO_SURFACE)
3067 eglDestroySurface(gr->egl_display, gr->dummy_surface);
3068 goto fail_with_error;
3069 }
3070
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003071 return 0;
3072
Derek Foreman066ca0c2015-06-11 12:14:45 -05003073fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003074 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003075fail_terminate:
3076 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003077fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003078 free(gr);
3079 return -1;
3080}
3081
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003082static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003083gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003084{
3085 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003086}
3087
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003088static int
3089compile_shaders(struct weston_compositor *ec)
3090{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003091 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003092
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003093 gr->texture_shader_rgba.vertex_source = vertex_shader;
3094 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3095
3096 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3097 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3098
3099 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3100 gr->texture_shader_egl_external.fragment_source =
3101 texture_fragment_shader_egl_external;
3102
3103 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3104 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3105
3106 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3107 gr->texture_shader_y_u_v.fragment_source =
3108 texture_fragment_shader_y_u_v;
3109
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003110 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003111 gr->texture_shader_y_xuxv.fragment_source =
3112 texture_fragment_shader_y_xuxv;
3113
3114 gr->solid_shader.vertex_source = vertex_shader;
3115 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003116
3117 return 0;
3118}
3119
3120static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003121fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3122 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003123{
3124 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003125 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003126 struct weston_output *output;
3127
John Kåre Alsaker40684142012-11-13 19:10:25 +01003128 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003129
John Kåre Alsaker40684142012-11-13 19:10:25 +01003130 shader_release(&gr->texture_shader_rgba);
3131 shader_release(&gr->texture_shader_rgbx);
3132 shader_release(&gr->texture_shader_egl_external);
3133 shader_release(&gr->texture_shader_y_uv);
3134 shader_release(&gr->texture_shader_y_u_v);
3135 shader_release(&gr->texture_shader_y_xuxv);
3136 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003137
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003138 /* Force use_shader() to call glUseProgram(), since we need to use
3139 * the recompiled version of the shader. */
3140 gr->current_shader = NULL;
3141
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003142 wl_list_for_each(output, &ec->output_list, link)
3143 weston_output_damage(output);
3144}
3145
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003146static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003147fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3148 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003149{
3150 struct weston_compositor *compositor = data;
3151 struct gl_renderer *gr = get_renderer(compositor);
3152
3153 gr->fan_debug = !gr->fan_debug;
3154 weston_compositor_damage_all(compositor);
3155}
3156
John Kåre Alsaker94659272012-11-13 19:10:18 +01003157static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003158gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003159{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003160 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003161 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003162 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003163 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003164
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003165 static const EGLint context_attribs[] = {
3166 EGL_CONTEXT_CLIENT_VERSION, 2,
3167 EGL_NONE
3168 };
3169
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003170 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3171 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003172 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003173 return -1;
3174 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003175
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003176 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003177
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003178 if (gr->has_configless_context)
3179 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003180
3181 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003182 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003183 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003184 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003185 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003186 return -1;
3187 }
3188
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003189 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3190 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003191 if (ret == EGL_FALSE) {
3192 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003193 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003194 return -1;
3195 }
3196
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003197 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003198
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003199 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003200 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003201
3202 extensions = (const char *) glGetString(GL_EXTENSIONS);
3203 if (!extensions) {
3204 weston_log("Retrieving GL extension string failed.\n");
3205 return -1;
3206 }
3207
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003208 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003209 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3210 return -1;
3211 }
3212
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003213 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003214 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003215 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003216 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003217
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003218 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003219 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003220
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003221 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003222 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003223
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003224 glActiveTexture(GL_TEXTURE0);
3225
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003226 if (compile_shaders(ec))
3227 return -1;
3228
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003229 gr->fragment_binding =
3230 weston_compositor_add_debug_binding(ec, KEY_S,
3231 fragment_debug_binding,
3232 ec);
3233 gr->fan_binding =
3234 weston_compositor_add_debug_binding(ec, KEY_F,
3235 fan_debug_repaint_binding,
3236 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003237
Armin Krezović36d699a2016-08-05 15:28:30 +02003238 gr->output_destroy_listener.notify = output_handle_destroy;
3239 wl_signal_add(&ec->output_destroyed_signal,
3240 &gr->output_destroy_listener);
3241
Pekka Paalanen035a0322012-10-24 09:43:06 +03003242 weston_log("GL ES 2 renderer features:\n");
3243 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003244 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003245 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003246 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003247 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003248 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003249
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003250
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003251 return 0;
3252}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003253
3254WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3255 .opaque_attribs = gl_renderer_opaque_attribs,
3256 .alpha_attribs = gl_renderer_alpha_attribs,
3257
3258 .create = gl_renderer_create,
3259 .display = gl_renderer_display,
3260 .output_create = gl_renderer_output_create,
3261 .output_destroy = gl_renderer_output_destroy,
3262 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003263 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003264 .print_egl_error_state = gl_renderer_print_egl_error_state
3265};