blob: b18649dbcd1a22cd2bd92871f5f3d10ae8972a54 [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 }}
Matthias Treydteaca3ffb2016-07-25 12:15:41 +02001602 }, {
1603 .format = DRM_FORMAT_YUV444,
1604 .input_planes = 3,
1605 .output_planes = 3,
1606 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1607 {{
1608 .width_divisor = 1,
1609 .height_divisor = 1,
1610 .format = DRM_FORMAT_R8,
1611 .plane_index = 0
1612 }, {
1613 .width_divisor = 1,
1614 .height_divisor = 1,
1615 .format = DRM_FORMAT_R8,
1616 .plane_index = 1
1617 }, {
1618 .width_divisor = 1,
1619 .height_divisor = 1,
1620 .format = DRM_FORMAT_R8,
1621 .plane_index = 2
1622 }}
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001623 }
1624};
1625
1626static struct egl_image *
1627import_dmabuf_single_plane(struct gl_renderer *gr,
1628 const struct dmabuf_attributes *attributes,
1629 struct yuv_plane_descriptor *descriptor)
1630{
1631 struct dmabuf_attributes plane;
1632 struct egl_image *image;
1633 char fmt[4];
1634
1635 plane.width = attributes->width / descriptor->width_divisor;
1636 plane.height = attributes->height / descriptor->height_divisor;
1637 plane.format = descriptor->format;
1638 plane.n_planes = 1;
1639 plane.fd[0] = attributes->fd[descriptor->plane_index];
1640 plane.offset[0] = attributes->offset[descriptor->plane_index];
1641 plane.stride[0] = attributes->stride[descriptor->plane_index];
1642 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1643
1644 image = import_simple_dmabuf(gr, &plane);
1645 if (!image) {
1646 weston_log("Failed to import plane %d as %.4s\n",
1647 descriptor->plane_index,
1648 dump_format(descriptor->format, fmt));
1649 return NULL;
1650 }
1651
1652 return image;
1653}
1654
1655static bool
1656import_yuv_dmabuf(struct gl_renderer *gr,
1657 struct dmabuf_image *image)
1658{
1659 unsigned i;
1660 int j;
1661 int ret;
1662 struct yuv_format_descriptor *format = NULL;
1663 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1664 char fmt[4];
1665
1666 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1667 if (yuv_formats[i].format == attributes->format) {
1668 format = &yuv_formats[i];
1669 break;
1670 }
1671 }
1672
1673 if (!format) {
1674 weston_log("Error during import, and no known conversion for format "
1675 "%.4s in the renderer",
1676 dump_format(attributes->format, fmt));
1677 return false;
1678 }
1679
1680 if (attributes->n_planes != format->input_planes) {
1681 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1682 dump_format(format->format, fmt),
1683 format->input_planes,
1684 (format->input_planes > 1) ? "s" : "",
1685 attributes->n_planes);
1686 return false;
1687 }
1688
1689 for (j = 0; j < format->output_planes; ++j) {
1690 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1691 &format->plane[j]);
1692 if (!image->images[j]) {
1693 while (j) {
1694 ret = egl_image_unref(image->images[--j]);
1695 assert(ret == 0);
1696 }
1697 return false;
1698 }
1699 }
1700
1701 image->num_images = format->output_planes;
1702
1703 switch (format->texture_type) {
1704 case EGL_TEXTURE_Y_XUXV_WL:
1705 image->shader = &gr->texture_shader_y_xuxv;
1706 break;
1707 case EGL_TEXTURE_Y_UV_WL:
1708 image->shader = &gr->texture_shader_y_uv;
1709 break;
1710 case EGL_TEXTURE_Y_U_V_WL:
1711 image->shader = &gr->texture_shader_y_u_v;
1712 break;
1713 default:
1714 assert(false);
1715 }
1716
1717 return true;
1718}
1719
1720static GLenum
1721choose_texture_target(struct dmabuf_attributes *attributes)
1722{
1723 if (attributes->n_planes > 1)
1724 return GL_TEXTURE_EXTERNAL_OES;
1725
1726 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1727 case DRM_FORMAT_YUYV:
1728 case DRM_FORMAT_YVYU:
1729 case DRM_FORMAT_UYVY:
1730 case DRM_FORMAT_VYUY:
1731 case DRM_FORMAT_AYUV:
1732 return GL_TEXTURE_EXTERNAL_OES;
1733 default:
1734 return GL_TEXTURE_2D;
1735 }
1736}
1737
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001738static struct dmabuf_image *
1739import_dmabuf(struct gl_renderer *gr,
1740 struct linux_dmabuf_buffer *dmabuf)
1741{
1742 struct egl_image *egl_image;
1743 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001744
1745 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001746 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001747
1748 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1749 if (egl_image) {
1750 image->num_images = 1;
1751 image->images[0] = egl_image;
1752 image->import_type = IMPORT_TYPE_DIRECT;
1753 image->target = choose_texture_target(&dmabuf->attributes);
1754
1755 switch (image->target) {
1756 case GL_TEXTURE_2D:
1757 image->shader = &gr->texture_shader_rgba;
1758 break;
1759 default:
1760 image->shader = &gr->texture_shader_egl_external;
1761 }
1762 } else {
1763 if (!import_yuv_dmabuf(gr, image)) {
1764 dmabuf_image_destroy(image);
1765 return NULL;
1766 }
1767 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1768 image->target = GL_TEXTURE_2D;
1769 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001770
1771 return image;
1772}
1773
1774static bool
1775gl_renderer_import_dmabuf(struct weston_compositor *ec,
1776 struct linux_dmabuf_buffer *dmabuf)
1777{
1778 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001779 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001780 int i;
1781
1782 assert(gr->has_dmabuf_import);
1783
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001784 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001785 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001786 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001787 return false;
1788 }
1789
1790 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001791 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001792 return false;
1793
1794 image = import_dmabuf(gr, dmabuf);
1795 if (!image)
1796 return false;
1797
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001798 wl_list_insert(&gr->dmabuf_images, &image->link);
1799 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1800 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001801
1802 return true;
1803}
1804
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001805static bool
1806import_known_dmabuf(struct gl_renderer *gr,
1807 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001808{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001809 switch (image->import_type) {
1810 case IMPORT_TYPE_DIRECT:
1811 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1812 if (!image->images[0])
1813 return false;
1814 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001815
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001816 case IMPORT_TYPE_GL_CONVERSION:
1817 if (!import_yuv_dmabuf(gr, image))
1818 return false;
1819 break;
1820
Pekka Paalanena3525802014-06-12 16:49:29 +03001821 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001822 weston_log("Invalid import type for dmabuf\n");
1823 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001824 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001825
1826 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001827}
1828
1829static void
1830gl_renderer_attach_dmabuf(struct weston_surface *surface,
1831 struct weston_buffer *buffer,
1832 struct linux_dmabuf_buffer *dmabuf)
1833{
1834 struct gl_renderer *gr = get_renderer(surface->compositor);
1835 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001836 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001837 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001838 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001839
1840 if (!gr->has_dmabuf_import) {
1841 linux_dmabuf_buffer_send_server_error(dmabuf,
1842 "EGL dmabuf import not supported");
1843 return;
1844 }
1845
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001846 buffer->width = dmabuf->attributes.width;
1847 buffer->height = dmabuf->attributes.height;
Pekka Paalanen319397e2016-07-04 16:25:16 +03001848
1849 /*
1850 * GL-renderer uses the OpenGL convention of texture coordinates, where
1851 * the origin is at bottom-left. Because dmabuf buffers have the origin
1852 * at top-left, we must invert the Y_INVERT flag to get the image right.
1853 */
Pekka Paalanena3525802014-06-12 16:49:29 +03001854 buffer->y_inverted =
Pekka Paalanen319397e2016-07-04 16:25:16 +03001855 !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001856
1857 for (i = 0; i < gs->num_images; i++)
1858 egl_image_unref(gs->images[i]);
1859 gs->num_images = 0;
1860
Pekka Paalanena3525802014-06-12 16:49:29 +03001861 /*
1862 * We try to always hold an imported EGLImage from the dmabuf
1863 * to prevent the client from preventing re-imports. But, we also
1864 * need to re-import every time the contents may change because
1865 * GL driver's caching may need flushing.
1866 *
1867 * Here we release the cache reference which has to be final.
1868 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001869 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001870
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001871 /* The dmabuf_image should have been created during the import */
1872 assert(image != NULL);
1873
1874 for (i = 0; i < image->num_images; ++i) {
1875 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001876 assert(ret == 0);
1877 }
1878
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001879 if (!import_known_dmabuf(gr, image)) {
1880 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1881 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001882 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001883
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001884 gs->num_images = image->num_images;
1885 for (i = 0; i < gs->num_images; ++i)
1886 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001887
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001888 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001889 ensure_textures(gs, gs->num_images);
1890 for (i = 0; i < gs->num_images; ++i) {
1891 glActiveTexture(GL_TEXTURE0 + i);
1892 glBindTexture(gs->target, gs->textures[i]);
1893 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1894 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001895
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001896 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001897 gs->pitch = buffer->width;
1898 gs->height = buffer->height;
1899 gs->buffer_type = BUFFER_TYPE_EGL;
1900 gs->y_inverted = buffer->y_inverted;
1901}
1902
1903static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001904gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001905{
1906 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001907 struct gl_renderer *gr = get_renderer(ec);
1908 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001909 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001910 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001911 EGLint format;
1912 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001913
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001914 weston_buffer_reference(&gs->buffer_ref, buffer);
1915
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001916 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001917 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001918 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001919 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001920 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001921 gs->num_images = 0;
1922 glDeleteTextures(gs->num_textures, gs->textures);
1923 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001924 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001925 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001926 return;
1927 }
1928
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001930
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001931 if (shm_buffer)
1932 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001933 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001934 EGL_TEXTURE_FORMAT, &format))
1935 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001936 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1937 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001938 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001939 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001940 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001941 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001942 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001943 }
1944}
1945
Kristian Høgsberg42263852012-09-06 21:59:29 -04001946static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001947gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001948 float red, float green, float blue, float alpha)
1949{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001950 struct gl_surface_state *gs = get_surface_state(surface);
1951 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001952
1953 gs->color[0] = red;
1954 gs->color[1] = green;
1955 gs->color[2] = blue;
1956 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001957 gs->buffer_type = BUFFER_TYPE_SOLID;
1958 gs->pitch = 1;
1959 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001960
John Kåre Alsaker40684142012-11-13 19:10:25 +01001961 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001962}
1963
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001964static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001965gl_renderer_surface_get_content_size(struct weston_surface *surface,
1966 int *width, int *height)
1967{
1968 struct gl_surface_state *gs = get_surface_state(surface);
1969
1970 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1971 *width = 0;
1972 *height = 0;
1973 } else {
1974 *width = gs->pitch;
1975 *height = gs->height;
1976 }
1977}
1978
1979static uint32_t
1980pack_color(pixman_format_code_t format, float *c)
1981{
1982 uint8_t r = round(c[0] * 255.0f);
1983 uint8_t g = round(c[1] * 255.0f);
1984 uint8_t b = round(c[2] * 255.0f);
1985 uint8_t a = round(c[3] * 255.0f);
1986
1987 switch (format) {
1988 case PIXMAN_a8b8g8r8:
1989 return (a << 24) | (b << 16) | (g << 8) | r;
1990 default:
1991 assert(0);
1992 return 0;
1993 }
1994}
1995
1996static int
1997gl_renderer_surface_copy_content(struct weston_surface *surface,
1998 void *target, size_t size,
1999 int src_x, int src_y,
2000 int width, int height)
2001{
2002 static const GLfloat verts[4 * 2] = {
2003 0.0f, 0.0f,
2004 1.0f, 0.0f,
2005 1.0f, 1.0f,
2006 0.0f, 1.0f
2007 };
2008 static const GLfloat projmat_normal[16] = { /* transpose */
2009 2.0f, 0.0f, 0.0f, 0.0f,
2010 0.0f, 2.0f, 0.0f, 0.0f,
2011 0.0f, 0.0f, 1.0f, 0.0f,
2012 -1.0f, -1.0f, 0.0f, 1.0f
2013 };
2014 static const GLfloat projmat_yinvert[16] = { /* transpose */
2015 2.0f, 0.0f, 0.0f, 0.0f,
2016 0.0f, -2.0f, 0.0f, 0.0f,
2017 0.0f, 0.0f, 1.0f, 0.0f,
2018 -1.0f, 1.0f, 0.0f, 1.0f
2019 };
2020 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
2021 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2022 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
2023 struct gl_renderer *gr = get_renderer(surface->compositor);
2024 struct gl_surface_state *gs = get_surface_state(surface);
2025 int cw, ch;
2026 GLuint fbo;
2027 GLuint tex;
2028 GLenum status;
2029 const GLfloat *proj;
2030 int i;
2031
2032 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2033
2034 switch (gs->buffer_type) {
2035 case BUFFER_TYPE_NULL:
2036 return -1;
2037 case BUFFER_TYPE_SOLID:
2038 *(uint32_t *)target = pack_color(format, gs->color);
2039 return 0;
2040 case BUFFER_TYPE_SHM:
2041 gl_renderer_flush_damage(surface);
2042 /* fall through */
2043 case BUFFER_TYPE_EGL:
2044 break;
2045 }
2046
2047 glGenTextures(1, &tex);
2048 glBindTexture(GL_TEXTURE_2D, tex);
2049 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2050 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2051 glBindTexture(GL_TEXTURE_2D, 0);
2052
2053 glGenFramebuffers(1, &fbo);
2054 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2055 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2056 GL_TEXTURE_2D, tex, 0);
2057
2058 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2059 if (status != GL_FRAMEBUFFER_COMPLETE) {
2060 weston_log("%s: fbo error: %#x\n", __func__, status);
2061 glDeleteFramebuffers(1, &fbo);
2062 glDeleteTextures(1, &tex);
2063 return -1;
2064 }
2065
2066 glViewport(0, 0, cw, ch);
2067 glDisable(GL_BLEND);
2068 use_shader(gr, gs->shader);
2069 if (gs->y_inverted)
2070 proj = projmat_normal;
2071 else
2072 proj = projmat_yinvert;
2073
2074 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2075 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2076
2077 for (i = 0; i < gs->num_textures; i++) {
2078 glUniform1i(gs->shader->tex_uniforms[i], i);
2079
2080 glActiveTexture(GL_TEXTURE0 + i);
2081 glBindTexture(gs->target, gs->textures[i]);
2082 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2083 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2084 }
2085
2086 /* position: */
2087 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2088 glEnableVertexAttribArray(0);
2089
2090 /* texcoord: */
2091 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2092 glEnableVertexAttribArray(1);
2093
2094 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2095
2096 glDisableVertexAttribArray(1);
2097 glDisableVertexAttribArray(0);
2098
2099 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2100 glReadPixels(src_x, src_y, width, height, gl_format,
2101 GL_UNSIGNED_BYTE, target);
2102
2103 glDeleteFramebuffers(1, &fbo);
2104 glDeleteTextures(1, &tex);
2105
2106 return 0;
2107}
2108
2109static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002110surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002111{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002112 int i;
2113
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002114 wl_list_remove(&gs->surface_destroy_listener.link);
2115 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002116
2117 gs->surface->renderer_state = NULL;
2118
2119 glDeleteTextures(gs->num_textures, gs->textures);
2120
2121 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002122 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002123
2124 weston_buffer_reference(&gs->buffer_ref, NULL);
2125 pixman_region32_fini(&gs->texture_damage);
2126 free(gs);
2127}
2128
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002129static void
2130surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2131{
2132 struct gl_surface_state *gs;
2133 struct gl_renderer *gr;
2134
2135 gs = container_of(listener, struct gl_surface_state,
2136 surface_destroy_listener);
2137
2138 gr = get_renderer(gs->surface->compositor);
2139
2140 surface_state_destroy(gs, gr);
2141}
2142
2143static void
2144surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2145{
2146 struct gl_surface_state *gs;
2147 struct gl_renderer *gr;
2148
2149 gr = data;
2150
2151 gs = container_of(listener, struct gl_surface_state,
2152 renderer_destroy_listener);
2153
2154 surface_state_destroy(gs, gr);
2155}
2156
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002157static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002158gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002159{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002160 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002161 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002162
Bryce Harringtonde16d892014-11-20 22:21:57 -08002163 gs = zalloc(sizeof *gs);
2164 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002165 return -1;
2166
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002167 /* A buffer is never attached to solid color surfaces, yet
2168 * they still go through texcoord computations. Do not divide
2169 * by zero there.
2170 */
2171 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002172 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002173
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002174 gs->surface = surface;
2175
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002176 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002177 surface->renderer_state = gs;
2178
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002179 gs->surface_destroy_listener.notify =
2180 surface_state_handle_surface_destroy;
2181 wl_signal_add(&surface->destroy_signal,
2182 &gs->surface_destroy_listener);
2183
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002184 gs->renderer_destroy_listener.notify =
2185 surface_state_handle_renderer_destroy;
2186 wl_signal_add(&gr->destroy_signal,
2187 &gs->renderer_destroy_listener);
2188
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002189 if (surface->buffer_ref.buffer) {
2190 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2191 gl_renderer_flush_damage(surface);
2192 }
2193
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002194 return 0;
2195}
2196
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002197static const char vertex_shader[] =
2198 "uniform mat4 proj;\n"
2199 "attribute vec2 position;\n"
2200 "attribute vec2 texcoord;\n"
2201 "varying vec2 v_texcoord;\n"
2202 "void main()\n"
2203 "{\n"
2204 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2205 " v_texcoord = texcoord;\n"
2206 "}\n";
2207
2208/* Declare common fragment shader uniforms */
2209#define FRAGMENT_CONVERT_YUV \
2210 " y *= alpha;\n" \
2211 " u *= alpha;\n" \
2212 " v *= alpha;\n" \
2213 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2214 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2215 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2216 " gl_FragColor.a = alpha;\n"
2217
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002218static const char fragment_debug[] =
2219 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2220
2221static const char fragment_brace[] =
2222 "}\n";
2223
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002224static const char texture_fragment_shader_rgba[] =
2225 "precision mediump float;\n"
2226 "varying vec2 v_texcoord;\n"
2227 "uniform sampler2D tex;\n"
2228 "uniform float alpha;\n"
2229 "void main()\n"
2230 "{\n"
2231 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002232 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002233
2234static const char texture_fragment_shader_rgbx[] =
2235 "precision mediump float;\n"
2236 "varying vec2 v_texcoord;\n"
2237 "uniform sampler2D tex;\n"
2238 "uniform float alpha;\n"
2239 "void main()\n"
2240 "{\n"
2241 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2242 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002243 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002244
2245static const char texture_fragment_shader_egl_external[] =
2246 "#extension GL_OES_EGL_image_external : require\n"
2247 "precision mediump float;\n"
2248 "varying vec2 v_texcoord;\n"
2249 "uniform samplerExternalOES tex;\n"
2250 "uniform float alpha;\n"
2251 "void main()\n"
2252 "{\n"
2253 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002254 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002255
2256static const char texture_fragment_shader_y_uv[] =
2257 "precision mediump float;\n"
2258 "uniform sampler2D tex;\n"
2259 "uniform sampler2D tex1;\n"
2260 "varying vec2 v_texcoord;\n"
2261 "uniform float alpha;\n"
2262 "void main() {\n"
2263 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2264 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2265 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2266 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002267 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002268
2269static const char texture_fragment_shader_y_u_v[] =
2270 "precision mediump float;\n"
2271 "uniform sampler2D tex;\n"
2272 "uniform sampler2D tex1;\n"
2273 "uniform sampler2D tex2;\n"
2274 "varying vec2 v_texcoord;\n"
2275 "uniform float alpha;\n"
2276 "void main() {\n"
2277 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2278 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2279 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2280 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002281 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002282
2283static const char texture_fragment_shader_y_xuxv[] =
2284 "precision mediump float;\n"
2285 "uniform sampler2D tex;\n"
2286 "uniform sampler2D tex1;\n"
2287 "varying vec2 v_texcoord;\n"
2288 "uniform float alpha;\n"
2289 "void main() {\n"
2290 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2291 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2292 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2293 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002294 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002295
2296static const char solid_fragment_shader[] =
2297 "precision mediump float;\n"
2298 "uniform vec4 color;\n"
2299 "uniform float alpha;\n"
2300 "void main()\n"
2301 "{\n"
2302 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002303 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002304
2305static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002306compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002307{
2308 GLuint s;
2309 char msg[512];
2310 GLint status;
2311
2312 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002313 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002314 glCompileShader(s);
2315 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2316 if (!status) {
2317 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2318 weston_log("shader info: %s\n", msg);
2319 return GL_NONE;
2320 }
2321
2322 return s;
2323}
2324
2325static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002326shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002327 const char *vertex_source, const char *fragment_source)
2328{
2329 char msg[512];
2330 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002331 int count;
2332 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002333
2334 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002335 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2336
2337 if (renderer->fragment_shader_debug) {
2338 sources[0] = fragment_source;
2339 sources[1] = fragment_debug;
2340 sources[2] = fragment_brace;
2341 count = 3;
2342 } else {
2343 sources[0] = fragment_source;
2344 sources[1] = fragment_brace;
2345 count = 2;
2346 }
2347
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002348 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002349 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002350
2351 shader->program = glCreateProgram();
2352 glAttachShader(shader->program, shader->vertex_shader);
2353 glAttachShader(shader->program, shader->fragment_shader);
2354 glBindAttribLocation(shader->program, 0, "position");
2355 glBindAttribLocation(shader->program, 1, "texcoord");
2356
2357 glLinkProgram(shader->program);
2358 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2359 if (!status) {
2360 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2361 weston_log("link info: %s\n", msg);
2362 return -1;
2363 }
2364
2365 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2366 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2367 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2368 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2369 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2370 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2371
2372 return 0;
2373}
2374
2375static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002376shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002377{
2378 glDeleteShader(shader->vertex_shader);
2379 glDeleteShader(shader->fragment_shader);
2380 glDeleteProgram(shader->program);
2381
2382 shader->vertex_shader = 0;
2383 shader->fragment_shader = 0;
2384 shader->program = 0;
2385}
2386
2387static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002388log_extensions(const char *name, const char *extensions)
2389{
2390 const char *p, *end;
2391 int l;
2392 int len;
2393
2394 l = weston_log("%s:", name);
2395 p = extensions;
2396 while (*p) {
2397 end = strchrnul(p, ' ');
2398 len = end - p;
2399 if (l + len > 78)
2400 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2401 len, p);
2402 else
2403 l += weston_log_continue(" %.*s", len, p);
2404 for (p = end; isspace(*p); p++)
2405 ;
2406 }
2407 weston_log_continue("\n");
2408}
2409
2410static void
2411log_egl_gl_info(EGLDisplay egldpy)
2412{
2413 const char *str;
2414
2415 str = eglQueryString(egldpy, EGL_VERSION);
2416 weston_log("EGL version: %s\n", str ? str : "(null)");
2417
2418 str = eglQueryString(egldpy, EGL_VENDOR);
2419 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2420
2421 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2422 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2423
2424 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2425 log_extensions("EGL extensions", str ? str : "(null)");
2426
2427 str = (char *)glGetString(GL_VERSION);
2428 weston_log("GL version: %s\n", str ? str : "(null)");
2429
2430 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2431 weston_log("GLSL version: %s\n", str ? str : "(null)");
2432
2433 str = (char *)glGetString(GL_VENDOR);
2434 weston_log("GL vendor: %s\n", str ? str : "(null)");
2435
2436 str = (char *)glGetString(GL_RENDERER);
2437 weston_log("GL renderer: %s\n", str ? str : "(null)");
2438
2439 str = (char *)glGetString(GL_EXTENSIONS);
2440 log_extensions("GL extensions", str ? str : "(null)");
2441}
2442
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002443static void
2444log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2445{
2446 EGLint r, g, b, a;
2447
2448 weston_log("Chosen EGL config details:\n");
2449
2450 weston_log_continue(STAMP_SPACE "RGBA bits");
2451 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2452 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2453 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2454 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2455 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2456 else
2457 weston_log_continue(" unknown\n");
2458
2459 weston_log_continue(STAMP_SPACE "swap interval range");
2460 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2461 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2462 weston_log_continue(": %d - %d\n", a, b);
2463 else
2464 weston_log_continue(" unknown\n");
2465}
2466
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002467static int
Derek Foremane76f1852015-05-15 12:12:39 -05002468match_config_to_visual(EGLDisplay egl_display,
2469 EGLint visual_id,
2470 EGLConfig *configs,
2471 int count)
2472{
2473 int i;
2474
2475 for (i = 0; i < count; ++i) {
2476 EGLint id;
2477
2478 if (!eglGetConfigAttrib(egl_display,
2479 configs[i], EGL_NATIVE_VISUAL_ID,
2480 &id))
2481 continue;
2482
2483 if (id == visual_id)
2484 return i;
2485 }
2486
Derek Foremane76f1852015-05-15 12:12:39 -05002487 return -1;
2488}
2489
2490static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002491egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002492 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002493 EGLConfig *config_out)
2494{
2495 EGLint count = 0;
2496 EGLint matched = 0;
2497 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002498 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002499
Derek Foremana7e19912015-05-20 14:57:58 -05002500 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2501 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002502 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002503 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002504 configs = calloc(count, sizeof *configs);
2505 if (!configs)
2506 return -1;
2507
2508 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002509 count, &matched) || !matched) {
2510 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002511 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002512 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002513
Derek Foremane76f1852015-05-15 12:12:39 -05002514 if (!visual_id)
2515 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002516
Derek Foremane76f1852015-05-15 12:12:39 -05002517 for (i = 0; config_index == -1 && i < n_ids; i++)
2518 config_index = match_config_to_visual(gr->egl_display,
2519 visual_id[i],
2520 configs,
2521 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002522
Derek Foremane76f1852015-05-15 12:12:39 -05002523 if (config_index != -1)
2524 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002525
2526out:
2527 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002528 if (config_index == -1)
2529 return -1;
2530
Derek Foremana7e19912015-05-20 14:57:58 -05002531 if (i > 1)
2532 weston_log("Unable to use first choice EGL config with id"
2533 " 0x%x, succeeded with alternate id 0x%x.\n",
2534 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002535 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002536}
2537
John Kåre Alsaker44154502012-11-13 19:10:20 +01002538static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002539gl_renderer_output_set_border(struct weston_output *output,
2540 enum gl_renderer_border_side side,
2541 int32_t width, int32_t height,
2542 int32_t tex_width, unsigned char *data)
2543{
2544 struct gl_output_state *go = get_output_state(output);
2545
Jason Ekstrande5512d42014-02-04 21:36:38 -06002546 if (go->borders[side].width != width ||
2547 go->borders[side].height != height)
2548 /* In this case, we have to blow everything and do a full
2549 * repaint. */
2550 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2551
2552 if (data == NULL) {
2553 width = 0;
2554 height = 0;
2555 }
2556
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002557 go->borders[side].width = width;
2558 go->borders[side].height = height;
2559 go->borders[side].tex_width = tex_width;
2560 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002561 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002562}
2563
John Kåre Alsaker94659272012-11-13 19:10:18 +01002564static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002565gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002566
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002567static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002568gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002569 EGLNativeWindowType window_for_legacy,
2570 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002571 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002572 const EGLint *visual_id,
2573 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002574{
2575 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002576 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002577 struct gl_output_state *go;
2578 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002579 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002580
Derek Foremane76f1852015-05-15 12:12:39 -05002581 if (egl_choose_config(gr, attribs, visual_id,
2582 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002583 weston_log("failed to choose EGL config for output\n");
2584 return -1;
2585 }
2586
2587 if (egl_config != gr->egl_config &&
2588 !gr->has_configless_context) {
2589 weston_log("attempted to use a different EGL config for an "
2590 "output but EGL_MESA_configless_context is not "
2591 "supported\n");
2592 return -1;
2593 }
2594
Bryce Harringtonde16d892014-11-20 22:21:57 -08002595 go = zalloc(sizeof *go);
2596 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002597 return -1;
2598
Jonny Lamb671148f2015-03-20 15:26:52 +01002599 if (gr->create_platform_window) {
2600 go->egl_surface =
2601 gr->create_platform_window(gr->egl_display,
2602 egl_config,
2603 window_for_platform,
2604 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002605 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002606 go->egl_surface =
2607 eglCreateWindowSurface(gr->egl_display,
2608 egl_config,
2609 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002610 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002611
2612 if (go->egl_surface == EGL_NO_SURFACE) {
2613 weston_log("failed to create egl surface\n");
2614 free(go);
2615 return -1;
2616 }
2617
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002618 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002619 pixman_region32_init(&go->buffer_damage[i]);
2620
John Kåre Alsaker94659272012-11-13 19:10:18 +01002621 output->renderer_state = go;
2622
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002623 log_egl_config_info(gr->egl_display, egl_config);
2624
John Kåre Alsaker94659272012-11-13 19:10:18 +01002625 return 0;
2626}
2627
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002628static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002629gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002630{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002631 struct gl_renderer *gr = get_renderer(output->compositor);
2632 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002633 int i;
2634
2635 for (i = 0; i < 2; i++)
2636 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002637
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002638 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002639
2640 free(go);
2641}
2642
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002643static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002644gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002645{
2646 return get_output_state(output)->egl_surface;
2647}
2648
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002649static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002650gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002651{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002652 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002653 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002654
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002655 wl_signal_emit(&gr->destroy_signal, gr);
2656
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002657 if (gr->has_bind_display)
2658 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002659
2660 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2661 eglMakeCurrent(gr->egl_display,
2662 EGL_NO_SURFACE, EGL_NO_SURFACE,
2663 EGL_NO_CONTEXT);
2664
Pekka Paalanena3525802014-06-12 16:49:29 +03002665
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002666 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2667 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002668
Armin Krezović28d240f2016-06-23 11:59:35 +02002669 if (gr->dummy_surface != EGL_NO_SURFACE)
2670 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2671
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002672 eglTerminate(gr->egl_display);
2673 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002674
Armin Krezović36d699a2016-08-05 15:28:30 +02002675 wl_list_remove(&gr->output_destroy_listener.link);
2676
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002677 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002678 wl_array_release(&gr->vtxcnt);
2679
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002680 if (gr->fragment_binding)
2681 weston_binding_destroy(gr->fragment_binding);
2682 if (gr->fan_binding)
2683 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002684
Scott Moreau976a0502013-03-07 10:15:17 -07002685 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002686}
2687
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002688static void
2689renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2690{
2691 const char *extensions;
2692
2693 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2694 if (!extensions) {
2695 weston_log("Retrieving EGL client extension string failed.\n");
2696 return;
2697 }
2698
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002699 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002700 gr->create_platform_window =
2701 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2702 else
2703 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2704}
2705
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002706static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002707gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2708{
2709 struct gl_renderer *gr = get_renderer(ec);
2710 const char *extensions;
2711 EGLBoolean ret;
2712
2713 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2714 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2715 gr->bind_display =
2716 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2717 gr->unbind_display =
2718 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2719 gr->query_buffer =
2720 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2721
2722 extensions =
2723 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2724 if (!extensions) {
2725 weston_log("Retrieving EGL extension string failed.\n");
2726 return -1;
2727 }
2728
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002729 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002730 gr->has_bind_display = 1;
2731 if (gr->has_bind_display) {
2732 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2733 if (!ret)
2734 gr->has_bind_display = 0;
2735 }
2736
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002737 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002738 gr->has_egl_buffer_age = 1;
2739 else
2740 weston_log("warning: EGL_EXT_buffer_age not supported. "
2741 "Performance could be affected.\n");
2742
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002743 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002744 gr->swap_buffers_with_damage =
2745 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2746 else
2747 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2748 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002749
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002750 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002751 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002752
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002753 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002754 gr->has_surfaceless_context = 1;
2755
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002756 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002757 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002758
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002759 renderer_setup_egl_client_extensions(gr);
2760
Neil Robertsb7f85332014-03-07 18:05:49 +00002761 return 0;
2762}
2763
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002764static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002765 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2766 EGL_RED_SIZE, 1,
2767 EGL_GREEN_SIZE, 1,
2768 EGL_BLUE_SIZE, 1,
2769 EGL_ALPHA_SIZE, 0,
2770 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2771 EGL_NONE
2772};
2773
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002774static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002775 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2776 EGL_RED_SIZE, 1,
2777 EGL_GREEN_SIZE, 1,
2778 EGL_BLUE_SIZE, 1,
2779 EGL_ALPHA_SIZE, 1,
2780 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2781 EGL_NONE
2782};
2783
Armin Krezović28d240f2016-06-23 11:59:35 +02002784
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002785/** Checks whether a platform EGL client extension is supported
2786 *
2787 * \param ec The weston compositor
2788 * \param extension_suffix The EGL client extension suffix
2789 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2790 *
2791 * This function checks whether a specific platform_* extension is supported
2792 * by EGL.
2793 *
2794 * The extension suffix should be the suffix of the platform extension (that
2795 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2796 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2797 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2798 *
2799 * The return value is 1:
2800 * - if the supplied EGL client extension is supported.
2801 * The return value is 0:
2802 * - if the platform_base client extension isn't supported so will
2803 * fallback to eglGetDisplay and friends.
2804 * The return value is -1:
2805 * - if the supplied EGL client extension is not supported.
2806 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002807static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002808gl_renderer_supports(struct weston_compositor *ec,
2809 const char *extension_suffix)
2810{
2811 static const char *extensions = NULL;
2812 char s[64];
2813
2814 if (!extensions) {
2815 extensions = (const char *) eglQueryString(
2816 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2817
2818 if (!extensions)
2819 return 0;
2820
2821 log_extensions("EGL client extensions",
2822 extensions);
2823 }
2824
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002825 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002826 return 0;
2827
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002828 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002829 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002830 return 1;
2831
2832 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002833 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002834 return 1;
2835
2836 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002837 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002838 return 1;
2839
Pekka Paalanenf2824542015-04-08 17:02:21 +03002840 /* at this point we definitely have some platform extensions but
2841 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002842 * not supported. */
2843
2844 return -1;
2845}
2846
Jonny Lamb74eed312015-03-24 13:12:04 +01002847static const char *
2848platform_to_extension(EGLenum platform)
2849{
2850 switch (platform) {
2851 case EGL_PLATFORM_GBM_KHR:
2852 return "gbm";
2853 case EGL_PLATFORM_WAYLAND_KHR:
2854 return "wayland";
2855 case EGL_PLATFORM_X11_KHR:
2856 return "x11";
2857 default:
2858 assert(0 && "bad EGL platform enum");
2859 }
2860}
2861
Armin Krezović36d699a2016-08-05 15:28:30 +02002862static void
2863output_handle_destroy(struct wl_listener *listener, void *data)
2864{
2865 struct gl_renderer *gr;
2866 struct weston_output *output = data;
2867
2868 gr = container_of(listener, struct gl_renderer,
2869 output_destroy_listener);
2870
2871 if (wl_list_empty(&output->compositor->output_list))
2872 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
2873 gr->dummy_surface, gr->egl_context);
2874}
2875
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002876static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002877gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2878 EGLConfig pbuffer_config;
2879
2880 static const EGLint pbuffer_config_attribs[] = {
2881 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2882 EGL_RED_SIZE, 1,
2883 EGL_GREEN_SIZE, 1,
2884 EGL_BLUE_SIZE, 1,
2885 EGL_ALPHA_SIZE, 0,
2886 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2887 EGL_NONE
2888 };
2889
2890 static const EGLint pbuffer_attribs[] = {
2891 EGL_WIDTH, 10,
2892 EGL_HEIGHT, 10,
2893 EGL_NONE
2894 };
2895
2896 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2897 weston_log("failed to choose EGL config for PbufferSurface");
2898 return -1;
2899 }
2900
2901 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2902 pbuffer_config,
2903 pbuffer_attribs);
2904
2905 if (gr->dummy_surface == EGL_NO_SURFACE) {
2906 weston_log("failed to create PbufferSurface\n");
2907 return -1;
2908 }
2909
2910 return 0;
2911}
2912
2913static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002914gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2915 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002916 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002917{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002918 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002919 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002920 int supports = 0;
2921
2922 if (platform) {
2923 supports = gl_renderer_supports(
2924 ec, platform_to_extension(platform));
2925 if (supports < 0)
2926 return -1;
2927 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002928
Bryce Harringtonde16d892014-11-20 22:21:57 -08002929 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002930 if (gr == NULL)
2931 return -1;
2932
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002933 gr->base.read_pixels = gl_renderer_read_pixels;
2934 gr->base.repaint_output = gl_renderer_repaint_output;
2935 gr->base.flush_damage = gl_renderer_flush_damage;
2936 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002937 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002938 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002939 gr->base.surface_get_content_size =
2940 gl_renderer_surface_get_content_size;
2941 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002942 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002943
Jonny Lambf1ec5062015-03-24 13:12:05 +01002944 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002945 if (supports) {
2946 if (!get_platform_display) {
2947 get_platform_display = (void *) eglGetProcAddress(
2948 "eglGetPlatformDisplayEXT");
2949 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002950
Jonny Lamb74eed312015-03-24 13:12:04 +01002951 /* also wrap this in the supports check because
2952 * eglGetProcAddress can return non-NULL and still not
2953 * support the feature at runtime, so ensure the
2954 * appropriate extension checks have been done. */
2955 if (get_platform_display && platform) {
2956 gr->egl_display = get_platform_display(platform,
2957 native_window,
2958 NULL);
2959 }
2960 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002961
2962 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002963 weston_log("warning: either no EGL_EXT_platform_base "
2964 "support or specific platform support; "
2965 "falling back to eglGetDisplay.\n");
2966 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002967 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002968
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002969 if (gr->egl_display == EGL_NO_DISPLAY) {
2970 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002971 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002972 }
2973
2974 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2975 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002976 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002977 }
2978
Derek Foremane76f1852015-05-15 12:12:39 -05002979 if (egl_choose_config(gr, attribs, visual_id,
2980 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002981 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002982 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002983 }
2984
2985 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002986 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002987 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002988 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002989
Neil Robertsb7f85332014-03-07 18:05:49 +00002990 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002991 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002992
Pekka Paalanena3525802014-06-12 16:49:29 +03002993 wl_list_init(&gr->dmabuf_images);
2994 if (gr->has_dmabuf_import)
2995 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2996
Armin Krezović28d240f2016-06-23 11:59:35 +02002997 if (gr->has_surfaceless_context) {
2998 weston_log("EGL_KHR_surfaceless_context available\n");
2999 gr->dummy_surface = EGL_NO_SURFACE;
3000 } else {
3001 weston_log("EGL_KHR_surfaceless_context unavailable. "
3002 "Trying PbufferSurface\n");
3003
3004 if (gl_renderer_create_pbuffer_surface(gr) < 0)
3005 goto fail_with_error;
3006 }
3007
Tomeu Vizoso12072b62013-08-06 20:05:55 +02003008 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
3009
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03003010 wl_signal_init(&gr->destroy_signal);
3011
Armin Krezović28d240f2016-06-23 11:59:35 +02003012 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
3013 if (gr->dummy_surface != EGL_NO_SURFACE)
3014 eglDestroySurface(gr->egl_display, gr->dummy_surface);
3015 goto fail_with_error;
3016 }
3017
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003018 return 0;
3019
Derek Foreman066ca0c2015-06-11 12:14:45 -05003020fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02003021 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03003022fail_terminate:
3023 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05003024fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003025 free(gr);
3026 return -1;
3027}
3028
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003029static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003030gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003031{
3032 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003033}
3034
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003035static int
3036compile_shaders(struct weston_compositor *ec)
3037{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003038 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003039
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003040 gr->texture_shader_rgba.vertex_source = vertex_shader;
3041 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3042
3043 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3044 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3045
3046 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3047 gr->texture_shader_egl_external.fragment_source =
3048 texture_fragment_shader_egl_external;
3049
3050 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3051 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3052
3053 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3054 gr->texture_shader_y_u_v.fragment_source =
3055 texture_fragment_shader_y_u_v;
3056
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003057 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003058 gr->texture_shader_y_xuxv.fragment_source =
3059 texture_fragment_shader_y_xuxv;
3060
3061 gr->solid_shader.vertex_source = vertex_shader;
3062 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003063
3064 return 0;
3065}
3066
3067static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003068fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3069 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003070{
3071 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003072 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003073 struct weston_output *output;
3074
John Kåre Alsaker40684142012-11-13 19:10:25 +01003075 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003076
John Kåre Alsaker40684142012-11-13 19:10:25 +01003077 shader_release(&gr->texture_shader_rgba);
3078 shader_release(&gr->texture_shader_rgbx);
3079 shader_release(&gr->texture_shader_egl_external);
3080 shader_release(&gr->texture_shader_y_uv);
3081 shader_release(&gr->texture_shader_y_u_v);
3082 shader_release(&gr->texture_shader_y_xuxv);
3083 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003084
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003085 /* Force use_shader() to call glUseProgram(), since we need to use
3086 * the recompiled version of the shader. */
3087 gr->current_shader = NULL;
3088
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003089 wl_list_for_each(output, &ec->output_list, link)
3090 weston_output_damage(output);
3091}
3092
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003093static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003094fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3095 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003096{
3097 struct weston_compositor *compositor = data;
3098 struct gl_renderer *gr = get_renderer(compositor);
3099
3100 gr->fan_debug = !gr->fan_debug;
3101 weston_compositor_damage_all(compositor);
3102}
3103
John Kåre Alsaker94659272012-11-13 19:10:18 +01003104static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003105gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003106{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003107 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003108 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003109 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003110 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003111
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003112 static const EGLint context_attribs[] = {
3113 EGL_CONTEXT_CLIENT_VERSION, 2,
3114 EGL_NONE
3115 };
3116
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003117 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3118 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003119 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003120 return -1;
3121 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003122
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003123 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003124
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003125 if (gr->has_configless_context)
3126 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003127
3128 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003129 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003130 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003131 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003132 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003133 return -1;
3134 }
3135
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003136 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3137 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003138 if (ret == EGL_FALSE) {
3139 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003140 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003141 return -1;
3142 }
3143
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003144 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003145
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003146 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003147 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003148
3149 extensions = (const char *) glGetString(GL_EXTENSIONS);
3150 if (!extensions) {
3151 weston_log("Retrieving GL extension string failed.\n");
3152 return -1;
3153 }
3154
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003155 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003156 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3157 return -1;
3158 }
3159
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003160 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003161 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003162 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003163 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003164
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003165 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003166 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003167
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003168 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003169 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003170
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003171 glActiveTexture(GL_TEXTURE0);
3172
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003173 if (compile_shaders(ec))
3174 return -1;
3175
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003176 gr->fragment_binding =
3177 weston_compositor_add_debug_binding(ec, KEY_S,
3178 fragment_debug_binding,
3179 ec);
3180 gr->fan_binding =
3181 weston_compositor_add_debug_binding(ec, KEY_F,
3182 fan_debug_repaint_binding,
3183 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003184
Armin Krezović36d699a2016-08-05 15:28:30 +02003185 gr->output_destroy_listener.notify = output_handle_destroy;
3186 wl_signal_add(&ec->output_destroyed_signal,
3187 &gr->output_destroy_listener);
3188
Pekka Paalanen035a0322012-10-24 09:43:06 +03003189 weston_log("GL ES 2 renderer features:\n");
3190 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003191 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003192 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003193 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003194 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003195 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003196
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003197
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003198 return 0;
3199}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003200
3201WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3202 .opaque_attribs = gl_renderer_opaque_attribs,
3203 .alpha_attribs = gl_renderer_alpha_attribs,
3204
3205 .create = gl_renderer_create,
3206 .display = gl_renderer_display,
3207 .output_create = gl_renderer_output_create,
3208 .output_destroy = gl_renderer_output_destroy,
3209 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003210 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003211 .print_egl_error_state = gl_renderer_print_egl_error_state
3212};