blob: 17377d24e64ccf8483e30c11e9e389cee251c224 [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>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040033#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040034#include <string.h>
35#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030036#include <float.h>
37#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020038#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030039#include <drm_fourcc.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040040
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010041#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080042#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030043#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080044#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010045
Jon Cruz35b2eaa2015-06-15 15:37:08 -070046#include "shared/helpers.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010047#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040048
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010049struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010050 GLuint program;
51 GLuint vertex_shader, fragment_shader;
52 GLint proj_uniform;
53 GLint tex_uniforms[3];
54 GLint alpha_uniform;
55 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030056 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010057};
58
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020059#define BUFFER_DAMAGE_COUNT 2
60
Jason Ekstrande5512d42014-02-04 21:36:38 -060061enum gl_border_status {
62 BORDER_STATUS_CLEAN = 0,
63 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
64 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
65 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
66 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
67 BORDER_ALL_DIRTY = 0xf,
68 BORDER_SIZE_CHANGED = 0x10
69};
70
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050071struct gl_border_image {
72 GLuint tex;
73 int32_t width, height;
74 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050075 void *data;
76};
77
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010078struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010079 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020080 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050081 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060082 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050083 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060084 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050085
86 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010087};
88
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030089enum buffer_type {
90 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020091 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030092 BUFFER_TYPE_SHM,
93 BUFFER_TYPE_EGL
94};
95
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030096struct gl_renderer;
97
98struct egl_image {
99 struct gl_renderer *renderer;
100 EGLImageKHR image;
101 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000102};
Pekka Paalanena3525802014-06-12 16:49:29 +0300103
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000104enum import_type {
105 IMPORT_TYPE_INVALID,
106 IMPORT_TYPE_DIRECT,
107 IMPORT_TYPE_GL_CONVERSION
108};
109
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000110struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300111 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000112 int num_images;
113 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300114 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000115
116 enum import_type import_type;
117 GLenum target;
118 struct gl_shader *shader;
119};
120
121struct yuv_plane_descriptor {
122 int width_divisor;
123 int height_divisor;
124 uint32_t format;
125 int plane_index;
126};
127
128struct yuv_format_descriptor {
129 uint32_t format;
130 int input_planes;
131 int output_planes;
132 int texture_type;
133 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300134};
135
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100136struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100137 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100138 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100139
140 GLuint textures[3];
141 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600142 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200143 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100144
Neil Roberts4d085e72014-04-07 15:01:01 +0100145 /* These are only used by SHM surfaces to detect when we need
146 * to do a full upload to specify a new internal texture
147 * format */
148 GLenum gl_format;
149 GLenum gl_pixel_type;
150
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300151 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100152 GLenum target;
153 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200154
155 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300156 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200157 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200158 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400159 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300160
161 struct weston_surface *surface;
162
163 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300164 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100165};
166
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100167struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100168 struct weston_renderer base;
169 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400170 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300171 struct weston_binding *fragment_binding;
172 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100173
174 EGLDisplay egl_display;
175 EGLContext egl_context;
176 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100177
Armin Krezović28d240f2016-06-23 11:59:35 +0200178 EGLSurface dummy_surface;
179
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400180 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400181 struct wl_array vtxcnt;
182
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100183 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
184 PFNEGLCREATEIMAGEKHRPROC create_image;
185 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600186 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100187 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100188
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100189 int has_unpack_subimage;
190
191 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
192 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
193 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
194 int has_bind_display;
195
196 int has_egl_image_external;
197
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200198 int has_egl_buffer_age;
199
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000200 int has_configless_context;
201
Armin Krezović28d240f2016-06-23 11:59:35 +0200202 int has_surfaceless_context;
203
Pekka Paalanena3525802014-06-12 16:49:29 +0300204 int has_dmabuf_import;
205 struct wl_list dmabuf_images;
206
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100207 struct gl_shader texture_shader_rgba;
208 struct gl_shader texture_shader_rgbx;
209 struct gl_shader texture_shader_egl_external;
210 struct gl_shader texture_shader_y_uv;
211 struct gl_shader texture_shader_y_u_v;
212 struct gl_shader texture_shader_y_xuxv;
213 struct gl_shader invert_color_shader;
214 struct gl_shader solid_shader;
215 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300216
217 struct wl_signal destroy_signal;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100218};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100219
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100220static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100221
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000222static inline const char *
223dump_format(uint32_t format, char out[4])
224{
225#if BYTE_ORDER == BIG_ENDIAN
226 format = __builtin_bswap32(format);
227#endif
228 memcpy(out, &format, 4);
229 return out;
230}
231
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100232static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100233get_output_state(struct weston_output *output)
234{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100235 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100236}
237
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300238static int
239gl_renderer_create_surface(struct weston_surface *surface);
240
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100241static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100242get_surface_state(struct weston_surface *surface)
243{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300244 if (!surface->renderer_state)
245 gl_renderer_create_surface(surface);
246
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100247 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100248}
249
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100250static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100251get_renderer(struct weston_compositor *ec)
252{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100253 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100254}
255
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300256static struct egl_image*
257egl_image_create(struct gl_renderer *gr, EGLenum target,
258 EGLClientBuffer buffer, const EGLint *attribs)
259{
260 struct egl_image *img;
261
262 img = zalloc(sizeof *img);
263 img->renderer = gr;
264 img->refcount = 1;
265 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
266 target, buffer, attribs);
267
268 if (img->image == EGL_NO_IMAGE_KHR) {
269 free(img);
270 return NULL;
271 }
272
273 return img;
274}
275
276static struct egl_image*
277egl_image_ref(struct egl_image *image)
278{
279 image->refcount++;
280
281 return image;
282}
283
284static int
285egl_image_unref(struct egl_image *image)
286{
287 struct gl_renderer *gr = image->renderer;
288
289 assert(image->refcount > 0);
290
291 image->refcount--;
292 if (image->refcount > 0)
293 return image->refcount;
294
295 gr->destroy_image(gr->egl_display, image->image);
296 free(image);
297
298 return 0;
299}
300
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000301static struct dmabuf_image*
302dmabuf_image_create(void)
303{
304 struct dmabuf_image *img;
305
306 img = zalloc(sizeof *img);
307 wl_list_init(&img->link);
308
309 return img;
310}
311
312static void
313dmabuf_image_destroy(struct dmabuf_image *image)
314{
315 int i;
316
317 for (i = 0; i < image->num_images; ++i)
318 egl_image_unref(image->images[i]);
319
320 if (image->dmabuf)
321 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
322
323 wl_list_remove(&image->link);
324}
325
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400326static const char *
327egl_error_string(EGLint code)
328{
329#define MYERRCODE(x) case x: return #x;
330 switch (code) {
331 MYERRCODE(EGL_SUCCESS)
332 MYERRCODE(EGL_NOT_INITIALIZED)
333 MYERRCODE(EGL_BAD_ACCESS)
334 MYERRCODE(EGL_BAD_ALLOC)
335 MYERRCODE(EGL_BAD_ATTRIBUTE)
336 MYERRCODE(EGL_BAD_CONTEXT)
337 MYERRCODE(EGL_BAD_CONFIG)
338 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
339 MYERRCODE(EGL_BAD_DISPLAY)
340 MYERRCODE(EGL_BAD_SURFACE)
341 MYERRCODE(EGL_BAD_MATCH)
342 MYERRCODE(EGL_BAD_PARAMETER)
343 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
344 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
345 MYERRCODE(EGL_CONTEXT_LOST)
346 default:
347 return "unknown";
348 }
349#undef MYERRCODE
350}
351
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300352static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200353gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400354{
355 EGLint code;
356
357 code = eglGetError();
358 weston_log("EGL error state: %s (0x%04lx)\n",
359 egl_error_string(code), (long)code);
360}
361
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400362#define max(a, b) (((a) > (b)) ? (a) : (b))
363#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400364
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300365/*
366 * Compute the boundary vertices of the intersection of the global coordinate
367 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
368 * 'surf_rect' when transformed from surface coordinates into global coordinates.
369 * The vertices are written to 'ex' and 'ey', and the return value is the
370 * number of vertices. Vertices are produced in clockwise winding order.
371 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
372 * polygon area.
373 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400374static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400376 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
377{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800378
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300379 struct clip_context ctx;
380 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400381 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300382 struct polygon8 surf = {
383 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
384 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
385 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400386 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400387
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300388 ctx.clip.x1 = rect->x1;
389 ctx.clip.y1 = rect->y1;
390 ctx.clip.x2 = rect->x2;
391 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400392
393 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300394 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
396 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400397
398 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300399 min_x = max_x = surf.x[0];
400 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400401
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300402 for (i = 1; i < surf.n; i++) {
403 min_x = min(min_x, surf.x[i]);
404 max_x = max(max_x, surf.x[i]);
405 min_y = min(min_y, surf.y[i]);
406 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400407 }
408
409 /* First, simple bounding box check to discard early transformed
410 * surface rects that do not intersect with the clip region:
411 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300412 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
413 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400414 return 0;
415
416 /* Simple case, bounding box edges are parallel to surface edges,
417 * there will be only four edges. We just need to clip the surface
418 * vertices to the clip rect bounds:
419 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800421 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400422
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300423 /* Transformed case: use a general polygon clipping algorithm to
424 * clip the surface rectangle with each side of 'rect'.
425 * The algorithm is Sutherland-Hodgman, as explained in
426 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
427 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400428 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800429 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300430
431 if (n < 3)
432 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400433
434 return n;
435}
436
Derek Foremanf8180982014-10-16 16:37:02 -0500437static bool
438merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
439{
440 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
441 merge->x1 = a->x1;
442 merge->x2 = a->x2;
443 merge->y1 = b->y1;
444 merge->y2 = a->y2;
445 return true;
446 }
447 return false;
448}
449
450static int
451compress_bands(pixman_box32_t *inrects, int nrects,
452 pixman_box32_t **outrects)
453{
454 bool merged;
455 pixman_box32_t *out, merge_rect;
456 int i, j, nout;
457
458 if (!nrects) {
459 *outrects = NULL;
460 return 0;
461 }
462
463 /* nrects is an upper bound - we're not too worried about
464 * allocating a little extra
465 */
466 out = malloc(sizeof(pixman_box32_t) * nrects);
467 out[0] = inrects[0];
468 nout = 1;
469 for (i = 1; i < nrects; i++) {
470 for (j = 0; j < nout; j++) {
471 merged = merge_down(&inrects[i], &out[j], &merge_rect);
472 if (merged) {
473 out[j] = merge_rect;
474 break;
475 }
476 }
477 if (!merged) {
478 out[nout] = inrects[i];
479 nout++;
480 }
481 }
482 *outrects = out;
483 return nout;
484}
485
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400486static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500487texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400488 pixman_region32_t *surf_region)
489{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500490 struct gl_surface_state *gs = get_surface_state(ev->surface);
491 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400492 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400493 GLfloat *v, inv_width, inv_height;
494 unsigned int *vtxcnt, nvtx = 0;
495 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500496 pixman_box32_t *raw_rects;
497 int i, j, k, nrects, nsurf, raw_nrects;
498 bool used_band_compression;
499 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400500 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
501
Derek Foremanf8180982014-10-16 16:37:02 -0500502 if (raw_nrects < 4) {
503 used_band_compression = false;
504 nrects = raw_nrects;
505 rects = raw_rects;
506 } else {
507 nrects = compress_bands(raw_rects, raw_nrects, &rects);
508 used_band_compression = true;
509 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400510 /* worst case we can have 8 vertices per rect (ie. clipped into
511 * an octagon):
512 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400513 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
514 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400515
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200516 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200517 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400518
519 for (i = 0; i < nrects; i++) {
520 pixman_box32_t *rect = &rects[i];
521 for (j = 0; j < nsurf; j++) {
522 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200523 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400524 GLfloat ex[8], ey[8]; /* edge points in screen space */
525 int n;
526
527 /* The transformed surface, after clipping to the clip region,
528 * can have as many as eight sides, emitted as a triangle-fan.
529 * The first vertex in the triangle fan can be chosen arbitrarily,
530 * since the area is guaranteed to be convex.
531 *
532 * If a corner of the transformed surface falls outside of the
533 * clip region, instead of emitting one vertex for the corner
534 * of the surface, up to two are emitted for two corresponding
535 * intersection point(s) between the surface and the clip region.
536 *
537 * To do this, we first calculate the (up to eight) points that
538 * form the intersection of the clip rect and the transformed
539 * surface.
540 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500541 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400542 if (n < 3)
543 continue;
544
545 /* emit edge points: */
546 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500547 weston_view_from_global_float(ev, ex[k], ey[k],
548 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400549 /* position: */
550 *(v++) = ex[k];
551 *(v++) = ey[k];
552 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 weston_surface_to_buffer_float(ev->surface,
554 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200555 &bx, &by);
556 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400557 if (gs->y_inverted) {
558 *(v++) = by * inv_height;
559 } else {
560 *(v++) = (gs->height - by) * inv_height;
561 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400562 }
563
564 vtxcnt[nvtx++] = n;
565 }
566 }
567
Derek Foremanf8180982014-10-16 16:37:02 -0500568 if (used_band_compression)
569 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400570 return nvtx;
571}
572
573static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500574triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400575{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100577 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400578 int i;
579 GLushort *buffer;
580 GLushort *index;
581 int nelems;
582 static int color_idx = 0;
583 static const GLfloat color[][4] = {
584 { 1.0, 0.0, 0.0, 1.0 },
585 { 0.0, 1.0, 0.0, 1.0 },
586 { 0.0, 0.0, 1.0, 1.0 },
587 { 1.0, 1.0, 1.0, 1.0 },
588 };
589
590 nelems = (count - 1 + count - 2) * 2;
591
592 buffer = malloc(sizeof(GLushort) * nelems);
593 index = buffer;
594
595 for (i = 1; i < count; i++) {
596 *index++ = first;
597 *index++ = first + i;
598 }
599
600 for (i = 2; i < count; i++) {
601 *index++ = first + i - 1;
602 *index++ = first + i;
603 }
604
John Kåre Alsaker40684142012-11-13 19:10:25 +0100605 glUseProgram(gr->solid_shader.program);
606 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400607 color[color_idx++ % ARRAY_LENGTH(color)]);
608 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100609 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400610 free(buffer);
611}
612
613static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400615 pixman_region32_t *surf_region)
616{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500617 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400618 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400619 GLfloat *v;
620 unsigned int *vtxcnt;
621 int i, first, nfans;
622
623 /* The final region to be painted is the intersection of
624 * 'region' and 'surf_region'. However, 'region' is in the global
625 * coordinates, and 'surf_region' is in the surface-local
626 * coordinates. texture_region() will iterate over all pairs of
627 * rectangles from both regions, compute the intersection
628 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800629 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400630 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500631 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400632
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400633 v = gr->vertices.data;
634 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400635
636 /* position: */
637 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
638 glEnableVertexAttribArray(0);
639
640 /* texcoord: */
641 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
642 glEnableVertexAttribArray(1);
643
644 for (i = 0, first = 0; i < nfans; i++) {
645 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400646 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400648 first += vtxcnt[i];
649 }
650
651 glDisableVertexAttribArray(1);
652 glDisableVertexAttribArray(0);
653
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400654 gr->vertices.size = 0;
655 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400656}
657
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100658static int
659use_output(struct weston_output *output)
660{
661 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100662 struct gl_output_state *go = get_output_state(output);
663 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100664 EGLBoolean ret;
665
666 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
667 go->egl_surface, gr->egl_context);
668
669 if (ret == EGL_FALSE) {
670 if (errored)
671 return -1;
672 errored = 1;
673 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200674 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100675 return -1;
676 }
677
678 return 0;
679}
680
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300681static int
682shader_init(struct gl_shader *shader, struct gl_renderer *gr,
683 const char *vertex_source, const char *fragment_source);
684
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400685static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300686use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400687{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300688 if (!shader->program) {
689 int ret;
690
691 ret = shader_init(shader, gr,
692 shader->vertex_source,
693 shader->fragment_source);
694
695 if (ret < 0)
696 weston_log("warning: failed to compile shader\n");
697 }
698
John Kåre Alsaker40684142012-11-13 19:10:25 +0100699 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400700 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400701 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100702 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400703}
704
705static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100706shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500707 struct weston_view *view,
708 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400709{
710 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500712 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713
714 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500715 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100716 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400718
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100719 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400720 glUniform1i(shader->tex_uniforms[i], i);
721}
722
723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500724draw_view(struct weston_view *ev, struct weston_output *output,
725 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400726{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100728 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500729 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400730 /* repaint bounding region in global coordinates: */
731 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200732 /* opaque region in surface coordinates: */
733 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734 /* non-opaque region in surface coordinates: */
735 pixman_region32_t surface_blend;
736 GLint filter;
737 int i;
738
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200739 /* In case of a runtime switch of renderers, we may not have received
740 * an attach for this surface since the switch. In that case we don't
741 * have a valid buffer or a proper shader set up so skip rendering. */
742 if (!gs->shader)
743 return;
744
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400745 pixman_region32_init(&repaint);
746 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200747 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500748 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400749
750 if (!pixman_region32_not_empty(&repaint))
751 goto out;
752
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400753 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
754
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400755 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100756 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500757 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400758 }
759
John Kåre Alsaker40684142012-11-13 19:10:25 +0100760 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500761 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762
Jason Ekstranda7af7042013-10-12 22:38:11 -0500763 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200764 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400765 filter = GL_LINEAR;
766 else
767 filter = GL_NEAREST;
768
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100769 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400770 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100771 glBindTexture(gs->target, gs->textures[i]);
772 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
773 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 }
775
776 /* blended region is whole surface minus opaque region: */
777 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600778 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200779 if (ev->geometry.scissor_enabled)
780 pixman_region32_intersect(&surface_blend, &surface_blend,
781 &ev->geometry.scissor);
782 pixman_region32_subtract(&surface_blend, &surface_blend,
783 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400784
Jason Ekstranda7af7042013-10-12 22:38:11 -0500785 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200786 pixman_region32_init(&surface_opaque);
787 if (ev->geometry.scissor_enabled)
788 pixman_region32_intersect(&surface_opaque,
789 &ev->surface->opaque,
790 &ev->geometry.scissor);
791 else
792 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
793
794 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100795 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400796 /* Special case for RGBA textures with possibly
797 * bad data in alpha channel: use the shader
798 * that forces texture alpha = 1.0.
799 * Xwayland surfaces need this.
800 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100801 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500802 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400803 }
804
Jason Ekstranda7af7042013-10-12 22:38:11 -0500805 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 glEnable(GL_BLEND);
807 else
808 glDisable(GL_BLEND);
809
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200810 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400811 }
812
813 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100814 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400815 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500816 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400817 }
818
819 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200820 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400821
822out:
823 pixman_region32_fini(&repaint);
824}
825
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400826static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400828{
829 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500830 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400831
Jason Ekstranda7af7042013-10-12 22:38:11 -0500832 wl_list_for_each_reverse(view, &compositor->view_list, link)
833 if (view->plane == &compositor->primary_plane)
834 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400835}
836
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500837static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600838draw_output_border_texture(struct gl_output_state *go,
839 enum gl_renderer_border_side side,
840 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500841 int32_t width, int32_t height)
842{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600843 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500844 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
845
846 if (!img->data) {
847 if (img->tex) {
848 glDeleteTextures(1, &img->tex);
849 img->tex = 0;
850 }
851
852 return;
853 }
854
855 if (!img->tex) {
856 glGenTextures(1, &img->tex);
857 glBindTexture(GL_TEXTURE_2D, img->tex);
858
859 glTexParameteri(GL_TEXTURE_2D,
860 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
861 glTexParameteri(GL_TEXTURE_2D,
862 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
863 glTexParameteri(GL_TEXTURE_2D,
864 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
865 glTexParameteri(GL_TEXTURE_2D,
866 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
867 } else {
868 glBindTexture(GL_TEXTURE_2D, img->tex);
869 }
870
Jason Ekstrande5512d42014-02-04 21:36:38 -0600871 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500872 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
873 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
874 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500875 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
876 img->tex_width, img->height, 0,
877 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500878 }
879
880 GLfloat texcoord[] = {
881 0.0f, 0.0f,
882 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
883 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
884 0.0f, 1.0f,
885 };
886
887 GLfloat verts[] = {
888 x, y,
889 x + width, y,
890 x + width, y + height,
891 x, y + height
892 };
893
894 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
895 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
896 glEnableVertexAttribArray(0);
897 glEnableVertexAttribArray(1);
898
899 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
900
901 glDisableVertexAttribArray(1);
902 glDisableVertexAttribArray(0);
903}
904
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600905static int
906output_has_borders(struct weston_output *output)
907{
908 struct gl_output_state *go = get_output_state(output);
909
910 return go->borders[GL_RENDERER_BORDER_TOP].data ||
911 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
912 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
913 go->borders[GL_RENDERER_BORDER_LEFT].data;
914}
915
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500916static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600917draw_output_borders(struct weston_output *output,
918 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500919{
920 struct gl_output_state *go = get_output_state(output);
921 struct gl_renderer *gr = get_renderer(output->compositor);
922 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500923 struct gl_border_image *top, *bottom, *left, *right;
924 struct weston_matrix matrix;
925 int full_width, full_height;
926
Jason Ekstrande5512d42014-02-04 21:36:38 -0600927 if (border_status == BORDER_STATUS_CLEAN)
928 return; /* Clean. Nothing to do. */
929
Jason Ekstrand00b84282013-10-27 22:24:59 -0500930 top = &go->borders[GL_RENDERER_BORDER_TOP];
931 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
932 left = &go->borders[GL_RENDERER_BORDER_LEFT];
933 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
934
935 full_width = output->current_mode->width + left->width + right->width;
936 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500937
938 glDisable(GL_BLEND);
939 use_shader(gr, shader);
940
Jason Ekstrand00b84282013-10-27 22:24:59 -0500941 glViewport(0, 0, full_width, full_height);
942
943 weston_matrix_init(&matrix);
944 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
945 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
946 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500947
948 glUniform1i(shader->tex_uniforms[0], 0);
949 glUniform1f(shader->alpha_uniform, 1);
950 glActiveTexture(GL_TEXTURE0);
951
Jason Ekstrande5512d42014-02-04 21:36:38 -0600952 if (border_status & BORDER_TOP_DIRTY)
953 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
954 0, 0,
955 full_width, top->height);
956 if (border_status & BORDER_LEFT_DIRTY)
957 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
958 0, top->height,
959 left->width, output->current_mode->height);
960 if (border_status & BORDER_RIGHT_DIRTY)
961 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
962 full_width - right->width, top->height,
963 right->width, output->current_mode->height);
964 if (border_status & BORDER_BOTTOM_DIRTY)
965 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
966 0, full_height - bottom->height,
967 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500968}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100969
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400970static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600971output_get_border_damage(struct weston_output *output,
972 enum gl_border_status border_status,
973 pixman_region32_t *damage)
974{
975 struct gl_output_state *go = get_output_state(output);
976 struct gl_border_image *top, *bottom, *left, *right;
977 int full_width, full_height;
978
979 if (border_status == BORDER_STATUS_CLEAN)
980 return; /* Clean. Nothing to do. */
981
982 top = &go->borders[GL_RENDERER_BORDER_TOP];
983 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
984 left = &go->borders[GL_RENDERER_BORDER_LEFT];
985 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
986
987 full_width = output->current_mode->width + left->width + right->width;
988 full_height = output->current_mode->height + top->height + bottom->height;
989 if (border_status & BORDER_TOP_DIRTY)
990 pixman_region32_union_rect(damage, damage,
991 0, 0,
992 full_width, top->height);
993 if (border_status & BORDER_LEFT_DIRTY)
994 pixman_region32_union_rect(damage, damage,
995 0, top->height,
996 left->width, output->current_mode->height);
997 if (border_status & BORDER_RIGHT_DIRTY)
998 pixman_region32_union_rect(damage, damage,
999 full_width - right->width, top->height,
1000 right->width, output->current_mode->height);
1001 if (border_status & BORDER_BOTTOM_DIRTY)
1002 pixman_region32_union_rect(damage, damage,
1003 0, full_height - bottom->height,
1004 full_width, bottom->height);
1005}
1006
1007static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001008output_get_damage(struct weston_output *output,
1009 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001010{
1011 struct gl_output_state *go = get_output_state(output);
1012 struct gl_renderer *gr = get_renderer(output->compositor);
1013 EGLint buffer_age = 0;
1014 EGLBoolean ret;
1015 int i;
1016
1017 if (gr->has_egl_buffer_age) {
1018 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1019 EGL_BUFFER_AGE_EXT, &buffer_age);
1020 if (ret == EGL_FALSE) {
1021 weston_log("buffer age query failed.\n");
1022 gl_renderer_print_egl_error_state();
1023 }
1024 }
1025
Jason Ekstrande5512d42014-02-04 21:36:38 -06001026 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001027 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001028 *border_damage = BORDER_ALL_DIRTY;
1029 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001030 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001031 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001032
1033 if (*border_damage & BORDER_SIZE_CHANGED) {
1034 /* If we've had a resize, we have to do a full
1035 * repaint. */
1036 *border_damage |= BORDER_ALL_DIRTY;
1037 pixman_region32_copy(buffer_damage, &output->region);
1038 } else {
1039 for (i = 0; i < buffer_age - 1; i++)
1040 pixman_region32_union(buffer_damage,
1041 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001042 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001043 }
1044 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001045}
1046
1047static void
1048output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001049 pixman_region32_t *output_damage,
1050 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001051{
1052 struct gl_output_state *go = get_output_state(output);
1053 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001054
1055 if (!gr->has_egl_buffer_age)
1056 return;
1057
Derek Foreman4c582662014-10-09 18:39:44 -05001058 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1059 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001060
Derek Foreman4c582662014-10-09 18:39:44 -05001061 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1062 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001063}
1064
Derek Foremanc4cfe852015-05-15 12:12:40 -05001065/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1066 * unavailable, so we're assuming the background has no transparency
1067 * and that everything with a blend, like drop shadows, will have something
1068 * opaque (like the background) drawn underneath it.
1069 *
1070 * Depending on the underlying hardware, violating that assumption could
1071 * result in seeing through to another display plane.
1072 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001073static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001074gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001075 pixman_region32_t *output_damage)
1076{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001077 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001078 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001079 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001080 EGLBoolean ret;
1081 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001082 int i, nrects, buffer_height;
1083 EGLint *egl_damage, *d;
1084 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001085 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001086 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001087
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001088 if (use_output(output) < 0)
1089 return;
1090
Jason Ekstrand00b84282013-10-27 22:24:59 -05001091 /* Calculate the viewport */
1092 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1093 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1094 output->current_mode->width,
1095 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001096
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001097 /* Calculate the global GL matrix */
1098 go->output_matrix = output->matrix;
1099 weston_matrix_translate(&go->output_matrix,
1100 -(output->current_mode->width / 2.0),
1101 -(output->current_mode->height / 2.0), 0);
1102 weston_matrix_scale(&go->output_matrix,
1103 2.0 / output->current_mode->width,
1104 -2.0 / output->current_mode->height, 1);
1105
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001106 /* if debugging, redraw everything outside the damage to clean up
1107 * debug lines from the previous draw on this buffer:
1108 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001109 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001110 pixman_region32_t undamaged;
1111 pixman_region32_init(&undamaged);
1112 pixman_region32_subtract(&undamaged, &output->region,
1113 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001114 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001116 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001117 pixman_region32_fini(&undamaged);
1118 }
1119
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001120 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001121 pixman_region32_init(&buffer_damage);
1122
Jason Ekstrande5512d42014-02-04 21:36:38 -06001123 output_get_damage(output, &buffer_damage, &border_damage);
1124 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001125
1126 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001127 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001128
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001130
1131 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001132 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001133
Jason Ekstrande5512d42014-02-04 21:36:38 -06001134 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001135
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001136 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001137 wl_signal_emit(&output->frame_signal, output);
1138
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001139 if (gr->swap_buffers_with_damage) {
1140 pixman_region32_init(&buffer_damage);
1141 weston_transformed_region(output->width, output->height,
1142 output->transform,
1143 output->current_scale,
1144 output_damage, &buffer_damage);
1145
1146 if (output_has_borders(output)) {
1147 pixman_region32_translate(&buffer_damage,
1148 go->borders[GL_RENDERER_BORDER_LEFT].width,
1149 go->borders[GL_RENDERER_BORDER_TOP].height);
1150 output_get_border_damage(output, go->border_status,
1151 &buffer_damage);
1152 }
1153
1154 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1155 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1156
1157 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1158 output->current_mode->height +
1159 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1160
1161 d = egl_damage;
1162 for (i = 0; i < nrects; ++i) {
1163 *d++ = rects[i].x1;
1164 *d++ = buffer_height - rects[i].y2;
1165 *d++ = rects[i].x2 - rects[i].x1;
1166 *d++ = rects[i].y2 - rects[i].y1;
1167 }
1168 ret = gr->swap_buffers_with_damage(gr->egl_display,
1169 go->egl_surface,
1170 egl_damage, nrects);
1171 free(egl_damage);
1172 pixman_region32_fini(&buffer_damage);
1173 } else {
1174 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1175 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001176
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001177 if (ret == EGL_FALSE && !errored) {
1178 errored = 1;
1179 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001180 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001181 }
1182
Jason Ekstrande5512d42014-02-04 21:36:38 -06001183 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001184}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001185
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001186static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001187gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001188 pixman_format_code_t format, void *pixels,
1189 uint32_t x, uint32_t y,
1190 uint32_t width, uint32_t height)
1191{
1192 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001193 struct gl_output_state *go = get_output_state(output);
1194
1195 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1196 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001197
1198 switch (format) {
1199 case PIXMAN_a8r8g8b8:
1200 gl_format = GL_BGRA_EXT;
1201 break;
1202 case PIXMAN_a8b8g8r8:
1203 gl_format = GL_RGBA;
1204 break;
1205 default:
1206 return -1;
1207 }
1208
1209 if (use_output(output) < 0)
1210 return -1;
1211
1212 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1213 glReadPixels(x, y, width, height, gl_format,
1214 GL_UNSIGNED_BYTE, pixels);
1215
1216 return 0;
1217}
1218
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001219static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001220gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001221{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001222 struct gl_renderer *gr = get_renderer(surface->compositor);
1223 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001224 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001226 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001227 pixman_box32_t *rectangles;
1228 void *data;
1229 int i, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001230
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001231 pixman_region32_union(&gs->texture_damage,
1232 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001233
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001234 if (!buffer)
1235 return;
1236
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001237 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001238 * We still accumulate the damage in texture_damage, and
1239 * hold the reference to the buffer, in case the surface
1240 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001241 */
Derek Foreman97746792015-11-18 16:32:28 -06001242 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 wl_list_for_each(view, &surface->views, surface_link) {
1244 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001245 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 break;
1247 }
1248 }
1249 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001250 return;
1251
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001252 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1253 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001254 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001255
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001256 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001257
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001258 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001259 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001260 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001261 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001262 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001263 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001264 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001265
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001266 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001267 }
1268
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001269 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001270 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001271
1272 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001273 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1274 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001275 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001276 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001277 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001278 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001279 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001280 goto done;
1281 }
1282
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001283 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001284 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001285 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001286 pixman_box32_t r;
1287
1288 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1289
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001290 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1291 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001292 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1293 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001294 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001295 }
Neil Robertse5051712013-11-13 15:44:06 +00001296 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001297
1298done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001299 pixman_region32_fini(&gs->texture_damage);
1300 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001301 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001302
1303 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001304}
1305
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001306static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001307ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001308{
1309 int i;
1310
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001311 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001312 return;
1313
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001314 for (i = gs->num_textures; i < num_textures; i++) {
1315 glGenTextures(1, &gs->textures[i]);
1316 glBindTexture(gs->target, gs->textures[i]);
1317 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001318 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001319 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001320 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1321 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001322 gs->num_textures = num_textures;
1323 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001324}
1325
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001326static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001327gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1328 struct wl_shm_buffer *shm_buffer)
1329{
1330 struct weston_compositor *ec = es->compositor;
1331 struct gl_renderer *gr = get_renderer(ec);
1332 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001333 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001334 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001335
1336 buffer->shm_buffer = shm_buffer;
1337 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1338 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1339
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001340 switch (wl_shm_buffer_get_format(shm_buffer)) {
1341 case WL_SHM_FORMAT_XRGB8888:
1342 gs->shader = &gr->texture_shader_rgbx;
1343 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001344 gl_format = GL_BGRA_EXT;
1345 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001346 break;
1347 case WL_SHM_FORMAT_ARGB8888:
1348 gs->shader = &gr->texture_shader_rgba;
1349 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001350 gl_format = GL_BGRA_EXT;
1351 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001352 break;
1353 case WL_SHM_FORMAT_RGB565:
1354 gs->shader = &gr->texture_shader_rgbx;
1355 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001356 gl_format = GL_RGB;
1357 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001358 break;
1359 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001360 weston_log("warning: unknown shm buffer format: %08x\n",
1361 wl_shm_buffer_get_format(shm_buffer));
1362 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001363 }
1364
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001365 /* Only allocate a texture if it doesn't match existing one.
1366 * If a switch from DRM allocated buffer to a SHM buffer is
1367 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001368 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001369 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001370 gl_format != gs->gl_format ||
1371 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001372 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001373 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001374 gs->height = buffer->height;
1375 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001376 gs->gl_format = gl_format;
1377 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001378 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001379 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001380 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001381
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001382 gs->surface = es;
1383
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001384 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001385 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001386}
1387
1388static void
1389gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1390 uint32_t format)
1391{
1392 struct weston_compositor *ec = es->compositor;
1393 struct gl_renderer *gr = get_renderer(ec);
1394 struct gl_surface_state *gs = get_surface_state(es);
1395 EGLint attribs[3];
1396 int i, num_planes;
1397
1398 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1399 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1400 EGL_WIDTH, &buffer->width);
1401 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1402 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001403 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1404 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001405
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001406 for (i = 0; i < gs->num_images; i++) {
1407 egl_image_unref(gs->images[i]);
1408 gs->images[i] = NULL;
1409 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001410 gs->num_images = 0;
1411 gs->target = GL_TEXTURE_2D;
1412 switch (format) {
1413 case EGL_TEXTURE_RGB:
1414 case EGL_TEXTURE_RGBA:
1415 default:
1416 num_planes = 1;
1417 gs->shader = &gr->texture_shader_rgba;
1418 break;
1419 case EGL_TEXTURE_EXTERNAL_WL:
1420 num_planes = 1;
1421 gs->target = GL_TEXTURE_EXTERNAL_OES;
1422 gs->shader = &gr->texture_shader_egl_external;
1423 break;
1424 case EGL_TEXTURE_Y_UV_WL:
1425 num_planes = 2;
1426 gs->shader = &gr->texture_shader_y_uv;
1427 break;
1428 case EGL_TEXTURE_Y_U_V_WL:
1429 num_planes = 3;
1430 gs->shader = &gr->texture_shader_y_u_v;
1431 break;
1432 case EGL_TEXTURE_Y_XUXV_WL:
1433 num_planes = 2;
1434 gs->shader = &gr->texture_shader_y_xuxv;
1435 break;
1436 }
1437
1438 ensure_textures(gs, num_planes);
1439 for (i = 0; i < num_planes; i++) {
1440 attribs[0] = EGL_WAYLAND_PLANE_WL;
1441 attribs[1] = i;
1442 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001443 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001444 EGL_WAYLAND_BUFFER_WL,
1445 buffer->legacy_buffer,
1446 attribs);
1447 if (!gs->images[i]) {
1448 weston_log("failed to create img for plane %d\n", i);
1449 continue;
1450 }
1451 gs->num_images++;
1452
1453 glActiveTexture(GL_TEXTURE0 + i);
1454 glBindTexture(gs->target, gs->textures[i]);
1455 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001456 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001457 }
1458
1459 gs->pitch = buffer->width;
1460 gs->height = buffer->height;
1461 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001462 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001463}
1464
1465static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001466gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1467{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001468 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001469
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001470 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001471}
1472
1473static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001474import_simple_dmabuf(struct gl_renderer *gr,
1475 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001476{
1477 struct egl_image *image;
1478 EGLint attribs[30];
1479 int atti = 0;
1480
Pekka Paalanena3525802014-06-12 16:49:29 +03001481 /* This requires the Mesa commit in
1482 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1483 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1484 * Otherwise Mesa closes the fd behind our back and re-importing
1485 * will fail.
1486 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1487 */
1488
1489 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001490 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001491 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001492 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001493 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001494 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001495 /* XXX: Add modifier here when supported */
1496
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001497 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001498 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001499 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001500 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001501 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001502 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001503 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001504 }
1505
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001506 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001507 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001508 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001509 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001510 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001511 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001512 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001513 }
1514
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001515 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001516 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001517 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001518 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001519 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001520 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001521 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001522 }
1523
1524 attribs[atti++] = EGL_NONE;
1525
1526 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1527 attribs);
1528
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001529 return image;
1530}
Pekka Paalanena3525802014-06-12 16:49:29 +03001531
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001532/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1533 * some of the definitions here so that building Weston won't require
1534 * bleeding-edge kernel headers.
1535 */
1536#ifndef DRM_FORMAT_R8
1537#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1538#endif
1539
1540#ifndef DRM_FORMAT_GR88
1541#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1542#endif
1543
1544struct yuv_format_descriptor yuv_formats[] = {
1545 {
1546 .format = DRM_FORMAT_YUYV,
1547 .input_planes = 1,
1548 .output_planes = 2,
1549 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1550 {{
1551 .width_divisor = 1,
1552 .height_divisor = 1,
1553 .format = DRM_FORMAT_GR88,
1554 .plane_index = 0
1555 }, {
1556 .width_divisor = 2,
1557 .height_divisor = 1,
1558 .format = DRM_FORMAT_ARGB8888,
1559 .plane_index = 0
1560 }}
1561 }, {
1562 .format = DRM_FORMAT_NV12,
1563 .input_planes = 2,
1564 .output_planes = 2,
1565 .texture_type = EGL_TEXTURE_Y_UV_WL,
1566 {{
1567 .width_divisor = 1,
1568 .height_divisor = 1,
1569 .format = DRM_FORMAT_R8,
1570 .plane_index = 0
1571 }, {
1572 .width_divisor = 2,
1573 .height_divisor = 2,
1574 .format = DRM_FORMAT_GR88,
1575 .plane_index = 1
1576 }}
1577 }, {
1578 .format = DRM_FORMAT_YUV420,
1579 .input_planes = 3,
1580 .output_planes = 3,
1581 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1582 {{
1583 .width_divisor = 1,
1584 .height_divisor = 1,
1585 .format = DRM_FORMAT_R8,
1586 .plane_index = 0
1587 }, {
1588 .width_divisor = 2,
1589 .height_divisor = 2,
1590 .format = DRM_FORMAT_R8,
1591 .plane_index = 1
1592 }, {
1593 .width_divisor = 2,
1594 .height_divisor = 2,
1595 .format = DRM_FORMAT_R8,
1596 .plane_index = 2
1597 }}
1598 }
1599};
1600
1601static struct egl_image *
1602import_dmabuf_single_plane(struct gl_renderer *gr,
1603 const struct dmabuf_attributes *attributes,
1604 struct yuv_plane_descriptor *descriptor)
1605{
1606 struct dmabuf_attributes plane;
1607 struct egl_image *image;
1608 char fmt[4];
1609
1610 plane.width = attributes->width / descriptor->width_divisor;
1611 plane.height = attributes->height / descriptor->height_divisor;
1612 plane.format = descriptor->format;
1613 plane.n_planes = 1;
1614 plane.fd[0] = attributes->fd[descriptor->plane_index];
1615 plane.offset[0] = attributes->offset[descriptor->plane_index];
1616 plane.stride[0] = attributes->stride[descriptor->plane_index];
1617 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1618
1619 image = import_simple_dmabuf(gr, &plane);
1620 if (!image) {
1621 weston_log("Failed to import plane %d as %.4s\n",
1622 descriptor->plane_index,
1623 dump_format(descriptor->format, fmt));
1624 return NULL;
1625 }
1626
1627 return image;
1628}
1629
1630static bool
1631import_yuv_dmabuf(struct gl_renderer *gr,
1632 struct dmabuf_image *image)
1633{
1634 unsigned i;
1635 int j;
1636 int ret;
1637 struct yuv_format_descriptor *format = NULL;
1638 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1639 char fmt[4];
1640
1641 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1642 if (yuv_formats[i].format == attributes->format) {
1643 format = &yuv_formats[i];
1644 break;
1645 }
1646 }
1647
1648 if (!format) {
1649 weston_log("Error during import, and no known conversion for format "
1650 "%.4s in the renderer",
1651 dump_format(attributes->format, fmt));
1652 return false;
1653 }
1654
1655 if (attributes->n_planes != format->input_planes) {
1656 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1657 dump_format(format->format, fmt),
1658 format->input_planes,
1659 (format->input_planes > 1) ? "s" : "",
1660 attributes->n_planes);
1661 return false;
1662 }
1663
1664 for (j = 0; j < format->output_planes; ++j) {
1665 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1666 &format->plane[j]);
1667 if (!image->images[j]) {
1668 while (j) {
1669 ret = egl_image_unref(image->images[--j]);
1670 assert(ret == 0);
1671 }
1672 return false;
1673 }
1674 }
1675
1676 image->num_images = format->output_planes;
1677
1678 switch (format->texture_type) {
1679 case EGL_TEXTURE_Y_XUXV_WL:
1680 image->shader = &gr->texture_shader_y_xuxv;
1681 break;
1682 case EGL_TEXTURE_Y_UV_WL:
1683 image->shader = &gr->texture_shader_y_uv;
1684 break;
1685 case EGL_TEXTURE_Y_U_V_WL:
1686 image->shader = &gr->texture_shader_y_u_v;
1687 break;
1688 default:
1689 assert(false);
1690 }
1691
1692 return true;
1693}
1694
1695static GLenum
1696choose_texture_target(struct dmabuf_attributes *attributes)
1697{
1698 if (attributes->n_planes > 1)
1699 return GL_TEXTURE_EXTERNAL_OES;
1700
1701 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1702 case DRM_FORMAT_YUYV:
1703 case DRM_FORMAT_YVYU:
1704 case DRM_FORMAT_UYVY:
1705 case DRM_FORMAT_VYUY:
1706 case DRM_FORMAT_AYUV:
1707 return GL_TEXTURE_EXTERNAL_OES;
1708 default:
1709 return GL_TEXTURE_2D;
1710 }
1711}
1712
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001713static struct dmabuf_image *
1714import_dmabuf(struct gl_renderer *gr,
1715 struct linux_dmabuf_buffer *dmabuf)
1716{
1717 struct egl_image *egl_image;
1718 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001719
1720 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001721 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001722
1723 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1724 if (egl_image) {
1725 image->num_images = 1;
1726 image->images[0] = egl_image;
1727 image->import_type = IMPORT_TYPE_DIRECT;
1728 image->target = choose_texture_target(&dmabuf->attributes);
1729
1730 switch (image->target) {
1731 case GL_TEXTURE_2D:
1732 image->shader = &gr->texture_shader_rgba;
1733 break;
1734 default:
1735 image->shader = &gr->texture_shader_egl_external;
1736 }
1737 } else {
1738 if (!import_yuv_dmabuf(gr, image)) {
1739 dmabuf_image_destroy(image);
1740 return NULL;
1741 }
1742 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1743 image->target = GL_TEXTURE_2D;
1744 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001745
1746 return image;
1747}
1748
1749static bool
1750gl_renderer_import_dmabuf(struct weston_compositor *ec,
1751 struct linux_dmabuf_buffer *dmabuf)
1752{
1753 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001754 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001755 int i;
1756
1757 assert(gr->has_dmabuf_import);
1758
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001759 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001760 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001761 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001762 return false;
1763 }
1764
1765 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001766 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001767 return false;
1768
1769 image = import_dmabuf(gr, dmabuf);
1770 if (!image)
1771 return false;
1772
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001773 wl_list_insert(&gr->dmabuf_images, &image->link);
1774 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1775 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001776
1777 return true;
1778}
1779
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001780static bool
1781import_known_dmabuf(struct gl_renderer *gr,
1782 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001783{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001784 switch (image->import_type) {
1785 case IMPORT_TYPE_DIRECT:
1786 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1787 if (!image->images[0])
1788 return false;
1789 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001790
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001791 case IMPORT_TYPE_GL_CONVERSION:
1792 if (!import_yuv_dmabuf(gr, image))
1793 return false;
1794 break;
1795
Pekka Paalanena3525802014-06-12 16:49:29 +03001796 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001797 weston_log("Invalid import type for dmabuf\n");
1798 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001799 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001800
1801 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001802}
1803
1804static void
1805gl_renderer_attach_dmabuf(struct weston_surface *surface,
1806 struct weston_buffer *buffer,
1807 struct linux_dmabuf_buffer *dmabuf)
1808{
1809 struct gl_renderer *gr = get_renderer(surface->compositor);
1810 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001811 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001812 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001813 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001814
1815 if (!gr->has_dmabuf_import) {
1816 linux_dmabuf_buffer_send_server_error(dmabuf,
1817 "EGL dmabuf import not supported");
1818 return;
1819 }
1820
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001821 buffer->width = dmabuf->attributes.width;
1822 buffer->height = dmabuf->attributes.height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001823 buffer->y_inverted =
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001824 !!(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001825
1826 for (i = 0; i < gs->num_images; i++)
1827 egl_image_unref(gs->images[i]);
1828 gs->num_images = 0;
1829
Pekka Paalanena3525802014-06-12 16:49:29 +03001830 /*
1831 * We try to always hold an imported EGLImage from the dmabuf
1832 * to prevent the client from preventing re-imports. But, we also
1833 * need to re-import every time the contents may change because
1834 * GL driver's caching may need flushing.
1835 *
1836 * Here we release the cache reference which has to be final.
1837 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001838 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001839
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001840 /* The dmabuf_image should have been created during the import */
1841 assert(image != NULL);
1842
1843 for (i = 0; i < image->num_images; ++i) {
1844 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001845 assert(ret == 0);
1846 }
1847
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001848 if (!import_known_dmabuf(gr, image)) {
1849 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1850 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001851 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001852
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001853 gs->num_images = image->num_images;
1854 for (i = 0; i < gs->num_images; ++i)
1855 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001856
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001857 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001858 ensure_textures(gs, gs->num_images);
1859 for (i = 0; i < gs->num_images; ++i) {
1860 glActiveTexture(GL_TEXTURE0 + i);
1861 glBindTexture(gs->target, gs->textures[i]);
1862 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1863 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001864
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001865 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001866 gs->pitch = buffer->width;
1867 gs->height = buffer->height;
1868 gs->buffer_type = BUFFER_TYPE_EGL;
1869 gs->y_inverted = buffer->y_inverted;
1870}
1871
1872static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001873gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001874{
1875 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001876 struct gl_renderer *gr = get_renderer(ec);
1877 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001878 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001879 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001880 EGLint format;
1881 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001882
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001883 weston_buffer_reference(&gs->buffer_ref, buffer);
1884
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001885 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001886 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001887 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001888 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001889 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001890 gs->num_images = 0;
1891 glDeleteTextures(gs->num_textures, gs->textures);
1892 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001893 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001894 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001895 return;
1896 }
1897
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001898 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001900 if (shm_buffer)
1901 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001902 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001903 EGL_TEXTURE_FORMAT, &format))
1904 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001905 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1906 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001907 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001908 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001909 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001910 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001911 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001912 }
1913}
1914
Kristian Høgsberg42263852012-09-06 21:59:29 -04001915static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001916gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001917 float red, float green, float blue, float alpha)
1918{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001919 struct gl_surface_state *gs = get_surface_state(surface);
1920 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001921
1922 gs->color[0] = red;
1923 gs->color[1] = green;
1924 gs->color[2] = blue;
1925 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001926 gs->buffer_type = BUFFER_TYPE_SOLID;
1927 gs->pitch = 1;
1928 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001929
John Kåre Alsaker40684142012-11-13 19:10:25 +01001930 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001931}
1932
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001933static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001934gl_renderer_surface_get_content_size(struct weston_surface *surface,
1935 int *width, int *height)
1936{
1937 struct gl_surface_state *gs = get_surface_state(surface);
1938
1939 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1940 *width = 0;
1941 *height = 0;
1942 } else {
1943 *width = gs->pitch;
1944 *height = gs->height;
1945 }
1946}
1947
1948static uint32_t
1949pack_color(pixman_format_code_t format, float *c)
1950{
1951 uint8_t r = round(c[0] * 255.0f);
1952 uint8_t g = round(c[1] * 255.0f);
1953 uint8_t b = round(c[2] * 255.0f);
1954 uint8_t a = round(c[3] * 255.0f);
1955
1956 switch (format) {
1957 case PIXMAN_a8b8g8r8:
1958 return (a << 24) | (b << 16) | (g << 8) | r;
1959 default:
1960 assert(0);
1961 return 0;
1962 }
1963}
1964
1965static int
1966gl_renderer_surface_copy_content(struct weston_surface *surface,
1967 void *target, size_t size,
1968 int src_x, int src_y,
1969 int width, int height)
1970{
1971 static const GLfloat verts[4 * 2] = {
1972 0.0f, 0.0f,
1973 1.0f, 0.0f,
1974 1.0f, 1.0f,
1975 0.0f, 1.0f
1976 };
1977 static const GLfloat projmat_normal[16] = { /* transpose */
1978 2.0f, 0.0f, 0.0f, 0.0f,
1979 0.0f, 2.0f, 0.0f, 0.0f,
1980 0.0f, 0.0f, 1.0f, 0.0f,
1981 -1.0f, -1.0f, 0.0f, 1.0f
1982 };
1983 static const GLfloat projmat_yinvert[16] = { /* transpose */
1984 2.0f, 0.0f, 0.0f, 0.0f,
1985 0.0f, -2.0f, 0.0f, 0.0f,
1986 0.0f, 0.0f, 1.0f, 0.0f,
1987 -1.0f, 1.0f, 0.0f, 1.0f
1988 };
1989 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1990 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1991 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1992 struct gl_renderer *gr = get_renderer(surface->compositor);
1993 struct gl_surface_state *gs = get_surface_state(surface);
1994 int cw, ch;
1995 GLuint fbo;
1996 GLuint tex;
1997 GLenum status;
1998 const GLfloat *proj;
1999 int i;
2000
2001 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2002
2003 switch (gs->buffer_type) {
2004 case BUFFER_TYPE_NULL:
2005 return -1;
2006 case BUFFER_TYPE_SOLID:
2007 *(uint32_t *)target = pack_color(format, gs->color);
2008 return 0;
2009 case BUFFER_TYPE_SHM:
2010 gl_renderer_flush_damage(surface);
2011 /* fall through */
2012 case BUFFER_TYPE_EGL:
2013 break;
2014 }
2015
2016 glGenTextures(1, &tex);
2017 glBindTexture(GL_TEXTURE_2D, tex);
2018 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2019 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2020 glBindTexture(GL_TEXTURE_2D, 0);
2021
2022 glGenFramebuffers(1, &fbo);
2023 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2024 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2025 GL_TEXTURE_2D, tex, 0);
2026
2027 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2028 if (status != GL_FRAMEBUFFER_COMPLETE) {
2029 weston_log("%s: fbo error: %#x\n", __func__, status);
2030 glDeleteFramebuffers(1, &fbo);
2031 glDeleteTextures(1, &tex);
2032 return -1;
2033 }
2034
2035 glViewport(0, 0, cw, ch);
2036 glDisable(GL_BLEND);
2037 use_shader(gr, gs->shader);
2038 if (gs->y_inverted)
2039 proj = projmat_normal;
2040 else
2041 proj = projmat_yinvert;
2042
2043 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2044 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2045
2046 for (i = 0; i < gs->num_textures; i++) {
2047 glUniform1i(gs->shader->tex_uniforms[i], i);
2048
2049 glActiveTexture(GL_TEXTURE0 + i);
2050 glBindTexture(gs->target, gs->textures[i]);
2051 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2052 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2053 }
2054
2055 /* position: */
2056 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2057 glEnableVertexAttribArray(0);
2058
2059 /* texcoord: */
2060 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2061 glEnableVertexAttribArray(1);
2062
2063 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2064
2065 glDisableVertexAttribArray(1);
2066 glDisableVertexAttribArray(0);
2067
2068 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2069 glReadPixels(src_x, src_y, width, height, gl_format,
2070 GL_UNSIGNED_BYTE, target);
2071
2072 glDeleteFramebuffers(1, &fbo);
2073 glDeleteTextures(1, &tex);
2074
2075 return 0;
2076}
2077
2078static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002079surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002080{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002081 int i;
2082
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002083 wl_list_remove(&gs->surface_destroy_listener.link);
2084 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002085
2086 gs->surface->renderer_state = NULL;
2087
2088 glDeleteTextures(gs->num_textures, gs->textures);
2089
2090 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002091 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002092
2093 weston_buffer_reference(&gs->buffer_ref, NULL);
2094 pixman_region32_fini(&gs->texture_damage);
2095 free(gs);
2096}
2097
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002098static void
2099surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2100{
2101 struct gl_surface_state *gs;
2102 struct gl_renderer *gr;
2103
2104 gs = container_of(listener, struct gl_surface_state,
2105 surface_destroy_listener);
2106
2107 gr = get_renderer(gs->surface->compositor);
2108
2109 surface_state_destroy(gs, gr);
2110}
2111
2112static void
2113surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2114{
2115 struct gl_surface_state *gs;
2116 struct gl_renderer *gr;
2117
2118 gr = data;
2119
2120 gs = container_of(listener, struct gl_surface_state,
2121 renderer_destroy_listener);
2122
2123 surface_state_destroy(gs, gr);
2124}
2125
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002126static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002127gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002128{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002129 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002130 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002131
Bryce Harringtonde16d892014-11-20 22:21:57 -08002132 gs = zalloc(sizeof *gs);
2133 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002134 return -1;
2135
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002136 /* A buffer is never attached to solid color surfaces, yet
2137 * they still go through texcoord computations. Do not divide
2138 * by zero there.
2139 */
2140 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002141 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002142
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002143 gs->surface = surface;
2144
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002145 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002146 surface->renderer_state = gs;
2147
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002148 gs->surface_destroy_listener.notify =
2149 surface_state_handle_surface_destroy;
2150 wl_signal_add(&surface->destroy_signal,
2151 &gs->surface_destroy_listener);
2152
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002153 gs->renderer_destroy_listener.notify =
2154 surface_state_handle_renderer_destroy;
2155 wl_signal_add(&gr->destroy_signal,
2156 &gs->renderer_destroy_listener);
2157
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002158 if (surface->buffer_ref.buffer) {
2159 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2160 gl_renderer_flush_damage(surface);
2161 }
2162
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002163 return 0;
2164}
2165
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002166static const char vertex_shader[] =
2167 "uniform mat4 proj;\n"
2168 "attribute vec2 position;\n"
2169 "attribute vec2 texcoord;\n"
2170 "varying vec2 v_texcoord;\n"
2171 "void main()\n"
2172 "{\n"
2173 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2174 " v_texcoord = texcoord;\n"
2175 "}\n";
2176
2177/* Declare common fragment shader uniforms */
2178#define FRAGMENT_CONVERT_YUV \
2179 " y *= alpha;\n" \
2180 " u *= alpha;\n" \
2181 " v *= alpha;\n" \
2182 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2183 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2184 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2185 " gl_FragColor.a = alpha;\n"
2186
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002187static const char fragment_debug[] =
2188 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2189
2190static const char fragment_brace[] =
2191 "}\n";
2192
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002193static const char texture_fragment_shader_rgba[] =
2194 "precision mediump float;\n"
2195 "varying vec2 v_texcoord;\n"
2196 "uniform sampler2D tex;\n"
2197 "uniform float alpha;\n"
2198 "void main()\n"
2199 "{\n"
2200 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002201 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002202
2203static const char texture_fragment_shader_rgbx[] =
2204 "precision mediump float;\n"
2205 "varying vec2 v_texcoord;\n"
2206 "uniform sampler2D tex;\n"
2207 "uniform float alpha;\n"
2208 "void main()\n"
2209 "{\n"
2210 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2211 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002212 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002213
2214static const char texture_fragment_shader_egl_external[] =
2215 "#extension GL_OES_EGL_image_external : require\n"
2216 "precision mediump float;\n"
2217 "varying vec2 v_texcoord;\n"
2218 "uniform samplerExternalOES tex;\n"
2219 "uniform float alpha;\n"
2220 "void main()\n"
2221 "{\n"
2222 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002223 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002224
2225static const char texture_fragment_shader_y_uv[] =
2226 "precision mediump float;\n"
2227 "uniform sampler2D tex;\n"
2228 "uniform sampler2D tex1;\n"
2229 "varying vec2 v_texcoord;\n"
2230 "uniform float alpha;\n"
2231 "void main() {\n"
2232 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2233 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2234 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2235 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002236 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002237
2238static const char texture_fragment_shader_y_u_v[] =
2239 "precision mediump float;\n"
2240 "uniform sampler2D tex;\n"
2241 "uniform sampler2D tex1;\n"
2242 "uniform sampler2D tex2;\n"
2243 "varying vec2 v_texcoord;\n"
2244 "uniform float alpha;\n"
2245 "void main() {\n"
2246 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2247 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2248 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2249 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002250 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002251
2252static const char texture_fragment_shader_y_xuxv[] =
2253 "precision mediump float;\n"
2254 "uniform sampler2D tex;\n"
2255 "uniform sampler2D tex1;\n"
2256 "varying vec2 v_texcoord;\n"
2257 "uniform float alpha;\n"
2258 "void main() {\n"
2259 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2260 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2261 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2262 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002263 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002264
2265static const char solid_fragment_shader[] =
2266 "precision mediump float;\n"
2267 "uniform vec4 color;\n"
2268 "uniform float alpha;\n"
2269 "void main()\n"
2270 "{\n"
2271 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002272 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002273
2274static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002275compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002276{
2277 GLuint s;
2278 char msg[512];
2279 GLint status;
2280
2281 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002282 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002283 glCompileShader(s);
2284 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2285 if (!status) {
2286 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2287 weston_log("shader info: %s\n", msg);
2288 return GL_NONE;
2289 }
2290
2291 return s;
2292}
2293
2294static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002295shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002296 const char *vertex_source, const char *fragment_source)
2297{
2298 char msg[512];
2299 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002300 int count;
2301 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002302
2303 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002304 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2305
2306 if (renderer->fragment_shader_debug) {
2307 sources[0] = fragment_source;
2308 sources[1] = fragment_debug;
2309 sources[2] = fragment_brace;
2310 count = 3;
2311 } else {
2312 sources[0] = fragment_source;
2313 sources[1] = fragment_brace;
2314 count = 2;
2315 }
2316
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002317 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002318 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002319
2320 shader->program = glCreateProgram();
2321 glAttachShader(shader->program, shader->vertex_shader);
2322 glAttachShader(shader->program, shader->fragment_shader);
2323 glBindAttribLocation(shader->program, 0, "position");
2324 glBindAttribLocation(shader->program, 1, "texcoord");
2325
2326 glLinkProgram(shader->program);
2327 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2328 if (!status) {
2329 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2330 weston_log("link info: %s\n", msg);
2331 return -1;
2332 }
2333
2334 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2335 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2336 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2337 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2338 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2339 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2340
2341 return 0;
2342}
2343
2344static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002345shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002346{
2347 glDeleteShader(shader->vertex_shader);
2348 glDeleteShader(shader->fragment_shader);
2349 glDeleteProgram(shader->program);
2350
2351 shader->vertex_shader = 0;
2352 shader->fragment_shader = 0;
2353 shader->program = 0;
2354}
2355
2356static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002357log_extensions(const char *name, const char *extensions)
2358{
2359 const char *p, *end;
2360 int l;
2361 int len;
2362
2363 l = weston_log("%s:", name);
2364 p = extensions;
2365 while (*p) {
2366 end = strchrnul(p, ' ');
2367 len = end - p;
2368 if (l + len > 78)
2369 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2370 len, p);
2371 else
2372 l += weston_log_continue(" %.*s", len, p);
2373 for (p = end; isspace(*p); p++)
2374 ;
2375 }
2376 weston_log_continue("\n");
2377}
2378
2379static void
2380log_egl_gl_info(EGLDisplay egldpy)
2381{
2382 const char *str;
2383
2384 str = eglQueryString(egldpy, EGL_VERSION);
2385 weston_log("EGL version: %s\n", str ? str : "(null)");
2386
2387 str = eglQueryString(egldpy, EGL_VENDOR);
2388 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2389
2390 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2391 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2392
2393 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2394 log_extensions("EGL extensions", str ? str : "(null)");
2395
2396 str = (char *)glGetString(GL_VERSION);
2397 weston_log("GL version: %s\n", str ? str : "(null)");
2398
2399 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2400 weston_log("GLSL version: %s\n", str ? str : "(null)");
2401
2402 str = (char *)glGetString(GL_VENDOR);
2403 weston_log("GL vendor: %s\n", str ? str : "(null)");
2404
2405 str = (char *)glGetString(GL_RENDERER);
2406 weston_log("GL renderer: %s\n", str ? str : "(null)");
2407
2408 str = (char *)glGetString(GL_EXTENSIONS);
2409 log_extensions("GL extensions", str ? str : "(null)");
2410}
2411
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002412static void
2413log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2414{
2415 EGLint r, g, b, a;
2416
2417 weston_log("Chosen EGL config details:\n");
2418
2419 weston_log_continue(STAMP_SPACE "RGBA bits");
2420 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2421 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2422 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2423 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2424 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2425 else
2426 weston_log_continue(" unknown\n");
2427
2428 weston_log_continue(STAMP_SPACE "swap interval range");
2429 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2430 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2431 weston_log_continue(": %d - %d\n", a, b);
2432 else
2433 weston_log_continue(" unknown\n");
2434}
2435
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002436static int
Derek Foremane76f1852015-05-15 12:12:39 -05002437match_config_to_visual(EGLDisplay egl_display,
2438 EGLint visual_id,
2439 EGLConfig *configs,
2440 int count)
2441{
2442 int i;
2443
2444 for (i = 0; i < count; ++i) {
2445 EGLint id;
2446
2447 if (!eglGetConfigAttrib(egl_display,
2448 configs[i], EGL_NATIVE_VISUAL_ID,
2449 &id))
2450 continue;
2451
2452 if (id == visual_id)
2453 return i;
2454 }
2455
Derek Foremane76f1852015-05-15 12:12:39 -05002456 return -1;
2457}
2458
2459static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002460egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002461 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002462 EGLConfig *config_out)
2463{
2464 EGLint count = 0;
2465 EGLint matched = 0;
2466 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002467 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002468
Derek Foremana7e19912015-05-20 14:57:58 -05002469 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2470 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002471 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002472 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002473 configs = calloc(count, sizeof *configs);
2474 if (!configs)
2475 return -1;
2476
2477 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002478 count, &matched) || !matched) {
2479 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002480 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002481 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002482
Derek Foremane76f1852015-05-15 12:12:39 -05002483 if (!visual_id)
2484 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002485
Derek Foremane76f1852015-05-15 12:12:39 -05002486 for (i = 0; config_index == -1 && i < n_ids; i++)
2487 config_index = match_config_to_visual(gr->egl_display,
2488 visual_id[i],
2489 configs,
2490 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002491
Derek Foremane76f1852015-05-15 12:12:39 -05002492 if (config_index != -1)
2493 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002494
2495out:
2496 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002497 if (config_index == -1)
2498 return -1;
2499
Derek Foremana7e19912015-05-20 14:57:58 -05002500 if (i > 1)
2501 weston_log("Unable to use first choice EGL config with id"
2502 " 0x%x, succeeded with alternate id 0x%x.\n",
2503 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002504 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002505}
2506
John Kåre Alsaker44154502012-11-13 19:10:20 +01002507static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002508gl_renderer_output_set_border(struct weston_output *output,
2509 enum gl_renderer_border_side side,
2510 int32_t width, int32_t height,
2511 int32_t tex_width, unsigned char *data)
2512{
2513 struct gl_output_state *go = get_output_state(output);
2514
Jason Ekstrande5512d42014-02-04 21:36:38 -06002515 if (go->borders[side].width != width ||
2516 go->borders[side].height != height)
2517 /* In this case, we have to blow everything and do a full
2518 * repaint. */
2519 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2520
2521 if (data == NULL) {
2522 width = 0;
2523 height = 0;
2524 }
2525
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002526 go->borders[side].width = width;
2527 go->borders[side].height = height;
2528 go->borders[side].tex_width = tex_width;
2529 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002530 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002531}
2532
John Kåre Alsaker94659272012-11-13 19:10:18 +01002533static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002534gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002535
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002536static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002537gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002538 EGLNativeWindowType window_for_legacy,
2539 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002540 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002541 const EGLint *visual_id,
2542 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002543{
2544 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002545 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002546 struct gl_output_state *go;
2547 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002548 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002549
Derek Foremane76f1852015-05-15 12:12:39 -05002550 if (egl_choose_config(gr, attribs, visual_id,
2551 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002552 weston_log("failed to choose EGL config for output\n");
2553 return -1;
2554 }
2555
2556 if (egl_config != gr->egl_config &&
2557 !gr->has_configless_context) {
2558 weston_log("attempted to use a different EGL config for an "
2559 "output but EGL_MESA_configless_context is not "
2560 "supported\n");
2561 return -1;
2562 }
2563
Bryce Harringtonde16d892014-11-20 22:21:57 -08002564 go = zalloc(sizeof *go);
2565 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002566 return -1;
2567
Jonny Lamb671148f2015-03-20 15:26:52 +01002568 if (gr->create_platform_window) {
2569 go->egl_surface =
2570 gr->create_platform_window(gr->egl_display,
2571 egl_config,
2572 window_for_platform,
2573 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002574 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002575 go->egl_surface =
2576 eglCreateWindowSurface(gr->egl_display,
2577 egl_config,
2578 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002579 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002580
2581 if (go->egl_surface == EGL_NO_SURFACE) {
2582 weston_log("failed to create egl surface\n");
2583 free(go);
2584 return -1;
2585 }
2586
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002587 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002588 pixman_region32_init(&go->buffer_damage[i]);
2589
John Kåre Alsaker94659272012-11-13 19:10:18 +01002590 output->renderer_state = go;
2591
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002592 log_egl_config_info(gr->egl_display, egl_config);
2593
John Kåre Alsaker94659272012-11-13 19:10:18 +01002594 return 0;
2595}
2596
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002597static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002598gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002599{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002600 struct gl_renderer *gr = get_renderer(output->compositor);
2601 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002602 int i;
2603
2604 for (i = 0; i < 2; i++)
2605 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002606
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002607 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002608
2609 free(go);
2610}
2611
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002612static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002613gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002614{
2615 return get_output_state(output)->egl_surface;
2616}
2617
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002618static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002619gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002620{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002621 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002622 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002623
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002624 wl_signal_emit(&gr->destroy_signal, gr);
2625
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002626 if (gr->has_bind_display)
2627 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002628
2629 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2630 eglMakeCurrent(gr->egl_display,
2631 EGL_NO_SURFACE, EGL_NO_SURFACE,
2632 EGL_NO_CONTEXT);
2633
Pekka Paalanena3525802014-06-12 16:49:29 +03002634
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002635 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2636 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002637
Armin Krezović28d240f2016-06-23 11:59:35 +02002638 if (gr->dummy_surface != EGL_NO_SURFACE)
2639 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2640
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002641 eglTerminate(gr->egl_display);
2642 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002643
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002644 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002645 wl_array_release(&gr->vtxcnt);
2646
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002647 if (gr->fragment_binding)
2648 weston_binding_destroy(gr->fragment_binding);
2649 if (gr->fan_binding)
2650 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002651
Scott Moreau976a0502013-03-07 10:15:17 -07002652 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002653}
2654
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002655static bool
2656check_extension(const char *extensions, const char *extension)
2657{
2658 size_t extlen = strlen(extension);
2659 const char *end = extensions + strlen(extensions);
2660
2661 while (extensions < end) {
2662 size_t n = 0;
2663
2664 /* Skip whitespaces, if any */
2665 if (*extensions == ' ') {
2666 extensions++;
2667 continue;
2668 }
2669
2670 n = strcspn(extensions, " ");
2671
2672 /* Compare strings */
2673 if (n == extlen && strncmp(extension, extensions, n) == 0)
2674 return true; /* Found */
2675
2676 extensions += n;
2677 }
2678
2679 /* Not found */
2680 return false;
2681}
2682
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002683static void
2684renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2685{
2686 const char *extensions;
2687
2688 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2689 if (!extensions) {
2690 weston_log("Retrieving EGL client extension string failed.\n");
2691 return;
2692 }
2693
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002694 if (check_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002695 gr->create_platform_window =
2696 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2697 else
2698 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2699}
2700
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002701static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002702gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2703{
2704 struct gl_renderer *gr = get_renderer(ec);
2705 const char *extensions;
2706 EGLBoolean ret;
2707
2708 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2709 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2710 gr->bind_display =
2711 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2712 gr->unbind_display =
2713 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2714 gr->query_buffer =
2715 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2716
2717 extensions =
2718 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2719 if (!extensions) {
2720 weston_log("Retrieving EGL extension string failed.\n");
2721 return -1;
2722 }
2723
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002724 if (check_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002725 gr->has_bind_display = 1;
2726 if (gr->has_bind_display) {
2727 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2728 if (!ret)
2729 gr->has_bind_display = 0;
2730 }
2731
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002732 if (check_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002733 gr->has_egl_buffer_age = 1;
2734 else
2735 weston_log("warning: EGL_EXT_buffer_age not supported. "
2736 "Performance could be affected.\n");
2737
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002738 if (check_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002739 gr->swap_buffers_with_damage =
2740 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2741 else
2742 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2743 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002744
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002745 if (check_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002746 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002747
Armin Krezović28d240f2016-06-23 11:59:35 +02002748 if (check_extension(extensions, "EGL_KHR_surfaceless_context"))
2749 gr->has_surfaceless_context = 1;
2750
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002751 if (check_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002752 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002753
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002754 renderer_setup_egl_client_extensions(gr);
2755
Neil Robertsb7f85332014-03-07 18:05:49 +00002756 return 0;
2757}
2758
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002759static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002760 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2761 EGL_RED_SIZE, 1,
2762 EGL_GREEN_SIZE, 1,
2763 EGL_BLUE_SIZE, 1,
2764 EGL_ALPHA_SIZE, 0,
2765 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2766 EGL_NONE
2767};
2768
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002769static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002770 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2771 EGL_RED_SIZE, 1,
2772 EGL_GREEN_SIZE, 1,
2773 EGL_BLUE_SIZE, 1,
2774 EGL_ALPHA_SIZE, 1,
2775 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2776 EGL_NONE
2777};
2778
Armin Krezović28d240f2016-06-23 11:59:35 +02002779
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002780/** Checks whether a platform EGL client extension is supported
2781 *
2782 * \param ec The weston compositor
2783 * \param extension_suffix The EGL client extension suffix
2784 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2785 *
2786 * This function checks whether a specific platform_* extension is supported
2787 * by EGL.
2788 *
2789 * The extension suffix should be the suffix of the platform extension (that
2790 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2791 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2792 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2793 *
2794 * The return value is 1:
2795 * - if the supplied EGL client extension is supported.
2796 * The return value is 0:
2797 * - if the platform_base client extension isn't supported so will
2798 * fallback to eglGetDisplay and friends.
2799 * The return value is -1:
2800 * - if the supplied EGL client extension is not supported.
2801 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002802static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002803gl_renderer_supports(struct weston_compositor *ec,
2804 const char *extension_suffix)
2805{
2806 static const char *extensions = NULL;
2807 char s[64];
2808
2809 if (!extensions) {
2810 extensions = (const char *) eglQueryString(
2811 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2812
2813 if (!extensions)
2814 return 0;
2815
2816 log_extensions("EGL client extensions",
2817 extensions);
2818 }
2819
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002820 if (!check_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002821 return 0;
2822
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002823 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002824 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002825 return 1;
2826
2827 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002828 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002829 return 1;
2830
2831 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002832 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002833 return 1;
2834
Pekka Paalanenf2824542015-04-08 17:02:21 +03002835 /* at this point we definitely have some platform extensions but
2836 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002837 * not supported. */
2838
2839 return -1;
2840}
2841
Jonny Lamb74eed312015-03-24 13:12:04 +01002842static const char *
2843platform_to_extension(EGLenum platform)
2844{
2845 switch (platform) {
2846 case EGL_PLATFORM_GBM_KHR:
2847 return "gbm";
2848 case EGL_PLATFORM_WAYLAND_KHR:
2849 return "wayland";
2850 case EGL_PLATFORM_X11_KHR:
2851 return "x11";
2852 default:
2853 assert(0 && "bad EGL platform enum");
2854 }
2855}
2856
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002857static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002858gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2859 EGLConfig pbuffer_config;
2860
2861 static const EGLint pbuffer_config_attribs[] = {
2862 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2863 EGL_RED_SIZE, 1,
2864 EGL_GREEN_SIZE, 1,
2865 EGL_BLUE_SIZE, 1,
2866 EGL_ALPHA_SIZE, 0,
2867 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2868 EGL_NONE
2869 };
2870
2871 static const EGLint pbuffer_attribs[] = {
2872 EGL_WIDTH, 10,
2873 EGL_HEIGHT, 10,
2874 EGL_NONE
2875 };
2876
2877 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2878 weston_log("failed to choose EGL config for PbufferSurface");
2879 return -1;
2880 }
2881
2882 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2883 pbuffer_config,
2884 pbuffer_attribs);
2885
2886 if (gr->dummy_surface == EGL_NO_SURFACE) {
2887 weston_log("failed to create PbufferSurface\n");
2888 return -1;
2889 }
2890
2891 return 0;
2892}
2893
2894static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002895gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2896 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002897 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002898{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002899 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002900 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002901 int supports = 0;
2902
2903 if (platform) {
2904 supports = gl_renderer_supports(
2905 ec, platform_to_extension(platform));
2906 if (supports < 0)
2907 return -1;
2908 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002909
Bryce Harringtonde16d892014-11-20 22:21:57 -08002910 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002911 if (gr == NULL)
2912 return -1;
2913
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002914 gr->base.read_pixels = gl_renderer_read_pixels;
2915 gr->base.repaint_output = gl_renderer_repaint_output;
2916 gr->base.flush_damage = gl_renderer_flush_damage;
2917 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002918 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002919 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002920 gr->base.surface_get_content_size =
2921 gl_renderer_surface_get_content_size;
2922 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002923 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002924
Jonny Lambf1ec5062015-03-24 13:12:05 +01002925 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002926 if (supports) {
2927 if (!get_platform_display) {
2928 get_platform_display = (void *) eglGetProcAddress(
2929 "eglGetPlatformDisplayEXT");
2930 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002931
Jonny Lamb74eed312015-03-24 13:12:04 +01002932 /* also wrap this in the supports check because
2933 * eglGetProcAddress can return non-NULL and still not
2934 * support the feature at runtime, so ensure the
2935 * appropriate extension checks have been done. */
2936 if (get_platform_display && platform) {
2937 gr->egl_display = get_platform_display(platform,
2938 native_window,
2939 NULL);
2940 }
2941 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002942
2943 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002944 weston_log("warning: either no EGL_EXT_platform_base "
2945 "support or specific platform support; "
2946 "falling back to eglGetDisplay.\n");
2947 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002948 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002949
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002950 if (gr->egl_display == EGL_NO_DISPLAY) {
2951 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002952 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002953 }
2954
2955 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2956 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002957 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002958 }
2959
Derek Foremane76f1852015-05-15 12:12:39 -05002960 if (egl_choose_config(gr, attribs, visual_id,
2961 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002962 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002963 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002964 }
2965
2966 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002967 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002968 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002969 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002970
Neil Robertsb7f85332014-03-07 18:05:49 +00002971 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002972 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002973
Pekka Paalanena3525802014-06-12 16:49:29 +03002974 wl_list_init(&gr->dmabuf_images);
2975 if (gr->has_dmabuf_import)
2976 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2977
Armin Krezović28d240f2016-06-23 11:59:35 +02002978 if (gr->has_surfaceless_context) {
2979 weston_log("EGL_KHR_surfaceless_context available\n");
2980 gr->dummy_surface = EGL_NO_SURFACE;
2981 } else {
2982 weston_log("EGL_KHR_surfaceless_context unavailable. "
2983 "Trying PbufferSurface\n");
2984
2985 if (gl_renderer_create_pbuffer_surface(gr) < 0)
2986 goto fail_with_error;
2987 }
2988
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002989 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2990
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002991 wl_signal_init(&gr->destroy_signal);
2992
Armin Krezović28d240f2016-06-23 11:59:35 +02002993 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
2994 if (gr->dummy_surface != EGL_NO_SURFACE)
2995 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2996 goto fail_with_error;
2997 }
2998
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002999 return 0;
3000
Derek Foreman066ca0c2015-06-11 12:14:45 -05003001fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003002 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003003fail_terminate:
3004 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003005fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003006 free(gr);
3007 return -1;
3008}
3009
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003010static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003011gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003012{
3013 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003014}
3015
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003016static int
3017compile_shaders(struct weston_compositor *ec)
3018{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003019 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003020
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003021 gr->texture_shader_rgba.vertex_source = vertex_shader;
3022 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3023
3024 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3025 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3026
3027 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3028 gr->texture_shader_egl_external.fragment_source =
3029 texture_fragment_shader_egl_external;
3030
3031 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3032 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3033
3034 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3035 gr->texture_shader_y_u_v.fragment_source =
3036 texture_fragment_shader_y_u_v;
3037
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003038 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003039 gr->texture_shader_y_xuxv.fragment_source =
3040 texture_fragment_shader_y_xuxv;
3041
3042 gr->solid_shader.vertex_source = vertex_shader;
3043 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003044
3045 return 0;
3046}
3047
3048static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003049fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3050 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003051{
3052 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003053 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003054 struct weston_output *output;
3055
John Kåre Alsaker40684142012-11-13 19:10:25 +01003056 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003057
John Kåre Alsaker40684142012-11-13 19:10:25 +01003058 shader_release(&gr->texture_shader_rgba);
3059 shader_release(&gr->texture_shader_rgbx);
3060 shader_release(&gr->texture_shader_egl_external);
3061 shader_release(&gr->texture_shader_y_uv);
3062 shader_release(&gr->texture_shader_y_u_v);
3063 shader_release(&gr->texture_shader_y_xuxv);
3064 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003065
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003066 /* Force use_shader() to call glUseProgram(), since we need to use
3067 * the recompiled version of the shader. */
3068 gr->current_shader = NULL;
3069
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003070 wl_list_for_each(output, &ec->output_list, link)
3071 weston_output_damage(output);
3072}
3073
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003074static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003075fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3076 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003077{
3078 struct weston_compositor *compositor = data;
3079 struct gl_renderer *gr = get_renderer(compositor);
3080
3081 gr->fan_debug = !gr->fan_debug;
3082 weston_compositor_damage_all(compositor);
3083}
3084
John Kåre Alsaker94659272012-11-13 19:10:18 +01003085static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003086gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003087{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003088 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003089 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003090 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003091 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003092
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003093 static const EGLint context_attribs[] = {
3094 EGL_CONTEXT_CLIENT_VERSION, 2,
3095 EGL_NONE
3096 };
3097
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003098 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3099 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003100 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003101 return -1;
3102 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003103
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003104 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003105
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003106 if (gr->has_configless_context)
3107 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003108
3109 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003110 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003111 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003112 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003113 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003114 return -1;
3115 }
3116
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003117 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3118 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003119 if (ret == EGL_FALSE) {
3120 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003121 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003122 return -1;
3123 }
3124
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003125 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003126
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003127 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003128 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003129
3130 extensions = (const char *) glGetString(GL_EXTENSIONS);
3131 if (!extensions) {
3132 weston_log("Retrieving GL extension string failed.\n");
3133 return -1;
3134 }
3135
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003136 if (!check_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003137 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3138 return -1;
3139 }
3140
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003141 if (check_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003142 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003143 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003144 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003145
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003146 if (check_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003147 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003148
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003149 if (check_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003150 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003151
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003152 glActiveTexture(GL_TEXTURE0);
3153
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003154 if (compile_shaders(ec))
3155 return -1;
3156
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003157 gr->fragment_binding =
3158 weston_compositor_add_debug_binding(ec, KEY_S,
3159 fragment_debug_binding,
3160 ec);
3161 gr->fan_binding =
3162 weston_compositor_add_debug_binding(ec, KEY_F,
3163 fan_debug_repaint_binding,
3164 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003165
Pekka Paalanen035a0322012-10-24 09:43:06 +03003166 weston_log("GL ES 2 renderer features:\n");
3167 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003168 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003169 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003170 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003171 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003172 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003173
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003174
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003175 return 0;
3176}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003177
3178WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3179 .opaque_attribs = gl_renderer_opaque_attribs,
3180 .alpha_attribs = gl_renderer_alpha_attribs,
3181
3182 .create = gl_renderer_create,
3183 .display = gl_renderer_display,
3184 .output_create = gl_renderer_output_create,
3185 .output_destroy = gl_renderer_output_destroy,
3186 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003187 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003188 .print_egl_error_state = gl_renderer_print_egl_error_state
3189};