blob: 64ef628e31f6917186998f61706f41dbbbc10b9f [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#ifdef GL_EXT_unpack_subimage
873 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
874 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
875 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
876#endif
877 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
878 img->tex_width, img->height, 0,
879 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500880 }
881
882 GLfloat texcoord[] = {
883 0.0f, 0.0f,
884 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
885 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
886 0.0f, 1.0f,
887 };
888
889 GLfloat verts[] = {
890 x, y,
891 x + width, y,
892 x + width, y + height,
893 x, y + height
894 };
895
896 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
897 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
898 glEnableVertexAttribArray(0);
899 glEnableVertexAttribArray(1);
900
901 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
902
903 glDisableVertexAttribArray(1);
904 glDisableVertexAttribArray(0);
905}
906
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600907static int
908output_has_borders(struct weston_output *output)
909{
910 struct gl_output_state *go = get_output_state(output);
911
912 return go->borders[GL_RENDERER_BORDER_TOP].data ||
913 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
914 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
915 go->borders[GL_RENDERER_BORDER_LEFT].data;
916}
917
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500918static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600919draw_output_borders(struct weston_output *output,
920 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500921{
922 struct gl_output_state *go = get_output_state(output);
923 struct gl_renderer *gr = get_renderer(output->compositor);
924 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500925 struct gl_border_image *top, *bottom, *left, *right;
926 struct weston_matrix matrix;
927 int full_width, full_height;
928
Jason Ekstrande5512d42014-02-04 21:36:38 -0600929 if (border_status == BORDER_STATUS_CLEAN)
930 return; /* Clean. Nothing to do. */
931
Jason Ekstrand00b84282013-10-27 22:24:59 -0500932 top = &go->borders[GL_RENDERER_BORDER_TOP];
933 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
934 left = &go->borders[GL_RENDERER_BORDER_LEFT];
935 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
936
937 full_width = output->current_mode->width + left->width + right->width;
938 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500939
940 glDisable(GL_BLEND);
941 use_shader(gr, shader);
942
Jason Ekstrand00b84282013-10-27 22:24:59 -0500943 glViewport(0, 0, full_width, full_height);
944
945 weston_matrix_init(&matrix);
946 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
947 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
948 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500949
950 glUniform1i(shader->tex_uniforms[0], 0);
951 glUniform1f(shader->alpha_uniform, 1);
952 glActiveTexture(GL_TEXTURE0);
953
Jason Ekstrande5512d42014-02-04 21:36:38 -0600954 if (border_status & BORDER_TOP_DIRTY)
955 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
956 0, 0,
957 full_width, top->height);
958 if (border_status & BORDER_LEFT_DIRTY)
959 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
960 0, top->height,
961 left->width, output->current_mode->height);
962 if (border_status & BORDER_RIGHT_DIRTY)
963 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
964 full_width - right->width, top->height,
965 right->width, output->current_mode->height);
966 if (border_status & BORDER_BOTTOM_DIRTY)
967 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
968 0, full_height - bottom->height,
969 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500970}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100971
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400972static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600973output_get_border_damage(struct weston_output *output,
974 enum gl_border_status border_status,
975 pixman_region32_t *damage)
976{
977 struct gl_output_state *go = get_output_state(output);
978 struct gl_border_image *top, *bottom, *left, *right;
979 int full_width, full_height;
980
981 if (border_status == BORDER_STATUS_CLEAN)
982 return; /* Clean. Nothing to do. */
983
984 top = &go->borders[GL_RENDERER_BORDER_TOP];
985 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
986 left = &go->borders[GL_RENDERER_BORDER_LEFT];
987 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
988
989 full_width = output->current_mode->width + left->width + right->width;
990 full_height = output->current_mode->height + top->height + bottom->height;
991 if (border_status & BORDER_TOP_DIRTY)
992 pixman_region32_union_rect(damage, damage,
993 0, 0,
994 full_width, top->height);
995 if (border_status & BORDER_LEFT_DIRTY)
996 pixman_region32_union_rect(damage, damage,
997 0, top->height,
998 left->width, output->current_mode->height);
999 if (border_status & BORDER_RIGHT_DIRTY)
1000 pixman_region32_union_rect(damage, damage,
1001 full_width - right->width, top->height,
1002 right->width, output->current_mode->height);
1003 if (border_status & BORDER_BOTTOM_DIRTY)
1004 pixman_region32_union_rect(damage, damage,
1005 0, full_height - bottom->height,
1006 full_width, bottom->height);
1007}
1008
1009static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001010output_get_damage(struct weston_output *output,
1011 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001012{
1013 struct gl_output_state *go = get_output_state(output);
1014 struct gl_renderer *gr = get_renderer(output->compositor);
1015 EGLint buffer_age = 0;
1016 EGLBoolean ret;
1017 int i;
1018
1019 if (gr->has_egl_buffer_age) {
1020 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1021 EGL_BUFFER_AGE_EXT, &buffer_age);
1022 if (ret == EGL_FALSE) {
1023 weston_log("buffer age query failed.\n");
1024 gl_renderer_print_egl_error_state();
1025 }
1026 }
1027
Jason Ekstrande5512d42014-02-04 21:36:38 -06001028 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001029 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001030 *border_damage = BORDER_ALL_DIRTY;
1031 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001032 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001033 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001034
1035 if (*border_damage & BORDER_SIZE_CHANGED) {
1036 /* If we've had a resize, we have to do a full
1037 * repaint. */
1038 *border_damage |= BORDER_ALL_DIRTY;
1039 pixman_region32_copy(buffer_damage, &output->region);
1040 } else {
1041 for (i = 0; i < buffer_age - 1; i++)
1042 pixman_region32_union(buffer_damage,
1043 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001044 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001045 }
1046 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001047}
1048
1049static void
1050output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001051 pixman_region32_t *output_damage,
1052 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001053{
1054 struct gl_output_state *go = get_output_state(output);
1055 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001056
1057 if (!gr->has_egl_buffer_age)
1058 return;
1059
Derek Foreman4c582662014-10-09 18:39:44 -05001060 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1061 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001062
Derek Foreman4c582662014-10-09 18:39:44 -05001063 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1064 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001065}
1066
Derek Foremanc4cfe852015-05-15 12:12:40 -05001067/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1068 * unavailable, so we're assuming the background has no transparency
1069 * and that everything with a blend, like drop shadows, will have something
1070 * opaque (like the background) drawn underneath it.
1071 *
1072 * Depending on the underlying hardware, violating that assumption could
1073 * result in seeing through to another display plane.
1074 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001075static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001076gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001077 pixman_region32_t *output_damage)
1078{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001079 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001080 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001081 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001082 EGLBoolean ret;
1083 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001084 int i, nrects, buffer_height;
1085 EGLint *egl_damage, *d;
1086 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001087 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001088 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001089
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001090 if (use_output(output) < 0)
1091 return;
1092
Jason Ekstrand00b84282013-10-27 22:24:59 -05001093 /* Calculate the viewport */
1094 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1095 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1096 output->current_mode->width,
1097 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001098
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001099 /* Calculate the global GL matrix */
1100 go->output_matrix = output->matrix;
1101 weston_matrix_translate(&go->output_matrix,
1102 -(output->current_mode->width / 2.0),
1103 -(output->current_mode->height / 2.0), 0);
1104 weston_matrix_scale(&go->output_matrix,
1105 2.0 / output->current_mode->width,
1106 -2.0 / output->current_mode->height, 1);
1107
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001108 /* if debugging, redraw everything outside the damage to clean up
1109 * debug lines from the previous draw on this buffer:
1110 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001111 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001112 pixman_region32_t undamaged;
1113 pixman_region32_init(&undamaged);
1114 pixman_region32_subtract(&undamaged, &output->region,
1115 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001116 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001118 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001119 pixman_region32_fini(&undamaged);
1120 }
1121
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001122 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001123 pixman_region32_init(&buffer_damage);
1124
Jason Ekstrande5512d42014-02-04 21:36:38 -06001125 output_get_damage(output, &buffer_damage, &border_damage);
1126 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001127
1128 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001129 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001130
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001132
1133 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001134 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001135
Jason Ekstrande5512d42014-02-04 21:36:38 -06001136 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001137
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001138 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001139 wl_signal_emit(&output->frame_signal, output);
1140
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001141 if (gr->swap_buffers_with_damage) {
1142 pixman_region32_init(&buffer_damage);
1143 weston_transformed_region(output->width, output->height,
1144 output->transform,
1145 output->current_scale,
1146 output_damage, &buffer_damage);
1147
1148 if (output_has_borders(output)) {
1149 pixman_region32_translate(&buffer_damage,
1150 go->borders[GL_RENDERER_BORDER_LEFT].width,
1151 go->borders[GL_RENDERER_BORDER_TOP].height);
1152 output_get_border_damage(output, go->border_status,
1153 &buffer_damage);
1154 }
1155
1156 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1157 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1158
1159 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1160 output->current_mode->height +
1161 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1162
1163 d = egl_damage;
1164 for (i = 0; i < nrects; ++i) {
1165 *d++ = rects[i].x1;
1166 *d++ = buffer_height - rects[i].y2;
1167 *d++ = rects[i].x2 - rects[i].x1;
1168 *d++ = rects[i].y2 - rects[i].y1;
1169 }
1170 ret = gr->swap_buffers_with_damage(gr->egl_display,
1171 go->egl_surface,
1172 egl_damage, nrects);
1173 free(egl_damage);
1174 pixman_region32_fini(&buffer_damage);
1175 } else {
1176 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1177 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001178
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001179 if (ret == EGL_FALSE && !errored) {
1180 errored = 1;
1181 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001182 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001183 }
1184
Jason Ekstrande5512d42014-02-04 21:36:38 -06001185 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001186}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001187
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001188static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001189gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001190 pixman_format_code_t format, void *pixels,
1191 uint32_t x, uint32_t y,
1192 uint32_t width, uint32_t height)
1193{
1194 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001195 struct gl_output_state *go = get_output_state(output);
1196
1197 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1198 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001199
1200 switch (format) {
1201 case PIXMAN_a8r8g8b8:
1202 gl_format = GL_BGRA_EXT;
1203 break;
1204 case PIXMAN_a8b8g8r8:
1205 gl_format = GL_RGBA;
1206 break;
1207 default:
1208 return -1;
1209 }
1210
1211 if (use_output(output) < 0)
1212 return -1;
1213
1214 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1215 glReadPixels(x, y, width, height, gl_format,
1216 GL_UNSIGNED_BYTE, pixels);
1217
1218 return 0;
1219}
1220
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001221static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001222gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001223{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001224 struct gl_renderer *gr = get_renderer(surface->compositor);
1225 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001226 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001228 bool texture_used;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001229
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001230#ifdef GL_EXT_unpack_subimage
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231 pixman_box32_t *rectangles;
1232 void *data;
1233 int i, n;
1234#endif
1235
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001236 pixman_region32_union(&gs->texture_damage,
1237 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001238
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001239 if (!buffer)
1240 return;
1241
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001242 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001243 * We still accumulate the damage in texture_damage, and
1244 * hold the reference to the buffer, in case the surface
1245 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001246 */
Derek Foreman97746792015-11-18 16:32:28 -06001247 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 wl_list_for_each(view, &surface->views, surface_link) {
1249 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001250 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 break;
1252 }
1253 }
1254 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001255 return;
1256
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001257 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1258 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001259 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001260
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001261 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001262
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001263 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001264 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001265 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001266 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001267 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001268 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001269 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001270
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001271 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001272 }
1273
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001274#ifdef GL_EXT_unpack_subimage
1275 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001276 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001277
1278 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001279 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1280 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001281 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001282 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001283 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001284 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001285 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001286 goto done;
1287 }
1288
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001289 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001290 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001291 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001292 pixman_box32_t r;
1293
1294 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1295
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001296 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1297 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001298 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1299 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001300 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001301 }
Neil Robertse5051712013-11-13 15:44:06 +00001302 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001303#endif
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001304
1305done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001306 pixman_region32_fini(&gs->texture_damage);
1307 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001308 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001309
1310 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001311}
1312
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001313static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001314ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001315{
1316 int i;
1317
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001318 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001319 return;
1320
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001321 for (i = gs->num_textures; i < num_textures; i++) {
1322 glGenTextures(1, &gs->textures[i]);
1323 glBindTexture(gs->target, gs->textures[i]);
1324 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001325 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001326 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001327 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1328 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001329 gs->num_textures = num_textures;
1330 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001331}
1332
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001333static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001334gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1335 struct wl_shm_buffer *shm_buffer)
1336{
1337 struct weston_compositor *ec = es->compositor;
1338 struct gl_renderer *gr = get_renderer(ec);
1339 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001340 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001341 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001342
1343 buffer->shm_buffer = shm_buffer;
1344 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1345 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1346
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001347 switch (wl_shm_buffer_get_format(shm_buffer)) {
1348 case WL_SHM_FORMAT_XRGB8888:
1349 gs->shader = &gr->texture_shader_rgbx;
1350 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001351 gl_format = GL_BGRA_EXT;
1352 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001353 break;
1354 case WL_SHM_FORMAT_ARGB8888:
1355 gs->shader = &gr->texture_shader_rgba;
1356 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001357 gl_format = GL_BGRA_EXT;
1358 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001359 break;
1360 case WL_SHM_FORMAT_RGB565:
1361 gs->shader = &gr->texture_shader_rgbx;
1362 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001363 gl_format = GL_RGB;
1364 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001365 break;
1366 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001367 weston_log("warning: unknown shm buffer format: %08x\n",
1368 wl_shm_buffer_get_format(shm_buffer));
1369 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001370 }
1371
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001372 /* Only allocate a texture if it doesn't match existing one.
1373 * If a switch from DRM allocated buffer to a SHM buffer is
1374 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001375 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001376 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001377 gl_format != gs->gl_format ||
1378 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001379 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001380 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001381 gs->height = buffer->height;
1382 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001383 gs->gl_format = gl_format;
1384 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001385 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001386 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001387 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001388
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001389 gs->surface = es;
1390
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001391 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001392 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001393}
1394
1395static void
1396gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1397 uint32_t format)
1398{
1399 struct weston_compositor *ec = es->compositor;
1400 struct gl_renderer *gr = get_renderer(ec);
1401 struct gl_surface_state *gs = get_surface_state(es);
1402 EGLint attribs[3];
1403 int i, num_planes;
1404
1405 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1406 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1407 EGL_WIDTH, &buffer->width);
1408 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1409 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001410 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1411 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001412
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001413 for (i = 0; i < gs->num_images; i++) {
1414 egl_image_unref(gs->images[i]);
1415 gs->images[i] = NULL;
1416 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001417 gs->num_images = 0;
1418 gs->target = GL_TEXTURE_2D;
1419 switch (format) {
1420 case EGL_TEXTURE_RGB:
1421 case EGL_TEXTURE_RGBA:
1422 default:
1423 num_planes = 1;
1424 gs->shader = &gr->texture_shader_rgba;
1425 break;
1426 case EGL_TEXTURE_EXTERNAL_WL:
1427 num_planes = 1;
1428 gs->target = GL_TEXTURE_EXTERNAL_OES;
1429 gs->shader = &gr->texture_shader_egl_external;
1430 break;
1431 case EGL_TEXTURE_Y_UV_WL:
1432 num_planes = 2;
1433 gs->shader = &gr->texture_shader_y_uv;
1434 break;
1435 case EGL_TEXTURE_Y_U_V_WL:
1436 num_planes = 3;
1437 gs->shader = &gr->texture_shader_y_u_v;
1438 break;
1439 case EGL_TEXTURE_Y_XUXV_WL:
1440 num_planes = 2;
1441 gs->shader = &gr->texture_shader_y_xuxv;
1442 break;
1443 }
1444
1445 ensure_textures(gs, num_planes);
1446 for (i = 0; i < num_planes; i++) {
1447 attribs[0] = EGL_WAYLAND_PLANE_WL;
1448 attribs[1] = i;
1449 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001450 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001451 EGL_WAYLAND_BUFFER_WL,
1452 buffer->legacy_buffer,
1453 attribs);
1454 if (!gs->images[i]) {
1455 weston_log("failed to create img for plane %d\n", i);
1456 continue;
1457 }
1458 gs->num_images++;
1459
1460 glActiveTexture(GL_TEXTURE0 + i);
1461 glBindTexture(gs->target, gs->textures[i]);
1462 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001463 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001464 }
1465
1466 gs->pitch = buffer->width;
1467 gs->height = buffer->height;
1468 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001469 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001470}
1471
1472static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001473gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1474{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001475 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001476
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001477 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001478}
1479
1480static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001481import_simple_dmabuf(struct gl_renderer *gr,
1482 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001483{
1484 struct egl_image *image;
1485 EGLint attribs[30];
1486 int atti = 0;
1487
Pekka Paalanena3525802014-06-12 16:49:29 +03001488 /* This requires the Mesa commit in
1489 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1490 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1491 * Otherwise Mesa closes the fd behind our back and re-importing
1492 * will fail.
1493 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1494 */
1495
1496 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001497 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001498 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001499 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001500 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001501 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001502 /* XXX: Add modifier here when supported */
1503
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001504 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001505 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001506 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001507 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001508 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001509 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001510 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001511 }
1512
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001513 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001514 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001515 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001516 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001517 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001518 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001519 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001520 }
1521
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001522 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001523 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001524 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001525 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001526 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001527 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001528 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001529 }
1530
1531 attribs[atti++] = EGL_NONE;
1532
1533 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1534 attribs);
1535
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001536 return image;
1537}
Pekka Paalanena3525802014-06-12 16:49:29 +03001538
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001539/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1540 * some of the definitions here so that building Weston won't require
1541 * bleeding-edge kernel headers.
1542 */
1543#ifndef DRM_FORMAT_R8
1544#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1545#endif
1546
1547#ifndef DRM_FORMAT_GR88
1548#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1549#endif
1550
1551struct yuv_format_descriptor yuv_formats[] = {
1552 {
1553 .format = DRM_FORMAT_YUYV,
1554 .input_planes = 1,
1555 .output_planes = 2,
1556 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1557 {{
1558 .width_divisor = 1,
1559 .height_divisor = 1,
1560 .format = DRM_FORMAT_GR88,
1561 .plane_index = 0
1562 }, {
1563 .width_divisor = 2,
1564 .height_divisor = 1,
1565 .format = DRM_FORMAT_ARGB8888,
1566 .plane_index = 0
1567 }}
1568 }, {
1569 .format = DRM_FORMAT_NV12,
1570 .input_planes = 2,
1571 .output_planes = 2,
1572 .texture_type = EGL_TEXTURE_Y_UV_WL,
1573 {{
1574 .width_divisor = 1,
1575 .height_divisor = 1,
1576 .format = DRM_FORMAT_R8,
1577 .plane_index = 0
1578 }, {
1579 .width_divisor = 2,
1580 .height_divisor = 2,
1581 .format = DRM_FORMAT_GR88,
1582 .plane_index = 1
1583 }}
1584 }, {
1585 .format = DRM_FORMAT_YUV420,
1586 .input_planes = 3,
1587 .output_planes = 3,
1588 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1589 {{
1590 .width_divisor = 1,
1591 .height_divisor = 1,
1592 .format = DRM_FORMAT_R8,
1593 .plane_index = 0
1594 }, {
1595 .width_divisor = 2,
1596 .height_divisor = 2,
1597 .format = DRM_FORMAT_R8,
1598 .plane_index = 1
1599 }, {
1600 .width_divisor = 2,
1601 .height_divisor = 2,
1602 .format = DRM_FORMAT_R8,
1603 .plane_index = 2
1604 }}
1605 }
1606};
1607
1608static struct egl_image *
1609import_dmabuf_single_plane(struct gl_renderer *gr,
1610 const struct dmabuf_attributes *attributes,
1611 struct yuv_plane_descriptor *descriptor)
1612{
1613 struct dmabuf_attributes plane;
1614 struct egl_image *image;
1615 char fmt[4];
1616
1617 plane.width = attributes->width / descriptor->width_divisor;
1618 plane.height = attributes->height / descriptor->height_divisor;
1619 plane.format = descriptor->format;
1620 plane.n_planes = 1;
1621 plane.fd[0] = attributes->fd[descriptor->plane_index];
1622 plane.offset[0] = attributes->offset[descriptor->plane_index];
1623 plane.stride[0] = attributes->stride[descriptor->plane_index];
1624 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1625
1626 image = import_simple_dmabuf(gr, &plane);
1627 if (!image) {
1628 weston_log("Failed to import plane %d as %.4s\n",
1629 descriptor->plane_index,
1630 dump_format(descriptor->format, fmt));
1631 return NULL;
1632 }
1633
1634 return image;
1635}
1636
1637static bool
1638import_yuv_dmabuf(struct gl_renderer *gr,
1639 struct dmabuf_image *image)
1640{
1641 unsigned i;
1642 int j;
1643 int ret;
1644 struct yuv_format_descriptor *format = NULL;
1645 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1646 char fmt[4];
1647
1648 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1649 if (yuv_formats[i].format == attributes->format) {
1650 format = &yuv_formats[i];
1651 break;
1652 }
1653 }
1654
1655 if (!format) {
1656 weston_log("Error during import, and no known conversion for format "
1657 "%.4s in the renderer",
1658 dump_format(attributes->format, fmt));
1659 return false;
1660 }
1661
1662 if (attributes->n_planes != format->input_planes) {
1663 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1664 dump_format(format->format, fmt),
1665 format->input_planes,
1666 (format->input_planes > 1) ? "s" : "",
1667 attributes->n_planes);
1668 return false;
1669 }
1670
1671 for (j = 0; j < format->output_planes; ++j) {
1672 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1673 &format->plane[j]);
1674 if (!image->images[j]) {
1675 while (j) {
1676 ret = egl_image_unref(image->images[--j]);
1677 assert(ret == 0);
1678 }
1679 return false;
1680 }
1681 }
1682
1683 image->num_images = format->output_planes;
1684
1685 switch (format->texture_type) {
1686 case EGL_TEXTURE_Y_XUXV_WL:
1687 image->shader = &gr->texture_shader_y_xuxv;
1688 break;
1689 case EGL_TEXTURE_Y_UV_WL:
1690 image->shader = &gr->texture_shader_y_uv;
1691 break;
1692 case EGL_TEXTURE_Y_U_V_WL:
1693 image->shader = &gr->texture_shader_y_u_v;
1694 break;
1695 default:
1696 assert(false);
1697 }
1698
1699 return true;
1700}
1701
1702static GLenum
1703choose_texture_target(struct dmabuf_attributes *attributes)
1704{
1705 if (attributes->n_planes > 1)
1706 return GL_TEXTURE_EXTERNAL_OES;
1707
1708 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1709 case DRM_FORMAT_YUYV:
1710 case DRM_FORMAT_YVYU:
1711 case DRM_FORMAT_UYVY:
1712 case DRM_FORMAT_VYUY:
1713 case DRM_FORMAT_AYUV:
1714 return GL_TEXTURE_EXTERNAL_OES;
1715 default:
1716 return GL_TEXTURE_2D;
1717 }
1718}
1719
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001720static struct dmabuf_image *
1721import_dmabuf(struct gl_renderer *gr,
1722 struct linux_dmabuf_buffer *dmabuf)
1723{
1724 struct egl_image *egl_image;
1725 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001726
1727 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001728 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001729
1730 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1731 if (egl_image) {
1732 image->num_images = 1;
1733 image->images[0] = egl_image;
1734 image->import_type = IMPORT_TYPE_DIRECT;
1735 image->target = choose_texture_target(&dmabuf->attributes);
1736
1737 switch (image->target) {
1738 case GL_TEXTURE_2D:
1739 image->shader = &gr->texture_shader_rgba;
1740 break;
1741 default:
1742 image->shader = &gr->texture_shader_egl_external;
1743 }
1744 } else {
1745 if (!import_yuv_dmabuf(gr, image)) {
1746 dmabuf_image_destroy(image);
1747 return NULL;
1748 }
1749 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1750 image->target = GL_TEXTURE_2D;
1751 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001752
1753 return image;
1754}
1755
1756static bool
1757gl_renderer_import_dmabuf(struct weston_compositor *ec,
1758 struct linux_dmabuf_buffer *dmabuf)
1759{
1760 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001761 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001762 int i;
1763
1764 assert(gr->has_dmabuf_import);
1765
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001766 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001767 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001768 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001769 return false;
1770 }
1771
1772 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001773 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001774 return false;
1775
1776 image = import_dmabuf(gr, dmabuf);
1777 if (!image)
1778 return false;
1779
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001780 wl_list_insert(&gr->dmabuf_images, &image->link);
1781 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1782 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001783
1784 return true;
1785}
1786
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001787static bool
1788import_known_dmabuf(struct gl_renderer *gr,
1789 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001790{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001791 switch (image->import_type) {
1792 case IMPORT_TYPE_DIRECT:
1793 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1794 if (!image->images[0])
1795 return false;
1796 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001797
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001798 case IMPORT_TYPE_GL_CONVERSION:
1799 if (!import_yuv_dmabuf(gr, image))
1800 return false;
1801 break;
1802
Pekka Paalanena3525802014-06-12 16:49:29 +03001803 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001804 weston_log("Invalid import type for dmabuf\n");
1805 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001806 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001807
1808 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001809}
1810
1811static void
1812gl_renderer_attach_dmabuf(struct weston_surface *surface,
1813 struct weston_buffer *buffer,
1814 struct linux_dmabuf_buffer *dmabuf)
1815{
1816 struct gl_renderer *gr = get_renderer(surface->compositor);
1817 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001818 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001819 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001820 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001821
1822 if (!gr->has_dmabuf_import) {
1823 linux_dmabuf_buffer_send_server_error(dmabuf,
1824 "EGL dmabuf import not supported");
1825 return;
1826 }
1827
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001828 buffer->width = dmabuf->attributes.width;
1829 buffer->height = dmabuf->attributes.height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001830 buffer->y_inverted =
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001831 !!(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001832
1833 for (i = 0; i < gs->num_images; i++)
1834 egl_image_unref(gs->images[i]);
1835 gs->num_images = 0;
1836
Pekka Paalanena3525802014-06-12 16:49:29 +03001837 /*
1838 * We try to always hold an imported EGLImage from the dmabuf
1839 * to prevent the client from preventing re-imports. But, we also
1840 * need to re-import every time the contents may change because
1841 * GL driver's caching may need flushing.
1842 *
1843 * Here we release the cache reference which has to be final.
1844 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001845 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001846
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001847 /* The dmabuf_image should have been created during the import */
1848 assert(image != NULL);
1849
1850 for (i = 0; i < image->num_images; ++i) {
1851 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001852 assert(ret == 0);
1853 }
1854
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001855 if (!import_known_dmabuf(gr, image)) {
1856 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1857 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001858 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001859
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001860 gs->num_images = image->num_images;
1861 for (i = 0; i < gs->num_images; ++i)
1862 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001863
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001864 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001865 ensure_textures(gs, gs->num_images);
1866 for (i = 0; i < gs->num_images; ++i) {
1867 glActiveTexture(GL_TEXTURE0 + i);
1868 glBindTexture(gs->target, gs->textures[i]);
1869 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1870 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001871
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001872 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001873 gs->pitch = buffer->width;
1874 gs->height = buffer->height;
1875 gs->buffer_type = BUFFER_TYPE_EGL;
1876 gs->y_inverted = buffer->y_inverted;
1877}
1878
1879static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001880gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001881{
1882 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001883 struct gl_renderer *gr = get_renderer(ec);
1884 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001885 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001886 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001887 EGLint format;
1888 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001889
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001890 weston_buffer_reference(&gs->buffer_ref, buffer);
1891
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001892 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001893 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001894 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001895 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001896 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001897 gs->num_images = 0;
1898 glDeleteTextures(gs->num_textures, gs->textures);
1899 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001900 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001901 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001902 return;
1903 }
1904
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001905 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001906
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001907 if (shm_buffer)
1908 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001909 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001910 EGL_TEXTURE_FORMAT, &format))
1911 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001912 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1913 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001914 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001915 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001916 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001917 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001918 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001919 }
1920}
1921
Kristian Høgsberg42263852012-09-06 21:59:29 -04001922static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001923gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001924 float red, float green, float blue, float alpha)
1925{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001926 struct gl_surface_state *gs = get_surface_state(surface);
1927 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001928
1929 gs->color[0] = red;
1930 gs->color[1] = green;
1931 gs->color[2] = blue;
1932 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001933 gs->buffer_type = BUFFER_TYPE_SOLID;
1934 gs->pitch = 1;
1935 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001936
John Kåre Alsaker40684142012-11-13 19:10:25 +01001937 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001938}
1939
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001940static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001941gl_renderer_surface_get_content_size(struct weston_surface *surface,
1942 int *width, int *height)
1943{
1944 struct gl_surface_state *gs = get_surface_state(surface);
1945
1946 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1947 *width = 0;
1948 *height = 0;
1949 } else {
1950 *width = gs->pitch;
1951 *height = gs->height;
1952 }
1953}
1954
1955static uint32_t
1956pack_color(pixman_format_code_t format, float *c)
1957{
1958 uint8_t r = round(c[0] * 255.0f);
1959 uint8_t g = round(c[1] * 255.0f);
1960 uint8_t b = round(c[2] * 255.0f);
1961 uint8_t a = round(c[3] * 255.0f);
1962
1963 switch (format) {
1964 case PIXMAN_a8b8g8r8:
1965 return (a << 24) | (b << 16) | (g << 8) | r;
1966 default:
1967 assert(0);
1968 return 0;
1969 }
1970}
1971
1972static int
1973gl_renderer_surface_copy_content(struct weston_surface *surface,
1974 void *target, size_t size,
1975 int src_x, int src_y,
1976 int width, int height)
1977{
1978 static const GLfloat verts[4 * 2] = {
1979 0.0f, 0.0f,
1980 1.0f, 0.0f,
1981 1.0f, 1.0f,
1982 0.0f, 1.0f
1983 };
1984 static const GLfloat projmat_normal[16] = { /* transpose */
1985 2.0f, 0.0f, 0.0f, 0.0f,
1986 0.0f, 2.0f, 0.0f, 0.0f,
1987 0.0f, 0.0f, 1.0f, 0.0f,
1988 -1.0f, -1.0f, 0.0f, 1.0f
1989 };
1990 static const GLfloat projmat_yinvert[16] = { /* transpose */
1991 2.0f, 0.0f, 0.0f, 0.0f,
1992 0.0f, -2.0f, 0.0f, 0.0f,
1993 0.0f, 0.0f, 1.0f, 0.0f,
1994 -1.0f, 1.0f, 0.0f, 1.0f
1995 };
1996 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1997 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1998 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1999 struct gl_renderer *gr = get_renderer(surface->compositor);
2000 struct gl_surface_state *gs = get_surface_state(surface);
2001 int cw, ch;
2002 GLuint fbo;
2003 GLuint tex;
2004 GLenum status;
2005 const GLfloat *proj;
2006 int i;
2007
2008 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2009
2010 switch (gs->buffer_type) {
2011 case BUFFER_TYPE_NULL:
2012 return -1;
2013 case BUFFER_TYPE_SOLID:
2014 *(uint32_t *)target = pack_color(format, gs->color);
2015 return 0;
2016 case BUFFER_TYPE_SHM:
2017 gl_renderer_flush_damage(surface);
2018 /* fall through */
2019 case BUFFER_TYPE_EGL:
2020 break;
2021 }
2022
2023 glGenTextures(1, &tex);
2024 glBindTexture(GL_TEXTURE_2D, tex);
2025 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2026 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2027 glBindTexture(GL_TEXTURE_2D, 0);
2028
2029 glGenFramebuffers(1, &fbo);
2030 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2031 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2032 GL_TEXTURE_2D, tex, 0);
2033
2034 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2035 if (status != GL_FRAMEBUFFER_COMPLETE) {
2036 weston_log("%s: fbo error: %#x\n", __func__, status);
2037 glDeleteFramebuffers(1, &fbo);
2038 glDeleteTextures(1, &tex);
2039 return -1;
2040 }
2041
2042 glViewport(0, 0, cw, ch);
2043 glDisable(GL_BLEND);
2044 use_shader(gr, gs->shader);
2045 if (gs->y_inverted)
2046 proj = projmat_normal;
2047 else
2048 proj = projmat_yinvert;
2049
2050 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2051 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2052
2053 for (i = 0; i < gs->num_textures; i++) {
2054 glUniform1i(gs->shader->tex_uniforms[i], i);
2055
2056 glActiveTexture(GL_TEXTURE0 + i);
2057 glBindTexture(gs->target, gs->textures[i]);
2058 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2059 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2060 }
2061
2062 /* position: */
2063 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2064 glEnableVertexAttribArray(0);
2065
2066 /* texcoord: */
2067 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2068 glEnableVertexAttribArray(1);
2069
2070 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2071
2072 glDisableVertexAttribArray(1);
2073 glDisableVertexAttribArray(0);
2074
2075 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2076 glReadPixels(src_x, src_y, width, height, gl_format,
2077 GL_UNSIGNED_BYTE, target);
2078
2079 glDeleteFramebuffers(1, &fbo);
2080 glDeleteTextures(1, &tex);
2081
2082 return 0;
2083}
2084
2085static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002086surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002087{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002088 int i;
2089
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002090 wl_list_remove(&gs->surface_destroy_listener.link);
2091 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002092
2093 gs->surface->renderer_state = NULL;
2094
2095 glDeleteTextures(gs->num_textures, gs->textures);
2096
2097 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002098 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002099
2100 weston_buffer_reference(&gs->buffer_ref, NULL);
2101 pixman_region32_fini(&gs->texture_damage);
2102 free(gs);
2103}
2104
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002105static void
2106surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2107{
2108 struct gl_surface_state *gs;
2109 struct gl_renderer *gr;
2110
2111 gs = container_of(listener, struct gl_surface_state,
2112 surface_destroy_listener);
2113
2114 gr = get_renderer(gs->surface->compositor);
2115
2116 surface_state_destroy(gs, gr);
2117}
2118
2119static void
2120surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2121{
2122 struct gl_surface_state *gs;
2123 struct gl_renderer *gr;
2124
2125 gr = data;
2126
2127 gs = container_of(listener, struct gl_surface_state,
2128 renderer_destroy_listener);
2129
2130 surface_state_destroy(gs, gr);
2131}
2132
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002133static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002134gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002135{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002136 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002137 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002138
Bryce Harringtonde16d892014-11-20 22:21:57 -08002139 gs = zalloc(sizeof *gs);
2140 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002141 return -1;
2142
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002143 /* A buffer is never attached to solid color surfaces, yet
2144 * they still go through texcoord computations. Do not divide
2145 * by zero there.
2146 */
2147 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002148 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002149
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002150 gs->surface = surface;
2151
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002152 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002153 surface->renderer_state = gs;
2154
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002155 gs->surface_destroy_listener.notify =
2156 surface_state_handle_surface_destroy;
2157 wl_signal_add(&surface->destroy_signal,
2158 &gs->surface_destroy_listener);
2159
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002160 gs->renderer_destroy_listener.notify =
2161 surface_state_handle_renderer_destroy;
2162 wl_signal_add(&gr->destroy_signal,
2163 &gs->renderer_destroy_listener);
2164
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002165 if (surface->buffer_ref.buffer) {
2166 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2167 gl_renderer_flush_damage(surface);
2168 }
2169
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002170 return 0;
2171}
2172
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002173static const char vertex_shader[] =
2174 "uniform mat4 proj;\n"
2175 "attribute vec2 position;\n"
2176 "attribute vec2 texcoord;\n"
2177 "varying vec2 v_texcoord;\n"
2178 "void main()\n"
2179 "{\n"
2180 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2181 " v_texcoord = texcoord;\n"
2182 "}\n";
2183
2184/* Declare common fragment shader uniforms */
2185#define FRAGMENT_CONVERT_YUV \
2186 " y *= alpha;\n" \
2187 " u *= alpha;\n" \
2188 " v *= alpha;\n" \
2189 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2190 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2191 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2192 " gl_FragColor.a = alpha;\n"
2193
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002194static const char fragment_debug[] =
2195 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2196
2197static const char fragment_brace[] =
2198 "}\n";
2199
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002200static const char texture_fragment_shader_rgba[] =
2201 "precision mediump float;\n"
2202 "varying vec2 v_texcoord;\n"
2203 "uniform sampler2D tex;\n"
2204 "uniform float alpha;\n"
2205 "void main()\n"
2206 "{\n"
2207 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002208 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002209
2210static const char texture_fragment_shader_rgbx[] =
2211 "precision mediump float;\n"
2212 "varying vec2 v_texcoord;\n"
2213 "uniform sampler2D tex;\n"
2214 "uniform float alpha;\n"
2215 "void main()\n"
2216 "{\n"
2217 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2218 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002219 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002220
2221static const char texture_fragment_shader_egl_external[] =
2222 "#extension GL_OES_EGL_image_external : require\n"
2223 "precision mediump float;\n"
2224 "varying vec2 v_texcoord;\n"
2225 "uniform samplerExternalOES tex;\n"
2226 "uniform float alpha;\n"
2227 "void main()\n"
2228 "{\n"
2229 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002230 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002231
2232static const char texture_fragment_shader_y_uv[] =
2233 "precision mediump float;\n"
2234 "uniform sampler2D tex;\n"
2235 "uniform sampler2D tex1;\n"
2236 "varying vec2 v_texcoord;\n"
2237 "uniform float alpha;\n"
2238 "void main() {\n"
2239 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2240 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2241 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2242 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002243 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002244
2245static const char texture_fragment_shader_y_u_v[] =
2246 "precision mediump float;\n"
2247 "uniform sampler2D tex;\n"
2248 "uniform sampler2D tex1;\n"
2249 "uniform sampler2D tex2;\n"
2250 "varying vec2 v_texcoord;\n"
2251 "uniform float alpha;\n"
2252 "void main() {\n"
2253 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2254 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2255 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2256 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002257 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002258
2259static const char texture_fragment_shader_y_xuxv[] =
2260 "precision mediump float;\n"
2261 "uniform sampler2D tex;\n"
2262 "uniform sampler2D tex1;\n"
2263 "varying vec2 v_texcoord;\n"
2264 "uniform float alpha;\n"
2265 "void main() {\n"
2266 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2267 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2268 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2269 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002270 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002271
2272static const char solid_fragment_shader[] =
2273 "precision mediump float;\n"
2274 "uniform vec4 color;\n"
2275 "uniform float alpha;\n"
2276 "void main()\n"
2277 "{\n"
2278 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002279 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002280
2281static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002282compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002283{
2284 GLuint s;
2285 char msg[512];
2286 GLint status;
2287
2288 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002289 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002290 glCompileShader(s);
2291 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2292 if (!status) {
2293 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2294 weston_log("shader info: %s\n", msg);
2295 return GL_NONE;
2296 }
2297
2298 return s;
2299}
2300
2301static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002302shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002303 const char *vertex_source, const char *fragment_source)
2304{
2305 char msg[512];
2306 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002307 int count;
2308 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002309
2310 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002311 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2312
2313 if (renderer->fragment_shader_debug) {
2314 sources[0] = fragment_source;
2315 sources[1] = fragment_debug;
2316 sources[2] = fragment_brace;
2317 count = 3;
2318 } else {
2319 sources[0] = fragment_source;
2320 sources[1] = fragment_brace;
2321 count = 2;
2322 }
2323
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002324 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002325 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002326
2327 shader->program = glCreateProgram();
2328 glAttachShader(shader->program, shader->vertex_shader);
2329 glAttachShader(shader->program, shader->fragment_shader);
2330 glBindAttribLocation(shader->program, 0, "position");
2331 glBindAttribLocation(shader->program, 1, "texcoord");
2332
2333 glLinkProgram(shader->program);
2334 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2335 if (!status) {
2336 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2337 weston_log("link info: %s\n", msg);
2338 return -1;
2339 }
2340
2341 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2342 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2343 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2344 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2345 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2346 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2347
2348 return 0;
2349}
2350
2351static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002352shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002353{
2354 glDeleteShader(shader->vertex_shader);
2355 glDeleteShader(shader->fragment_shader);
2356 glDeleteProgram(shader->program);
2357
2358 shader->vertex_shader = 0;
2359 shader->fragment_shader = 0;
2360 shader->program = 0;
2361}
2362
2363static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002364log_extensions(const char *name, const char *extensions)
2365{
2366 const char *p, *end;
2367 int l;
2368 int len;
2369
2370 l = weston_log("%s:", name);
2371 p = extensions;
2372 while (*p) {
2373 end = strchrnul(p, ' ');
2374 len = end - p;
2375 if (l + len > 78)
2376 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2377 len, p);
2378 else
2379 l += weston_log_continue(" %.*s", len, p);
2380 for (p = end; isspace(*p); p++)
2381 ;
2382 }
2383 weston_log_continue("\n");
2384}
2385
2386static void
2387log_egl_gl_info(EGLDisplay egldpy)
2388{
2389 const char *str;
2390
2391 str = eglQueryString(egldpy, EGL_VERSION);
2392 weston_log("EGL version: %s\n", str ? str : "(null)");
2393
2394 str = eglQueryString(egldpy, EGL_VENDOR);
2395 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2396
2397 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2398 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2399
2400 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2401 log_extensions("EGL extensions", str ? str : "(null)");
2402
2403 str = (char *)glGetString(GL_VERSION);
2404 weston_log("GL version: %s\n", str ? str : "(null)");
2405
2406 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2407 weston_log("GLSL version: %s\n", str ? str : "(null)");
2408
2409 str = (char *)glGetString(GL_VENDOR);
2410 weston_log("GL vendor: %s\n", str ? str : "(null)");
2411
2412 str = (char *)glGetString(GL_RENDERER);
2413 weston_log("GL renderer: %s\n", str ? str : "(null)");
2414
2415 str = (char *)glGetString(GL_EXTENSIONS);
2416 log_extensions("GL extensions", str ? str : "(null)");
2417}
2418
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002419static void
2420log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2421{
2422 EGLint r, g, b, a;
2423
2424 weston_log("Chosen EGL config details:\n");
2425
2426 weston_log_continue(STAMP_SPACE "RGBA bits");
2427 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2428 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2429 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2430 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2431 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2432 else
2433 weston_log_continue(" unknown\n");
2434
2435 weston_log_continue(STAMP_SPACE "swap interval range");
2436 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2437 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2438 weston_log_continue(": %d - %d\n", a, b);
2439 else
2440 weston_log_continue(" unknown\n");
2441}
2442
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002443static int
Derek Foremane76f1852015-05-15 12:12:39 -05002444match_config_to_visual(EGLDisplay egl_display,
2445 EGLint visual_id,
2446 EGLConfig *configs,
2447 int count)
2448{
2449 int i;
2450
2451 for (i = 0; i < count; ++i) {
2452 EGLint id;
2453
2454 if (!eglGetConfigAttrib(egl_display,
2455 configs[i], EGL_NATIVE_VISUAL_ID,
2456 &id))
2457 continue;
2458
2459 if (id == visual_id)
2460 return i;
2461 }
2462
Derek Foremane76f1852015-05-15 12:12:39 -05002463 return -1;
2464}
2465
2466static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002467egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002468 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002469 EGLConfig *config_out)
2470{
2471 EGLint count = 0;
2472 EGLint matched = 0;
2473 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002474 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002475
Derek Foremana7e19912015-05-20 14:57:58 -05002476 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2477 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002478 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002479 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002480 configs = calloc(count, sizeof *configs);
2481 if (!configs)
2482 return -1;
2483
2484 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002485 count, &matched) || !matched) {
2486 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002487 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002488 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002489
Derek Foremane76f1852015-05-15 12:12:39 -05002490 if (!visual_id)
2491 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002492
Derek Foremane76f1852015-05-15 12:12:39 -05002493 for (i = 0; config_index == -1 && i < n_ids; i++)
2494 config_index = match_config_to_visual(gr->egl_display,
2495 visual_id[i],
2496 configs,
2497 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002498
Derek Foremane76f1852015-05-15 12:12:39 -05002499 if (config_index != -1)
2500 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002501
2502out:
2503 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002504 if (config_index == -1)
2505 return -1;
2506
Derek Foremana7e19912015-05-20 14:57:58 -05002507 if (i > 1)
2508 weston_log("Unable to use first choice EGL config with id"
2509 " 0x%x, succeeded with alternate id 0x%x.\n",
2510 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002511 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002512}
2513
John Kåre Alsaker44154502012-11-13 19:10:20 +01002514static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002515gl_renderer_output_set_border(struct weston_output *output,
2516 enum gl_renderer_border_side side,
2517 int32_t width, int32_t height,
2518 int32_t tex_width, unsigned char *data)
2519{
2520 struct gl_output_state *go = get_output_state(output);
2521
Jason Ekstrande5512d42014-02-04 21:36:38 -06002522 if (go->borders[side].width != width ||
2523 go->borders[side].height != height)
2524 /* In this case, we have to blow everything and do a full
2525 * repaint. */
2526 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2527
2528 if (data == NULL) {
2529 width = 0;
2530 height = 0;
2531 }
2532
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002533 go->borders[side].width = width;
2534 go->borders[side].height = height;
2535 go->borders[side].tex_width = tex_width;
2536 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002537 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002538}
2539
John Kåre Alsaker94659272012-11-13 19:10:18 +01002540static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002541gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002542
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002543static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002544gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002545 EGLNativeWindowType window_for_legacy,
2546 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002547 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002548 const EGLint *visual_id,
2549 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002550{
2551 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002552 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002553 struct gl_output_state *go;
2554 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002555 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002556
Derek Foremane76f1852015-05-15 12:12:39 -05002557 if (egl_choose_config(gr, attribs, visual_id,
2558 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002559 weston_log("failed to choose EGL config for output\n");
2560 return -1;
2561 }
2562
2563 if (egl_config != gr->egl_config &&
2564 !gr->has_configless_context) {
2565 weston_log("attempted to use a different EGL config for an "
2566 "output but EGL_MESA_configless_context is not "
2567 "supported\n");
2568 return -1;
2569 }
2570
Bryce Harringtonde16d892014-11-20 22:21:57 -08002571 go = zalloc(sizeof *go);
2572 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002573 return -1;
2574
Jonny Lamb671148f2015-03-20 15:26:52 +01002575 if (gr->create_platform_window) {
2576 go->egl_surface =
2577 gr->create_platform_window(gr->egl_display,
2578 egl_config,
2579 window_for_platform,
2580 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002581 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002582 go->egl_surface =
2583 eglCreateWindowSurface(gr->egl_display,
2584 egl_config,
2585 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002586 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002587
2588 if (go->egl_surface == EGL_NO_SURFACE) {
2589 weston_log("failed to create egl surface\n");
2590 free(go);
2591 return -1;
2592 }
2593
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002594 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002595 pixman_region32_init(&go->buffer_damage[i]);
2596
John Kåre Alsaker94659272012-11-13 19:10:18 +01002597 output->renderer_state = go;
2598
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002599 log_egl_config_info(gr->egl_display, egl_config);
2600
John Kåre Alsaker94659272012-11-13 19:10:18 +01002601 return 0;
2602}
2603
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002604static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002605gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002606{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002607 struct gl_renderer *gr = get_renderer(output->compositor);
2608 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002609 int i;
2610
2611 for (i = 0; i < 2; i++)
2612 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002613
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002614 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002615
2616 free(go);
2617}
2618
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002619static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002620gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002621{
2622 return get_output_state(output)->egl_surface;
2623}
2624
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002625static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002626gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002627{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002628 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002629 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002630
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002631 wl_signal_emit(&gr->destroy_signal, gr);
2632
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002633 if (gr->has_bind_display)
2634 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002635
2636 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2637 eglMakeCurrent(gr->egl_display,
2638 EGL_NO_SURFACE, EGL_NO_SURFACE,
2639 EGL_NO_CONTEXT);
2640
Pekka Paalanena3525802014-06-12 16:49:29 +03002641
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002642 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2643 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002644
Armin Krezović28d240f2016-06-23 11:59:35 +02002645 if (gr->dummy_surface != EGL_NO_SURFACE)
2646 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2647
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002648 eglTerminate(gr->egl_display);
2649 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002650
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002651 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002652 wl_array_release(&gr->vtxcnt);
2653
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002654 if (gr->fragment_binding)
2655 weston_binding_destroy(gr->fragment_binding);
2656 if (gr->fan_binding)
2657 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002658
Scott Moreau976a0502013-03-07 10:15:17 -07002659 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002660}
2661
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002662static bool
2663check_extension(const char *extensions, const char *extension)
2664{
2665 size_t extlen = strlen(extension);
2666 const char *end = extensions + strlen(extensions);
2667
2668 while (extensions < end) {
2669 size_t n = 0;
2670
2671 /* Skip whitespaces, if any */
2672 if (*extensions == ' ') {
2673 extensions++;
2674 continue;
2675 }
2676
2677 n = strcspn(extensions, " ");
2678
2679 /* Compare strings */
2680 if (n == extlen && strncmp(extension, extensions, n) == 0)
2681 return true; /* Found */
2682
2683 extensions += n;
2684 }
2685
2686 /* Not found */
2687 return false;
2688}
2689
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002690static void
2691renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2692{
2693 const char *extensions;
2694
2695 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2696 if (!extensions) {
2697 weston_log("Retrieving EGL client extension string failed.\n");
2698 return;
2699 }
2700
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002701 if (check_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002702 gr->create_platform_window =
2703 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2704 else
2705 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2706}
2707
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002708static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002709gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2710{
2711 struct gl_renderer *gr = get_renderer(ec);
2712 const char *extensions;
2713 EGLBoolean ret;
2714
2715 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2716 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2717 gr->bind_display =
2718 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2719 gr->unbind_display =
2720 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2721 gr->query_buffer =
2722 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2723
2724 extensions =
2725 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2726 if (!extensions) {
2727 weston_log("Retrieving EGL extension string failed.\n");
2728 return -1;
2729 }
2730
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002731 if (check_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002732 gr->has_bind_display = 1;
2733 if (gr->has_bind_display) {
2734 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2735 if (!ret)
2736 gr->has_bind_display = 0;
2737 }
2738
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002739 if (check_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002740 gr->has_egl_buffer_age = 1;
2741 else
2742 weston_log("warning: EGL_EXT_buffer_age not supported. "
2743 "Performance could be affected.\n");
2744
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002745 if (check_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002746 gr->swap_buffers_with_damage =
2747 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2748 else
2749 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2750 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002751
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002752 if (check_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002753 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002754
Armin Krezović28d240f2016-06-23 11:59:35 +02002755 if (check_extension(extensions, "EGL_KHR_surfaceless_context"))
2756 gr->has_surfaceless_context = 1;
2757
Pekka Paalanena3525802014-06-12 16:49:29 +03002758#ifdef EGL_EXT_image_dma_buf_import
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002759 if (check_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002760 gr->has_dmabuf_import = 1;
2761#endif
2762
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002763 renderer_setup_egl_client_extensions(gr);
2764
Neil Robertsb7f85332014-03-07 18:05:49 +00002765 return 0;
2766}
2767
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002768static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002769 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2770 EGL_RED_SIZE, 1,
2771 EGL_GREEN_SIZE, 1,
2772 EGL_BLUE_SIZE, 1,
2773 EGL_ALPHA_SIZE, 0,
2774 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2775 EGL_NONE
2776};
2777
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002778static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002779 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2780 EGL_RED_SIZE, 1,
2781 EGL_GREEN_SIZE, 1,
2782 EGL_BLUE_SIZE, 1,
2783 EGL_ALPHA_SIZE, 1,
2784 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2785 EGL_NONE
2786};
2787
Armin Krezović28d240f2016-06-23 11:59:35 +02002788
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002789/** Checks whether a platform EGL client extension is supported
2790 *
2791 * \param ec The weston compositor
2792 * \param extension_suffix The EGL client extension suffix
2793 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2794 *
2795 * This function checks whether a specific platform_* extension is supported
2796 * by EGL.
2797 *
2798 * The extension suffix should be the suffix of the platform extension (that
2799 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2800 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2801 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2802 *
2803 * The return value is 1:
2804 * - if the supplied EGL client extension is supported.
2805 * The return value is 0:
2806 * - if the platform_base client extension isn't supported so will
2807 * fallback to eglGetDisplay and friends.
2808 * The return value is -1:
2809 * - if the supplied EGL client extension is not supported.
2810 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002811static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002812gl_renderer_supports(struct weston_compositor *ec,
2813 const char *extension_suffix)
2814{
2815 static const char *extensions = NULL;
2816 char s[64];
2817
2818 if (!extensions) {
2819 extensions = (const char *) eglQueryString(
2820 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2821
2822 if (!extensions)
2823 return 0;
2824
2825 log_extensions("EGL client extensions",
2826 extensions);
2827 }
2828
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002829 if (!check_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002830 return 0;
2831
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002832 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002833 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002834 return 1;
2835
2836 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002837 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002838 return 1;
2839
2840 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01002841 if (check_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002842 return 1;
2843
Pekka Paalanenf2824542015-04-08 17:02:21 +03002844 /* at this point we definitely have some platform extensions but
2845 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002846 * not supported. */
2847
2848 return -1;
2849}
2850
Jonny Lamb74eed312015-03-24 13:12:04 +01002851static const char *
2852platform_to_extension(EGLenum platform)
2853{
2854 switch (platform) {
2855 case EGL_PLATFORM_GBM_KHR:
2856 return "gbm";
2857 case EGL_PLATFORM_WAYLAND_KHR:
2858 return "wayland";
2859 case EGL_PLATFORM_X11_KHR:
2860 return "x11";
2861 default:
2862 assert(0 && "bad EGL platform enum");
2863 }
2864}
2865
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002866static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002867gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2868 EGLConfig pbuffer_config;
2869
2870 static const EGLint pbuffer_config_attribs[] = {
2871 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2872 EGL_RED_SIZE, 1,
2873 EGL_GREEN_SIZE, 1,
2874 EGL_BLUE_SIZE, 1,
2875 EGL_ALPHA_SIZE, 0,
2876 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2877 EGL_NONE
2878 };
2879
2880 static const EGLint pbuffer_attribs[] = {
2881 EGL_WIDTH, 10,
2882 EGL_HEIGHT, 10,
2883 EGL_NONE
2884 };
2885
2886 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2887 weston_log("failed to choose EGL config for PbufferSurface");
2888 return -1;
2889 }
2890
2891 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2892 pbuffer_config,
2893 pbuffer_attribs);
2894
2895 if (gr->dummy_surface == EGL_NO_SURFACE) {
2896 weston_log("failed to create PbufferSurface\n");
2897 return -1;
2898 }
2899
2900 return 0;
2901}
2902
2903static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002904gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2905 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002906 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002907{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002908 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002909 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002910 int supports = 0;
2911
2912 if (platform) {
2913 supports = gl_renderer_supports(
2914 ec, platform_to_extension(platform));
2915 if (supports < 0)
2916 return -1;
2917 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002918
Bryce Harringtonde16d892014-11-20 22:21:57 -08002919 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002920 if (gr == NULL)
2921 return -1;
2922
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002923 gr->base.read_pixels = gl_renderer_read_pixels;
2924 gr->base.repaint_output = gl_renderer_repaint_output;
2925 gr->base.flush_damage = gl_renderer_flush_damage;
2926 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002927 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002928 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002929 gr->base.surface_get_content_size =
2930 gl_renderer_surface_get_content_size;
2931 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002932 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002933
Jonny Lambf1ec5062015-03-24 13:12:05 +01002934 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002935 if (supports) {
2936 if (!get_platform_display) {
2937 get_platform_display = (void *) eglGetProcAddress(
2938 "eglGetPlatformDisplayEXT");
2939 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002940
Jonny Lamb74eed312015-03-24 13:12:04 +01002941 /* also wrap this in the supports check because
2942 * eglGetProcAddress can return non-NULL and still not
2943 * support the feature at runtime, so ensure the
2944 * appropriate extension checks have been done. */
2945 if (get_platform_display && platform) {
2946 gr->egl_display = get_platform_display(platform,
2947 native_window,
2948 NULL);
2949 }
2950 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002951
2952 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002953 weston_log("warning: either no EGL_EXT_platform_base "
2954 "support or specific platform support; "
2955 "falling back to eglGetDisplay.\n");
2956 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002957 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002958
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002959 if (gr->egl_display == EGL_NO_DISPLAY) {
2960 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002961 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002962 }
2963
2964 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2965 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002966 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002967 }
2968
Derek Foremane76f1852015-05-15 12:12:39 -05002969 if (egl_choose_config(gr, attribs, visual_id,
2970 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002971 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002972 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002973 }
2974
2975 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002976 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002977 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002978 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002979
Neil Robertsb7f85332014-03-07 18:05:49 +00002980 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002981 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002982
Pekka Paalanena3525802014-06-12 16:49:29 +03002983 wl_list_init(&gr->dmabuf_images);
2984 if (gr->has_dmabuf_import)
2985 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2986
Armin Krezović28d240f2016-06-23 11:59:35 +02002987 if (gr->has_surfaceless_context) {
2988 weston_log("EGL_KHR_surfaceless_context available\n");
2989 gr->dummy_surface = EGL_NO_SURFACE;
2990 } else {
2991 weston_log("EGL_KHR_surfaceless_context unavailable. "
2992 "Trying PbufferSurface\n");
2993
2994 if (gl_renderer_create_pbuffer_surface(gr) < 0)
2995 goto fail_with_error;
2996 }
2997
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002998 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2999
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003000 wl_signal_init(&gr->destroy_signal);
3001
Armin Krezović28d240f2016-06-23 11:59:35 +02003002 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3003 if (gr->dummy_surface != EGL_NO_SURFACE)
3004 eglDestroySurface(gr->egl_display, gr->dummy_surface);
3005 goto fail_with_error;
3006 }
3007
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003008 return 0;
3009
Derek Foreman066ca0c2015-06-11 12:14:45 -05003010fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003011 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003012fail_terminate:
3013 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003014fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003015 free(gr);
3016 return -1;
3017}
3018
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003019static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003020gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003021{
3022 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003023}
3024
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003025static int
3026compile_shaders(struct weston_compositor *ec)
3027{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003028 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003029
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003030 gr->texture_shader_rgba.vertex_source = vertex_shader;
3031 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3032
3033 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3034 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3035
3036 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3037 gr->texture_shader_egl_external.fragment_source =
3038 texture_fragment_shader_egl_external;
3039
3040 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3041 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3042
3043 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3044 gr->texture_shader_y_u_v.fragment_source =
3045 texture_fragment_shader_y_u_v;
3046
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003047 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003048 gr->texture_shader_y_xuxv.fragment_source =
3049 texture_fragment_shader_y_xuxv;
3050
3051 gr->solid_shader.vertex_source = vertex_shader;
3052 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003053
3054 return 0;
3055}
3056
3057static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003058fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3059 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003060{
3061 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003062 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003063 struct weston_output *output;
3064
John Kåre Alsaker40684142012-11-13 19:10:25 +01003065 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003066
John Kåre Alsaker40684142012-11-13 19:10:25 +01003067 shader_release(&gr->texture_shader_rgba);
3068 shader_release(&gr->texture_shader_rgbx);
3069 shader_release(&gr->texture_shader_egl_external);
3070 shader_release(&gr->texture_shader_y_uv);
3071 shader_release(&gr->texture_shader_y_u_v);
3072 shader_release(&gr->texture_shader_y_xuxv);
3073 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003074
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003075 /* Force use_shader() to call glUseProgram(), since we need to use
3076 * the recompiled version of the shader. */
3077 gr->current_shader = NULL;
3078
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003079 wl_list_for_each(output, &ec->output_list, link)
3080 weston_output_damage(output);
3081}
3082
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003083static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003084fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3085 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003086{
3087 struct weston_compositor *compositor = data;
3088 struct gl_renderer *gr = get_renderer(compositor);
3089
3090 gr->fan_debug = !gr->fan_debug;
3091 weston_compositor_damage_all(compositor);
3092}
3093
John Kåre Alsaker94659272012-11-13 19:10:18 +01003094static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003095gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003096{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003097 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003098 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003099 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003100 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003101
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003102 static const EGLint context_attribs[] = {
3103 EGL_CONTEXT_CLIENT_VERSION, 2,
3104 EGL_NONE
3105 };
3106
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003107 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3108 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003109 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003110 return -1;
3111 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003112
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003113 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003114
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003115 if (gr->has_configless_context)
3116 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003117
3118 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003119 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003120 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003121 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003122 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003123 return -1;
3124 }
3125
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003126 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3127 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003128 if (ret == EGL_FALSE) {
3129 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003130 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003131 return -1;
3132 }
3133
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003134 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003135
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003136 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003137 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003138
3139 extensions = (const char *) glGetString(GL_EXTENSIONS);
3140 if (!extensions) {
3141 weston_log("Retrieving GL extension string failed.\n");
3142 return -1;
3143 }
3144
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003145 if (!check_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003146 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3147 return -1;
3148 }
3149
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003150 if (check_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003151 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003152 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003153 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003154
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07003155#ifdef GL_EXT_unpack_subimage
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003156 if (check_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003157 gr->has_unpack_subimage = 1;
Kristian Høgsberg1c4f1632013-08-07 12:11:27 -07003158#endif
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003159
Miguel A. Vicocc3a1922016-03-21 17:37:33 +01003160 if (check_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003161 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003162
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003163 glActiveTexture(GL_TEXTURE0);
3164
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003165 if (compile_shaders(ec))
3166 return -1;
3167
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003168 gr->fragment_binding =
3169 weston_compositor_add_debug_binding(ec, KEY_S,
3170 fragment_debug_binding,
3171 ec);
3172 gr->fan_binding =
3173 weston_compositor_add_debug_binding(ec, KEY_F,
3174 fan_debug_repaint_binding,
3175 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003176
Pekka Paalanen035a0322012-10-24 09:43:06 +03003177 weston_log("GL ES 2 renderer features:\n");
3178 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003179 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003180 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003181 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003182 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003183 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003184
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003185
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003186 return 0;
3187}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003188
3189WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3190 .opaque_attribs = gl_renderer_opaque_attribs,
3191 .alpha_attribs = gl_renderer_alpha_attribs,
3192
3193 .create = gl_renderer_create,
3194 .display = gl_renderer_display,
3195 .output_create = gl_renderer_output_create,
3196 .output_destroy = gl_renderer_output_destroy,
3197 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003198 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003199 .print_egl_error_state = gl_renderer_print_egl_error_state
3200};