blob: 031576bcf9970013480a81d2a671fd0f04526d7d [file] [log] [blame]
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001/*
2 * Copyright © 2012 Intel Corporation
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02003 * Copyright © 2015 Collabora, Ltd.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040028
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010029#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
31
Derek Foremanf8180982014-10-16 16:37:02 -050032#include <stdbool.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030033#include <stdint.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040034#include <stdlib.h>
Kristian Høgsberg25894fc2012-09-05 22:06:26 -040035#include <string.h>
36#include <ctype.h>
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030037#include <float.h>
38#include <assert.h>
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +020039#include <linux/input.h>
Pekka Paalanena3525802014-06-12 16:49:29 +030040#include <drm_fourcc.h>
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -040041
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010042#include "gl-renderer.h"
Sam Spilsbury619859c2013-09-13 10:01:21 +080043#include "vertex-clipping.h"
Pekka Paalanena3525802014-06-12 16:49:29 +030044#include "linux-dmabuf.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080045#include "linux-dmabuf-unstable-v1-server-protocol.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010046
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010048#include "shared/platform.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010049#include "weston-egl-ext.h"
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040050
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010051struct gl_shader {
John Kåre Alsaker40684142012-11-13 19:10:25 +010052 GLuint program;
53 GLuint vertex_shader, fragment_shader;
54 GLint proj_uniform;
55 GLint tex_uniforms[3];
56 GLint alpha_uniform;
57 GLint color_uniform;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +030058 const char *vertex_source, *fragment_source;
John Kåre Alsaker40684142012-11-13 19:10:25 +010059};
60
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020061#define BUFFER_DAMAGE_COUNT 2
62
Jason Ekstrande5512d42014-02-04 21:36:38 -060063enum gl_border_status {
64 BORDER_STATUS_CLEAN = 0,
65 BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP,
66 BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT,
67 BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT,
68 BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM,
69 BORDER_ALL_DIRTY = 0xf,
70 BORDER_SIZE_CHANGED = 0x10
71};
72
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050073struct gl_border_image {
74 GLuint tex;
75 int32_t width, height;
76 int32_t tex_width;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050077 void *data;
78};
79
John Kåre Alsaker779b52a2012-11-13 19:10:29 +010080struct gl_output_state {
John Kåre Alsaker94659272012-11-13 19:10:18 +010081 EGLSurface egl_surface;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +020082 pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
Derek Foreman4c582662014-10-09 18:39:44 -050083 int buffer_damage_index;
Jason Ekstrande5512d42014-02-04 21:36:38 -060084 enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -050085 struct gl_border_image borders[4];
Jason Ekstrande5512d42014-02-04 21:36:38 -060086 enum gl_border_status border_status;
Jason Ekstrandfb23df72014-10-16 10:55:21 -050087
88 struct weston_matrix output_matrix;
John Kåre Alsaker94659272012-11-13 19:10:18 +010089};
90
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030091enum buffer_type {
92 BUFFER_TYPE_NULL,
Pekka Paalanenaeb917e2015-02-09 13:56:56 +020093 BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +030094 BUFFER_TYPE_SHM,
95 BUFFER_TYPE_EGL
96};
97
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +030098struct gl_renderer;
99
100struct egl_image {
101 struct gl_renderer *renderer;
102 EGLImageKHR image;
103 int refcount;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000104};
Pekka Paalanena3525802014-06-12 16:49:29 +0300105
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000106enum import_type {
107 IMPORT_TYPE_INVALID,
108 IMPORT_TYPE_DIRECT,
109 IMPORT_TYPE_GL_CONVERSION
110};
111
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000112struct dmabuf_image {
Pekka Paalanena3525802014-06-12 16:49:29 +0300113 struct linux_dmabuf_buffer *dmabuf;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000114 int num_images;
115 struct egl_image *images[3];
Pekka Paalanena3525802014-06-12 16:49:29 +0300116 struct wl_list link;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +0000117
118 enum import_type import_type;
119 GLenum target;
120 struct gl_shader *shader;
121};
122
123struct yuv_plane_descriptor {
124 int width_divisor;
125 int height_divisor;
126 uint32_t format;
127 int plane_index;
128};
129
130struct yuv_format_descriptor {
131 uint32_t format;
132 int input_planes;
133 int output_planes;
134 int texture_type;
135 struct yuv_plane_descriptor plane[4];
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300136};
137
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100138struct gl_surface_state {
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100139 GLfloat color[4];
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100140 struct gl_shader *shader;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100141
142 GLuint textures[3];
143 int num_textures;
Derek Foreman4c11fe72015-11-18 16:32:27 -0600144 bool needs_full_upload;
Pekka Paalanen81ee3f52012-12-04 15:58:16 +0200145 pixman_region32_t texture_damage;
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100146
Neil Roberts4d085e72014-04-07 15:01:01 +0100147 /* These are only used by SHM surfaces to detect when we need
148 * to do a full upload to specify a new internal texture
149 * format */
150 GLenum gl_format;
151 GLenum gl_pixel_type;
152
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300153 struct egl_image* images[3];
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100154 GLenum target;
155 int num_images;
Pekka Paalanenfb003d32012-12-04 15:58:13 +0200156
157 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +0300158 enum buffer_type buffer_type;
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200159 int pitch; /* in pixels */
Alexander Larsson4ea95522013-05-22 14:41:37 +0200160 int height; /* in pixels */
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400161 int y_inverted;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300162
163 struct weston_surface *surface;
164
165 struct wl_listener surface_destroy_listener;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300166 struct wl_listener renderer_destroy_listener;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100167};
168
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100169struct gl_renderer {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100170 struct weston_renderer base;
171 int fragment_shader_debug;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400172 int fan_debug;
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +0300173 struct weston_binding *fragment_binding;
174 struct weston_binding *fan_binding;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100175
176 EGLDisplay egl_display;
177 EGLContext egl_context;
178 EGLConfig egl_config;
John Kåre Alsaker44154502012-11-13 19:10:20 +0100179
Armin Krezović28d240f2016-06-23 11:59:35 +0200180 EGLSurface dummy_surface;
181
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400182 struct wl_array vertices;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400183 struct wl_array vtxcnt;
184
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100185 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
186 PFNEGLCREATEIMAGEKHRPROC create_image;
187 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600188 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Jonny Lamb671148f2015-03-20 15:26:52 +0100189 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window;
Jonny Lamb671148f2015-03-20 15:26:52 +0100190
John Kåre Alsaker320711d2012-11-13 19:10:27 +0100191 int has_unpack_subimage;
192
193 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
194 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
195 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
196 int has_bind_display;
197
198 int has_egl_image_external;
199
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +0200200 int has_egl_buffer_age;
201
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000202 int has_configless_context;
203
Armin Krezović28d240f2016-06-23 11:59:35 +0200204 int has_surfaceless_context;
205
Pekka Paalanena3525802014-06-12 16:49:29 +0300206 int has_dmabuf_import;
207 struct wl_list dmabuf_images;
208
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100209 struct gl_shader texture_shader_rgba;
210 struct gl_shader texture_shader_rgbx;
211 struct gl_shader texture_shader_egl_external;
212 struct gl_shader texture_shader_y_uv;
213 struct gl_shader texture_shader_y_u_v;
214 struct gl_shader texture_shader_y_xuxv;
215 struct gl_shader invert_color_shader;
216 struct gl_shader solid_shader;
217 struct gl_shader *current_shader;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +0300218
219 struct wl_signal destroy_signal;
Armin Krezović36d699a2016-08-05 15:28:30 +0200220
221 struct wl_listener output_destroy_listener;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100222};
John Kåre Alsaker94659272012-11-13 19:10:18 +0100223
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100224static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb70eba3f2015-03-20 15:26:50 +0100225
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000226static inline const char *
227dump_format(uint32_t format, char out[4])
228{
229#if BYTE_ORDER == BIG_ENDIAN
230 format = __builtin_bswap32(format);
231#endif
232 memcpy(out, &format, 4);
233 return out;
234}
235
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100236static inline struct gl_output_state *
John Kåre Alsaker94659272012-11-13 19:10:18 +0100237get_output_state(struct weston_output *output)
238{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100239 return (struct gl_output_state *)output->renderer_state;
John Kåre Alsaker94659272012-11-13 19:10:18 +0100240}
241
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300242static int
243gl_renderer_create_surface(struct weston_surface *surface);
244
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100245static inline struct gl_surface_state *
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100246get_surface_state(struct weston_surface *surface)
247{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300248 if (!surface->renderer_state)
249 gl_renderer_create_surface(surface);
250
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100251 return (struct gl_surface_state *)surface->renderer_state;
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100252}
253
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100254static inline struct gl_renderer *
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100255get_renderer(struct weston_compositor *ec)
256{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100257 return (struct gl_renderer *)ec->renderer;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100258}
259
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +0300260static struct egl_image*
261egl_image_create(struct gl_renderer *gr, EGLenum target,
262 EGLClientBuffer buffer, const EGLint *attribs)
263{
264 struct egl_image *img;
265
266 img = zalloc(sizeof *img);
267 img->renderer = gr;
268 img->refcount = 1;
269 img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT,
270 target, buffer, attribs);
271
272 if (img->image == EGL_NO_IMAGE_KHR) {
273 free(img);
274 return NULL;
275 }
276
277 return img;
278}
279
280static struct egl_image*
281egl_image_ref(struct egl_image *image)
282{
283 image->refcount++;
284
285 return image;
286}
287
288static int
289egl_image_unref(struct egl_image *image)
290{
291 struct gl_renderer *gr = image->renderer;
292
293 assert(image->refcount > 0);
294
295 image->refcount--;
296 if (image->refcount > 0)
297 return image->refcount;
298
299 gr->destroy_image(gr->egl_display, image->image);
300 free(image);
301
302 return 0;
303}
304
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +0000305static struct dmabuf_image*
306dmabuf_image_create(void)
307{
308 struct dmabuf_image *img;
309
310 img = zalloc(sizeof *img);
311 wl_list_init(&img->link);
312
313 return img;
314}
315
316static void
317dmabuf_image_destroy(struct dmabuf_image *image)
318{
319 int i;
320
321 for (i = 0; i < image->num_images; ++i)
322 egl_image_unref(image->images[i]);
323
324 if (image->dmabuf)
325 linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL);
326
327 wl_list_remove(&image->link);
328}
329
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400330static const char *
331egl_error_string(EGLint code)
332{
333#define MYERRCODE(x) case x: return #x;
334 switch (code) {
335 MYERRCODE(EGL_SUCCESS)
336 MYERRCODE(EGL_NOT_INITIALIZED)
337 MYERRCODE(EGL_BAD_ACCESS)
338 MYERRCODE(EGL_BAD_ALLOC)
339 MYERRCODE(EGL_BAD_ATTRIBUTE)
340 MYERRCODE(EGL_BAD_CONTEXT)
341 MYERRCODE(EGL_BAD_CONFIG)
342 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
343 MYERRCODE(EGL_BAD_DISPLAY)
344 MYERRCODE(EGL_BAD_SURFACE)
345 MYERRCODE(EGL_BAD_MATCH)
346 MYERRCODE(EGL_BAD_PARAMETER)
347 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
348 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
349 MYERRCODE(EGL_CONTEXT_LOST)
350 default:
351 return "unknown";
352 }
353#undef MYERRCODE
354}
355
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300356static void
Pekka Paalanen326529f2012-11-27 12:25:25 +0200357gl_renderer_print_egl_error_state(void)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400358{
359 EGLint code;
360
361 code = eglGetError();
362 weston_log("EGL error state: %s (0x%04lx)\n",
363 egl_error_string(code), (long)code);
364}
365
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400366#define max(a, b) (((a) > (b)) ? (a) : (b))
367#define min(a, b) (((a) > (b)) ? (b) : (a))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400368
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300369/*
370 * Compute the boundary vertices of the intersection of the global coordinate
371 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
372 * 'surf_rect' when transformed from surface coordinates into global coordinates.
373 * The vertices are written to 'ex' and 'ey', and the return value is the
374 * number of vertices. Vertices are produced in clockwise winding order.
375 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
376 * polygon area.
377 */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400378static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400380 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
381{
Sam Spilsbury619859c2013-09-13 10:01:21 +0800382
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300383 struct clip_context ctx;
384 int i, n;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400385 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300386 struct polygon8 surf = {
387 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
388 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
389 4
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400390 };
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400391
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300392 ctx.clip.x1 = rect->x1;
393 ctx.clip.y1 = rect->y1;
394 ctx.clip.x2 = rect->x2;
395 ctx.clip.y2 = rect->y2;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400396
397 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300398 for (i = 0; i < surf.n; i++)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500399 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
400 &surf.x[i], &surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400401
402 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300403 min_x = max_x = surf.x[0];
404 min_y = max_y = surf.y[0];
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400405
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300406 for (i = 1; i < surf.n; i++) {
407 min_x = min(min_x, surf.x[i]);
408 max_x = max(max_x, surf.x[i]);
409 min_y = min(min_y, surf.y[i]);
410 max_y = max(max_y, surf.y[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400411 }
412
413 /* First, simple bounding box check to discard early transformed
414 * surface rects that do not intersect with the clip region:
415 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300416 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
417 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400418 return 0;
419
420 /* Simple case, bounding box edges are parallel to surface edges,
421 * there will be only four edges. We just need to clip the surface
422 * vertices to the clip rect bounds:
423 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424 if (!ev->transform.enabled)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800425 return clip_simple(&ctx, &surf, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400426
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300427 /* Transformed case: use a general polygon clipping algorithm to
428 * clip the surface rectangle with each side of 'rect'.
429 * The algorithm is Sutherland-Hodgman, as explained in
430 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
431 * but without looking at any of that code.
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400432 */
Sam Spilsbury619859c2013-09-13 10:01:21 +0800433 n = clip_transformed(&ctx, &surf, ex, ey);
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300434
435 if (n < 3)
436 return 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400437
438 return n;
439}
440
Derek Foremanf8180982014-10-16 16:37:02 -0500441static bool
442merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge)
443{
444 if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) {
445 merge->x1 = a->x1;
446 merge->x2 = a->x2;
447 merge->y1 = b->y1;
448 merge->y2 = a->y2;
449 return true;
450 }
451 return false;
452}
453
454static int
455compress_bands(pixman_box32_t *inrects, int nrects,
456 pixman_box32_t **outrects)
457{
Quentin Glidicd84af7c2016-07-10 11:00:50 +0200458 bool merged = false;
Derek Foremanf8180982014-10-16 16:37:02 -0500459 pixman_box32_t *out, merge_rect;
460 int i, j, nout;
461
462 if (!nrects) {
463 *outrects = NULL;
464 return 0;
465 }
466
467 /* nrects is an upper bound - we're not too worried about
468 * allocating a little extra
469 */
470 out = malloc(sizeof(pixman_box32_t) * nrects);
471 out[0] = inrects[0];
472 nout = 1;
473 for (i = 1; i < nrects; i++) {
474 for (j = 0; j < nout; j++) {
475 merged = merge_down(&inrects[i], &out[j], &merge_rect);
476 if (merged) {
477 out[j] = merge_rect;
478 break;
479 }
480 }
481 if (!merged) {
482 out[nout] = inrects[i];
483 nout++;
484 }
485 }
486 *outrects = out;
487 return nout;
488}
489
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400490static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400492 pixman_region32_t *surf_region)
493{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494 struct gl_surface_state *gs = get_surface_state(ev->surface);
495 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400496 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400497 GLfloat *v, inv_width, inv_height;
498 unsigned int *vtxcnt, nvtx = 0;
499 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500500 pixman_box32_t *raw_rects;
501 int i, j, k, nrects, nsurf, raw_nrects;
502 bool used_band_compression;
503 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400504 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
505
Derek Foremanf8180982014-10-16 16:37:02 -0500506 if (raw_nrects < 4) {
507 used_band_compression = false;
508 nrects = raw_nrects;
509 rects = raw_rects;
510 } else {
511 nrects = compress_bands(raw_rects, raw_nrects, &rects);
512 used_band_compression = true;
513 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400514 /* worst case we can have 8 vertices per rect (ie. clipped into
515 * an octagon):
516 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400517 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
518 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200520 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200521 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400522
523 for (i = 0; i < nrects; i++) {
524 pixman_box32_t *rect = &rects[i];
525 for (j = 0; j < nsurf; j++) {
526 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200527 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528 GLfloat ex[8], ey[8]; /* edge points in screen space */
529 int n;
530
531 /* The transformed surface, after clipping to the clip region,
532 * can have as many as eight sides, emitted as a triangle-fan.
533 * The first vertex in the triangle fan can be chosen arbitrarily,
534 * since the area is guaranteed to be convex.
535 *
536 * If a corner of the transformed surface falls outside of the
537 * clip region, instead of emitting one vertex for the corner
538 * of the surface, up to two are emitted for two corresponding
539 * intersection point(s) between the surface and the clip region.
540 *
541 * To do this, we first calculate the (up to eight) points that
542 * form the intersection of the clip rect and the transformed
543 * surface.
544 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500545 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400546 if (n < 3)
547 continue;
548
549 /* emit edge points: */
550 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 weston_view_from_global_float(ev, ex[k], ey[k],
552 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400553 /* position: */
554 *(v++) = ex[k];
555 *(v++) = ey[k];
556 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_surface_to_buffer_float(ev->surface,
558 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200559 &bx, &by);
560 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400561 if (gs->y_inverted) {
562 *(v++) = by * inv_height;
563 } else {
564 *(v++) = (gs->height - by) * inv_height;
565 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566 }
567
568 vtxcnt[nvtx++] = n;
569 }
570 }
571
Derek Foremanf8180982014-10-16 16:37:02 -0500572 if (used_band_compression)
573 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574 return nvtx;
575}
576
577static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500578triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400579{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500580 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100581 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400582 int i;
583 GLushort *buffer;
584 GLushort *index;
585 int nelems;
586 static int color_idx = 0;
587 static const GLfloat color[][4] = {
588 { 1.0, 0.0, 0.0, 1.0 },
589 { 0.0, 1.0, 0.0, 1.0 },
590 { 0.0, 0.0, 1.0, 1.0 },
591 { 1.0, 1.0, 1.0, 1.0 },
592 };
593
594 nelems = (count - 1 + count - 2) * 2;
595
596 buffer = malloc(sizeof(GLushort) * nelems);
597 index = buffer;
598
599 for (i = 1; i < count; i++) {
600 *index++ = first;
601 *index++ = first + i;
602 }
603
604 for (i = 2; i < count; i++) {
605 *index++ = first + i - 1;
606 *index++ = first + i;
607 }
608
John Kåre Alsaker40684142012-11-13 19:10:25 +0100609 glUseProgram(gr->solid_shader.program);
610 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400611 color[color_idx++ % ARRAY_LENGTH(color)]);
612 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100613 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400614 free(buffer);
615}
616
617static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400619 pixman_region32_t *surf_region)
620{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500621 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400622 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400623 GLfloat *v;
624 unsigned int *vtxcnt;
625 int i, first, nfans;
626
627 /* The final region to be painted is the intersection of
628 * 'region' and 'surf_region'. However, 'region' is in the global
629 * coordinates, and 'surf_region' is in the surface-local
630 * coordinates. texture_region() will iterate over all pairs of
631 * rectangles from both regions, compute the intersection
632 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800633 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400634 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500635 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400636
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400637 v = gr->vertices.data;
638 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400639
640 /* position: */
641 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
642 glEnableVertexAttribArray(0);
643
644 /* texcoord: */
645 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
646 glEnableVertexAttribArray(1);
647
648 for (i = 0, first = 0; i < nfans; i++) {
649 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400650 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500651 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400652 first += vtxcnt[i];
653 }
654
655 glDisableVertexAttribArray(1);
656 glDisableVertexAttribArray(0);
657
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400658 gr->vertices.size = 0;
659 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400660}
661
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100662static int
663use_output(struct weston_output *output)
664{
665 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100666 struct gl_output_state *go = get_output_state(output);
667 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100668 EGLBoolean ret;
669
670 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
671 go->egl_surface, gr->egl_context);
672
673 if (ret == EGL_FALSE) {
674 if (errored)
675 return -1;
676 errored = 1;
677 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200678 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100679 return -1;
680 }
681
682 return 0;
683}
684
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300685static int
686shader_init(struct gl_shader *shader, struct gl_renderer *gr,
687 const char *vertex_source, const char *fragment_source);
688
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400689static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300690use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400691{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300692 if (!shader->program) {
693 int ret;
694
695 ret = shader_init(shader, gr,
696 shader->vertex_source,
697 shader->fragment_source);
698
699 if (ret < 0)
700 weston_log("warning: failed to compile shader\n");
701 }
702
John Kåre Alsaker40684142012-11-13 19:10:25 +0100703 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400704 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400705 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100706 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400707}
708
709static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100710shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 struct weston_view *view,
712 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713{
714 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500715 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500716 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400717
718 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500719 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100720 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400722
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100723 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400724 glUniform1i(shader->tex_uniforms[i], i);
725}
726
727static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728draw_view(struct weston_view *ev, struct weston_output *output,
729 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400730{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100732 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734 /* repaint bounding region in global coordinates: */
735 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200736 /* opaque region in surface coordinates: */
737 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400738 /* non-opaque region in surface coordinates: */
739 pixman_region32_t surface_blend;
740 GLint filter;
741 int i;
742
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200743 /* In case of a runtime switch of renderers, we may not have received
744 * an attach for this surface since the switch. In that case we don't
745 * have a valid buffer or a proper shader set up so skip rendering. */
746 if (!gs->shader)
747 return;
748
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400749 pixman_region32_init(&repaint);
750 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200751 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500752 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400753
754 if (!pixman_region32_not_empty(&repaint))
755 goto out;
756
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400757 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
758
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400759 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100760 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500761 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762 }
763
John Kåre Alsaker40684142012-11-13 19:10:25 +0100764 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500765 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400766
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200768 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400769 filter = GL_LINEAR;
770 else
771 filter = GL_NEAREST;
772
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100773 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100775 glBindTexture(gs->target, gs->textures[i]);
776 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
777 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400778 }
779
780 /* blended region is whole surface minus opaque region: */
781 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600782 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200783 if (ev->geometry.scissor_enabled)
784 pixman_region32_intersect(&surface_blend, &surface_blend,
785 &ev->geometry.scissor);
786 pixman_region32_subtract(&surface_blend, &surface_blend,
787 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400788
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200790 pixman_region32_init(&surface_opaque);
791 if (ev->geometry.scissor_enabled)
792 pixman_region32_intersect(&surface_opaque,
793 &ev->surface->opaque,
794 &ev->geometry.scissor);
795 else
796 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
797
798 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100799 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400800 /* Special case for RGBA textures with possibly
801 * bad data in alpha channel: use the shader
802 * that forces texture alpha = 1.0.
803 * Xwayland surfaces need this.
804 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100805 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400807 }
808
Jason Ekstranda7af7042013-10-12 22:38:11 -0500809 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400810 glEnable(GL_BLEND);
811 else
812 glDisable(GL_BLEND);
813
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200814 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400815 }
816
817 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100818 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400819 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500820 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400821 }
822
823 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200824 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400825
826out:
827 pixman_region32_fini(&repaint);
828}
829
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400830static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500831repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400832{
833 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500834 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400835
Jason Ekstranda7af7042013-10-12 22:38:11 -0500836 wl_list_for_each_reverse(view, &compositor->view_list, link)
837 if (view->plane == &compositor->primary_plane)
838 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400839}
840
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500841static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600842draw_output_border_texture(struct gl_output_state *go,
843 enum gl_renderer_border_side side,
844 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500845 int32_t width, int32_t height)
846{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600847 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500848 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
849
850 if (!img->data) {
851 if (img->tex) {
852 glDeleteTextures(1, &img->tex);
853 img->tex = 0;
854 }
855
856 return;
857 }
858
859 if (!img->tex) {
860 glGenTextures(1, &img->tex);
861 glBindTexture(GL_TEXTURE_2D, img->tex);
862
863 glTexParameteri(GL_TEXTURE_2D,
864 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
865 glTexParameteri(GL_TEXTURE_2D,
866 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
867 glTexParameteri(GL_TEXTURE_2D,
868 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
869 glTexParameteri(GL_TEXTURE_2D,
870 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
871 } else {
872 glBindTexture(GL_TEXTURE_2D, img->tex);
873 }
874
Jason Ekstrande5512d42014-02-04 21:36:38 -0600875 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500876 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
877 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
878 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500879 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
880 img->tex_width, img->height, 0,
881 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500882 }
883
884 GLfloat texcoord[] = {
885 0.0f, 0.0f,
886 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
887 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
888 0.0f, 1.0f,
889 };
890
891 GLfloat verts[] = {
892 x, y,
893 x + width, y,
894 x + width, y + height,
895 x, y + height
896 };
897
898 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
899 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
900 glEnableVertexAttribArray(0);
901 glEnableVertexAttribArray(1);
902
903 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
904
905 glDisableVertexAttribArray(1);
906 glDisableVertexAttribArray(0);
907}
908
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600909static int
910output_has_borders(struct weston_output *output)
911{
912 struct gl_output_state *go = get_output_state(output);
913
914 return go->borders[GL_RENDERER_BORDER_TOP].data ||
915 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
916 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
917 go->borders[GL_RENDERER_BORDER_LEFT].data;
918}
919
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500920static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600921draw_output_borders(struct weston_output *output,
922 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500923{
924 struct gl_output_state *go = get_output_state(output);
925 struct gl_renderer *gr = get_renderer(output->compositor);
926 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500927 struct gl_border_image *top, *bottom, *left, *right;
928 struct weston_matrix matrix;
929 int full_width, full_height;
930
Jason Ekstrande5512d42014-02-04 21:36:38 -0600931 if (border_status == BORDER_STATUS_CLEAN)
932 return; /* Clean. Nothing to do. */
933
Jason Ekstrand00b84282013-10-27 22:24:59 -0500934 top = &go->borders[GL_RENDERER_BORDER_TOP];
935 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
936 left = &go->borders[GL_RENDERER_BORDER_LEFT];
937 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
938
939 full_width = output->current_mode->width + left->width + right->width;
940 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500941
942 glDisable(GL_BLEND);
943 use_shader(gr, shader);
944
Jason Ekstrand00b84282013-10-27 22:24:59 -0500945 glViewport(0, 0, full_width, full_height);
946
947 weston_matrix_init(&matrix);
948 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
949 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
950 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500951
952 glUniform1i(shader->tex_uniforms[0], 0);
953 glUniform1f(shader->alpha_uniform, 1);
954 glActiveTexture(GL_TEXTURE0);
955
Jason Ekstrande5512d42014-02-04 21:36:38 -0600956 if (border_status & BORDER_TOP_DIRTY)
957 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
958 0, 0,
959 full_width, top->height);
960 if (border_status & BORDER_LEFT_DIRTY)
961 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
962 0, top->height,
963 left->width, output->current_mode->height);
964 if (border_status & BORDER_RIGHT_DIRTY)
965 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
966 full_width - right->width, top->height,
967 right->width, output->current_mode->height);
968 if (border_status & BORDER_BOTTOM_DIRTY)
969 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
970 0, full_height - bottom->height,
971 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500972}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100973
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400974static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600975output_get_border_damage(struct weston_output *output,
976 enum gl_border_status border_status,
977 pixman_region32_t *damage)
978{
979 struct gl_output_state *go = get_output_state(output);
980 struct gl_border_image *top, *bottom, *left, *right;
981 int full_width, full_height;
982
983 if (border_status == BORDER_STATUS_CLEAN)
984 return; /* Clean. Nothing to do. */
985
986 top = &go->borders[GL_RENDERER_BORDER_TOP];
987 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
988 left = &go->borders[GL_RENDERER_BORDER_LEFT];
989 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
990
991 full_width = output->current_mode->width + left->width + right->width;
992 full_height = output->current_mode->height + top->height + bottom->height;
993 if (border_status & BORDER_TOP_DIRTY)
994 pixman_region32_union_rect(damage, damage,
995 0, 0,
996 full_width, top->height);
997 if (border_status & BORDER_LEFT_DIRTY)
998 pixman_region32_union_rect(damage, damage,
999 0, top->height,
1000 left->width, output->current_mode->height);
1001 if (border_status & BORDER_RIGHT_DIRTY)
1002 pixman_region32_union_rect(damage, damage,
1003 full_width - right->width, top->height,
1004 right->width, output->current_mode->height);
1005 if (border_status & BORDER_BOTTOM_DIRTY)
1006 pixman_region32_union_rect(damage, damage,
1007 0, full_height - bottom->height,
1008 full_width, bottom->height);
1009}
1010
1011static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001012output_get_damage(struct weston_output *output,
1013 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001014{
1015 struct gl_output_state *go = get_output_state(output);
1016 struct gl_renderer *gr = get_renderer(output->compositor);
1017 EGLint buffer_age = 0;
1018 EGLBoolean ret;
1019 int i;
1020
1021 if (gr->has_egl_buffer_age) {
1022 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1023 EGL_BUFFER_AGE_EXT, &buffer_age);
1024 if (ret == EGL_FALSE) {
1025 weston_log("buffer age query failed.\n");
1026 gl_renderer_print_egl_error_state();
1027 }
1028 }
1029
Jason Ekstrande5512d42014-02-04 21:36:38 -06001030 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001031 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001032 *border_damage = BORDER_ALL_DIRTY;
1033 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001034 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001035 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001036
1037 if (*border_damage & BORDER_SIZE_CHANGED) {
1038 /* If we've had a resize, we have to do a full
1039 * repaint. */
1040 *border_damage |= BORDER_ALL_DIRTY;
1041 pixman_region32_copy(buffer_damage, &output->region);
1042 } else {
1043 for (i = 0; i < buffer_age - 1; i++)
1044 pixman_region32_union(buffer_damage,
1045 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001046 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001047 }
1048 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001049}
1050
1051static void
1052output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001053 pixman_region32_t *output_damage,
1054 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055{
1056 struct gl_output_state *go = get_output_state(output);
1057 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001058
1059 if (!gr->has_egl_buffer_age)
1060 return;
1061
Derek Foreman4c582662014-10-09 18:39:44 -05001062 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1063 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001064
Derek Foreman4c582662014-10-09 18:39:44 -05001065 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1066 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001067}
1068
Derek Foremanc4cfe852015-05-15 12:12:40 -05001069/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1070 * unavailable, so we're assuming the background has no transparency
1071 * and that everything with a blend, like drop shadows, will have something
1072 * opaque (like the background) drawn underneath it.
1073 *
1074 * Depending on the underlying hardware, violating that assumption could
1075 * result in seeing through to another display plane.
1076 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001077static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001078gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001079 pixman_region32_t *output_damage)
1080{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001081 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001082 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001083 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001084 EGLBoolean ret;
1085 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001086 int i, nrects, buffer_height;
1087 EGLint *egl_damage, *d;
1088 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001089 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001090 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001091
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001092 if (use_output(output) < 0)
1093 return;
1094
Jason Ekstrand00b84282013-10-27 22:24:59 -05001095 /* Calculate the viewport */
1096 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1097 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1098 output->current_mode->width,
1099 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001100
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001101 /* Calculate the global GL matrix */
1102 go->output_matrix = output->matrix;
1103 weston_matrix_translate(&go->output_matrix,
1104 -(output->current_mode->width / 2.0),
1105 -(output->current_mode->height / 2.0), 0);
1106 weston_matrix_scale(&go->output_matrix,
1107 2.0 / output->current_mode->width,
1108 -2.0 / output->current_mode->height, 1);
1109
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001110 /* if debugging, redraw everything outside the damage to clean up
1111 * debug lines from the previous draw on this buffer:
1112 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001113 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001114 pixman_region32_t undamaged;
1115 pixman_region32_init(&undamaged);
1116 pixman_region32_subtract(&undamaged, &output->region,
1117 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001118 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001120 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001121 pixman_region32_fini(&undamaged);
1122 }
1123
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001124 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001125 pixman_region32_init(&buffer_damage);
1126
Jason Ekstrande5512d42014-02-04 21:36:38 -06001127 output_get_damage(output, &buffer_damage, &border_damage);
1128 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001129
1130 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001131 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001134
1135 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001136 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001137
Jason Ekstrande5512d42014-02-04 21:36:38 -06001138 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001139
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001140 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001141 wl_signal_emit(&output->frame_signal, output);
1142
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001143 if (gr->swap_buffers_with_damage) {
1144 pixman_region32_init(&buffer_damage);
1145 weston_transformed_region(output->width, output->height,
1146 output->transform,
1147 output->current_scale,
1148 output_damage, &buffer_damage);
1149
1150 if (output_has_borders(output)) {
1151 pixman_region32_translate(&buffer_damage,
1152 go->borders[GL_RENDERER_BORDER_LEFT].width,
1153 go->borders[GL_RENDERER_BORDER_TOP].height);
1154 output_get_border_damage(output, go->border_status,
1155 &buffer_damage);
1156 }
1157
1158 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1159 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1160
1161 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1162 output->current_mode->height +
1163 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1164
1165 d = egl_damage;
1166 for (i = 0; i < nrects; ++i) {
1167 *d++ = rects[i].x1;
1168 *d++ = buffer_height - rects[i].y2;
1169 *d++ = rects[i].x2 - rects[i].x1;
1170 *d++ = rects[i].y2 - rects[i].y1;
1171 }
1172 ret = gr->swap_buffers_with_damage(gr->egl_display,
1173 go->egl_surface,
1174 egl_damage, nrects);
1175 free(egl_damage);
1176 pixman_region32_fini(&buffer_damage);
1177 } else {
1178 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1179 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001180
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001181 if (ret == EGL_FALSE && !errored) {
1182 errored = 1;
1183 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001184 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001185 }
1186
Jason Ekstrande5512d42014-02-04 21:36:38 -06001187 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001188}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001189
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001190static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001191gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001192 pixman_format_code_t format, void *pixels,
1193 uint32_t x, uint32_t y,
1194 uint32_t width, uint32_t height)
1195{
1196 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001197 struct gl_output_state *go = get_output_state(output);
1198
1199 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1200 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001201
1202 switch (format) {
1203 case PIXMAN_a8r8g8b8:
1204 gl_format = GL_BGRA_EXT;
1205 break;
1206 case PIXMAN_a8b8g8r8:
1207 gl_format = GL_RGBA;
1208 break;
1209 default:
1210 return -1;
1211 }
1212
1213 if (use_output(output) < 0)
1214 return -1;
1215
1216 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1217 glReadPixels(x, y, width, height, gl_format,
1218 GL_UNSIGNED_BYTE, pixels);
1219
1220 return 0;
1221}
1222
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001223static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001224gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001225{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001226 struct gl_renderer *gr = get_renderer(surface->compositor);
1227 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001228 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001230 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231 pixman_box32_t *rectangles;
1232 void *data;
1233 int i, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001234
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001235 pixman_region32_union(&gs->texture_damage,
1236 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001237
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001238 if (!buffer)
1239 return;
1240
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001241 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001242 * We still accumulate the damage in texture_damage, and
1243 * hold the reference to the buffer, in case the surface
1244 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001245 */
Derek Foreman97746792015-11-18 16:32:28 -06001246 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_for_each(view, &surface->views, surface_link) {
1248 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001249 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 break;
1251 }
1252 }
1253 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001254 return;
1255
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001256 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1257 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001258 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001259
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001260 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001261
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001262 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001263 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001264 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001265 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001266 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001267 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001268 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001269
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001270 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001271 }
1272
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001273 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001274 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001275
1276 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001277 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1278 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001279 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001280 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001281 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001282 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001283 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001284 goto done;
1285 }
1286
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001287 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001288 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001289 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001290 pixman_box32_t r;
1291
1292 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1293
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001294 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1295 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001296 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1297 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001298 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001299 }
Neil Robertse5051712013-11-13 15:44:06 +00001300 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001301
1302done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001303 pixman_region32_fini(&gs->texture_damage);
1304 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001305 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001306
1307 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001308}
1309
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001310static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001311ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001312{
1313 int i;
1314
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001315 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001316 return;
1317
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001318 for (i = gs->num_textures; i < num_textures; i++) {
1319 glGenTextures(1, &gs->textures[i]);
1320 glBindTexture(gs->target, gs->textures[i]);
1321 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001322 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001323 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001324 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1325 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001326 gs->num_textures = num_textures;
1327 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001328}
1329
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001330static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001331gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1332 struct wl_shm_buffer *shm_buffer)
1333{
1334 struct weston_compositor *ec = es->compositor;
1335 struct gl_renderer *gr = get_renderer(ec);
1336 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001337 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001338 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001339
1340 buffer->shm_buffer = shm_buffer;
1341 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1342 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1343
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001344 switch (wl_shm_buffer_get_format(shm_buffer)) {
1345 case WL_SHM_FORMAT_XRGB8888:
1346 gs->shader = &gr->texture_shader_rgbx;
1347 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001348 gl_format = GL_BGRA_EXT;
1349 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001350 break;
1351 case WL_SHM_FORMAT_ARGB8888:
1352 gs->shader = &gr->texture_shader_rgba;
1353 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001354 gl_format = GL_BGRA_EXT;
1355 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001356 break;
1357 case WL_SHM_FORMAT_RGB565:
1358 gs->shader = &gr->texture_shader_rgbx;
1359 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001360 gl_format = GL_RGB;
1361 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001362 break;
1363 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001364 weston_log("warning: unknown shm buffer format: %08x\n",
1365 wl_shm_buffer_get_format(shm_buffer));
1366 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001367 }
1368
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001369 /* Only allocate a texture if it doesn't match existing one.
1370 * If a switch from DRM allocated buffer to a SHM buffer is
1371 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001372 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001373 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001374 gl_format != gs->gl_format ||
1375 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001376 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001377 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001378 gs->height = buffer->height;
1379 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001380 gs->gl_format = gl_format;
1381 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001382 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001383 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001384 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001385
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001386 gs->surface = es;
1387
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001388 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001389 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001390}
1391
1392static void
1393gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1394 uint32_t format)
1395{
1396 struct weston_compositor *ec = es->compositor;
1397 struct gl_renderer *gr = get_renderer(ec);
1398 struct gl_surface_state *gs = get_surface_state(es);
1399 EGLint attribs[3];
1400 int i, num_planes;
1401
1402 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1403 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1404 EGL_WIDTH, &buffer->width);
1405 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1406 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001407 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1408 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001409
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001410 for (i = 0; i < gs->num_images; i++) {
1411 egl_image_unref(gs->images[i]);
1412 gs->images[i] = NULL;
1413 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001414 gs->num_images = 0;
1415 gs->target = GL_TEXTURE_2D;
1416 switch (format) {
1417 case EGL_TEXTURE_RGB:
1418 case EGL_TEXTURE_RGBA:
1419 default:
1420 num_planes = 1;
1421 gs->shader = &gr->texture_shader_rgba;
1422 break;
1423 case EGL_TEXTURE_EXTERNAL_WL:
1424 num_planes = 1;
1425 gs->target = GL_TEXTURE_EXTERNAL_OES;
1426 gs->shader = &gr->texture_shader_egl_external;
1427 break;
1428 case EGL_TEXTURE_Y_UV_WL:
1429 num_planes = 2;
1430 gs->shader = &gr->texture_shader_y_uv;
1431 break;
1432 case EGL_TEXTURE_Y_U_V_WL:
1433 num_planes = 3;
1434 gs->shader = &gr->texture_shader_y_u_v;
1435 break;
1436 case EGL_TEXTURE_Y_XUXV_WL:
1437 num_planes = 2;
1438 gs->shader = &gr->texture_shader_y_xuxv;
1439 break;
1440 }
1441
1442 ensure_textures(gs, num_planes);
1443 for (i = 0; i < num_planes; i++) {
1444 attribs[0] = EGL_WAYLAND_PLANE_WL;
1445 attribs[1] = i;
1446 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001447 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001448 EGL_WAYLAND_BUFFER_WL,
1449 buffer->legacy_buffer,
1450 attribs);
1451 if (!gs->images[i]) {
1452 weston_log("failed to create img for plane %d\n", i);
1453 continue;
1454 }
1455 gs->num_images++;
1456
1457 glActiveTexture(GL_TEXTURE0 + i);
1458 glBindTexture(gs->target, gs->textures[i]);
1459 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001460 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001461 }
1462
1463 gs->pitch = buffer->width;
1464 gs->height = buffer->height;
1465 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001466 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001467}
1468
1469static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001470gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1471{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001472 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001473
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001474 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001475}
1476
1477static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001478import_simple_dmabuf(struct gl_renderer *gr,
1479 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001480{
1481 struct egl_image *image;
1482 EGLint attribs[30];
1483 int atti = 0;
1484
Pekka Paalanena3525802014-06-12 16:49:29 +03001485 /* This requires the Mesa commit in
1486 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1487 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1488 * Otherwise Mesa closes the fd behind our back and re-importing
1489 * will fail.
1490 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1491 */
1492
1493 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001494 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001495 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001496 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001497 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001498 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001499 /* XXX: Add modifier here when supported */
1500
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001501 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001502 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001503 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001504 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001505 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001506 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001507 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001508 }
1509
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001510 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001511 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001512 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001513 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001514 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001515 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001516 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001517 }
1518
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001519 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001520 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001521 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001522 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001523 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001524 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001525 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001526 }
1527
1528 attribs[atti++] = EGL_NONE;
1529
1530 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1531 attribs);
1532
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001533 return image;
1534}
Pekka Paalanena3525802014-06-12 16:49:29 +03001535
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001536/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1537 * some of the definitions here so that building Weston won't require
1538 * bleeding-edge kernel headers.
1539 */
1540#ifndef DRM_FORMAT_R8
1541#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1542#endif
1543
1544#ifndef DRM_FORMAT_GR88
1545#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1546#endif
1547
1548struct yuv_format_descriptor yuv_formats[] = {
1549 {
1550 .format = DRM_FORMAT_YUYV,
1551 .input_planes = 1,
1552 .output_planes = 2,
1553 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1554 {{
1555 .width_divisor = 1,
1556 .height_divisor = 1,
1557 .format = DRM_FORMAT_GR88,
1558 .plane_index = 0
1559 }, {
1560 .width_divisor = 2,
1561 .height_divisor = 1,
1562 .format = DRM_FORMAT_ARGB8888,
1563 .plane_index = 0
1564 }}
1565 }, {
1566 .format = DRM_FORMAT_NV12,
1567 .input_planes = 2,
1568 .output_planes = 2,
1569 .texture_type = EGL_TEXTURE_Y_UV_WL,
1570 {{
1571 .width_divisor = 1,
1572 .height_divisor = 1,
1573 .format = DRM_FORMAT_R8,
1574 .plane_index = 0
1575 }, {
1576 .width_divisor = 2,
1577 .height_divisor = 2,
1578 .format = DRM_FORMAT_GR88,
1579 .plane_index = 1
1580 }}
1581 }, {
1582 .format = DRM_FORMAT_YUV420,
1583 .input_planes = 3,
1584 .output_planes = 3,
1585 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1586 {{
1587 .width_divisor = 1,
1588 .height_divisor = 1,
1589 .format = DRM_FORMAT_R8,
1590 .plane_index = 0
1591 }, {
1592 .width_divisor = 2,
1593 .height_divisor = 2,
1594 .format = DRM_FORMAT_R8,
1595 .plane_index = 1
1596 }, {
1597 .width_divisor = 2,
1598 .height_divisor = 2,
1599 .format = DRM_FORMAT_R8,
1600 .plane_index = 2
1601 }}
1602 }
1603};
1604
1605static struct egl_image *
1606import_dmabuf_single_plane(struct gl_renderer *gr,
1607 const struct dmabuf_attributes *attributes,
1608 struct yuv_plane_descriptor *descriptor)
1609{
1610 struct dmabuf_attributes plane;
1611 struct egl_image *image;
1612 char fmt[4];
1613
1614 plane.width = attributes->width / descriptor->width_divisor;
1615 plane.height = attributes->height / descriptor->height_divisor;
1616 plane.format = descriptor->format;
1617 plane.n_planes = 1;
1618 plane.fd[0] = attributes->fd[descriptor->plane_index];
1619 plane.offset[0] = attributes->offset[descriptor->plane_index];
1620 plane.stride[0] = attributes->stride[descriptor->plane_index];
1621 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1622
1623 image = import_simple_dmabuf(gr, &plane);
1624 if (!image) {
1625 weston_log("Failed to import plane %d as %.4s\n",
1626 descriptor->plane_index,
1627 dump_format(descriptor->format, fmt));
1628 return NULL;
1629 }
1630
1631 return image;
1632}
1633
1634static bool
1635import_yuv_dmabuf(struct gl_renderer *gr,
1636 struct dmabuf_image *image)
1637{
1638 unsigned i;
1639 int j;
1640 int ret;
1641 struct yuv_format_descriptor *format = NULL;
1642 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1643 char fmt[4];
1644
1645 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1646 if (yuv_formats[i].format == attributes->format) {
1647 format = &yuv_formats[i];
1648 break;
1649 }
1650 }
1651
1652 if (!format) {
1653 weston_log("Error during import, and no known conversion for format "
1654 "%.4s in the renderer",
1655 dump_format(attributes->format, fmt));
1656 return false;
1657 }
1658
1659 if (attributes->n_planes != format->input_planes) {
1660 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1661 dump_format(format->format, fmt),
1662 format->input_planes,
1663 (format->input_planes > 1) ? "s" : "",
1664 attributes->n_planes);
1665 return false;
1666 }
1667
1668 for (j = 0; j < format->output_planes; ++j) {
1669 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1670 &format->plane[j]);
1671 if (!image->images[j]) {
1672 while (j) {
1673 ret = egl_image_unref(image->images[--j]);
1674 assert(ret == 0);
1675 }
1676 return false;
1677 }
1678 }
1679
1680 image->num_images = format->output_planes;
1681
1682 switch (format->texture_type) {
1683 case EGL_TEXTURE_Y_XUXV_WL:
1684 image->shader = &gr->texture_shader_y_xuxv;
1685 break;
1686 case EGL_TEXTURE_Y_UV_WL:
1687 image->shader = &gr->texture_shader_y_uv;
1688 break;
1689 case EGL_TEXTURE_Y_U_V_WL:
1690 image->shader = &gr->texture_shader_y_u_v;
1691 break;
1692 default:
1693 assert(false);
1694 }
1695
1696 return true;
1697}
1698
1699static GLenum
1700choose_texture_target(struct dmabuf_attributes *attributes)
1701{
1702 if (attributes->n_planes > 1)
1703 return GL_TEXTURE_EXTERNAL_OES;
1704
1705 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1706 case DRM_FORMAT_YUYV:
1707 case DRM_FORMAT_YVYU:
1708 case DRM_FORMAT_UYVY:
1709 case DRM_FORMAT_VYUY:
1710 case DRM_FORMAT_AYUV:
1711 return GL_TEXTURE_EXTERNAL_OES;
1712 default:
1713 return GL_TEXTURE_2D;
1714 }
1715}
1716
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001717static struct dmabuf_image *
1718import_dmabuf(struct gl_renderer *gr,
1719 struct linux_dmabuf_buffer *dmabuf)
1720{
1721 struct egl_image *egl_image;
1722 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001723
1724 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001725 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001726
1727 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1728 if (egl_image) {
1729 image->num_images = 1;
1730 image->images[0] = egl_image;
1731 image->import_type = IMPORT_TYPE_DIRECT;
1732 image->target = choose_texture_target(&dmabuf->attributes);
1733
1734 switch (image->target) {
1735 case GL_TEXTURE_2D:
1736 image->shader = &gr->texture_shader_rgba;
1737 break;
1738 default:
1739 image->shader = &gr->texture_shader_egl_external;
1740 }
1741 } else {
1742 if (!import_yuv_dmabuf(gr, image)) {
1743 dmabuf_image_destroy(image);
1744 return NULL;
1745 }
1746 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1747 image->target = GL_TEXTURE_2D;
1748 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001749
1750 return image;
1751}
1752
1753static bool
1754gl_renderer_import_dmabuf(struct weston_compositor *ec,
1755 struct linux_dmabuf_buffer *dmabuf)
1756{
1757 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001758 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001759 int i;
1760
1761 assert(gr->has_dmabuf_import);
1762
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001763 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001764 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001765 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001766 return false;
1767 }
1768
1769 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001770 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001771 return false;
1772
1773 image = import_dmabuf(gr, dmabuf);
1774 if (!image)
1775 return false;
1776
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001777 wl_list_insert(&gr->dmabuf_images, &image->link);
1778 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1779 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001780
1781 return true;
1782}
1783
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001784static bool
1785import_known_dmabuf(struct gl_renderer *gr,
1786 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001787{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001788 switch (image->import_type) {
1789 case IMPORT_TYPE_DIRECT:
1790 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1791 if (!image->images[0])
1792 return false;
1793 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001794
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001795 case IMPORT_TYPE_GL_CONVERSION:
1796 if (!import_yuv_dmabuf(gr, image))
1797 return false;
1798 break;
1799
Pekka Paalanena3525802014-06-12 16:49:29 +03001800 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001801 weston_log("Invalid import type for dmabuf\n");
1802 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001803 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001804
1805 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001806}
1807
1808static void
1809gl_renderer_attach_dmabuf(struct weston_surface *surface,
1810 struct weston_buffer *buffer,
1811 struct linux_dmabuf_buffer *dmabuf)
1812{
1813 struct gl_renderer *gr = get_renderer(surface->compositor);
1814 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001815 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001816 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001817 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001818
1819 if (!gr->has_dmabuf_import) {
1820 linux_dmabuf_buffer_send_server_error(dmabuf,
1821 "EGL dmabuf import not supported");
1822 return;
1823 }
1824
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001825 buffer->width = dmabuf->attributes.width;
1826 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03001827
1828 /*
1829 * GL-renderer uses the OpenGL convention of texture coordinates, where
1830 * the origin is at bottom-left. Because dmabuf buffers have the origin
1831 * at top-left, we must invert the Y_INVERT flag to get the image right.
1832 */
Pekka Paalanena3525802014-06-12 16:49:29 +03001833 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03001834 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001835
1836 for (i = 0; i < gs->num_images; i++)
1837 egl_image_unref(gs->images[i]);
1838 gs->num_images = 0;
1839
Pekka Paalanena3525802014-06-12 16:49:29 +03001840 /*
1841 * We try to always hold an imported EGLImage from the dmabuf
1842 * to prevent the client from preventing re-imports. But, we also
1843 * need to re-import every time the contents may change because
1844 * GL driver's caching may need flushing.
1845 *
1846 * Here we release the cache reference which has to be final.
1847 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001848 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001849
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001850 /* The dmabuf_image should have been created during the import */
1851 assert(image != NULL);
1852
1853 for (i = 0; i < image->num_images; ++i) {
1854 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001855 assert(ret == 0);
1856 }
1857
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001858 if (!import_known_dmabuf(gr, image)) {
1859 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1860 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001861 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001862
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001863 gs->num_images = image->num_images;
1864 for (i = 0; i < gs->num_images; ++i)
1865 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001866
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001867 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001868 ensure_textures(gs, gs->num_images);
1869 for (i = 0; i < gs->num_images; ++i) {
1870 glActiveTexture(GL_TEXTURE0 + i);
1871 glBindTexture(gs->target, gs->textures[i]);
1872 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1873 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001874
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001875 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001876 gs->pitch = buffer->width;
1877 gs->height = buffer->height;
1878 gs->buffer_type = BUFFER_TYPE_EGL;
1879 gs->y_inverted = buffer->y_inverted;
1880}
1881
1882static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001883gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001884{
1885 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001886 struct gl_renderer *gr = get_renderer(ec);
1887 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001888 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001889 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001890 EGLint format;
1891 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001892
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001893 weston_buffer_reference(&gs->buffer_ref, buffer);
1894
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001895 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001896 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001897 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001898 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001899 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001900 gs->num_images = 0;
1901 glDeleteTextures(gs->num_textures, gs->textures);
1902 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001903 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001904 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001905 return;
1906 }
1907
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001908 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001909
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001910 if (shm_buffer)
1911 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001912 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001913 EGL_TEXTURE_FORMAT, &format))
1914 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001915 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1916 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001917 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001918 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001919 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001920 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001921 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001922 }
1923}
1924
Kristian Høgsberg42263852012-09-06 21:59:29 -04001925static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001926gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001927 float red, float green, float blue, float alpha)
1928{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001929 struct gl_surface_state *gs = get_surface_state(surface);
1930 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001931
1932 gs->color[0] = red;
1933 gs->color[1] = green;
1934 gs->color[2] = blue;
1935 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001936 gs->buffer_type = BUFFER_TYPE_SOLID;
1937 gs->pitch = 1;
1938 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001939
John Kåre Alsaker40684142012-11-13 19:10:25 +01001940 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001941}
1942
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001943static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001944gl_renderer_surface_get_content_size(struct weston_surface *surface,
1945 int *width, int *height)
1946{
1947 struct gl_surface_state *gs = get_surface_state(surface);
1948
1949 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1950 *width = 0;
1951 *height = 0;
1952 } else {
1953 *width = gs->pitch;
1954 *height = gs->height;
1955 }
1956}
1957
1958static uint32_t
1959pack_color(pixman_format_code_t format, float *c)
1960{
1961 uint8_t r = round(c[0] * 255.0f);
1962 uint8_t g = round(c[1] * 255.0f);
1963 uint8_t b = round(c[2] * 255.0f);
1964 uint8_t a = round(c[3] * 255.0f);
1965
1966 switch (format) {
1967 case PIXMAN_a8b8g8r8:
1968 return (a << 24) | (b << 16) | (g << 8) | r;
1969 default:
1970 assert(0);
1971 return 0;
1972 }
1973}
1974
1975static int
1976gl_renderer_surface_copy_content(struct weston_surface *surface,
1977 void *target, size_t size,
1978 int src_x, int src_y,
1979 int width, int height)
1980{
1981 static const GLfloat verts[4 * 2] = {
1982 0.0f, 0.0f,
1983 1.0f, 0.0f,
1984 1.0f, 1.0f,
1985 0.0f, 1.0f
1986 };
1987 static const GLfloat projmat_normal[16] = { /* transpose */
1988 2.0f, 0.0f, 0.0f, 0.0f,
1989 0.0f, 2.0f, 0.0f, 0.0f,
1990 0.0f, 0.0f, 1.0f, 0.0f,
1991 -1.0f, -1.0f, 0.0f, 1.0f
1992 };
1993 static const GLfloat projmat_yinvert[16] = { /* transpose */
1994 2.0f, 0.0f, 0.0f, 0.0f,
1995 0.0f, -2.0f, 0.0f, 0.0f,
1996 0.0f, 0.0f, 1.0f, 0.0f,
1997 -1.0f, 1.0f, 0.0f, 1.0f
1998 };
1999 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2000 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2001 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2002 struct gl_renderer *gr = get_renderer(surface->compositor);
2003 struct gl_surface_state *gs = get_surface_state(surface);
2004 int cw, ch;
2005 GLuint fbo;
2006 GLuint tex;
2007 GLenum status;
2008 const GLfloat *proj;
2009 int i;
2010
2011 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2012
2013 switch (gs->buffer_type) {
2014 case BUFFER_TYPE_NULL:
2015 return -1;
2016 case BUFFER_TYPE_SOLID:
2017 *(uint32_t *)target = pack_color(format, gs->color);
2018 return 0;
2019 case BUFFER_TYPE_SHM:
2020 gl_renderer_flush_damage(surface);
2021 /* fall through */
2022 case BUFFER_TYPE_EGL:
2023 break;
2024 }
2025
2026 glGenTextures(1, &tex);
2027 glBindTexture(GL_TEXTURE_2D, tex);
2028 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2029 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2030 glBindTexture(GL_TEXTURE_2D, 0);
2031
2032 glGenFramebuffers(1, &fbo);
2033 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2034 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2035 GL_TEXTURE_2D, tex, 0);
2036
2037 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2038 if (status != GL_FRAMEBUFFER_COMPLETE) {
2039 weston_log("%s: fbo error: %#x\n", __func__, status);
2040 glDeleteFramebuffers(1, &fbo);
2041 glDeleteTextures(1, &tex);
2042 return -1;
2043 }
2044
2045 glViewport(0, 0, cw, ch);
2046 glDisable(GL_BLEND);
2047 use_shader(gr, gs->shader);
2048 if (gs->y_inverted)
2049 proj = projmat_normal;
2050 else
2051 proj = projmat_yinvert;
2052
2053 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2054 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2055
2056 for (i = 0; i < gs->num_textures; i++) {
2057 glUniform1i(gs->shader->tex_uniforms[i], i);
2058
2059 glActiveTexture(GL_TEXTURE0 + i);
2060 glBindTexture(gs->target, gs->textures[i]);
2061 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2062 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2063 }
2064
2065 /* position: */
2066 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2067 glEnableVertexAttribArray(0);
2068
2069 /* texcoord: */
2070 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2071 glEnableVertexAttribArray(1);
2072
2073 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2074
2075 glDisableVertexAttribArray(1);
2076 glDisableVertexAttribArray(0);
2077
2078 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2079 glReadPixels(src_x, src_y, width, height, gl_format,
2080 GL_UNSIGNED_BYTE, target);
2081
2082 glDeleteFramebuffers(1, &fbo);
2083 glDeleteTextures(1, &tex);
2084
2085 return 0;
2086}
2087
2088static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002089surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002090{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002091 int i;
2092
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002093 wl_list_remove(&gs->surface_destroy_listener.link);
2094 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002095
2096 gs->surface->renderer_state = NULL;
2097
2098 glDeleteTextures(gs->num_textures, gs->textures);
2099
2100 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002101 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002102
2103 weston_buffer_reference(&gs->buffer_ref, NULL);
2104 pixman_region32_fini(&gs->texture_damage);
2105 free(gs);
2106}
2107
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002108static void
2109surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2110{
2111 struct gl_surface_state *gs;
2112 struct gl_renderer *gr;
2113
2114 gs = container_of(listener, struct gl_surface_state,
2115 surface_destroy_listener);
2116
2117 gr = get_renderer(gs->surface->compositor);
2118
2119 surface_state_destroy(gs, gr);
2120}
2121
2122static void
2123surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2124{
2125 struct gl_surface_state *gs;
2126 struct gl_renderer *gr;
2127
2128 gr = data;
2129
2130 gs = container_of(listener, struct gl_surface_state,
2131 renderer_destroy_listener);
2132
2133 surface_state_destroy(gs, gr);
2134}
2135
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002136static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002137gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002138{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002139 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002140 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002141
Bryce Harringtonde16d892014-11-20 22:21:57 -08002142 gs = zalloc(sizeof *gs);
2143 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002144 return -1;
2145
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002146 /* A buffer is never attached to solid color surfaces, yet
2147 * they still go through texcoord computations. Do not divide
2148 * by zero there.
2149 */
2150 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002151 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002152
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002153 gs->surface = surface;
2154
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002155 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002156 surface->renderer_state = gs;
2157
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002158 gs->surface_destroy_listener.notify =
2159 surface_state_handle_surface_destroy;
2160 wl_signal_add(&surface->destroy_signal,
2161 &gs->surface_destroy_listener);
2162
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002163 gs->renderer_destroy_listener.notify =
2164 surface_state_handle_renderer_destroy;
2165 wl_signal_add(&gr->destroy_signal,
2166 &gs->renderer_destroy_listener);
2167
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002168 if (surface->buffer_ref.buffer) {
2169 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2170 gl_renderer_flush_damage(surface);
2171 }
2172
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002173 return 0;
2174}
2175
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002176static const char vertex_shader[] =
2177 "uniform mat4 proj;\n"
2178 "attribute vec2 position;\n"
2179 "attribute vec2 texcoord;\n"
2180 "varying vec2 v_texcoord;\n"
2181 "void main()\n"
2182 "{\n"
2183 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2184 " v_texcoord = texcoord;\n"
2185 "}\n";
2186
2187/* Declare common fragment shader uniforms */
2188#define FRAGMENT_CONVERT_YUV \
2189 " y *= alpha;\n" \
2190 " u *= alpha;\n" \
2191 " v *= alpha;\n" \
2192 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2193 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2194 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2195 " gl_FragColor.a = alpha;\n"
2196
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002197static const char fragment_debug[] =
2198 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2199
2200static const char fragment_brace[] =
2201 "}\n";
2202
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002203static const char texture_fragment_shader_rgba[] =
2204 "precision mediump float;\n"
2205 "varying vec2 v_texcoord;\n"
2206 "uniform sampler2D tex;\n"
2207 "uniform float alpha;\n"
2208 "void main()\n"
2209 "{\n"
2210 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002211 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002212
2213static const char texture_fragment_shader_rgbx[] =
2214 "precision mediump float;\n"
2215 "varying vec2 v_texcoord;\n"
2216 "uniform sampler2D tex;\n"
2217 "uniform float alpha;\n"
2218 "void main()\n"
2219 "{\n"
2220 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2221 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002222 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002223
2224static const char texture_fragment_shader_egl_external[] =
2225 "#extension GL_OES_EGL_image_external : require\n"
2226 "precision mediump float;\n"
2227 "varying vec2 v_texcoord;\n"
2228 "uniform samplerExternalOES tex;\n"
2229 "uniform float alpha;\n"
2230 "void main()\n"
2231 "{\n"
2232 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002233 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002234
2235static const char texture_fragment_shader_y_uv[] =
2236 "precision mediump float;\n"
2237 "uniform sampler2D tex;\n"
2238 "uniform sampler2D tex1;\n"
2239 "varying vec2 v_texcoord;\n"
2240 "uniform float alpha;\n"
2241 "void main() {\n"
2242 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2243 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2244 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2245 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002246 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002247
2248static const char texture_fragment_shader_y_u_v[] =
2249 "precision mediump float;\n"
2250 "uniform sampler2D tex;\n"
2251 "uniform sampler2D tex1;\n"
2252 "uniform sampler2D tex2;\n"
2253 "varying vec2 v_texcoord;\n"
2254 "uniform float alpha;\n"
2255 "void main() {\n"
2256 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2257 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2258 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2259 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002260 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002261
2262static const char texture_fragment_shader_y_xuxv[] =
2263 "precision mediump float;\n"
2264 "uniform sampler2D tex;\n"
2265 "uniform sampler2D tex1;\n"
2266 "varying vec2 v_texcoord;\n"
2267 "uniform float alpha;\n"
2268 "void main() {\n"
2269 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2270 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2271 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2272 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002273 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002274
2275static const char solid_fragment_shader[] =
2276 "precision mediump float;\n"
2277 "uniform vec4 color;\n"
2278 "uniform float alpha;\n"
2279 "void main()\n"
2280 "{\n"
2281 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002282 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002283
2284static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002285compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002286{
2287 GLuint s;
2288 char msg[512];
2289 GLint status;
2290
2291 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002292 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002293 glCompileShader(s);
2294 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2295 if (!status) {
2296 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2297 weston_log("shader info: %s\n", msg);
2298 return GL_NONE;
2299 }
2300
2301 return s;
2302}
2303
2304static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002305shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002306 const char *vertex_source, const char *fragment_source)
2307{
2308 char msg[512];
2309 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002310 int count;
2311 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002312
2313 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002314 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2315
2316 if (renderer->fragment_shader_debug) {
2317 sources[0] = fragment_source;
2318 sources[1] = fragment_debug;
2319 sources[2] = fragment_brace;
2320 count = 3;
2321 } else {
2322 sources[0] = fragment_source;
2323 sources[1] = fragment_brace;
2324 count = 2;
2325 }
2326
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002327 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002328 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002329
2330 shader->program = glCreateProgram();
2331 glAttachShader(shader->program, shader->vertex_shader);
2332 glAttachShader(shader->program, shader->fragment_shader);
2333 glBindAttribLocation(shader->program, 0, "position");
2334 glBindAttribLocation(shader->program, 1, "texcoord");
2335
2336 glLinkProgram(shader->program);
2337 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2338 if (!status) {
2339 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2340 weston_log("link info: %s\n", msg);
2341 return -1;
2342 }
2343
2344 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2345 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2346 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2347 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2348 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2349 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2350
2351 return 0;
2352}
2353
2354static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002355shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002356{
2357 glDeleteShader(shader->vertex_shader);
2358 glDeleteShader(shader->fragment_shader);
2359 glDeleteProgram(shader->program);
2360
2361 shader->vertex_shader = 0;
2362 shader->fragment_shader = 0;
2363 shader->program = 0;
2364}
2365
2366static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002367log_extensions(const char *name, const char *extensions)
2368{
2369 const char *p, *end;
2370 int l;
2371 int len;
2372
2373 l = weston_log("%s:", name);
2374 p = extensions;
2375 while (*p) {
2376 end = strchrnul(p, ' ');
2377 len = end - p;
2378 if (l + len > 78)
2379 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2380 len, p);
2381 else
2382 l += weston_log_continue(" %.*s", len, p);
2383 for (p = end; isspace(*p); p++)
2384 ;
2385 }
2386 weston_log_continue("\n");
2387}
2388
2389static void
2390log_egl_gl_info(EGLDisplay egldpy)
2391{
2392 const char *str;
2393
2394 str = eglQueryString(egldpy, EGL_VERSION);
2395 weston_log("EGL version: %s\n", str ? str : "(null)");
2396
2397 str = eglQueryString(egldpy, EGL_VENDOR);
2398 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2399
2400 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2401 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2402
2403 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2404 log_extensions("EGL extensions", str ? str : "(null)");
2405
2406 str = (char *)glGetString(GL_VERSION);
2407 weston_log("GL version: %s\n", str ? str : "(null)");
2408
2409 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2410 weston_log("GLSL version: %s\n", str ? str : "(null)");
2411
2412 str = (char *)glGetString(GL_VENDOR);
2413 weston_log("GL vendor: %s\n", str ? str : "(null)");
2414
2415 str = (char *)glGetString(GL_RENDERER);
2416 weston_log("GL renderer: %s\n", str ? str : "(null)");
2417
2418 str = (char *)glGetString(GL_EXTENSIONS);
2419 log_extensions("GL extensions", str ? str : "(null)");
2420}
2421
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002422static void
2423log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2424{
2425 EGLint r, g, b, a;
2426
2427 weston_log("Chosen EGL config details:\n");
2428
2429 weston_log_continue(STAMP_SPACE "RGBA bits");
2430 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2431 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2432 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2433 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2434 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2435 else
2436 weston_log_continue(" unknown\n");
2437
2438 weston_log_continue(STAMP_SPACE "swap interval range");
2439 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2440 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2441 weston_log_continue(": %d - %d\n", a, b);
2442 else
2443 weston_log_continue(" unknown\n");
2444}
2445
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002446static int
Derek Foremane76f1852015-05-15 12:12:39 -05002447match_config_to_visual(EGLDisplay egl_display,
2448 EGLint visual_id,
2449 EGLConfig *configs,
2450 int count)
2451{
2452 int i;
2453
2454 for (i = 0; i < count; ++i) {
2455 EGLint id;
2456
2457 if (!eglGetConfigAttrib(egl_display,
2458 configs[i], EGL_NATIVE_VISUAL_ID,
2459 &id))
2460 continue;
2461
2462 if (id == visual_id)
2463 return i;
2464 }
2465
Derek Foremane76f1852015-05-15 12:12:39 -05002466 return -1;
2467}
2468
2469static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002470egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002471 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002472 EGLConfig *config_out)
2473{
2474 EGLint count = 0;
2475 EGLint matched = 0;
2476 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002477 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002478
Derek Foremana7e19912015-05-20 14:57:58 -05002479 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2480 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002481 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002482 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002483 configs = calloc(count, sizeof *configs);
2484 if (!configs)
2485 return -1;
2486
2487 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002488 count, &matched) || !matched) {
2489 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002490 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002491 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002492
Derek Foremane76f1852015-05-15 12:12:39 -05002493 if (!visual_id)
2494 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002495
Derek Foremane76f1852015-05-15 12:12:39 -05002496 for (i = 0; config_index == -1 && i < n_ids; i++)
2497 config_index = match_config_to_visual(gr->egl_display,
2498 visual_id[i],
2499 configs,
2500 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002501
Derek Foremane76f1852015-05-15 12:12:39 -05002502 if (config_index != -1)
2503 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002504
2505out:
2506 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002507 if (config_index == -1)
2508 return -1;
2509
Derek Foremana7e19912015-05-20 14:57:58 -05002510 if (i > 1)
2511 weston_log("Unable to use first choice EGL config with id"
2512 " 0x%x, succeeded with alternate id 0x%x.\n",
2513 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002514 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002515}
2516
John Kåre Alsaker44154502012-11-13 19:10:20 +01002517static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002518gl_renderer_output_set_border(struct weston_output *output,
2519 enum gl_renderer_border_side side,
2520 int32_t width, int32_t height,
2521 int32_t tex_width, unsigned char *data)
2522{
2523 struct gl_output_state *go = get_output_state(output);
2524
Jason Ekstrande5512d42014-02-04 21:36:38 -06002525 if (go->borders[side].width != width ||
2526 go->borders[side].height != height)
2527 /* In this case, we have to blow everything and do a full
2528 * repaint. */
2529 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2530
2531 if (data == NULL) {
2532 width = 0;
2533 height = 0;
2534 }
2535
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002536 go->borders[side].width = width;
2537 go->borders[side].height = height;
2538 go->borders[side].tex_width = tex_width;
2539 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002540 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002541}
2542
John Kåre Alsaker94659272012-11-13 19:10:18 +01002543static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002544gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002545
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002546static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002547gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002548 EGLNativeWindowType window_for_legacy,
2549 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002550 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002551 const EGLint *visual_id,
2552 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002553{
2554 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002555 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002556 struct gl_output_state *go;
2557 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002558 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002559
Derek Foremane76f1852015-05-15 12:12:39 -05002560 if (egl_choose_config(gr, attribs, visual_id,
2561 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002562 weston_log("failed to choose EGL config for output\n");
2563 return -1;
2564 }
2565
2566 if (egl_config != gr->egl_config &&
2567 !gr->has_configless_context) {
2568 weston_log("attempted to use a different EGL config for an "
2569 "output but EGL_MESA_configless_context is not "
2570 "supported\n");
2571 return -1;
2572 }
2573
Bryce Harringtonde16d892014-11-20 22:21:57 -08002574 go = zalloc(sizeof *go);
2575 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002576 return -1;
2577
Jonny Lamb671148f2015-03-20 15:26:52 +01002578 if (gr->create_platform_window) {
2579 go->egl_surface =
2580 gr->create_platform_window(gr->egl_display,
2581 egl_config,
2582 window_for_platform,
2583 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002584 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002585 go->egl_surface =
2586 eglCreateWindowSurface(gr->egl_display,
2587 egl_config,
2588 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002589 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002590
2591 if (go->egl_surface == EGL_NO_SURFACE) {
2592 weston_log("failed to create egl surface\n");
2593 free(go);
2594 return -1;
2595 }
2596
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002597 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002598 pixman_region32_init(&go->buffer_damage[i]);
2599
John Kåre Alsaker94659272012-11-13 19:10:18 +01002600 output->renderer_state = go;
2601
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002602 log_egl_config_info(gr->egl_display, egl_config);
2603
John Kåre Alsaker94659272012-11-13 19:10:18 +01002604 return 0;
2605}
2606
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002607static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002608gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002609{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002610 struct gl_renderer *gr = get_renderer(output->compositor);
2611 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002612 int i;
2613
2614 for (i = 0; i < 2; i++)
2615 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002616
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002617 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002618
2619 free(go);
2620}
2621
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002622static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002623gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002624{
2625 return get_output_state(output)->egl_surface;
2626}
2627
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002628static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002629gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002630{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002631 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002632 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002633
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002634 wl_signal_emit(&gr->destroy_signal, gr);
2635
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002636 if (gr->has_bind_display)
2637 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002638
2639 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2640 eglMakeCurrent(gr->egl_display,
2641 EGL_NO_SURFACE, EGL_NO_SURFACE,
2642 EGL_NO_CONTEXT);
2643
Pekka Paalanena3525802014-06-12 16:49:29 +03002644
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002645 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2646 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002647
Armin Krezović28d240f2016-06-23 11:59:35 +02002648 if (gr->dummy_surface != EGL_NO_SURFACE)
2649 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2650
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002651 eglTerminate(gr->egl_display);
2652 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002653
Armin Krezović36d699a2016-08-05 15:28:30 +02002654 wl_list_remove(&gr->output_destroy_listener.link);
2655
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002656 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002657 wl_array_release(&gr->vtxcnt);
2658
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002659 if (gr->fragment_binding)
2660 weston_binding_destroy(gr->fragment_binding);
2661 if (gr->fan_binding)
2662 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002663
Scott Moreau976a0502013-03-07 10:15:17 -07002664 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002665}
2666
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002667static void
2668renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2669{
2670 const char *extensions;
2671
2672 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2673 if (!extensions) {
2674 weston_log("Retrieving EGL client extension string failed.\n");
2675 return;
2676 }
2677
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002678 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002679 gr->create_platform_window =
2680 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2681 else
2682 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2683}
2684
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002685static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002686gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2687{
2688 struct gl_renderer *gr = get_renderer(ec);
2689 const char *extensions;
2690 EGLBoolean ret;
2691
2692 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2693 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2694 gr->bind_display =
2695 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2696 gr->unbind_display =
2697 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2698 gr->query_buffer =
2699 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2700
2701 extensions =
2702 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2703 if (!extensions) {
2704 weston_log("Retrieving EGL extension string failed.\n");
2705 return -1;
2706 }
2707
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002708 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002709 gr->has_bind_display = 1;
2710 if (gr->has_bind_display) {
2711 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2712 if (!ret)
2713 gr->has_bind_display = 0;
2714 }
2715
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002716 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002717 gr->has_egl_buffer_age = 1;
2718 else
2719 weston_log("warning: EGL_EXT_buffer_age not supported. "
2720 "Performance could be affected.\n");
2721
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002722 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002723 gr->swap_buffers_with_damage =
2724 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2725 else
2726 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2727 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002728
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002729 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002730 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002731
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002732 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002733 gr->has_surfaceless_context = 1;
2734
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002735 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002736 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002737
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002738 renderer_setup_egl_client_extensions(gr);
2739
Neil Robertsb7f85332014-03-07 18:05:49 +00002740 return 0;
2741}
2742
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002743static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002744 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2745 EGL_RED_SIZE, 1,
2746 EGL_GREEN_SIZE, 1,
2747 EGL_BLUE_SIZE, 1,
2748 EGL_ALPHA_SIZE, 0,
2749 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2750 EGL_NONE
2751};
2752
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002753static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002754 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2755 EGL_RED_SIZE, 1,
2756 EGL_GREEN_SIZE, 1,
2757 EGL_BLUE_SIZE, 1,
2758 EGL_ALPHA_SIZE, 1,
2759 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2760 EGL_NONE
2761};
2762
Armin Krezović28d240f2016-06-23 11:59:35 +02002763
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002764/** Checks whether a platform EGL client extension is supported
2765 *
2766 * \param ec The weston compositor
2767 * \param extension_suffix The EGL client extension suffix
2768 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2769 *
2770 * This function checks whether a specific platform_* extension is supported
2771 * by EGL.
2772 *
2773 * The extension suffix should be the suffix of the platform extension (that
2774 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2775 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2776 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2777 *
2778 * The return value is 1:
2779 * - if the supplied EGL client extension is supported.
2780 * The return value is 0:
2781 * - if the platform_base client extension isn't supported so will
2782 * fallback to eglGetDisplay and friends.
2783 * The return value is -1:
2784 * - if the supplied EGL client extension is not supported.
2785 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002786static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002787gl_renderer_supports(struct weston_compositor *ec,
2788 const char *extension_suffix)
2789{
2790 static const char *extensions = NULL;
2791 char s[64];
2792
2793 if (!extensions) {
2794 extensions = (const char *) eglQueryString(
2795 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2796
2797 if (!extensions)
2798 return 0;
2799
2800 log_extensions("EGL client extensions",
2801 extensions);
2802 }
2803
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002804 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002805 return 0;
2806
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002807 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002808 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002809 return 1;
2810
2811 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002812 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002813 return 1;
2814
2815 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002816 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002817 return 1;
2818
Pekka Paalanenf2824542015-04-08 17:02:21 +03002819 /* at this point we definitely have some platform extensions but
2820 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002821 * not supported. */
2822
2823 return -1;
2824}
2825
Jonny Lamb74eed312015-03-24 13:12:04 +01002826static const char *
2827platform_to_extension(EGLenum platform)
2828{
2829 switch (platform) {
2830 case EGL_PLATFORM_GBM_KHR:
2831 return "gbm";
2832 case EGL_PLATFORM_WAYLAND_KHR:
2833 return "wayland";
2834 case EGL_PLATFORM_X11_KHR:
2835 return "x11";
2836 default:
2837 assert(0 && "bad EGL platform enum");
2838 }
2839}
2840
Armin Krezović36d699a2016-08-05 15:28:30 +02002841static void
2842output_handle_destroy(struct wl_listener *listener, void *data)
2843{
2844 struct gl_renderer *gr;
2845 struct weston_output *output = data;
2846
2847 gr = container_of(listener, struct gl_renderer,
2848 output_destroy_listener);
2849
2850 if (wl_list_empty(&output->compositor->output_list))
2851 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
2852 gr->dummy_surface, gr->egl_context);
2853}
2854
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002855static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002856gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2857 EGLConfig pbuffer_config;
2858
2859 static const EGLint pbuffer_config_attribs[] = {
2860 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2861 EGL_RED_SIZE, 1,
2862 EGL_GREEN_SIZE, 1,
2863 EGL_BLUE_SIZE, 1,
2864 EGL_ALPHA_SIZE, 0,
2865 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2866 EGL_NONE
2867 };
2868
2869 static const EGLint pbuffer_attribs[] = {
2870 EGL_WIDTH, 10,
2871 EGL_HEIGHT, 10,
2872 EGL_NONE
2873 };
2874
2875 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2876 weston_log("failed to choose EGL config for PbufferSurface");
2877 return -1;
2878 }
2879
2880 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2881 pbuffer_config,
2882 pbuffer_attribs);
2883
2884 if (gr->dummy_surface == EGL_NO_SURFACE) {
2885 weston_log("failed to create PbufferSurface\n");
2886 return -1;
2887 }
2888
2889 return 0;
2890}
2891
2892static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002893gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2894 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002895 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002896{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002897 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002898 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002899 int supports = 0;
2900
2901 if (platform) {
2902 supports = gl_renderer_supports(
2903 ec, platform_to_extension(platform));
2904 if (supports < 0)
2905 return -1;
2906 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002907
Bryce Harringtonde16d892014-11-20 22:21:57 -08002908 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002909 if (gr == NULL)
2910 return -1;
2911
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002912 gr->base.read_pixels = gl_renderer_read_pixels;
2913 gr->base.repaint_output = gl_renderer_repaint_output;
2914 gr->base.flush_damage = gl_renderer_flush_damage;
2915 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002916 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002917 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002918 gr->base.surface_get_content_size =
2919 gl_renderer_surface_get_content_size;
2920 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002921 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002922
Jonny Lambf1ec5062015-03-24 13:12:05 +01002923 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002924 if (supports) {
2925 if (!get_platform_display) {
2926 get_platform_display = (void *) eglGetProcAddress(
2927 "eglGetPlatformDisplayEXT");
2928 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002929
Jonny Lamb74eed312015-03-24 13:12:04 +01002930 /* also wrap this in the supports check because
2931 * eglGetProcAddress can return non-NULL and still not
2932 * support the feature at runtime, so ensure the
2933 * appropriate extension checks have been done. */
2934 if (get_platform_display && platform) {
2935 gr->egl_display = get_platform_display(platform,
2936 native_window,
2937 NULL);
2938 }
2939 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002940
2941 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002942 weston_log("warning: either no EGL_EXT_platform_base "
2943 "support or specific platform support; "
2944 "falling back to eglGetDisplay.\n");
2945 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002946 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002947
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002948 if (gr->egl_display == EGL_NO_DISPLAY) {
2949 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002950 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002951 }
2952
2953 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2954 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002955 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002956 }
2957
Derek Foremane76f1852015-05-15 12:12:39 -05002958 if (egl_choose_config(gr, attribs, visual_id,
2959 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002960 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002961 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002962 }
2963
2964 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002965 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002966 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002967 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002968
Neil Robertsb7f85332014-03-07 18:05:49 +00002969 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002970 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002971
Pekka Paalanena3525802014-06-12 16:49:29 +03002972 wl_list_init(&gr->dmabuf_images);
2973 if (gr->has_dmabuf_import)
2974 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2975
Armin Krezović28d240f2016-06-23 11:59:35 +02002976 if (gr->has_surfaceless_context) {
2977 weston_log("EGL_KHR_surfaceless_context available\n");
2978 gr->dummy_surface = EGL_NO_SURFACE;
2979 } else {
2980 weston_log("EGL_KHR_surfaceless_context unavailable. "
2981 "Trying PbufferSurface\n");
2982
2983 if (gl_renderer_create_pbuffer_surface(gr) < 0)
2984 goto fail_with_error;
2985 }
2986
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002987 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2988
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002989 wl_signal_init(&gr->destroy_signal);
2990
Armin Krezović28d240f2016-06-23 11:59:35 +02002991 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
2992 if (gr->dummy_surface != EGL_NO_SURFACE)
2993 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2994 goto fail_with_error;
2995 }
2996
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002997 return 0;
2998
Derek Foreman066ca0c2015-06-11 12:14:45 -05002999fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003000 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003001fail_terminate:
3002 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003003fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003004 free(gr);
3005 return -1;
3006}
3007
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003008static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003009gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003010{
3011 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003012}
3013
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003014static int
3015compile_shaders(struct weston_compositor *ec)
3016{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003017 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003018
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003019 gr->texture_shader_rgba.vertex_source = vertex_shader;
3020 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3021
3022 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3023 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3024
3025 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3026 gr->texture_shader_egl_external.fragment_source =
3027 texture_fragment_shader_egl_external;
3028
3029 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3030 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3031
3032 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3033 gr->texture_shader_y_u_v.fragment_source =
3034 texture_fragment_shader_y_u_v;
3035
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003036 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003037 gr->texture_shader_y_xuxv.fragment_source =
3038 texture_fragment_shader_y_xuxv;
3039
3040 gr->solid_shader.vertex_source = vertex_shader;
3041 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003042
3043 return 0;
3044}
3045
3046static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003047fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3048 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003049{
3050 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003051 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003052 struct weston_output *output;
3053
John Kåre Alsaker40684142012-11-13 19:10:25 +01003054 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003055
John Kåre Alsaker40684142012-11-13 19:10:25 +01003056 shader_release(&gr->texture_shader_rgba);
3057 shader_release(&gr->texture_shader_rgbx);
3058 shader_release(&gr->texture_shader_egl_external);
3059 shader_release(&gr->texture_shader_y_uv);
3060 shader_release(&gr->texture_shader_y_u_v);
3061 shader_release(&gr->texture_shader_y_xuxv);
3062 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003063
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003064 /* Force use_shader() to call glUseProgram(), since we need to use
3065 * the recompiled version of the shader. */
3066 gr->current_shader = NULL;
3067
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003068 wl_list_for_each(output, &ec->output_list, link)
3069 weston_output_damage(output);
3070}
3071
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003072static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003073fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3074 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003075{
3076 struct weston_compositor *compositor = data;
3077 struct gl_renderer *gr = get_renderer(compositor);
3078
3079 gr->fan_debug = !gr->fan_debug;
3080 weston_compositor_damage_all(compositor);
3081}
3082
John Kåre Alsaker94659272012-11-13 19:10:18 +01003083static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003084gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003085{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003086 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003087 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003088 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003089 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003090
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003091 static const EGLint context_attribs[] = {
3092 EGL_CONTEXT_CLIENT_VERSION, 2,
3093 EGL_NONE
3094 };
3095
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003096 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3097 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003098 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003099 return -1;
3100 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003101
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003102 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003103
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003104 if (gr->has_configless_context)
3105 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003106
3107 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003108 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003109 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003110 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003111 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003112 return -1;
3113 }
3114
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003115 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3116 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003117 if (ret == EGL_FALSE) {
3118 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003119 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003120 return -1;
3121 }
3122
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003123 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003124
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003125 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003126 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003127
3128 extensions = (const char *) glGetString(GL_EXTENSIONS);
3129 if (!extensions) {
3130 weston_log("Retrieving GL extension string failed.\n");
3131 return -1;
3132 }
3133
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003134 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003135 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3136 return -1;
3137 }
3138
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003139 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003140 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003141 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003142 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003143
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003144 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003145 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003146
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003147 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003148 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003149
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003150 glActiveTexture(GL_TEXTURE0);
3151
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003152 if (compile_shaders(ec))
3153 return -1;
3154
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003155 gr->fragment_binding =
3156 weston_compositor_add_debug_binding(ec, KEY_S,
3157 fragment_debug_binding,
3158 ec);
3159 gr->fan_binding =
3160 weston_compositor_add_debug_binding(ec, KEY_F,
3161 fan_debug_repaint_binding,
3162 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003163
Armin Krezović36d699a2016-08-05 15:28:30 +02003164 gr->output_destroy_listener.notify = output_handle_destroy;
3165 wl_signal_add(&ec->output_destroyed_signal,
3166 &gr->output_destroy_listener);
3167
Pekka Paalanen035a0322012-10-24 09:43:06 +03003168 weston_log("GL ES 2 renderer features:\n");
3169 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003170 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003171 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003172 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003173 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003174 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003175
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003176
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003177 return 0;
3178}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003179
3180WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3181 .opaque_attribs = gl_renderer_opaque_attribs,
3182 .alpha_attribs = gl_renderer_alpha_attribs,
3183
3184 .create = gl_renderer_create,
3185 .display = gl_renderer_display,
3186 .output_create = gl_renderer_output_create,
3187 .output_destroy = gl_renderer_output_destroy,
3188 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003189 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003190 .print_egl_error_state = gl_renderer_print_egl_error_state
3191};