blob: d624453b0fe882e691e732c135a5172ce3c49698 [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{
458 bool merged;
459 pixman_box32_t *out, merge_rect;
460 int i, j, nout;
461
462 if (!nrects) {
463 *outrects = NULL;
464 return 0;
465 }
466
467 /* nrects is an upper bound - we're not too worried about
468 * allocating a little extra
469 */
470 out = malloc(sizeof(pixman_box32_t) * nrects);
471 out[0] = inrects[0];
472 nout = 1;
473 for (i = 1; i < nrects; i++) {
474 for (j = 0; j < nout; j++) {
475 merged = merge_down(&inrects[i], &out[j], &merge_rect);
476 if (merged) {
477 out[j] = merge_rect;
478 break;
479 }
480 }
481 if (!merged) {
482 out[nout] = inrects[i];
483 nout++;
484 }
485 }
486 *outrects = out;
487 return nout;
488}
489
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400490static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491texture_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400492 pixman_region32_t *surf_region)
493{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494 struct gl_surface_state *gs = get_surface_state(ev->surface);
495 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400496 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400497 GLfloat *v, inv_width, inv_height;
498 unsigned int *vtxcnt, nvtx = 0;
499 pixman_box32_t *rects, *surf_rects;
Derek Foremanf8180982014-10-16 16:37:02 -0500500 pixman_box32_t *raw_rects;
501 int i, j, k, nrects, nsurf, raw_nrects;
502 bool used_band_compression;
503 raw_rects = pixman_region32_rectangles(region, &raw_nrects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400504 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
505
Derek Foremanf8180982014-10-16 16:37:02 -0500506 if (raw_nrects < 4) {
507 used_band_compression = false;
508 nrects = raw_nrects;
509 rects = raw_rects;
510 } else {
511 nrects = compress_bands(raw_rects, raw_nrects, &rects);
512 used_band_compression = true;
513 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400514 /* worst case we can have 8 vertices per rect (ie. clipped into
515 * an octagon):
516 */
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400517 v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
518 vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400519
Pekka Paalanen68033ac2012-12-04 15:58:15 +0200520 inv_width = 1.0 / gs->pitch;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200521 inv_height = 1.0 / gs->height;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400522
523 for (i = 0; i < nrects; i++) {
524 pixman_box32_t *rect = &rects[i];
525 for (j = 0; j < nsurf; j++) {
526 pixman_box32_t *surf_rect = &surf_rects[j];
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200527 GLfloat sx, sy, bx, by;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400528 GLfloat ex[8], ey[8]; /* edge points in screen space */
529 int n;
530
531 /* The transformed surface, after clipping to the clip region,
532 * can have as many as eight sides, emitted as a triangle-fan.
533 * The first vertex in the triangle fan can be chosen arbitrarily,
534 * since the area is guaranteed to be convex.
535 *
536 * If a corner of the transformed surface falls outside of the
537 * clip region, instead of emitting one vertex for the corner
538 * of the surface, up to two are emitted for two corresponding
539 * intersection point(s) between the surface and the clip region.
540 *
541 * To do this, we first calculate the (up to eight) points that
542 * form the intersection of the clip rect and the transformed
543 * surface.
544 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500545 n = calculate_edges(ev, rect, surf_rect, ex, ey);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400546 if (n < 3)
547 continue;
548
549 /* emit edge points: */
550 for (k = 0; k < n; k++) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 weston_view_from_global_float(ev, ex[k], ey[k],
552 &sx, &sy);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400553 /* position: */
554 *(v++) = ex[k];
555 *(v++) = ey[k];
556 /* texcoord: */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_surface_to_buffer_float(ev->surface,
558 sx, sy,
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200559 &bx, &by);
560 *(v++) = bx * inv_width;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +0400561 if (gs->y_inverted) {
562 *(v++) = by * inv_height;
563 } else {
564 *(v++) = (gs->height - by) * inv_height;
565 }
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400566 }
567
568 vtxcnt[nvtx++] = n;
569 }
570 }
571
Derek Foremanf8180982014-10-16 16:37:02 -0500572 if (used_band_compression)
573 free(rects);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574 return nvtx;
575}
576
577static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500578triangle_fan_debug(struct weston_view *view, int first, int count)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400579{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500580 struct weston_compositor *compositor = view->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100581 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400582 int i;
583 GLushort *buffer;
584 GLushort *index;
585 int nelems;
586 static int color_idx = 0;
587 static const GLfloat color[][4] = {
588 { 1.0, 0.0, 0.0, 1.0 },
589 { 0.0, 1.0, 0.0, 1.0 },
590 { 0.0, 0.0, 1.0, 1.0 },
591 { 1.0, 1.0, 1.0, 1.0 },
592 };
593
594 nelems = (count - 1 + count - 2) * 2;
595
596 buffer = malloc(sizeof(GLushort) * nelems);
597 index = buffer;
598
599 for (i = 1; i < count; i++) {
600 *index++ = first;
601 *index++ = first + i;
602 }
603
604 for (i = 2; i < count; i++) {
605 *index++ = first + i - 1;
606 *index++ = first + i;
607 }
608
John Kåre Alsaker40684142012-11-13 19:10:25 +0100609 glUseProgram(gr->solid_shader.program);
610 glUniform4fv(gr->solid_shader.color_uniform, 1,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400611 color[color_idx++ % ARRAY_LENGTH(color)]);
612 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100613 glUseProgram(gr->current_shader->program);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400614 free(buffer);
615}
616
617static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618repaint_region(struct weston_view *ev, pixman_region32_t *region,
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400619 pixman_region32_t *surf_region)
620{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500621 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400622 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400623 GLfloat *v;
624 unsigned int *vtxcnt;
625 int i, first, nfans;
626
627 /* The final region to be painted is the intersection of
628 * 'region' and 'surf_region'. However, 'region' is in the global
629 * coordinates, and 'surf_region' is in the surface-local
630 * coordinates. texture_region() will iterate over all pairs of
631 * rectangles from both regions, compute the intersection
632 * polygon for each pair, and store it as a triangle fan if
Bryce Harringtonea8fb942016-01-13 18:48:56 -0800633 * it has a non-zero area (at least 3 vertices, actually).
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400634 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500635 nfans = texture_region(ev, region, surf_region);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400636
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400637 v = gr->vertices.data;
638 vtxcnt = gr->vtxcnt.data;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400639
640 /* position: */
641 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
642 glEnableVertexAttribArray(0);
643
644 /* texcoord: */
645 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
646 glEnableVertexAttribArray(1);
647
648 for (i = 0, first = 0; i < nfans; i++) {
649 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400650 if (gr->fan_debug)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500651 triangle_fan_debug(ev, first, vtxcnt[i]);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400652 first += vtxcnt[i];
653 }
654
655 glDisableVertexAttribArray(1);
656 glDisableVertexAttribArray(0);
657
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -0400658 gr->vertices.size = 0;
659 gr->vtxcnt.size = 0;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400660}
661
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100662static int
663use_output(struct weston_output *output)
664{
665 static int errored;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100666 struct gl_output_state *go = get_output_state(output);
667 struct gl_renderer *gr = get_renderer(output->compositor);
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100668 EGLBoolean ret;
669
670 ret = eglMakeCurrent(gr->egl_display, go->egl_surface,
671 go->egl_surface, gr->egl_context);
672
673 if (ret == EGL_FALSE) {
674 if (errored)
675 return -1;
676 errored = 1;
677 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +0200678 gl_renderer_print_egl_error_state();
John Kåre Alsakera95b2d62012-11-13 19:10:21 +0100679 return -1;
680 }
681
682 return 0;
683}
684
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300685static int
686shader_init(struct gl_shader *shader, struct gl_renderer *gr,
687 const char *vertex_source, const char *fragment_source);
688
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400689static void
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300690use_shader(struct gl_renderer *gr, struct gl_shader *shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400691{
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +0300692 if (!shader->program) {
693 int ret;
694
695 ret = shader_init(shader, gr,
696 shader->vertex_source,
697 shader->fragment_source);
698
699 if (ret < 0)
700 weston_log("warning: failed to compile shader\n");
701 }
702
John Kåre Alsaker40684142012-11-13 19:10:25 +0100703 if (gr->current_shader == shader)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400704 return;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400705 glUseProgram(shader->program);
John Kåre Alsaker40684142012-11-13 19:10:25 +0100706 gr->current_shader = shader;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400707}
708
709static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100710shader_uniforms(struct gl_shader *shader,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 struct weston_view *view,
712 struct weston_output *output)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400713{
714 int i;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500715 struct gl_surface_state *gs = get_surface_state(view->surface);
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500716 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400717
718 glUniformMatrix4fv(shader->proj_uniform,
Jason Ekstrandfb23df72014-10-16 10:55:21 -0500719 1, GL_FALSE, go->output_matrix.d);
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100720 glUniform4fv(shader->color_uniform, 1, gs->color);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 glUniform1f(shader->alpha_uniform, view->alpha);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400722
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100723 for (i = 0; i < gs->num_textures; i++)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400724 glUniform1i(shader->tex_uniforms[i], i);
725}
726
727static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728draw_view(struct weston_view *ev, struct weston_output *output,
729 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400730{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 struct weston_compositor *ec = ev->surface->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100732 struct gl_renderer *gr = get_renderer(ec);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 struct gl_surface_state *gs = get_surface_state(ev->surface);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400734 /* repaint bounding region in global coordinates: */
735 pixman_region32_t repaint;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200736 /* opaque region in surface coordinates: */
737 pixman_region32_t surface_opaque;
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400738 /* non-opaque region in surface coordinates: */
739 pixman_region32_t surface_blend;
740 GLint filter;
741 int i;
742
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200743 /* In case of a runtime switch of renderers, we may not have received
744 * an attach for this surface since the switch. In that case we don't
745 * have a valid buffer or a proper shader set up so skip rendering. */
746 if (!gs->shader)
747 return;
748
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400749 pixman_region32_init(&repaint);
750 pixman_region32_intersect(&repaint,
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200751 &ev->transform.boundingbox, damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500752 pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400753
754 if (!pixman_region32_not_empty(&repaint))
755 goto out;
756
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400757 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
758
Kristian Høgsberg8799d412013-05-07 10:50:09 -0400759 if (gr->fan_debug) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100760 use_shader(gr, &gr->solid_shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500761 shader_uniforms(&gr->solid_shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762 }
763
John Kåre Alsaker40684142012-11-13 19:10:25 +0100764 use_shader(gr, gs->shader);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500765 shader_uniforms(gs->shader, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400766
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 if (ev->transform.enabled || output->zoom.active ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200768 output->current_scale != ev->surface->buffer_viewport.buffer.scale)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400769 filter = GL_LINEAR;
770 else
771 filter = GL_NEAREST;
772
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100773 for (i = 0; i < gs->num_textures; i++) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 glActiveTexture(GL_TEXTURE0 + i);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +0100775 glBindTexture(gs->target, gs->textures[i]);
776 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter);
777 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400778 }
779
780 /* blended region is whole surface minus opaque region: */
781 pixman_region32_init_rect(&surface_blend, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600782 ev->surface->width, ev->surface->height);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200783 if (ev->geometry.scissor_enabled)
784 pixman_region32_intersect(&surface_blend, &surface_blend,
785 &ev->geometry.scissor);
786 pixman_region32_subtract(&surface_blend, &surface_blend,
787 &ev->surface->opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400788
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 /* XXX: Should we be using ev->transform.opaque here? */
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200790 pixman_region32_init(&surface_opaque);
791 if (ev->geometry.scissor_enabled)
792 pixman_region32_intersect(&surface_opaque,
793 &ev->surface->opaque,
794 &ev->geometry.scissor);
795 else
796 pixman_region32_copy(&surface_opaque, &ev->surface->opaque);
797
798 if (pixman_region32_not_empty(&surface_opaque)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100799 if (gs->shader == &gr->texture_shader_rgba) {
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400800 /* Special case for RGBA textures with possibly
801 * bad data in alpha channel: use the shader
802 * that forces texture alpha = 1.0.
803 * Xwayland surfaces need this.
804 */
John Kåre Alsaker40684142012-11-13 19:10:25 +0100805 use_shader(gr, &gr->texture_shader_rgbx);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 shader_uniforms(&gr->texture_shader_rgbx, ev, output);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400807 }
808
Jason Ekstranda7af7042013-10-12 22:38:11 -0500809 if (ev->alpha < 1.0)
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400810 glEnable(GL_BLEND);
811 else
812 glDisable(GL_BLEND);
813
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200814 repaint_region(ev, &repaint, &surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400815 }
816
817 if (pixman_region32_not_empty(&surface_blend)) {
John Kåre Alsaker40684142012-11-13 19:10:25 +0100818 use_shader(gr, gs->shader);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400819 glEnable(GL_BLEND);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500820 repaint_region(ev, &repaint, &surface_blend);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400821 }
822
823 pixman_region32_fini(&surface_blend);
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +0200824 pixman_region32_fini(&surface_opaque);
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400825
826out:
827 pixman_region32_fini(&repaint);
828}
829
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400830static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500831repaint_views(struct weston_output *output, pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400832{
833 struct weston_compositor *compositor = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500834 struct weston_view *view;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400835
Jason Ekstranda7af7042013-10-12 22:38:11 -0500836 wl_list_for_each_reverse(view, &compositor->view_list, link)
837 if (view->plane == &compositor->primary_plane)
838 draw_view(view, output, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400839}
840
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500841static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600842draw_output_border_texture(struct gl_output_state *go,
843 enum gl_renderer_border_side side,
844 int32_t x, int32_t y,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500845 int32_t width, int32_t height)
846{
Jason Ekstrande5512d42014-02-04 21:36:38 -0600847 struct gl_border_image *img = &go->borders[side];
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500848 static GLushort indices [] = { 0, 1, 3, 3, 1, 2 };
849
850 if (!img->data) {
851 if (img->tex) {
852 glDeleteTextures(1, &img->tex);
853 img->tex = 0;
854 }
855
856 return;
857 }
858
859 if (!img->tex) {
860 glGenTextures(1, &img->tex);
861 glBindTexture(GL_TEXTURE_2D, img->tex);
862
863 glTexParameteri(GL_TEXTURE_2D,
864 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
865 glTexParameteri(GL_TEXTURE_2D,
866 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
867 glTexParameteri(GL_TEXTURE_2D,
868 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
869 glTexParameteri(GL_TEXTURE_2D,
870 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
871 } else {
872 glBindTexture(GL_TEXTURE_2D, img->tex);
873 }
874
Jason Ekstrande5512d42014-02-04 21:36:38 -0600875 if (go->border_status & (1 << side)) {
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500876 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
877 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
878 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500879 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
880 img->tex_width, img->height, 0,
881 GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500882 }
883
884 GLfloat texcoord[] = {
885 0.0f, 0.0f,
886 (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f,
887 (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f,
888 0.0f, 1.0f,
889 };
890
891 GLfloat verts[] = {
892 x, y,
893 x + width, y,
894 x + width, y + height,
895 x, y + height
896 };
897
898 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
899 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
900 glEnableVertexAttribArray(0);
901 glEnableVertexAttribArray(1);
902
903 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
904
905 glDisableVertexAttribArray(1);
906 glDisableVertexAttribArray(0);
907}
908
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600909static int
910output_has_borders(struct weston_output *output)
911{
912 struct gl_output_state *go = get_output_state(output);
913
914 return go->borders[GL_RENDERER_BORDER_TOP].data ||
915 go->borders[GL_RENDERER_BORDER_RIGHT].data ||
916 go->borders[GL_RENDERER_BORDER_BOTTOM].data ||
917 go->borders[GL_RENDERER_BORDER_LEFT].data;
918}
919
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500920static void
Jason Ekstrande5512d42014-02-04 21:36:38 -0600921draw_output_borders(struct weston_output *output,
922 enum gl_border_status border_status)
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500923{
924 struct gl_output_state *go = get_output_state(output);
925 struct gl_renderer *gr = get_renderer(output->compositor);
926 struct gl_shader *shader = &gr->texture_shader_rgba;
Jason Ekstrand00b84282013-10-27 22:24:59 -0500927 struct gl_border_image *top, *bottom, *left, *right;
928 struct weston_matrix matrix;
929 int full_width, full_height;
930
Jason Ekstrande5512d42014-02-04 21:36:38 -0600931 if (border_status == BORDER_STATUS_CLEAN)
932 return; /* Clean. Nothing to do. */
933
Jason Ekstrand00b84282013-10-27 22:24:59 -0500934 top = &go->borders[GL_RENDERER_BORDER_TOP];
935 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
936 left = &go->borders[GL_RENDERER_BORDER_LEFT];
937 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
938
939 full_width = output->current_mode->width + left->width + right->width;
940 full_height = output->current_mode->height + top->height + bottom->height;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500941
942 glDisable(GL_BLEND);
943 use_shader(gr, shader);
944
Jason Ekstrand00b84282013-10-27 22:24:59 -0500945 glViewport(0, 0, full_width, full_height);
946
947 weston_matrix_init(&matrix);
948 weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0);
949 weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1);
950 glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500951
952 glUniform1i(shader->tex_uniforms[0], 0);
953 glUniform1f(shader->alpha_uniform, 1);
954 glActiveTexture(GL_TEXTURE0);
955
Jason Ekstrande5512d42014-02-04 21:36:38 -0600956 if (border_status & BORDER_TOP_DIRTY)
957 draw_output_border_texture(go, GL_RENDERER_BORDER_TOP,
958 0, 0,
959 full_width, top->height);
960 if (border_status & BORDER_LEFT_DIRTY)
961 draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT,
962 0, top->height,
963 left->width, output->current_mode->height);
964 if (border_status & BORDER_RIGHT_DIRTY)
965 draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT,
966 full_width - right->width, top->height,
967 right->width, output->current_mode->height);
968 if (border_status & BORDER_BOTTOM_DIRTY)
969 draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM,
970 0, full_height - bottom->height,
971 full_width, bottom->height);
Jason Ekstrand0b61bf42013-10-27 22:24:54 -0500972}
John Kåre Alsaker44154502012-11-13 19:10:20 +0100973
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400974static void
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -0600975output_get_border_damage(struct weston_output *output,
976 enum gl_border_status border_status,
977 pixman_region32_t *damage)
978{
979 struct gl_output_state *go = get_output_state(output);
980 struct gl_border_image *top, *bottom, *left, *right;
981 int full_width, full_height;
982
983 if (border_status == BORDER_STATUS_CLEAN)
984 return; /* Clean. Nothing to do. */
985
986 top = &go->borders[GL_RENDERER_BORDER_TOP];
987 bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM];
988 left = &go->borders[GL_RENDERER_BORDER_LEFT];
989 right = &go->borders[GL_RENDERER_BORDER_RIGHT];
990
991 full_width = output->current_mode->width + left->width + right->width;
992 full_height = output->current_mode->height + top->height + bottom->height;
993 if (border_status & BORDER_TOP_DIRTY)
994 pixman_region32_union_rect(damage, damage,
995 0, 0,
996 full_width, top->height);
997 if (border_status & BORDER_LEFT_DIRTY)
998 pixman_region32_union_rect(damage, damage,
999 0, top->height,
1000 left->width, output->current_mode->height);
1001 if (border_status & BORDER_RIGHT_DIRTY)
1002 pixman_region32_union_rect(damage, damage,
1003 full_width - right->width, top->height,
1004 right->width, output->current_mode->height);
1005 if (border_status & BORDER_BOTTOM_DIRTY)
1006 pixman_region32_union_rect(damage, damage,
1007 0, full_height - bottom->height,
1008 full_width, bottom->height);
1009}
1010
1011static void
Jason Ekstrande5512d42014-02-04 21:36:38 -06001012output_get_damage(struct weston_output *output,
1013 pixman_region32_t *buffer_damage, uint32_t *border_damage)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001014{
1015 struct gl_output_state *go = get_output_state(output);
1016 struct gl_renderer *gr = get_renderer(output->compositor);
1017 EGLint buffer_age = 0;
1018 EGLBoolean ret;
1019 int i;
1020
1021 if (gr->has_egl_buffer_age) {
1022 ret = eglQuerySurface(gr->egl_display, go->egl_surface,
1023 EGL_BUFFER_AGE_EXT, &buffer_age);
1024 if (ret == EGL_FALSE) {
1025 weston_log("buffer age query failed.\n");
1026 gl_renderer_print_egl_error_state();
1027 }
1028 }
1029
Jason Ekstrande5512d42014-02-04 21:36:38 -06001030 if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001031 pixman_region32_copy(buffer_damage, &output->region);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001032 *border_damage = BORDER_ALL_DIRTY;
1033 } else {
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001034 for (i = 0; i < buffer_age - 1; i++)
Derek Foreman4c582662014-10-09 18:39:44 -05001035 *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT];
Jason Ekstrande5512d42014-02-04 21:36:38 -06001036
1037 if (*border_damage & BORDER_SIZE_CHANGED) {
1038 /* If we've had a resize, we have to do a full
1039 * repaint. */
1040 *border_damage |= BORDER_ALL_DIRTY;
1041 pixman_region32_copy(buffer_damage, &output->region);
1042 } else {
1043 for (i = 0; i < buffer_age - 1; i++)
1044 pixman_region32_union(buffer_damage,
1045 buffer_damage,
Derek Foreman4c582662014-10-09 18:39:44 -05001046 &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001047 }
1048 }
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001049}
1050
1051static void
1052output_rotate_damage(struct weston_output *output,
Jason Ekstrande5512d42014-02-04 21:36:38 -06001053 pixman_region32_t *output_damage,
1054 enum gl_border_status border_status)
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001055{
1056 struct gl_output_state *go = get_output_state(output);
1057 struct gl_renderer *gr = get_renderer(output->compositor);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001058
1059 if (!gr->has_egl_buffer_age)
1060 return;
1061
Derek Foreman4c582662014-10-09 18:39:44 -05001062 go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1;
1063 go->buffer_damage_index %= BUFFER_DAMAGE_COUNT;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001064
Derek Foreman4c582662014-10-09 18:39:44 -05001065 pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage);
1066 go->border_damage[go->buffer_damage_index] = border_status;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001067}
1068
Derek Foremanc4cfe852015-05-15 12:12:40 -05001069/* NOTE: We now allow falling back to ARGB gl visuals when XRGB is
1070 * unavailable, so we're assuming the background has no transparency
1071 * and that everything with a blend, like drop shadows, will have something
1072 * opaque (like the background) drawn underneath it.
1073 *
1074 * Depending on the underlying hardware, violating that assumption could
1075 * result in seeing through to another display plane.
1076 */
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001077static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001078gl_renderer_repaint_output(struct weston_output *output,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001079 pixman_region32_t *output_damage)
1080{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001081 struct gl_output_state *go = get_output_state(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001082 struct weston_compositor *compositor = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001083 struct gl_renderer *gr = get_renderer(compositor);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001084 EGLBoolean ret;
1085 static int errored;
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001086 int i, nrects, buffer_height;
1087 EGLint *egl_damage, *d;
1088 pixman_box32_t *rects;
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001089 pixman_region32_t buffer_damage, total_damage;
Jason Ekstrande5512d42014-02-04 21:36:38 -06001090 enum gl_border_status border_damage = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001091
Jason Ekstrandae0c6e32014-10-16 10:55:20 -05001092 if (use_output(output) < 0)
1093 return;
1094
Jason Ekstrand00b84282013-10-27 22:24:59 -05001095 /* Calculate the viewport */
1096 glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width,
1097 go->borders[GL_RENDERER_BORDER_BOTTOM].height,
1098 output->current_mode->width,
1099 output->current_mode->height);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001100
Jason Ekstrandfb23df72014-10-16 10:55:21 -05001101 /* Calculate the global GL matrix */
1102 go->output_matrix = output->matrix;
1103 weston_matrix_translate(&go->output_matrix,
1104 -(output->current_mode->width / 2.0),
1105 -(output->current_mode->height / 2.0), 0);
1106 weston_matrix_scale(&go->output_matrix,
1107 2.0 / output->current_mode->width,
1108 -2.0 / output->current_mode->height, 1);
1109
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001110 /* if debugging, redraw everything outside the damage to clean up
1111 * debug lines from the previous draw on this buffer:
1112 */
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001113 if (gr->fan_debug) {
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001114 pixman_region32_t undamaged;
1115 pixman_region32_init(&undamaged);
1116 pixman_region32_subtract(&undamaged, &output->region,
1117 output_damage);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001118 gr->fan_debug = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 repaint_views(output, &undamaged);
Kristian Høgsberg8799d412013-05-07 10:50:09 -04001120 gr->fan_debug = 1;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001121 pixman_region32_fini(&undamaged);
1122 }
1123
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001124 pixman_region32_init(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001125 pixman_region32_init(&buffer_damage);
1126
Jason Ekstrande5512d42014-02-04 21:36:38 -06001127 output_get_damage(output, &buffer_damage, &border_damage);
1128 output_rotate_damage(output, output_damage, go->border_status);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001129
1130 pixman_region32_union(&total_damage, &buffer_damage, output_damage);
Jason Ekstrande5512d42014-02-04 21:36:38 -06001131 border_damage |= go->border_status;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 repaint_views(output, &total_damage);
Ander Conselvan de Oliveirab605c062013-03-05 17:30:28 +02001134
1135 pixman_region32_fini(&total_damage);
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02001136 pixman_region32_fini(&buffer_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001137
Jason Ekstrande5512d42014-02-04 21:36:38 -06001138 draw_output_borders(output, border_damage);
John Kåre Alsaker44154502012-11-13 19:10:20 +01001139
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02001140 pixman_region32_copy(&output->previous_damage, output_damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001141 wl_signal_emit(&output->frame_signal, output);
1142
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001143 if (gr->swap_buffers_with_damage) {
1144 pixman_region32_init(&buffer_damage);
1145 weston_transformed_region(output->width, output->height,
1146 output->transform,
1147 output->current_scale,
1148 output_damage, &buffer_damage);
1149
1150 if (output_has_borders(output)) {
1151 pixman_region32_translate(&buffer_damage,
1152 go->borders[GL_RENDERER_BORDER_LEFT].width,
1153 go->borders[GL_RENDERER_BORDER_TOP].height);
1154 output_get_border_damage(output, go->border_status,
1155 &buffer_damage);
1156 }
1157
1158 rects = pixman_region32_rectangles(&buffer_damage, &nrects);
1159 egl_damage = malloc(nrects * 4 * sizeof(EGLint));
1160
1161 buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height +
1162 output->current_mode->height +
1163 go->borders[GL_RENDERER_BORDER_BOTTOM].height;
1164
1165 d = egl_damage;
1166 for (i = 0; i < nrects; ++i) {
1167 *d++ = rects[i].x1;
1168 *d++ = buffer_height - rects[i].y2;
1169 *d++ = rects[i].x2 - rects[i].x1;
1170 *d++ = rects[i].y2 - rects[i].y1;
1171 }
1172 ret = gr->swap_buffers_with_damage(gr->egl_display,
1173 go->egl_surface,
1174 egl_damage, nrects);
1175 free(egl_damage);
1176 pixman_region32_fini(&buffer_damage);
1177 } else {
1178 ret = eglSwapBuffers(gr->egl_display, go->egl_surface);
1179 }
Jason Ekstrand8e96f9e2014-02-04 21:36:39 -06001180
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001181 if (ret == EGL_FALSE && !errored) {
1182 errored = 1;
1183 weston_log("Failed in eglSwapBuffers.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02001184 gl_renderer_print_egl_error_state();
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001185 }
1186
Jason Ekstrande5512d42014-02-04 21:36:38 -06001187 go->border_status = BORDER_STATUS_CLEAN;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001188}
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04001189
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001190static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001191gl_renderer_read_pixels(struct weston_output *output,
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001192 pixman_format_code_t format, void *pixels,
1193 uint32_t x, uint32_t y,
1194 uint32_t width, uint32_t height)
1195{
1196 GLenum gl_format;
Jason Ekstrand701f6362014-04-02 19:53:59 -05001197 struct gl_output_state *go = get_output_state(output);
1198
1199 x += go->borders[GL_RENDERER_BORDER_LEFT].width;
1200 y += go->borders[GL_RENDERER_BORDER_BOTTOM].height;
John Kåre Alsakera95b2d62012-11-13 19:10:21 +01001201
1202 switch (format) {
1203 case PIXMAN_a8r8g8b8:
1204 gl_format = GL_BGRA_EXT;
1205 break;
1206 case PIXMAN_a8b8g8r8:
1207 gl_format = GL_RGBA;
1208 break;
1209 default:
1210 return -1;
1211 }
1212
1213 if (use_output(output) < 0)
1214 return -1;
1215
1216 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1217 glReadPixels(x, y, width, height, gl_format,
1218 GL_UNSIGNED_BYTE, pixels);
1219
1220 return 0;
1221}
1222
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001223static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001224gl_renderer_flush_damage(struct weston_surface *surface)
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001225{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001226 struct gl_renderer *gr = get_renderer(surface->compositor);
1227 struct gl_surface_state *gs = get_surface_state(surface);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001228 struct weston_buffer *buffer = gs->buffer_ref.buffer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 struct weston_view *view;
Derek Foreman97746792015-11-18 16:32:28 -06001230 bool texture_used;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001231 pixman_box32_t *rectangles;
1232 void *data;
1233 int i, n;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001234
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001235 pixman_region32_union(&gs->texture_damage,
1236 &gs->texture_damage, &surface->damage);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001237
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001238 if (!buffer)
1239 return;
1240
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001241 /* Avoid upload, if the texture won't be used this time.
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001242 * We still accumulate the damage in texture_damage, and
1243 * hold the reference to the buffer, in case the surface
1244 * migrates back to the primary plane.
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001245 */
Derek Foreman97746792015-11-18 16:32:28 -06001246 texture_used = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_for_each(view, &surface->views, surface_link) {
1248 if (view->plane == &surface->compositor->primary_plane) {
Derek Foreman97746792015-11-18 16:32:28 -06001249 texture_used = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 break;
1251 }
1252 }
1253 if (!texture_used)
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001254 return;
1255
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001256 if (!pixman_region32_not_empty(&gs->texture_damage) &&
1257 !gs->needs_full_upload)
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001258 goto done;
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001259
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001260 glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001261
John Kåre Alsaker320711d2012-11-13 19:10:27 +01001262 if (!gr->has_unpack_subimage) {
Neil Robertse5051712013-11-13 15:44:06 +00001263 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001264 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Pekka Paalanen68033ac2012-12-04 15:58:15 +02001265 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001266 gs->gl_format, gs->gl_pixel_type,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001267 wl_shm_buffer_get_data(buffer->shm_buffer));
Neil Robertse5051712013-11-13 15:44:06 +00001268 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001269
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001270 goto done;
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001271 }
1272
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001273 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001274 data = wl_shm_buffer_get_data(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001275
1276 if (gs->needs_full_upload) {
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001277 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
1278 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
Neil Robertse5051712013-11-13 15:44:06 +00001279 wl_shm_buffer_begin_access(buffer->shm_buffer);
Neil Roberts4d085e72014-04-07 15:01:01 +01001280 glTexImage2D(GL_TEXTURE_2D, 0, gs->gl_format,
Neil Roberts39a443f2014-04-04 16:24:54 +01001281 gs->pitch, buffer->height, 0,
Neil Roberts4d085e72014-04-07 15:01:01 +01001282 gs->gl_format, gs->gl_pixel_type, data);
Neil Robertse5051712013-11-13 15:44:06 +00001283 wl_shm_buffer_end_access(buffer->shm_buffer);
Ander Conselvan de Oliveira6be5f432013-06-07 16:52:45 +03001284 goto done;
1285 }
1286
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001287 rectangles = pixman_region32_rectangles(&gs->texture_damage, &n);
Neil Robertse5051712013-11-13 15:44:06 +00001288 wl_shm_buffer_begin_access(buffer->shm_buffer);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001289 for (i = 0; i < n; i++) {
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001290 pixman_box32_t r;
1291
1292 r = weston_surface_to_buffer_rect(surface, rectangles[i]);
1293
Kristian Høgsbergce7a5d82013-08-07 09:55:07 -07001294 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1);
1295 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001296 glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
1297 r.x2 - r.x1, r.y2 - r.y1,
Neil Roberts4d085e72014-04-07 15:01:01 +01001298 gs->gl_format, gs->gl_pixel_type, data);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001299 }
Neil Robertse5051712013-11-13 15:44:06 +00001300 wl_shm_buffer_end_access(buffer->shm_buffer);
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001301
1302done:
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02001303 pixman_region32_fini(&gs->texture_damage);
1304 pixman_region32_init(&gs->texture_damage);
Derek Foreman4c11fe72015-11-18 16:32:27 -06001305 gs->needs_full_upload = false;
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001306
1307 weston_buffer_reference(&gs->buffer_ref, NULL);
Kristian Høgsbergb1fd2d62012-09-05 22:13:58 -04001308}
1309
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001310static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001311ensure_textures(struct gl_surface_state *gs, int num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001312{
1313 int i;
1314
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001315 if (num_textures <= gs->num_textures)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001316 return;
1317
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001318 for (i = gs->num_textures; i < num_textures; i++) {
1319 glGenTextures(1, &gs->textures[i]);
1320 glBindTexture(gs->target, gs->textures[i]);
1321 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001322 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001323 glTexParameteri(gs->target,
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001324 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1325 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001326 gs->num_textures = num_textures;
1327 glBindTexture(gs->target, 0);
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001328}
1329
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001330static void
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001331gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer,
1332 struct wl_shm_buffer *shm_buffer)
1333{
1334 struct weston_compositor *ec = es->compositor;
1335 struct gl_renderer *gr = get_renderer(ec);
1336 struct gl_surface_state *gs = get_surface_state(es);
Neil Roberts4d085e72014-04-07 15:01:01 +01001337 GLenum gl_format, gl_pixel_type;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001338 int pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001339
1340 buffer->shm_buffer = shm_buffer;
1341 buffer->width = wl_shm_buffer_get_width(shm_buffer);
1342 buffer->height = wl_shm_buffer_get_height(shm_buffer);
1343
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001344 switch (wl_shm_buffer_get_format(shm_buffer)) {
1345 case WL_SHM_FORMAT_XRGB8888:
1346 gs->shader = &gr->texture_shader_rgbx;
1347 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001348 gl_format = GL_BGRA_EXT;
1349 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001350 break;
1351 case WL_SHM_FORMAT_ARGB8888:
1352 gs->shader = &gr->texture_shader_rgba;
1353 pitch = wl_shm_buffer_get_stride(shm_buffer) / 4;
Neil Roberts4d085e72014-04-07 15:01:01 +01001354 gl_format = GL_BGRA_EXT;
1355 gl_pixel_type = GL_UNSIGNED_BYTE;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001356 break;
1357 case WL_SHM_FORMAT_RGB565:
1358 gs->shader = &gr->texture_shader_rgbx;
1359 pitch = wl_shm_buffer_get_stride(shm_buffer) / 2;
Neil Roberts4d085e72014-04-07 15:01:01 +01001360 gl_format = GL_RGB;
1361 gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001362 break;
1363 default:
Neil Roberts4d085e72014-04-07 15:01:01 +01001364 weston_log("warning: unknown shm buffer format: %08x\n",
1365 wl_shm_buffer_get_format(shm_buffer));
1366 return;
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001367 }
1368
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001369 /* Only allocate a texture if it doesn't match existing one.
1370 * If a switch from DRM allocated buffer to a SHM buffer is
1371 * happening, we need to allocate a new texture buffer. */
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001372 if (pitch != gs->pitch ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001373 buffer->height != gs->height ||
Neil Roberts4d085e72014-04-07 15:01:01 +01001374 gl_format != gs->gl_format ||
1375 gl_pixel_type != gs->gl_pixel_type ||
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001376 gs->buffer_type != BUFFER_TYPE_SHM) {
Tomeu Vizoso12072b62013-08-06 20:05:55 +02001377 gs->pitch = pitch;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001378 gs->height = buffer->height;
1379 gs->target = GL_TEXTURE_2D;
Neil Roberts4d085e72014-04-07 15:01:01 +01001380 gs->gl_format = gl_format;
1381 gs->gl_pixel_type = gl_pixel_type;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001382 gs->buffer_type = BUFFER_TYPE_SHM;
Derek Foreman4c11fe72015-11-18 16:32:27 -06001383 gs->needs_full_upload = true;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001384 gs->y_inverted = 1;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001385
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001386 gs->surface = es;
1387
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001388 ensure_textures(gs, 1);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001389 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001390}
1391
1392static void
1393gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer,
1394 uint32_t format)
1395{
1396 struct weston_compositor *ec = es->compositor;
1397 struct gl_renderer *gr = get_renderer(ec);
1398 struct gl_surface_state *gs = get_surface_state(es);
1399 EGLint attribs[3];
1400 int i, num_planes;
1401
1402 buffer->legacy_buffer = (struct wl_buffer *)buffer->resource;
1403 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1404 EGL_WIDTH, &buffer->width);
1405 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1406 EGL_HEIGHT, &buffer->height);
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001407 gr->query_buffer(gr->egl_display, buffer->legacy_buffer,
1408 EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001409
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001410 for (i = 0; i < gs->num_images; i++) {
1411 egl_image_unref(gs->images[i]);
1412 gs->images[i] = NULL;
1413 }
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001414 gs->num_images = 0;
1415 gs->target = GL_TEXTURE_2D;
1416 switch (format) {
1417 case EGL_TEXTURE_RGB:
1418 case EGL_TEXTURE_RGBA:
1419 default:
1420 num_planes = 1;
1421 gs->shader = &gr->texture_shader_rgba;
1422 break;
1423 case EGL_TEXTURE_EXTERNAL_WL:
1424 num_planes = 1;
1425 gs->target = GL_TEXTURE_EXTERNAL_OES;
1426 gs->shader = &gr->texture_shader_egl_external;
1427 break;
1428 case EGL_TEXTURE_Y_UV_WL:
1429 num_planes = 2;
1430 gs->shader = &gr->texture_shader_y_uv;
1431 break;
1432 case EGL_TEXTURE_Y_U_V_WL:
1433 num_planes = 3;
1434 gs->shader = &gr->texture_shader_y_u_v;
1435 break;
1436 case EGL_TEXTURE_Y_XUXV_WL:
1437 num_planes = 2;
1438 gs->shader = &gr->texture_shader_y_xuxv;
1439 break;
1440 }
1441
1442 ensure_textures(gs, num_planes);
1443 for (i = 0; i < num_planes; i++) {
1444 attribs[0] = EGL_WAYLAND_PLANE_WL;
1445 attribs[1] = i;
1446 attribs[2] = EGL_NONE;
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001447 gs->images[i] = egl_image_create(gr,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001448 EGL_WAYLAND_BUFFER_WL,
1449 buffer->legacy_buffer,
1450 attribs);
1451 if (!gs->images[i]) {
1452 weston_log("failed to create img for plane %d\n", i);
1453 continue;
1454 }
1455 gs->num_images++;
1456
1457 glActiveTexture(GL_TEXTURE0 + i);
1458 glBindTexture(gs->target, gs->textures[i]);
1459 gr->image_target_texture_2d(gs->target,
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001460 gs->images[i]->image);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001461 }
1462
1463 gs->pitch = buffer->width;
1464 gs->height = buffer->height;
1465 gs->buffer_type = BUFFER_TYPE_EGL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001466 gs->y_inverted = buffer->y_inverted;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001467}
1468
1469static void
Pekka Paalanena3525802014-06-12 16:49:29 +03001470gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf)
1471{
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001472 struct dmabuf_image *image = dmabuf->user_data;
Pekka Paalanena3525802014-06-12 16:49:29 +03001473
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001474 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03001475}
1476
1477static struct egl_image *
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001478import_simple_dmabuf(struct gl_renderer *gr,
1479 struct dmabuf_attributes *attributes)
Pekka Paalanena3525802014-06-12 16:49:29 +03001480{
1481 struct egl_image *image;
1482 EGLint attribs[30];
1483 int atti = 0;
1484
Pekka Paalanena3525802014-06-12 16:49:29 +03001485 /* This requires the Mesa commit in
1486 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
1487 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
1488 * Otherwise Mesa closes the fd behind our back and re-importing
1489 * will fail.
1490 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
1491 */
1492
1493 attribs[atti++] = EGL_WIDTH;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001494 attribs[atti++] = attributes->width;
Pekka Paalanena3525802014-06-12 16:49:29 +03001495 attribs[atti++] = EGL_HEIGHT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001496 attribs[atti++] = attributes->height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001497 attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001498 attribs[atti++] = attributes->format;
Pekka Paalanena3525802014-06-12 16:49:29 +03001499 /* XXX: Add modifier here when supported */
1500
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001501 if (attributes->n_planes > 0) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001502 attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001503 attribs[atti++] = attributes->fd[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001504 attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001505 attribs[atti++] = attributes->offset[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001506 attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001507 attribs[atti++] = attributes->stride[0];
Pekka Paalanena3525802014-06-12 16:49:29 +03001508 }
1509
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001510 if (attributes->n_planes > 1) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001511 attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001512 attribs[atti++] = attributes->fd[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001513 attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001514 attribs[atti++] = attributes->offset[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001515 attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001516 attribs[atti++] = attributes->stride[1];
Pekka Paalanena3525802014-06-12 16:49:29 +03001517 }
1518
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001519 if (attributes->n_planes > 2) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001520 attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001521 attribs[atti++] = attributes->fd[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001522 attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001523 attribs[atti++] = attributes->offset[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001524 attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001525 attribs[atti++] = attributes->stride[2];
Pekka Paalanena3525802014-06-12 16:49:29 +03001526 }
1527
1528 attribs[atti++] = EGL_NONE;
1529
1530 image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL,
1531 attribs);
1532
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001533 return image;
1534}
Pekka Paalanena3525802014-06-12 16:49:29 +03001535
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001536/* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
1537 * some of the definitions here so that building Weston won't require
1538 * bleeding-edge kernel headers.
1539 */
1540#ifndef DRM_FORMAT_R8
1541#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
1542#endif
1543
1544#ifndef DRM_FORMAT_GR88
1545#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
1546#endif
1547
1548struct yuv_format_descriptor yuv_formats[] = {
1549 {
1550 .format = DRM_FORMAT_YUYV,
1551 .input_planes = 1,
1552 .output_planes = 2,
1553 .texture_type = EGL_TEXTURE_Y_XUXV_WL,
1554 {{
1555 .width_divisor = 1,
1556 .height_divisor = 1,
1557 .format = DRM_FORMAT_GR88,
1558 .plane_index = 0
1559 }, {
1560 .width_divisor = 2,
1561 .height_divisor = 1,
1562 .format = DRM_FORMAT_ARGB8888,
1563 .plane_index = 0
1564 }}
1565 }, {
1566 .format = DRM_FORMAT_NV12,
1567 .input_planes = 2,
1568 .output_planes = 2,
1569 .texture_type = EGL_TEXTURE_Y_UV_WL,
1570 {{
1571 .width_divisor = 1,
1572 .height_divisor = 1,
1573 .format = DRM_FORMAT_R8,
1574 .plane_index = 0
1575 }, {
1576 .width_divisor = 2,
1577 .height_divisor = 2,
1578 .format = DRM_FORMAT_GR88,
1579 .plane_index = 1
1580 }}
1581 }, {
1582 .format = DRM_FORMAT_YUV420,
1583 .input_planes = 3,
1584 .output_planes = 3,
1585 .texture_type = EGL_TEXTURE_Y_U_V_WL,
1586 {{
1587 .width_divisor = 1,
1588 .height_divisor = 1,
1589 .format = DRM_FORMAT_R8,
1590 .plane_index = 0
1591 }, {
1592 .width_divisor = 2,
1593 .height_divisor = 2,
1594 .format = DRM_FORMAT_R8,
1595 .plane_index = 1
1596 }, {
1597 .width_divisor = 2,
1598 .height_divisor = 2,
1599 .format = DRM_FORMAT_R8,
1600 .plane_index = 2
1601 }}
1602 }
1603};
1604
1605static struct egl_image *
1606import_dmabuf_single_plane(struct gl_renderer *gr,
1607 const struct dmabuf_attributes *attributes,
1608 struct yuv_plane_descriptor *descriptor)
1609{
1610 struct dmabuf_attributes plane;
1611 struct egl_image *image;
1612 char fmt[4];
1613
1614 plane.width = attributes->width / descriptor->width_divisor;
1615 plane.height = attributes->height / descriptor->height_divisor;
1616 plane.format = descriptor->format;
1617 plane.n_planes = 1;
1618 plane.fd[0] = attributes->fd[descriptor->plane_index];
1619 plane.offset[0] = attributes->offset[descriptor->plane_index];
1620 plane.stride[0] = attributes->stride[descriptor->plane_index];
1621 plane.modifier[0] = attributes->modifier[descriptor->plane_index];
1622
1623 image = import_simple_dmabuf(gr, &plane);
1624 if (!image) {
1625 weston_log("Failed to import plane %d as %.4s\n",
1626 descriptor->plane_index,
1627 dump_format(descriptor->format, fmt));
1628 return NULL;
1629 }
1630
1631 return image;
1632}
1633
1634static bool
1635import_yuv_dmabuf(struct gl_renderer *gr,
1636 struct dmabuf_image *image)
1637{
1638 unsigned i;
1639 int j;
1640 int ret;
1641 struct yuv_format_descriptor *format = NULL;
1642 struct dmabuf_attributes *attributes = &image->dmabuf->attributes;
1643 char fmt[4];
1644
1645 for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) {
1646 if (yuv_formats[i].format == attributes->format) {
1647 format = &yuv_formats[i];
1648 break;
1649 }
1650 }
1651
1652 if (!format) {
1653 weston_log("Error during import, and no known conversion for format "
1654 "%.4s in the renderer",
1655 dump_format(attributes->format, fmt));
1656 return false;
1657 }
1658
1659 if (attributes->n_planes != format->input_planes) {
1660 weston_log("%.4s dmabuf must contain %d plane%s (%d provided)",
1661 dump_format(format->format, fmt),
1662 format->input_planes,
1663 (format->input_planes > 1) ? "s" : "",
1664 attributes->n_planes);
1665 return false;
1666 }
1667
1668 for (j = 0; j < format->output_planes; ++j) {
1669 image->images[j] = import_dmabuf_single_plane(gr, attributes,
1670 &format->plane[j]);
1671 if (!image->images[j]) {
1672 while (j) {
1673 ret = egl_image_unref(image->images[--j]);
1674 assert(ret == 0);
1675 }
1676 return false;
1677 }
1678 }
1679
1680 image->num_images = format->output_planes;
1681
1682 switch (format->texture_type) {
1683 case EGL_TEXTURE_Y_XUXV_WL:
1684 image->shader = &gr->texture_shader_y_xuxv;
1685 break;
1686 case EGL_TEXTURE_Y_UV_WL:
1687 image->shader = &gr->texture_shader_y_uv;
1688 break;
1689 case EGL_TEXTURE_Y_U_V_WL:
1690 image->shader = &gr->texture_shader_y_u_v;
1691 break;
1692 default:
1693 assert(false);
1694 }
1695
1696 return true;
1697}
1698
1699static GLenum
1700choose_texture_target(struct dmabuf_attributes *attributes)
1701{
1702 if (attributes->n_planes > 1)
1703 return GL_TEXTURE_EXTERNAL_OES;
1704
1705 switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) {
1706 case DRM_FORMAT_YUYV:
1707 case DRM_FORMAT_YVYU:
1708 case DRM_FORMAT_UYVY:
1709 case DRM_FORMAT_VYUY:
1710 case DRM_FORMAT_AYUV:
1711 return GL_TEXTURE_EXTERNAL_OES;
1712 default:
1713 return GL_TEXTURE_2D;
1714 }
1715}
1716
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001717static struct dmabuf_image *
1718import_dmabuf(struct gl_renderer *gr,
1719 struct linux_dmabuf_buffer *dmabuf)
1720{
1721 struct egl_image *egl_image;
1722 struct dmabuf_image *image;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001723
1724 image = dmabuf_image_create();
Pekka Paalanena3525802014-06-12 16:49:29 +03001725 image->dmabuf = dmabuf;
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001726
1727 egl_image = import_simple_dmabuf(gr, &dmabuf->attributes);
1728 if (egl_image) {
1729 image->num_images = 1;
1730 image->images[0] = egl_image;
1731 image->import_type = IMPORT_TYPE_DIRECT;
1732 image->target = choose_texture_target(&dmabuf->attributes);
1733
1734 switch (image->target) {
1735 case GL_TEXTURE_2D:
1736 image->shader = &gr->texture_shader_rgba;
1737 break;
1738 default:
1739 image->shader = &gr->texture_shader_egl_external;
1740 }
1741 } else {
1742 if (!import_yuv_dmabuf(gr, image)) {
1743 dmabuf_image_destroy(image);
1744 return NULL;
1745 }
1746 image->import_type = IMPORT_TYPE_GL_CONVERSION;
1747 image->target = GL_TEXTURE_2D;
1748 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001749
1750 return image;
1751}
1752
1753static bool
1754gl_renderer_import_dmabuf(struct weston_compositor *ec,
1755 struct linux_dmabuf_buffer *dmabuf)
1756{
1757 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001758 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001759 int i;
1760
1761 assert(gr->has_dmabuf_import);
1762
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001763 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
Pekka Paalanena3525802014-06-12 16:49:29 +03001764 /* EGL import does not have modifiers */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001765 if (dmabuf->attributes.modifier[i] != 0)
Pekka Paalanena3525802014-06-12 16:49:29 +03001766 return false;
1767 }
1768
1769 /* reject all flags we do not recognize or handle */
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001770 if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT)
Pekka Paalanena3525802014-06-12 16:49:29 +03001771 return false;
1772
1773 image = import_dmabuf(gr, dmabuf);
1774 if (!image)
1775 return false;
1776
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001777 wl_list_insert(&gr->dmabuf_images, &image->link);
1778 linux_dmabuf_buffer_set_user_data(dmabuf, image,
1779 gl_renderer_destroy_dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001780
1781 return true;
1782}
1783
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001784static bool
1785import_known_dmabuf(struct gl_renderer *gr,
1786 struct dmabuf_image *image)
Pekka Paalanena3525802014-06-12 16:49:29 +03001787{
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001788 switch (image->import_type) {
1789 case IMPORT_TYPE_DIRECT:
1790 image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes);
1791 if (!image->images[0])
1792 return false;
1793 break;
Pekka Paalanena3525802014-06-12 16:49:29 +03001794
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001795 case IMPORT_TYPE_GL_CONVERSION:
1796 if (!import_yuv_dmabuf(gr, image))
1797 return false;
1798 break;
1799
Pekka Paalanena3525802014-06-12 16:49:29 +03001800 default:
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001801 weston_log("Invalid import type for dmabuf\n");
1802 return false;
Pekka Paalanena3525802014-06-12 16:49:29 +03001803 }
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001804
1805 return true;
Pekka Paalanena3525802014-06-12 16:49:29 +03001806}
1807
1808static void
1809gl_renderer_attach_dmabuf(struct weston_surface *surface,
1810 struct weston_buffer *buffer,
1811 struct linux_dmabuf_buffer *dmabuf)
1812{
1813 struct gl_renderer *gr = get_renderer(surface->compositor);
1814 struct gl_surface_state *gs = get_surface_state(surface);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001815 struct dmabuf_image *image;
Pekka Paalanena3525802014-06-12 16:49:29 +03001816 int i;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001817 int ret;
Pekka Paalanena3525802014-06-12 16:49:29 +03001818
1819 if (!gr->has_dmabuf_import) {
1820 linux_dmabuf_buffer_send_server_error(dmabuf,
1821 "EGL dmabuf import not supported");
1822 return;
1823 }
1824
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001825 buffer->width = dmabuf->attributes.width;
1826 buffer->height = dmabuf->attributes.height;
Pekka Paalanena3525802014-06-12 16:49:29 +03001827 buffer->y_inverted =
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001828 !!(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT);
Pekka Paalanena3525802014-06-12 16:49:29 +03001829
1830 for (i = 0; i < gs->num_images; i++)
1831 egl_image_unref(gs->images[i]);
1832 gs->num_images = 0;
1833
Pekka Paalanena3525802014-06-12 16:49:29 +03001834 /*
1835 * We try to always hold an imported EGLImage from the dmabuf
1836 * to prevent the client from preventing re-imports. But, we also
1837 * need to re-import every time the contents may change because
1838 * GL driver's caching may need flushing.
1839 *
1840 * Here we release the cache reference which has to be final.
1841 */
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001842 image = linux_dmabuf_buffer_get_user_data(dmabuf);
Pekka Paalanena3525802014-06-12 16:49:29 +03001843
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001844 /* The dmabuf_image should have been created during the import */
1845 assert(image != NULL);
1846
1847 for (i = 0; i < image->num_images; ++i) {
1848 ret = egl_image_unref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001849 assert(ret == 0);
1850 }
1851
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001852 if (!import_known_dmabuf(gr, image)) {
1853 linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed");
1854 return;
Pekka Paalanena3525802014-06-12 16:49:29 +03001855 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001856
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001857 gs->num_images = image->num_images;
1858 for (i = 0; i < gs->num_images; ++i)
1859 gs->images[i] = egl_image_ref(image->images[i]);
Pekka Paalanena3525802014-06-12 16:49:29 +03001860
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001861 gs->target = image->target;
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00001862 ensure_textures(gs, gs->num_images);
1863 for (i = 0; i < gs->num_images; ++i) {
1864 glActiveTexture(GL_TEXTURE0 + i);
1865 glBindTexture(gs->target, gs->textures[i]);
1866 gr->image_target_texture_2d(gs->target, gs->images[i]->image);
1867 }
Pekka Paalanena3525802014-06-12 16:49:29 +03001868
Emmanuel Gil Peyrotbc35fda2016-01-11 19:04:35 +00001869 gs->shader = image->shader;
Pekka Paalanena3525802014-06-12 16:49:29 +03001870 gs->pitch = buffer->width;
1871 gs->height = buffer->height;
1872 gs->buffer_type = BUFFER_TYPE_EGL;
1873 gs->y_inverted = buffer->y_inverted;
1874}
1875
1876static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001877gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001878{
1879 struct weston_compositor *ec = es->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001880 struct gl_renderer *gr = get_renderer(ec);
1881 struct gl_surface_state *gs = get_surface_state(es);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001882 struct wl_shm_buffer *shm_buffer;
Pekka Paalanena3525802014-06-12 16:49:29 +03001883 struct linux_dmabuf_buffer *dmabuf;
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001884 EGLint format;
1885 int i;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001886
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001887 weston_buffer_reference(&gs->buffer_ref, buffer);
1888
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001889 if (!buffer) {
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001890 for (i = 0; i < gs->num_images; i++) {
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03001891 egl_image_unref(gs->images[i]);
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001892 gs->images[i] = NULL;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001893 }
John Kåre Alsaker75cc5712012-11-13 19:10:26 +01001894 gs->num_images = 0;
1895 glDeleteTextures(gs->num_textures, gs->textures);
1896 gs->num_textures = 0;
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001897 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001898 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001899 return;
1900 }
1901
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902 shm_buffer = wl_shm_buffer_get(buffer->resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001903
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001904 if (shm_buffer)
1905 gl_renderer_attach_shm(es, buffer, shm_buffer);
Kristian Høgsberg47229392013-08-07 11:59:54 -07001906 else if (gr->query_buffer(gr->egl_display, (void *) buffer->resource,
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001907 EGL_TEXTURE_FORMAT, &format))
1908 gl_renderer_attach_egl(es, buffer, format);
Pekka Paalanena3525802014-06-12 16:49:29 +03001909 else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
1910 gl_renderer_attach_dmabuf(es, buffer, dmabuf);
Ander Conselvan de Oliveira047e9b92013-06-07 16:52:46 +03001911 else {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001912 weston_log("unhandled buffer type!\n");
Pekka Paalanenfb003d32012-12-04 15:58:13 +02001913 weston_buffer_reference(&gs->buffer_ref, NULL);
Ander Conselvan de Oliveiraa9bf1612013-06-07 16:52:44 +03001914 gs->buffer_type = BUFFER_TYPE_NULL;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001915 gs->y_inverted = 1;
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001916 }
1917}
1918
Kristian Høgsberg42263852012-09-06 21:59:29 -04001919static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001920gl_renderer_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001921 float red, float green, float blue, float alpha)
1922{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001923 struct gl_surface_state *gs = get_surface_state(surface);
1924 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001925
1926 gs->color[0] = red;
1927 gs->color[1] = green;
1928 gs->color[2] = blue;
1929 gs->color[3] = alpha;
Pekka Paalanenaeb917e2015-02-09 13:56:56 +02001930 gs->buffer_type = BUFFER_TYPE_SOLID;
1931 gs->pitch = 1;
1932 gs->height = 1;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001933
John Kåre Alsaker40684142012-11-13 19:10:25 +01001934 gs->shader = &gr->solid_shader;
John Kåre Alsaker878f4492012-11-13 19:10:23 +01001935}
1936
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001937static void
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02001938gl_renderer_surface_get_content_size(struct weston_surface *surface,
1939 int *width, int *height)
1940{
1941 struct gl_surface_state *gs = get_surface_state(surface);
1942
1943 if (gs->buffer_type == BUFFER_TYPE_NULL) {
1944 *width = 0;
1945 *height = 0;
1946 } else {
1947 *width = gs->pitch;
1948 *height = gs->height;
1949 }
1950}
1951
1952static uint32_t
1953pack_color(pixman_format_code_t format, float *c)
1954{
1955 uint8_t r = round(c[0] * 255.0f);
1956 uint8_t g = round(c[1] * 255.0f);
1957 uint8_t b = round(c[2] * 255.0f);
1958 uint8_t a = round(c[3] * 255.0f);
1959
1960 switch (format) {
1961 case PIXMAN_a8b8g8r8:
1962 return (a << 24) | (b << 16) | (g << 8) | r;
1963 default:
1964 assert(0);
1965 return 0;
1966 }
1967}
1968
1969static int
1970gl_renderer_surface_copy_content(struct weston_surface *surface,
1971 void *target, size_t size,
1972 int src_x, int src_y,
1973 int width, int height)
1974{
1975 static const GLfloat verts[4 * 2] = {
1976 0.0f, 0.0f,
1977 1.0f, 0.0f,
1978 1.0f, 1.0f,
1979 0.0f, 1.0f
1980 };
1981 static const GLfloat projmat_normal[16] = { /* transpose */
1982 2.0f, 0.0f, 0.0f, 0.0f,
1983 0.0f, 2.0f, 0.0f, 0.0f,
1984 0.0f, 0.0f, 1.0f, 0.0f,
1985 -1.0f, -1.0f, 0.0f, 1.0f
1986 };
1987 static const GLfloat projmat_yinvert[16] = { /* transpose */
1988 2.0f, 0.0f, 0.0f, 0.0f,
1989 0.0f, -2.0f, 0.0f, 0.0f,
1990 0.0f, 0.0f, 1.0f, 0.0f,
1991 -1.0f, 1.0f, 0.0f, 1.0f
1992 };
1993 const pixman_format_code_t format = PIXMAN_a8b8g8r8;
1994 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
1995 const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */
1996 struct gl_renderer *gr = get_renderer(surface->compositor);
1997 struct gl_surface_state *gs = get_surface_state(surface);
1998 int cw, ch;
1999 GLuint fbo;
2000 GLuint tex;
2001 GLenum status;
2002 const GLfloat *proj;
2003 int i;
2004
2005 gl_renderer_surface_get_content_size(surface, &cw, &ch);
2006
2007 switch (gs->buffer_type) {
2008 case BUFFER_TYPE_NULL:
2009 return -1;
2010 case BUFFER_TYPE_SOLID:
2011 *(uint32_t *)target = pack_color(format, gs->color);
2012 return 0;
2013 case BUFFER_TYPE_SHM:
2014 gl_renderer_flush_damage(surface);
2015 /* fall through */
2016 case BUFFER_TYPE_EGL:
2017 break;
2018 }
2019
2020 glGenTextures(1, &tex);
2021 glBindTexture(GL_TEXTURE_2D, tex);
2022 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch,
2023 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2024 glBindTexture(GL_TEXTURE_2D, 0);
2025
2026 glGenFramebuffers(1, &fbo);
2027 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2028 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2029 GL_TEXTURE_2D, tex, 0);
2030
2031 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2032 if (status != GL_FRAMEBUFFER_COMPLETE) {
2033 weston_log("%s: fbo error: %#x\n", __func__, status);
2034 glDeleteFramebuffers(1, &fbo);
2035 glDeleteTextures(1, &tex);
2036 return -1;
2037 }
2038
2039 glViewport(0, 0, cw, ch);
2040 glDisable(GL_BLEND);
2041 use_shader(gr, gs->shader);
2042 if (gs->y_inverted)
2043 proj = projmat_normal;
2044 else
2045 proj = projmat_yinvert;
2046
2047 glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj);
2048 glUniform1f(gs->shader->alpha_uniform, 1.0f);
2049
2050 for (i = 0; i < gs->num_textures; i++) {
2051 glUniform1i(gs->shader->tex_uniforms[i], i);
2052
2053 glActiveTexture(GL_TEXTURE0 + i);
2054 glBindTexture(gs->target, gs->textures[i]);
2055 glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2056 glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2057 }
2058
2059 /* position: */
2060 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
2061 glEnableVertexAttribArray(0);
2062
2063 /* texcoord: */
2064 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts);
2065 glEnableVertexAttribArray(1);
2066
2067 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2068
2069 glDisableVertexAttribArray(1);
2070 glDisableVertexAttribArray(0);
2071
2072 glPixelStorei(GL_PACK_ALIGNMENT, bytespp);
2073 glReadPixels(src_x, src_y, width, height, gl_format,
2074 GL_UNSIGNED_BYTE, target);
2075
2076 glDeleteFramebuffers(1, &fbo);
2077 glDeleteTextures(1, &tex);
2078
2079 return 0;
2080}
2081
2082static void
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002083surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr)
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002084{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002085 int i;
2086
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002087 wl_list_remove(&gs->surface_destroy_listener.link);
2088 wl_list_remove(&gs->renderer_destroy_listener.link);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002089
2090 gs->surface->renderer_state = NULL;
2091
2092 glDeleteTextures(gs->num_textures, gs->textures);
2093
2094 for (i = 0; i < gs->num_images; i++)
Louis-Francis Ratté-Boulianne534defd2015-06-08 16:37:05 +03002095 egl_image_unref(gs->images[i]);
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002096
2097 weston_buffer_reference(&gs->buffer_ref, NULL);
2098 pixman_region32_fini(&gs->texture_damage);
2099 free(gs);
2100}
2101
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002102static void
2103surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
2104{
2105 struct gl_surface_state *gs;
2106 struct gl_renderer *gr;
2107
2108 gs = container_of(listener, struct gl_surface_state,
2109 surface_destroy_listener);
2110
2111 gr = get_renderer(gs->surface->compositor);
2112
2113 surface_state_destroy(gs, gr);
2114}
2115
2116static void
2117surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
2118{
2119 struct gl_surface_state *gs;
2120 struct gl_renderer *gr;
2121
2122 gr = data;
2123
2124 gs = container_of(listener, struct gl_surface_state,
2125 renderer_destroy_listener);
2126
2127 surface_state_destroy(gs, gr);
2128}
2129
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002130static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002131gl_renderer_create_surface(struct weston_surface *surface)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002132{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002133 struct gl_surface_state *gs;
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002134 struct gl_renderer *gr = get_renderer(surface->compositor);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002135
Bryce Harringtonde16d892014-11-20 22:21:57 -08002136 gs = zalloc(sizeof *gs);
2137 if (gs == NULL)
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002138 return -1;
2139
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002140 /* A buffer is never attached to solid color surfaces, yet
2141 * they still go through texcoord computations. Do not divide
2142 * by zero there.
2143 */
2144 gs->pitch = 1;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002145 gs->y_inverted = 1;
Pekka Paalanen68033ac2012-12-04 15:58:15 +02002146
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002147 gs->surface = surface;
2148
Pekka Paalanen81ee3f52012-12-04 15:58:16 +02002149 pixman_region32_init(&gs->texture_damage);
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002150 surface->renderer_state = gs;
2151
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03002152 gs->surface_destroy_listener.notify =
2153 surface_state_handle_surface_destroy;
2154 wl_signal_add(&surface->destroy_signal,
2155 &gs->surface_destroy_listener);
2156
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002157 gs->renderer_destroy_listener.notify =
2158 surface_state_handle_renderer_destroy;
2159 wl_signal_add(&gr->destroy_signal,
2160 &gs->renderer_destroy_listener);
2161
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02002162 if (surface->buffer_ref.buffer) {
2163 gl_renderer_attach(surface, surface->buffer_ref.buffer);
2164 gl_renderer_flush_damage(surface);
2165 }
2166
John Kåre Alsaker878f4492012-11-13 19:10:23 +01002167 return 0;
2168}
2169
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002170static const char vertex_shader[] =
2171 "uniform mat4 proj;\n"
2172 "attribute vec2 position;\n"
2173 "attribute vec2 texcoord;\n"
2174 "varying vec2 v_texcoord;\n"
2175 "void main()\n"
2176 "{\n"
2177 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
2178 " v_texcoord = texcoord;\n"
2179 "}\n";
2180
2181/* Declare common fragment shader uniforms */
2182#define FRAGMENT_CONVERT_YUV \
2183 " y *= alpha;\n" \
2184 " u *= alpha;\n" \
2185 " v *= alpha;\n" \
2186 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2187 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2188 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2189 " gl_FragColor.a = alpha;\n"
2190
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002191static const char fragment_debug[] =
2192 " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n";
2193
2194static const char fragment_brace[] =
2195 "}\n";
2196
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002197static const char texture_fragment_shader_rgba[] =
2198 "precision mediump float;\n"
2199 "varying vec2 v_texcoord;\n"
2200 "uniform sampler2D tex;\n"
2201 "uniform float alpha;\n"
2202 "void main()\n"
2203 "{\n"
2204 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002205 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002206
2207static const char texture_fragment_shader_rgbx[] =
2208 "precision mediump float;\n"
2209 "varying vec2 v_texcoord;\n"
2210 "uniform sampler2D tex;\n"
2211 "uniform float alpha;\n"
2212 "void main()\n"
2213 "{\n"
2214 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
2215 " gl_FragColor.a = alpha;\n"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002216 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002217
2218static const char texture_fragment_shader_egl_external[] =
2219 "#extension GL_OES_EGL_image_external : require\n"
2220 "precision mediump float;\n"
2221 "varying vec2 v_texcoord;\n"
2222 "uniform samplerExternalOES tex;\n"
2223 "uniform float alpha;\n"
2224 "void main()\n"
2225 "{\n"
2226 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002227 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002228
2229static const char texture_fragment_shader_y_uv[] =
2230 "precision mediump float;\n"
2231 "uniform sampler2D tex;\n"
2232 "uniform sampler2D tex1;\n"
2233 "varying vec2 v_texcoord;\n"
2234 "uniform float alpha;\n"
2235 "void main() {\n"
2236 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2237 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2238 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2239 FRAGMENT_CONVERT_YUV
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002240 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002241
2242static const char texture_fragment_shader_y_u_v[] =
2243 "precision mediump float;\n"
2244 "uniform sampler2D tex;\n"
2245 "uniform sampler2D tex1;\n"
2246 "uniform sampler2D tex2;\n"
2247 "varying vec2 v_texcoord;\n"
2248 "uniform float alpha;\n"
2249 "void main() {\n"
2250 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2251 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2252 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2253 FRAGMENT_CONVERT_YUV
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_xuxv[] =
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).g - 0.5;\n"
2265 " float v = texture2D(tex1, v_texcoord).a - 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 solid_fragment_shader[] =
2270 "precision mediump float;\n"
2271 "uniform vec4 color;\n"
2272 "uniform float alpha;\n"
2273 "void main()\n"
2274 "{\n"
2275 " gl_FragColor = alpha * color\n;"
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002276 ;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002277
2278static int
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002279compile_shader(GLenum type, int count, const char **sources)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002280{
2281 GLuint s;
2282 char msg[512];
2283 GLint status;
2284
2285 s = glCreateShader(type);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002286 glShaderSource(s, count, sources, NULL);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002287 glCompileShader(s);
2288 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
2289 if (!status) {
2290 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
2291 weston_log("shader info: %s\n", msg);
2292 return GL_NONE;
2293 }
2294
2295 return s;
2296}
2297
2298static int
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03002299shader_init(struct gl_shader *shader, struct gl_renderer *renderer,
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002300 const char *vertex_source, const char *fragment_source)
2301{
2302 char msg[512];
2303 GLint status;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002304 int count;
2305 const char *sources[3];
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002306
2307 shader->vertex_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002308 compile_shader(GL_VERTEX_SHADER, 1, &vertex_source);
2309
2310 if (renderer->fragment_shader_debug) {
2311 sources[0] = fragment_source;
2312 sources[1] = fragment_debug;
2313 sources[2] = fragment_brace;
2314 count = 3;
2315 } else {
2316 sources[0] = fragment_source;
2317 sources[1] = fragment_brace;
2318 count = 2;
2319 }
2320
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002321 shader->fragment_shader =
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002322 compile_shader(GL_FRAGMENT_SHADER, count, sources);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002323
2324 shader->program = glCreateProgram();
2325 glAttachShader(shader->program, shader->vertex_shader);
2326 glAttachShader(shader->program, shader->fragment_shader);
2327 glBindAttribLocation(shader->program, 0, "position");
2328 glBindAttribLocation(shader->program, 1, "texcoord");
2329
2330 glLinkProgram(shader->program);
2331 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
2332 if (!status) {
2333 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
2334 weston_log("link info: %s\n", msg);
2335 return -1;
2336 }
2337
2338 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2339 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2340 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2341 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
2342 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
2343 shader->color_uniform = glGetUniformLocation(shader->program, "color");
2344
2345 return 0;
2346}
2347
2348static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002349shader_release(struct gl_shader *shader)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02002350{
2351 glDeleteShader(shader->vertex_shader);
2352 glDeleteShader(shader->fragment_shader);
2353 glDeleteProgram(shader->program);
2354
2355 shader->vertex_shader = 0;
2356 shader->fragment_shader = 0;
2357 shader->program = 0;
2358}
2359
2360static void
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04002361log_extensions(const char *name, const char *extensions)
2362{
2363 const char *p, *end;
2364 int l;
2365 int len;
2366
2367 l = weston_log("%s:", name);
2368 p = extensions;
2369 while (*p) {
2370 end = strchrnul(p, ' ');
2371 len = end - p;
2372 if (l + len > 78)
2373 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2374 len, p);
2375 else
2376 l += weston_log_continue(" %.*s", len, p);
2377 for (p = end; isspace(*p); p++)
2378 ;
2379 }
2380 weston_log_continue("\n");
2381}
2382
2383static void
2384log_egl_gl_info(EGLDisplay egldpy)
2385{
2386 const char *str;
2387
2388 str = eglQueryString(egldpy, EGL_VERSION);
2389 weston_log("EGL version: %s\n", str ? str : "(null)");
2390
2391 str = eglQueryString(egldpy, EGL_VENDOR);
2392 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2393
2394 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2395 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2396
2397 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2398 log_extensions("EGL extensions", str ? str : "(null)");
2399
2400 str = (char *)glGetString(GL_VERSION);
2401 weston_log("GL version: %s\n", str ? str : "(null)");
2402
2403 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2404 weston_log("GLSL version: %s\n", str ? str : "(null)");
2405
2406 str = (char *)glGetString(GL_VENDOR);
2407 weston_log("GL vendor: %s\n", str ? str : "(null)");
2408
2409 str = (char *)glGetString(GL_RENDERER);
2410 weston_log("GL renderer: %s\n", str ? str : "(null)");
2411
2412 str = (char *)glGetString(GL_EXTENSIONS);
2413 log_extensions("GL extensions", str ? str : "(null)");
2414}
2415
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03002416static void
2417log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig)
2418{
2419 EGLint r, g, b, a;
2420
2421 weston_log("Chosen EGL config details:\n");
2422
2423 weston_log_continue(STAMP_SPACE "RGBA bits");
2424 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) &&
2425 eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) &&
2426 eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) &&
2427 eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a))
2428 weston_log_continue(": %d %d %d %d\n", r, g, b, a);
2429 else
2430 weston_log_continue(" unknown\n");
2431
2432 weston_log_continue(STAMP_SPACE "swap interval range");
2433 if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) &&
2434 eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b))
2435 weston_log_continue(": %d - %d\n", a, b);
2436 else
2437 weston_log_continue(" unknown\n");
2438}
2439
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002440static int
Derek Foremane76f1852015-05-15 12:12:39 -05002441match_config_to_visual(EGLDisplay egl_display,
2442 EGLint visual_id,
2443 EGLConfig *configs,
2444 int count)
2445{
2446 int i;
2447
2448 for (i = 0; i < count; ++i) {
2449 EGLint id;
2450
2451 if (!eglGetConfigAttrib(egl_display,
2452 configs[i], EGL_NATIVE_VISUAL_ID,
2453 &id))
2454 continue;
2455
2456 if (id == visual_id)
2457 return i;
2458 }
2459
Derek Foremane76f1852015-05-15 12:12:39 -05002460 return -1;
2461}
2462
2463static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002464egl_choose_config(struct gl_renderer *gr, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002465 const EGLint *visual_id, const int n_ids,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002466 EGLConfig *config_out)
2467{
2468 EGLint count = 0;
2469 EGLint matched = 0;
2470 EGLConfig *configs;
Derek Foremane76f1852015-05-15 12:12:39 -05002471 int i, config_index = -1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002472
Derek Foremana7e19912015-05-20 14:57:58 -05002473 if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) {
2474 weston_log("No EGL configs to choose from.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002475 return -1;
Derek Foremana7e19912015-05-20 14:57:58 -05002476 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002477 configs = calloc(count, sizeof *configs);
2478 if (!configs)
2479 return -1;
2480
2481 if (!eglChooseConfig(gr->egl_display, attribs, configs,
Derek Foremana7e19912015-05-20 14:57:58 -05002482 count, &matched) || !matched) {
2483 weston_log("No EGL configs with appropriate attributes.\n");
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002484 goto out;
Derek Foremana7e19912015-05-20 14:57:58 -05002485 }
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002486
Derek Foremane76f1852015-05-15 12:12:39 -05002487 if (!visual_id)
2488 config_index = 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002489
Derek Foremane76f1852015-05-15 12:12:39 -05002490 for (i = 0; config_index == -1 && i < n_ids; i++)
2491 config_index = match_config_to_visual(gr->egl_display,
2492 visual_id[i],
2493 configs,
2494 matched);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002495
Derek Foremane76f1852015-05-15 12:12:39 -05002496 if (config_index != -1)
2497 *config_out = configs[config_index];
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002498
2499out:
2500 free(configs);
Derek Foremane76f1852015-05-15 12:12:39 -05002501 if (config_index == -1)
2502 return -1;
2503
Derek Foremana7e19912015-05-20 14:57:58 -05002504 if (i > 1)
2505 weston_log("Unable to use first choice EGL config with id"
2506 " 0x%x, succeeded with alternate id 0x%x.\n",
2507 visual_id[0], visual_id[i - 1]);
Derek Foremane76f1852015-05-15 12:12:39 -05002508 return 0;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002509}
2510
John Kåre Alsaker44154502012-11-13 19:10:20 +01002511static void
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002512gl_renderer_output_set_border(struct weston_output *output,
2513 enum gl_renderer_border_side side,
2514 int32_t width, int32_t height,
2515 int32_t tex_width, unsigned char *data)
2516{
2517 struct gl_output_state *go = get_output_state(output);
2518
Jason Ekstrande5512d42014-02-04 21:36:38 -06002519 if (go->borders[side].width != width ||
2520 go->borders[side].height != height)
2521 /* In this case, we have to blow everything and do a full
2522 * repaint. */
2523 go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY;
2524
2525 if (data == NULL) {
2526 width = 0;
2527 height = 0;
2528 }
2529
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002530 go->borders[side].width = width;
2531 go->borders[side].height = height;
2532 go->borders[side].tex_width = tex_width;
2533 go->borders[side].data = data;
Jason Ekstrande5512d42014-02-04 21:36:38 -06002534 go->border_status |= 1 << side;
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05002535}
2536
John Kåre Alsaker94659272012-11-13 19:10:18 +01002537static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002538gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002539
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002540static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002541gl_renderer_output_create(struct weston_output *output,
Jonny Lamb671148f2015-03-20 15:26:52 +01002542 EGLNativeWindowType window_for_legacy,
2543 void *window_for_platform,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002544 const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002545 const EGLint *visual_id,
2546 int n_ids)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002547{
2548 struct weston_compositor *ec = output->compositor;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002549 struct gl_renderer *gr = get_renderer(ec);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002550 struct gl_output_state *go;
2551 EGLConfig egl_config;
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002552 int i;
John Kåre Alsaker94659272012-11-13 19:10:18 +01002553
Derek Foremane76f1852015-05-15 12:12:39 -05002554 if (egl_choose_config(gr, attribs, visual_id,
2555 n_ids, &egl_config) == -1) {
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002556 weston_log("failed to choose EGL config for output\n");
2557 return -1;
2558 }
2559
2560 if (egl_config != gr->egl_config &&
2561 !gr->has_configless_context) {
2562 weston_log("attempted to use a different EGL config for an "
2563 "output but EGL_MESA_configless_context is not "
2564 "supported\n");
2565 return -1;
2566 }
2567
Bryce Harringtonde16d892014-11-20 22:21:57 -08002568 go = zalloc(sizeof *go);
2569 if (go == NULL)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002570 return -1;
2571
Jonny Lamb671148f2015-03-20 15:26:52 +01002572 if (gr->create_platform_window) {
2573 go->egl_surface =
2574 gr->create_platform_window(gr->egl_display,
2575 egl_config,
2576 window_for_platform,
2577 NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002578 } else {
Jonny Lamb671148f2015-03-20 15:26:52 +01002579 go->egl_surface =
2580 eglCreateWindowSurface(gr->egl_display,
2581 egl_config,
2582 window_for_legacy, NULL);
Jonny Lambf1ec5062015-03-24 13:12:05 +01002583 }
John Kåre Alsaker94659272012-11-13 19:10:18 +01002584
2585 if (go->egl_surface == EGL_NO_SURFACE) {
2586 weston_log("failed to create egl surface\n");
2587 free(go);
2588 return -1;
2589 }
2590
Ander Conselvan de Oliveira1c169ff2013-03-05 17:30:30 +02002591 for (i = 0; i < BUFFER_DAMAGE_COUNT; i++)
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002592 pixman_region32_init(&go->buffer_damage[i]);
2593
John Kåre Alsaker94659272012-11-13 19:10:18 +01002594 output->renderer_state = go;
2595
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002596 log_egl_config_info(gr->egl_display, egl_config);
2597
John Kåre Alsaker94659272012-11-13 19:10:18 +01002598 return 0;
2599}
2600
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002601static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002602gl_renderer_output_destroy(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002603{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002604 struct gl_renderer *gr = get_renderer(output->compositor);
2605 struct gl_output_state *go = get_output_state(output);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002606 int i;
2607
2608 for (i = 0; i < 2; i++)
2609 pixman_region32_fini(&go->buffer_damage[i]);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002610
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002611 eglDestroySurface(gr->egl_display, go->egl_surface);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002612
2613 free(go);
2614}
2615
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002616static EGLSurface
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002617gl_renderer_output_surface(struct weston_output *output)
John Kåre Alsaker94659272012-11-13 19:10:18 +01002618{
2619 return get_output_state(output)->egl_surface;
2620}
2621
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002622static void
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002623gl_renderer_destroy(struct weston_compositor *ec)
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04002624{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002625 struct gl_renderer *gr = get_renderer(ec);
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002626 struct dmabuf_image *image, *next;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002627
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002628 wl_signal_emit(&gr->destroy_signal, gr);
2629
John Kåre Alsaker320711d2012-11-13 19:10:27 +01002630 if (gr->has_bind_display)
2631 gr->unbind_display(gr->egl_display, ec->wl_display);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002632
2633 /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */
2634 eglMakeCurrent(gr->egl_display,
2635 EGL_NO_SURFACE, EGL_NO_SURFACE,
2636 EGL_NO_CONTEXT);
2637
Pekka Paalanena3525802014-06-12 16:49:29 +03002638
Emmanuel Gil Peyrotb8053502016-01-11 19:04:34 +00002639 wl_list_for_each_safe(image, next, &gr->dmabuf_images, link)
2640 dmabuf_image_destroy(image);
Pekka Paalanena3525802014-06-12 16:49:29 +03002641
Armin Krezović28d240f2016-06-23 11:59:35 +02002642 if (gr->dummy_surface != EGL_NO_SURFACE)
2643 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2644
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002645 eglTerminate(gr->egl_display);
2646 eglReleaseThread();
Scott Moreau976a0502013-03-07 10:15:17 -07002647
Armin Krezović36d699a2016-08-05 15:28:30 +02002648 wl_list_remove(&gr->output_destroy_listener.link);
2649
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002650 wl_array_release(&gr->vertices);
Kristian Høgsberg7b9195f2013-05-08 22:38:05 -04002651 wl_array_release(&gr->vtxcnt);
2652
Mariusz Ceiercbb91582014-02-08 20:11:24 +01002653 if (gr->fragment_binding)
2654 weston_binding_destroy(gr->fragment_binding);
2655 if (gr->fan_binding)
2656 weston_binding_destroy(gr->fan_binding);
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002657
Scott Moreau976a0502013-03-07 10:15:17 -07002658 free(gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002659}
2660
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002661static void
2662renderer_setup_egl_client_extensions(struct gl_renderer *gr)
2663{
2664 const char *extensions;
2665
2666 extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
2667 if (!extensions) {
2668 weston_log("Retrieving EGL client extension string failed.\n");
2669 return;
2670 }
2671
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002672 if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002673 gr->create_platform_window =
2674 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
2675 else
2676 weston_log("warning: EGL_EXT_platform_base not supported.\n");
2677}
2678
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002679static int
Neil Robertsb7f85332014-03-07 18:05:49 +00002680gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
2681{
2682 struct gl_renderer *gr = get_renderer(ec);
2683 const char *extensions;
2684 EGLBoolean ret;
2685
2686 gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2687 gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2688 gr->bind_display =
2689 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
2690 gr->unbind_display =
2691 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
2692 gr->query_buffer =
2693 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
2694
2695 extensions =
2696 (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS);
2697 if (!extensions) {
2698 weston_log("Retrieving EGL extension string failed.\n");
2699 return -1;
2700 }
2701
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002702 if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002703 gr->has_bind_display = 1;
2704 if (gr->has_bind_display) {
2705 ret = gr->bind_display(gr->egl_display, ec->wl_display);
2706 if (!ret)
2707 gr->has_bind_display = 0;
2708 }
2709
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002710 if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002711 gr->has_egl_buffer_age = 1;
2712 else
2713 weston_log("warning: EGL_EXT_buffer_age not supported. "
2714 "Performance could be affected.\n");
2715
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002716 if (weston_check_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
Neil Robertsb7f85332014-03-07 18:05:49 +00002717 gr->swap_buffers_with_damage =
2718 (void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
2719 else
2720 weston_log("warning: EGL_EXT_swap_buffers_with_damage not "
2721 "supported. Performance could be affected.\n");
Neil Robertsb7f85332014-03-07 18:05:49 +00002722
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002723 if (weston_check_egl_extension(extensions, "EGL_MESA_configless_context"))
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002724 gr->has_configless_context = 1;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002725
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002726 if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context"))
Armin Krezović28d240f2016-06-23 11:59:35 +02002727 gr->has_surfaceless_context = 1;
2728
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002729 if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import"))
Pekka Paalanena3525802014-06-12 16:49:29 +03002730 gr->has_dmabuf_import = 1;
Pekka Paalanena3525802014-06-12 16:49:29 +03002731
Pekka Paalanen8b69d032015-04-08 17:02:22 +03002732 renderer_setup_egl_client_extensions(gr);
2733
Neil Robertsb7f85332014-03-07 18:05:49 +00002734 return 0;
2735}
2736
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002737static const EGLint gl_renderer_opaque_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002738 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2739 EGL_RED_SIZE, 1,
2740 EGL_GREEN_SIZE, 1,
2741 EGL_BLUE_SIZE, 1,
2742 EGL_ALPHA_SIZE, 0,
2743 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2744 EGL_NONE
2745};
2746
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002747static const EGLint gl_renderer_alpha_attribs[] = {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002748 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
2749 EGL_RED_SIZE, 1,
2750 EGL_GREEN_SIZE, 1,
2751 EGL_BLUE_SIZE, 1,
2752 EGL_ALPHA_SIZE, 1,
2753 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2754 EGL_NONE
2755};
2756
Armin Krezović28d240f2016-06-23 11:59:35 +02002757
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002758/** Checks whether a platform EGL client extension is supported
2759 *
2760 * \param ec The weston compositor
2761 * \param extension_suffix The EGL client extension suffix
2762 * \return 1 if supported, 0 if using fallbacks, -1 unsupported
2763 *
2764 * This function checks whether a specific platform_* extension is supported
2765 * by EGL.
2766 *
2767 * The extension suffix should be the suffix of the platform extension (that
2768 * specifies a <platform> argument as defined in EGL_EXT_platform_base). For
2769 * example, passing "foo" will check whether either "EGL_KHR_platform_foo",
2770 * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported.
2771 *
2772 * The return value is 1:
2773 * - if the supplied EGL client extension is supported.
2774 * The return value is 0:
2775 * - if the platform_base client extension isn't supported so will
2776 * fallback to eglGetDisplay and friends.
2777 * The return value is -1:
2778 * - if the supplied EGL client extension is not supported.
2779 */
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002780static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002781gl_renderer_supports(struct weston_compositor *ec,
2782 const char *extension_suffix)
2783{
2784 static const char *extensions = NULL;
2785 char s[64];
2786
2787 if (!extensions) {
2788 extensions = (const char *) eglQueryString(
2789 EGL_NO_DISPLAY, EGL_EXTENSIONS);
2790
2791 if (!extensions)
2792 return 0;
2793
2794 log_extensions("EGL client extensions",
2795 extensions);
2796 }
2797
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002798 if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base"))
Pekka Paalanenf2824542015-04-08 17:02:21 +03002799 return 0;
2800
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002801 snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002802 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002803 return 1;
2804
2805 snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002806 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002807 return 1;
2808
2809 snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01002810 if (weston_check_egl_extension(extensions, s))
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002811 return 1;
2812
Pekka Paalanenf2824542015-04-08 17:02:21 +03002813 /* at this point we definitely have some platform extensions but
2814 * haven't found the supplied platform, so chances are it's
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002815 * not supported. */
2816
2817 return -1;
2818}
2819
Jonny Lamb74eed312015-03-24 13:12:04 +01002820static const char *
2821platform_to_extension(EGLenum platform)
2822{
2823 switch (platform) {
2824 case EGL_PLATFORM_GBM_KHR:
2825 return "gbm";
2826 case EGL_PLATFORM_WAYLAND_KHR:
2827 return "wayland";
2828 case EGL_PLATFORM_X11_KHR:
2829 return "x11";
2830 default:
2831 assert(0 && "bad EGL platform enum");
2832 }
2833}
2834
Armin Krezović36d699a2016-08-05 15:28:30 +02002835static void
2836output_handle_destroy(struct wl_listener *listener, void *data)
2837{
2838 struct gl_renderer *gr;
2839 struct weston_output *output = data;
2840
2841 gr = container_of(listener, struct gl_renderer,
2842 output_destroy_listener);
2843
2844 if (wl_list_empty(&output->compositor->output_list))
2845 eglMakeCurrent(gr->egl_display, gr->dummy_surface,
2846 gr->dummy_surface, gr->egl_context);
2847}
2848
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002849static int
Armin Krezović28d240f2016-06-23 11:59:35 +02002850gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) {
2851 EGLConfig pbuffer_config;
2852
2853 static const EGLint pbuffer_config_attribs[] = {
2854 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
2855 EGL_RED_SIZE, 1,
2856 EGL_GREEN_SIZE, 1,
2857 EGL_BLUE_SIZE, 1,
2858 EGL_ALPHA_SIZE, 0,
2859 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2860 EGL_NONE
2861 };
2862
2863 static const EGLint pbuffer_attribs[] = {
2864 EGL_WIDTH, 10,
2865 EGL_HEIGHT, 10,
2866 EGL_NONE
2867 };
2868
2869 if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) {
2870 weston_log("failed to choose EGL config for PbufferSurface");
2871 return -1;
2872 }
2873
2874 gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display,
2875 pbuffer_config,
2876 pbuffer_attribs);
2877
2878 if (gr->dummy_surface == EGL_NO_SURFACE) {
2879 weston_log("failed to create PbufferSurface\n");
2880 return -1;
2881 }
2882
2883 return 0;
2884}
2885
2886static int
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002887gl_renderer_create(struct weston_compositor *ec, EGLenum platform,
2888 void *native_window, const EGLint *attribs,
Derek Foremane76f1852015-05-15 12:12:39 -05002889 const EGLint *visual_id, int n_ids)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002890{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002891 struct gl_renderer *gr;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002892 EGLint major, minor;
Jonny Lamb74eed312015-03-24 13:12:04 +01002893 int supports = 0;
2894
2895 if (platform) {
2896 supports = gl_renderer_supports(
2897 ec, platform_to_extension(platform));
2898 if (supports < 0)
2899 return -1;
2900 }
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002901
Bryce Harringtonde16d892014-11-20 22:21:57 -08002902 gr = zalloc(sizeof *gr);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002903 if (gr == NULL)
2904 return -1;
2905
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002906 gr->base.read_pixels = gl_renderer_read_pixels;
2907 gr->base.repaint_output = gl_renderer_repaint_output;
2908 gr->base.flush_damage = gl_renderer_flush_damage;
2909 gr->base.attach = gl_renderer_attach;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01002910 gr->base.surface_set_color = gl_renderer_surface_set_color;
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03002911 gr->base.destroy = gl_renderer_destroy;
Pekka Paalaneneb35cbe2015-02-09 13:37:27 +02002912 gr->base.surface_get_content_size =
2913 gl_renderer_surface_get_content_size;
2914 gr->base.surface_copy_content = gl_renderer_surface_copy_content;
Jonny Lamb74eed312015-03-24 13:12:04 +01002915 gr->egl_display = NULL;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002916
Jonny Lambf1ec5062015-03-24 13:12:05 +01002917 /* extension_suffix is supported */
Jonny Lamb74eed312015-03-24 13:12:04 +01002918 if (supports) {
2919 if (!get_platform_display) {
2920 get_platform_display = (void *) eglGetProcAddress(
2921 "eglGetPlatformDisplayEXT");
2922 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002923
Jonny Lamb74eed312015-03-24 13:12:04 +01002924 /* also wrap this in the supports check because
2925 * eglGetProcAddress can return non-NULL and still not
2926 * support the feature at runtime, so ensure the
2927 * appropriate extension checks have been done. */
2928 if (get_platform_display && platform) {
2929 gr->egl_display = get_platform_display(platform,
2930 native_window,
2931 NULL);
2932 }
2933 }
Jonny Lamb74eed312015-03-24 13:12:04 +01002934
2935 if (!gr->egl_display) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002936 weston_log("warning: either no EGL_EXT_platform_base "
2937 "support or specific platform support; "
2938 "falling back to eglGetDisplay.\n");
2939 gr->egl_display = eglGetDisplay(native_window);
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002940 }
Jonny Lamb70eba3f2015-03-20 15:26:50 +01002941
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002942 if (gr->egl_display == EGL_NO_DISPLAY) {
2943 weston_log("failed to create display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002944 goto fail;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002945 }
2946
2947 if (!eglInitialize(gr->egl_display, &major, &minor)) {
2948 weston_log("failed to initialize display\n");
Derek Foreman066ca0c2015-06-11 12:14:45 -05002949 goto fail_with_error;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002950 }
2951
Derek Foremane76f1852015-05-15 12:12:39 -05002952 if (egl_choose_config(gr, attribs, visual_id,
2953 n_ids, &gr->egl_config) < 0) {
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002954 weston_log("failed to choose EGL config\n");
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002955 goto fail_terminate;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002956 }
2957
2958 ec->renderer = &gr->base;
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002959 ec->capabilities |= WESTON_CAP_ROTATION_ANY;
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002960 ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
Pekka Paalanenfa79b1d2015-02-18 09:48:59 +02002961 ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002962
Neil Robertsb7f85332014-03-07 18:05:49 +00002963 if (gl_renderer_setup_egl_extensions(ec) < 0)
Derek Foreman066ca0c2015-06-11 12:14:45 -05002964 goto fail_with_error;
Neil Robertsb7f85332014-03-07 18:05:49 +00002965
Pekka Paalanena3525802014-06-12 16:49:29 +03002966 wl_list_init(&gr->dmabuf_images);
2967 if (gr->has_dmabuf_import)
2968 gr->base.import_dmabuf = gl_renderer_import_dmabuf;
2969
Armin Krezović28d240f2016-06-23 11:59:35 +02002970 if (gr->has_surfaceless_context) {
2971 weston_log("EGL_KHR_surfaceless_context available\n");
2972 gr->dummy_surface = EGL_NO_SURFACE;
2973 } else {
2974 weston_log("EGL_KHR_surfaceless_context unavailable. "
2975 "Trying PbufferSurface\n");
2976
2977 if (gl_renderer_create_pbuffer_surface(gr) < 0)
2978 goto fail_with_error;
2979 }
2980
Tomeu Vizoso12072b62013-08-06 20:05:55 +02002981 wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);
2982
Ander Conselvan de Oliveiraadda00e2013-10-25 16:26:34 +03002983 wl_signal_init(&gr->destroy_signal);
2984
Armin Krezović28d240f2016-06-23 11:59:35 +02002985 if (gl_renderer_setup(ec, gr->dummy_surface) < 0) {
2986 if (gr->dummy_surface != EGL_NO_SURFACE)
2987 eglDestroySurface(gr->egl_display, gr->dummy_surface);
2988 goto fail_with_error;
2989 }
2990
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002991 return 0;
2992
Derek Foreman066ca0c2015-06-11 12:14:45 -05002993fail_with_error:
Pekka Paalanen326529f2012-11-27 12:25:25 +02002994 gl_renderer_print_egl_error_state();
Dawid Gajownik1a912a92015-08-21 00:20:54 -03002995fail_terminate:
2996 eglTerminate(gr->egl_display);
Derek Foreman066ca0c2015-06-11 12:14:45 -05002997fail:
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01002998 free(gr);
2999 return -1;
3000}
3001
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003002static EGLDisplay
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003003gl_renderer_display(struct weston_compositor *ec)
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003004{
3005 return get_renderer(ec)->egl_display;
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04003006}
3007
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003008static int
3009compile_shaders(struct weston_compositor *ec)
3010{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003011 struct gl_renderer *gr = get_renderer(ec);
John Kåre Alsaker40684142012-11-13 19:10:25 +01003012
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003013 gr->texture_shader_rgba.vertex_source = vertex_shader;
3014 gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba;
3015
3016 gr->texture_shader_rgbx.vertex_source = vertex_shader;
3017 gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx;
3018
3019 gr->texture_shader_egl_external.vertex_source = vertex_shader;
3020 gr->texture_shader_egl_external.fragment_source =
3021 texture_fragment_shader_egl_external;
3022
3023 gr->texture_shader_y_uv.vertex_source = vertex_shader;
3024 gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv;
3025
3026 gr->texture_shader_y_u_v.vertex_source = vertex_shader;
3027 gr->texture_shader_y_u_v.fragment_source =
3028 texture_fragment_shader_y_u_v;
3029
Ander Conselvan de Oliveira41a50ea2013-11-27 17:43:51 +02003030 gr->texture_shader_y_xuxv.vertex_source = vertex_shader;
Ander Conselvan de Oliveira1ed73242013-05-17 14:00:40 +03003031 gr->texture_shader_y_xuxv.fragment_source =
3032 texture_fragment_shader_y_xuxv;
3033
3034 gr->solid_shader.vertex_source = vertex_shader;
3035 gr->solid_shader.fragment_source = solid_fragment_shader;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003036
3037 return 0;
3038}
3039
3040static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003041fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time,
3042 uint32_t key, void *data)
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003043{
3044 struct weston_compositor *ec = data;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003045 struct gl_renderer *gr = get_renderer(ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003046 struct weston_output *output;
3047
John Kåre Alsaker40684142012-11-13 19:10:25 +01003048 gr->fragment_shader_debug ^= 1;
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003049
John Kåre Alsaker40684142012-11-13 19:10:25 +01003050 shader_release(&gr->texture_shader_rgba);
3051 shader_release(&gr->texture_shader_rgbx);
3052 shader_release(&gr->texture_shader_egl_external);
3053 shader_release(&gr->texture_shader_y_uv);
3054 shader_release(&gr->texture_shader_y_u_v);
3055 shader_release(&gr->texture_shader_y_xuxv);
3056 shader_release(&gr->solid_shader);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003057
Ander Conselvan de Oliveira03fb4ef2012-12-03 17:08:11 +02003058 /* Force use_shader() to call glUseProgram(), since we need to use
3059 * the recompiled version of the shader. */
3060 gr->current_shader = NULL;
3061
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003062 wl_list_for_each(output, &ec->output_list, link)
3063 weston_output_damage(output);
3064}
3065
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003066static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003067fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time,
3068 uint32_t key, void *data)
Kristian Høgsberg8799d412013-05-07 10:50:09 -04003069{
3070 struct weston_compositor *compositor = data;
3071 struct gl_renderer *gr = get_renderer(compositor);
3072
3073 gr->fan_debug = !gr->fan_debug;
3074 weston_compositor_damage_all(compositor);
3075}
3076
John Kåre Alsaker94659272012-11-13 19:10:18 +01003077static int
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003078gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003079{
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01003080 struct gl_renderer *gr = get_renderer(ec);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003081 const char *extensions;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003082 EGLConfig context_config;
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003083 EGLBoolean ret;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003084
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003085 static const EGLint context_attribs[] = {
3086 EGL_CONTEXT_CLIENT_VERSION, 2,
3087 EGL_NONE
3088 };
3089
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003090 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
3091 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003092 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003093 return -1;
3094 }
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003095
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003096 context_config = gr->egl_config;
Pekka Paalanen9c3fe252012-10-24 09:43:05 +03003097
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003098 if (gr->has_configless_context)
3099 context_config = EGL_NO_CONFIG_MESA;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003100
3101 gr->egl_context = eglCreateContext(gr->egl_display, context_config,
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003102 EGL_NO_CONTEXT, context_attribs);
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003103 if (gr->egl_context == NULL) {
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003104 weston_log("failed to create context\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003105 gl_renderer_print_egl_error_state();
Kristian Høgsberg9793fc72012-09-06 21:07:40 -04003106 return -1;
3107 }
3108
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003109 ret = eglMakeCurrent(gr->egl_display, egl_surface,
3110 egl_surface, gr->egl_context);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003111 if (ret == EGL_FALSE) {
3112 weston_log("Failed to make EGL context current.\n");
Pekka Paalanen326529f2012-11-27 12:25:25 +02003113 gl_renderer_print_egl_error_state();
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003114 return -1;
3115 }
3116
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003117 log_egl_gl_info(gr->egl_display);
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003118
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003119 gr->image_target_texture_2d =
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003120 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003121
3122 extensions = (const char *) glGetString(GL_EXTENSIONS);
3123 if (!extensions) {
3124 weston_log("Retrieving GL extension string failed.\n");
3125 return -1;
3126 }
3127
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003128 if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003129 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
3130 return -1;
3131 }
3132
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003133 if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra"))
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003134 ec->read_format = PIXMAN_a8r8g8b8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003135 else
John Kåre Alsakerf9e710b2012-11-13 19:10:22 +01003136 ec->read_format = PIXMAN_a8b8g8r8;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003137
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003138 if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003139 gr->has_unpack_subimage = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003140
Emil Velikovf0c3a1c2016-07-04 15:34:18 +01003141 if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external"))
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003142 gr->has_egl_image_external = 1;
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003143
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003144 glActiveTexture(GL_TEXTURE0);
3145
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003146 if (compile_shaders(ec))
3147 return -1;
3148
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003149 gr->fragment_binding =
3150 weston_compositor_add_debug_binding(ec, KEY_S,
3151 fragment_debug_binding,
3152 ec);
3153 gr->fan_binding =
3154 weston_compositor_add_debug_binding(ec, KEY_F,
3155 fan_debug_repaint_binding,
3156 ec);
Ander Conselvan de Oliveira27508c22012-11-08 17:20:46 +02003157
Armin Krezović36d699a2016-08-05 15:28:30 +02003158 gr->output_destroy_listener.notify = output_handle_destroy;
3159 wl_signal_add(&ec->output_destroyed_signal,
3160 &gr->output_destroy_listener);
3161
Pekka Paalanen035a0322012-10-24 09:43:06 +03003162 weston_log("GL ES 2 renderer features:\n");
3163 weston_log_continue(STAMP_SPACE "read-back format: %s\n",
Pekka Paalanenfe4eacf2013-01-10 16:50:42 +02003164 ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003165 weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003166 gr->has_unpack_subimage ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003167 weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n",
John Kåre Alsaker320711d2012-11-13 19:10:27 +01003168 gr->has_bind_display ? "yes" : "no");
Pekka Paalanen035a0322012-10-24 09:43:06 +03003169
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01003170
Kristian Høgsberg25894fc2012-09-05 22:06:26 -04003171 return 0;
3172}
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003173
3174WL_EXPORT struct gl_renderer_interface gl_renderer_interface = {
3175 .opaque_attribs = gl_renderer_opaque_attribs,
3176 .alpha_attribs = gl_renderer_alpha_attribs,
3177
3178 .create = gl_renderer_create,
3179 .display = gl_renderer_display,
3180 .output_create = gl_renderer_output_create,
3181 .output_destroy = gl_renderer_output_destroy,
3182 .output_surface = gl_renderer_output_surface,
Jason Ekstrand0b61bf42013-10-27 22:24:54 -05003183 .output_set_border = gl_renderer_output_set_border,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003184 .print_egl_error_state = gl_renderer_print_egl_error_state
3185};