Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
Pekka Paalanen | eb35cbe | 2015-02-09 13:37:27 +0200 | [diff] [blame] | 3 | * Copyright © 2015 Collabora, Ltd. |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 4 | * Copyright © 2016 NVIDIA Corporation |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 5 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 13 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial |
| 16 | * portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 26 | */ |
| 27 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 28 | #include "config.h" |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 29 | |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 30 | #include <GLES2/gl2.h> |
| 31 | #include <GLES2/gl2ext.h> |
| 32 | |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 33 | #include <stdbool.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 34 | #include <stdint.h> |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 35 | #include <stdlib.h> |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <ctype.h> |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 38 | #include <float.h> |
| 39 | #include <assert.h> |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 40 | #include <linux/input.h> |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 41 | #include <drm_fourcc.h> |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 42 | |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 43 | #include "gl-renderer.h" |
Sam Spilsbury | 619859c | 2013-09-13 10:01:21 +0800 | [diff] [blame] | 44 | #include "vertex-clipping.h" |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 45 | #include "linux-dmabuf.h" |
Jonas Ådahl | 57e48f0 | 2015-11-17 16:00:28 +0800 | [diff] [blame] | 46 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 47 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 48 | #include "shared/helpers.h" |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 49 | #include "shared/platform.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 50 | #include "weston-egl-ext.h" |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 51 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 52 | struct gl_shader { |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 53 | GLuint program; |
| 54 | GLuint vertex_shader, fragment_shader; |
| 55 | GLint proj_uniform; |
| 56 | GLint tex_uniforms[3]; |
| 57 | GLint alpha_uniform; |
| 58 | GLint color_uniform; |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 59 | const char *vertex_source, *fragment_source; |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 62 | #define BUFFER_DAMAGE_COUNT 2 |
| 63 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 64 | enum gl_border_status { |
| 65 | BORDER_STATUS_CLEAN = 0, |
| 66 | BORDER_TOP_DIRTY = 1 << GL_RENDERER_BORDER_TOP, |
| 67 | BORDER_LEFT_DIRTY = 1 << GL_RENDERER_BORDER_LEFT, |
| 68 | BORDER_RIGHT_DIRTY = 1 << GL_RENDERER_BORDER_RIGHT, |
| 69 | BORDER_BOTTOM_DIRTY = 1 << GL_RENDERER_BORDER_BOTTOM, |
| 70 | BORDER_ALL_DIRTY = 0xf, |
| 71 | BORDER_SIZE_CHANGED = 0x10 |
| 72 | }; |
| 73 | |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 74 | struct gl_border_image { |
| 75 | GLuint tex; |
| 76 | int32_t width, height; |
| 77 | int32_t tex_width; |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 78 | void *data; |
| 79 | }; |
| 80 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 81 | struct gl_output_state { |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 82 | EGLSurface egl_surface; |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 83 | pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT]; |
Derek Foreman | 4c58266 | 2014-10-09 18:39:44 -0500 | [diff] [blame] | 84 | int buffer_damage_index; |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 85 | enum gl_border_status border_damage[BUFFER_DAMAGE_COUNT]; |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 86 | struct gl_border_image borders[4]; |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 87 | enum gl_border_status border_status; |
Jason Ekstrand | fb23df7 | 2014-10-16 10:55:21 -0500 | [diff] [blame] | 88 | |
| 89 | struct weston_matrix output_matrix; |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Ander Conselvan de Oliveira | a9bf161 | 2013-06-07 16:52:44 +0300 | [diff] [blame] | 92 | enum buffer_type { |
| 93 | BUFFER_TYPE_NULL, |
Pekka Paalanen | aeb917e | 2015-02-09 13:56:56 +0200 | [diff] [blame] | 94 | BUFFER_TYPE_SOLID, /* internal solid color surfaces without a buffer */ |
Ander Conselvan de Oliveira | a9bf161 | 2013-06-07 16:52:44 +0300 | [diff] [blame] | 95 | BUFFER_TYPE_SHM, |
| 96 | BUFFER_TYPE_EGL |
| 97 | }; |
| 98 | |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 99 | struct gl_renderer; |
| 100 | |
| 101 | struct egl_image { |
| 102 | struct gl_renderer *renderer; |
| 103 | EGLImageKHR image; |
| 104 | int refcount; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 105 | }; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 106 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 107 | enum import_type { |
| 108 | IMPORT_TYPE_INVALID, |
| 109 | IMPORT_TYPE_DIRECT, |
| 110 | IMPORT_TYPE_GL_CONVERSION |
| 111 | }; |
| 112 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 113 | struct dmabuf_image { |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 114 | struct linux_dmabuf_buffer *dmabuf; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 115 | int num_images; |
| 116 | struct egl_image *images[3]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 117 | struct wl_list link; |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 118 | |
| 119 | enum import_type import_type; |
| 120 | GLenum target; |
| 121 | struct gl_shader *shader; |
| 122 | }; |
| 123 | |
| 124 | struct yuv_plane_descriptor { |
| 125 | int width_divisor; |
| 126 | int height_divisor; |
| 127 | uint32_t format; |
| 128 | int plane_index; |
| 129 | }; |
| 130 | |
| 131 | struct yuv_format_descriptor { |
| 132 | uint32_t format; |
| 133 | int input_planes; |
| 134 | int output_planes; |
| 135 | int texture_type; |
| 136 | struct yuv_plane_descriptor plane[4]; |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 137 | }; |
| 138 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 139 | struct gl_surface_state { |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 140 | GLfloat color[4]; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 141 | struct gl_shader *shader; |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 142 | |
| 143 | GLuint textures[3]; |
| 144 | int num_textures; |
Derek Foreman | 4c11fe7 | 2015-11-18 16:32:27 -0600 | [diff] [blame] | 145 | bool needs_full_upload; |
Pekka Paalanen | 81ee3f5 | 2012-12-04 15:58:16 +0200 | [diff] [blame] | 146 | pixman_region32_t texture_damage; |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 147 | |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 148 | /* These are only used by SHM surfaces to detect when we need |
| 149 | * to do a full upload to specify a new internal texture |
| 150 | * format */ |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 151 | GLenum gl_format[3]; |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 152 | GLenum gl_pixel_type; |
| 153 | |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 154 | struct egl_image* images[3]; |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 155 | GLenum target; |
| 156 | int num_images; |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 157 | |
| 158 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | a9bf161 | 2013-06-07 16:52:44 +0300 | [diff] [blame] | 159 | enum buffer_type buffer_type; |
Pekka Paalanen | 68033ac | 2012-12-04 15:58:15 +0200 | [diff] [blame] | 160 | int pitch; /* in pixels */ |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 161 | int height; /* in pixels */ |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 162 | int y_inverted; |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 163 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 164 | /* Extension needed for SHM YUV texture */ |
| 165 | int offset[3]; /* offset per plane */ |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 166 | int hsub[3]; /* horizontal subsampling per plane */ |
| 167 | int vsub[3]; /* vertical subsampling per plane */ |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 168 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 169 | struct weston_surface *surface; |
| 170 | |
| 171 | struct wl_listener surface_destroy_listener; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 172 | struct wl_listener renderer_destroy_listener; |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 175 | struct gl_renderer { |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 176 | struct weston_renderer base; |
| 177 | int fragment_shader_debug; |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 178 | int fan_debug; |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 179 | struct weston_binding *fragment_binding; |
| 180 | struct weston_binding *fan_binding; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 181 | |
| 182 | EGLDisplay egl_display; |
| 183 | EGLContext egl_context; |
| 184 | EGLConfig egl_config; |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 185 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 186 | EGLSurface dummy_surface; |
| 187 | |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 188 | struct wl_array vertices; |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 189 | struct wl_array vtxcnt; |
| 190 | |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 191 | PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d; |
| 192 | PFNEGLCREATEIMAGEKHRPROC create_image; |
| 193 | PFNEGLDESTROYIMAGEKHRPROC destroy_image; |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 194 | PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage; |
Jonny Lamb | 671148f | 2015-03-20 15:26:52 +0100 | [diff] [blame] | 195 | PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window; |
Jonny Lamb | 671148f | 2015-03-20 15:26:52 +0100 | [diff] [blame] | 196 | |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 197 | int has_unpack_subimage; |
| 198 | |
| 199 | PFNEGLBINDWAYLANDDISPLAYWL bind_display; |
| 200 | PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display; |
| 201 | PFNEGLQUERYWAYLANDBUFFERWL query_buffer; |
| 202 | int has_bind_display; |
| 203 | |
| 204 | int has_egl_image_external; |
| 205 | |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 206 | int has_egl_buffer_age; |
| 207 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 208 | int has_configless_context; |
| 209 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 210 | int has_surfaceless_context; |
| 211 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 212 | int has_dmabuf_import; |
| 213 | struct wl_list dmabuf_images; |
| 214 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 215 | int has_gl_texture_rg; |
| 216 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 217 | struct gl_shader texture_shader_rgba; |
| 218 | struct gl_shader texture_shader_rgbx; |
| 219 | struct gl_shader texture_shader_egl_external; |
| 220 | struct gl_shader texture_shader_y_uv; |
| 221 | struct gl_shader texture_shader_y_u_v; |
| 222 | struct gl_shader texture_shader_y_xuxv; |
| 223 | struct gl_shader invert_color_shader; |
| 224 | struct gl_shader solid_shader; |
| 225 | struct gl_shader *current_shader; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 226 | |
| 227 | struct wl_signal destroy_signal; |
Armin Krezović | 36d699a | 2016-08-05 15:28:30 +0200 | [diff] [blame] | 228 | |
| 229 | struct wl_listener output_destroy_listener; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 230 | }; |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 231 | |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 232 | static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL; |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 233 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 234 | static inline const char * |
| 235 | dump_format(uint32_t format, char out[4]) |
| 236 | { |
| 237 | #if BYTE_ORDER == BIG_ENDIAN |
| 238 | format = __builtin_bswap32(format); |
| 239 | #endif |
| 240 | memcpy(out, &format, 4); |
| 241 | return out; |
| 242 | } |
| 243 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 244 | static inline struct gl_output_state * |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 245 | get_output_state(struct weston_output *output) |
| 246 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 247 | return (struct gl_output_state *)output->renderer_state; |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 250 | static int |
| 251 | gl_renderer_create_surface(struct weston_surface *surface); |
| 252 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 253 | static inline struct gl_surface_state * |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 254 | get_surface_state(struct weston_surface *surface) |
| 255 | { |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 256 | if (!surface->renderer_state) |
| 257 | gl_renderer_create_surface(surface); |
| 258 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 259 | return (struct gl_surface_state *)surface->renderer_state; |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 260 | } |
| 261 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 262 | static inline struct gl_renderer * |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 263 | get_renderer(struct weston_compositor *ec) |
| 264 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 265 | return (struct gl_renderer *)ec->renderer; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 268 | static struct egl_image* |
| 269 | egl_image_create(struct gl_renderer *gr, EGLenum target, |
| 270 | EGLClientBuffer buffer, const EGLint *attribs) |
| 271 | { |
| 272 | struct egl_image *img; |
| 273 | |
| 274 | img = zalloc(sizeof *img); |
| 275 | img->renderer = gr; |
| 276 | img->refcount = 1; |
| 277 | img->image = gr->create_image(gr->egl_display, EGL_NO_CONTEXT, |
| 278 | target, buffer, attribs); |
| 279 | |
| 280 | if (img->image == EGL_NO_IMAGE_KHR) { |
| 281 | free(img); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | return img; |
| 286 | } |
| 287 | |
| 288 | static struct egl_image* |
| 289 | egl_image_ref(struct egl_image *image) |
| 290 | { |
| 291 | image->refcount++; |
| 292 | |
| 293 | return image; |
| 294 | } |
| 295 | |
| 296 | static int |
| 297 | egl_image_unref(struct egl_image *image) |
| 298 | { |
| 299 | struct gl_renderer *gr = image->renderer; |
| 300 | |
| 301 | assert(image->refcount > 0); |
| 302 | |
| 303 | image->refcount--; |
| 304 | if (image->refcount > 0) |
| 305 | return image->refcount; |
| 306 | |
| 307 | gr->destroy_image(gr->egl_display, image->image); |
| 308 | free(image); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 313 | static struct dmabuf_image* |
| 314 | dmabuf_image_create(void) |
| 315 | { |
| 316 | struct dmabuf_image *img; |
| 317 | |
| 318 | img = zalloc(sizeof *img); |
| 319 | wl_list_init(&img->link); |
| 320 | |
| 321 | return img; |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | dmabuf_image_destroy(struct dmabuf_image *image) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < image->num_images; ++i) |
| 330 | egl_image_unref(image->images[i]); |
| 331 | |
| 332 | if (image->dmabuf) |
| 333 | linux_dmabuf_buffer_set_user_data(image->dmabuf, NULL, NULL); |
| 334 | |
| 335 | wl_list_remove(&image->link); |
| 336 | } |
| 337 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 338 | static const char * |
| 339 | egl_error_string(EGLint code) |
| 340 | { |
| 341 | #define MYERRCODE(x) case x: return #x; |
| 342 | switch (code) { |
| 343 | MYERRCODE(EGL_SUCCESS) |
| 344 | MYERRCODE(EGL_NOT_INITIALIZED) |
| 345 | MYERRCODE(EGL_BAD_ACCESS) |
| 346 | MYERRCODE(EGL_BAD_ALLOC) |
| 347 | MYERRCODE(EGL_BAD_ATTRIBUTE) |
| 348 | MYERRCODE(EGL_BAD_CONTEXT) |
| 349 | MYERRCODE(EGL_BAD_CONFIG) |
| 350 | MYERRCODE(EGL_BAD_CURRENT_SURFACE) |
| 351 | MYERRCODE(EGL_BAD_DISPLAY) |
| 352 | MYERRCODE(EGL_BAD_SURFACE) |
| 353 | MYERRCODE(EGL_BAD_MATCH) |
| 354 | MYERRCODE(EGL_BAD_PARAMETER) |
| 355 | MYERRCODE(EGL_BAD_NATIVE_PIXMAP) |
| 356 | MYERRCODE(EGL_BAD_NATIVE_WINDOW) |
| 357 | MYERRCODE(EGL_CONTEXT_LOST) |
| 358 | default: |
| 359 | return "unknown"; |
| 360 | } |
| 361 | #undef MYERRCODE |
| 362 | } |
| 363 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 364 | static void |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 365 | gl_renderer_print_egl_error_state(void) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 366 | { |
| 367 | EGLint code; |
| 368 | |
| 369 | code = eglGetError(); |
| 370 | weston_log("EGL error state: %s (0x%04lx)\n", |
| 371 | egl_error_string(code), (long)code); |
| 372 | } |
| 373 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 374 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 375 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 376 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 377 | /* |
| 378 | * Compute the boundary vertices of the intersection of the global coordinate |
| 379 | * aligned rectangle 'rect', and an arbitrary quadrilateral produced from |
| 380 | * 'surf_rect' when transformed from surface coordinates into global coordinates. |
| 381 | * The vertices are written to 'ex' and 'ey', and the return value is the |
| 382 | * number of vertices. Vertices are produced in clockwise winding order. |
| 383 | * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero |
| 384 | * polygon area. |
| 385 | */ |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 386 | static int |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 387 | calculate_edges(struct weston_view *ev, pixman_box32_t *rect, |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 388 | pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey) |
| 389 | { |
Sam Spilsbury | 619859c | 2013-09-13 10:01:21 +0800 | [diff] [blame] | 390 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 391 | struct clip_context ctx; |
| 392 | int i, n; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 393 | GLfloat min_x, max_x, min_y, max_y; |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 394 | struct polygon8 surf = { |
| 395 | { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 }, |
| 396 | { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 }, |
| 397 | 4 |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 398 | }; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 399 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 400 | ctx.clip.x1 = rect->x1; |
| 401 | ctx.clip.y1 = rect->y1; |
| 402 | ctx.clip.x2 = rect->x2; |
| 403 | ctx.clip.y2 = rect->y2; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 404 | |
| 405 | /* transform surface to screen space: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 406 | for (i = 0; i < surf.n; i++) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 407 | weston_view_to_global_float(ev, surf.x[i], surf.y[i], |
| 408 | &surf.x[i], &surf.y[i]); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 409 | |
| 410 | /* find bounding box: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 411 | min_x = max_x = surf.x[0]; |
| 412 | min_y = max_y = surf.y[0]; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 413 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 414 | for (i = 1; i < surf.n; i++) { |
| 415 | min_x = min(min_x, surf.x[i]); |
| 416 | max_x = max(max_x, surf.x[i]); |
| 417 | min_y = min(min_y, surf.y[i]); |
| 418 | max_y = max(max_y, surf.y[i]); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* First, simple bounding box check to discard early transformed |
| 422 | * surface rects that do not intersect with the clip region: |
| 423 | */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 424 | if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) || |
| 425 | (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1)) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 426 | return 0; |
| 427 | |
| 428 | /* Simple case, bounding box edges are parallel to surface edges, |
| 429 | * there will be only four edges. We just need to clip the surface |
| 430 | * vertices to the clip rect bounds: |
| 431 | */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 432 | if (!ev->transform.enabled) |
Sam Spilsbury | 619859c | 2013-09-13 10:01:21 +0800 | [diff] [blame] | 433 | return clip_simple(&ctx, &surf, ex, ey); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 434 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 435 | /* Transformed case: use a general polygon clipping algorithm to |
| 436 | * clip the surface rectangle with each side of 'rect'. |
| 437 | * The algorithm is Sutherland-Hodgman, as explained in |
| 438 | * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm |
| 439 | * but without looking at any of that code. |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 440 | */ |
Sam Spilsbury | 619859c | 2013-09-13 10:01:21 +0800 | [diff] [blame] | 441 | n = clip_transformed(&ctx, &surf, ex, ey); |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 442 | |
| 443 | if (n < 3) |
| 444 | return 0; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 445 | |
| 446 | return n; |
| 447 | } |
| 448 | |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 449 | static bool |
| 450 | merge_down(pixman_box32_t *a, pixman_box32_t *b, pixman_box32_t *merge) |
| 451 | { |
| 452 | if (a->x1 == b->x1 && a->x2 == b->x2 && a->y1 == b->y2) { |
| 453 | merge->x1 = a->x1; |
| 454 | merge->x2 = a->x2; |
| 455 | merge->y1 = b->y1; |
| 456 | merge->y2 = a->y2; |
| 457 | return true; |
| 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | static int |
| 463 | compress_bands(pixman_box32_t *inrects, int nrects, |
| 464 | pixman_box32_t **outrects) |
| 465 | { |
Quentin Glidic | d84af7c | 2016-07-10 11:00:50 +0200 | [diff] [blame] | 466 | bool merged = false; |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 467 | pixman_box32_t *out, merge_rect; |
| 468 | int i, j, nout; |
| 469 | |
| 470 | if (!nrects) { |
| 471 | *outrects = NULL; |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /* nrects is an upper bound - we're not too worried about |
| 476 | * allocating a little extra |
| 477 | */ |
| 478 | out = malloc(sizeof(pixman_box32_t) * nrects); |
| 479 | out[0] = inrects[0]; |
| 480 | nout = 1; |
| 481 | for (i = 1; i < nrects; i++) { |
| 482 | for (j = 0; j < nout; j++) { |
| 483 | merged = merge_down(&inrects[i], &out[j], &merge_rect); |
| 484 | if (merged) { |
| 485 | out[j] = merge_rect; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | if (!merged) { |
| 490 | out[nout] = inrects[i]; |
| 491 | nout++; |
| 492 | } |
| 493 | } |
| 494 | *outrects = out; |
| 495 | return nout; |
| 496 | } |
| 497 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 498 | static int |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 499 | texture_region(struct weston_view *ev, pixman_region32_t *region, |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 500 | pixman_region32_t *surf_region) |
| 501 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 502 | struct gl_surface_state *gs = get_surface_state(ev->surface); |
| 503 | struct weston_compositor *ec = ev->surface->compositor; |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 504 | struct gl_renderer *gr = get_renderer(ec); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 505 | GLfloat *v, inv_width, inv_height; |
| 506 | unsigned int *vtxcnt, nvtx = 0; |
| 507 | pixman_box32_t *rects, *surf_rects; |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 508 | pixman_box32_t *raw_rects; |
| 509 | int i, j, k, nrects, nsurf, raw_nrects; |
| 510 | bool used_band_compression; |
| 511 | raw_rects = pixman_region32_rectangles(region, &raw_nrects); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 512 | surf_rects = pixman_region32_rectangles(surf_region, &nsurf); |
| 513 | |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 514 | if (raw_nrects < 4) { |
| 515 | used_band_compression = false; |
| 516 | nrects = raw_nrects; |
| 517 | rects = raw_rects; |
| 518 | } else { |
| 519 | nrects = compress_bands(raw_rects, raw_nrects, &rects); |
| 520 | used_band_compression = true; |
| 521 | } |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 522 | /* worst case we can have 8 vertices per rect (ie. clipped into |
| 523 | * an octagon): |
| 524 | */ |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 525 | v = wl_array_add(&gr->vertices, nrects * nsurf * 8 * 4 * sizeof *v); |
| 526 | vtxcnt = wl_array_add(&gr->vtxcnt, nrects * nsurf * sizeof *vtxcnt); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 527 | |
Pekka Paalanen | 68033ac | 2012-12-04 15:58:15 +0200 | [diff] [blame] | 528 | inv_width = 1.0 / gs->pitch; |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 529 | inv_height = 1.0 / gs->height; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 530 | |
| 531 | for (i = 0; i < nrects; i++) { |
| 532 | pixman_box32_t *rect = &rects[i]; |
| 533 | for (j = 0; j < nsurf; j++) { |
| 534 | pixman_box32_t *surf_rect = &surf_rects[j]; |
Ander Conselvan de Oliveira | 0396ba2 | 2012-11-28 17:10:26 +0200 | [diff] [blame] | 535 | GLfloat sx, sy, bx, by; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 536 | GLfloat ex[8], ey[8]; /* edge points in screen space */ |
| 537 | int n; |
| 538 | |
| 539 | /* The transformed surface, after clipping to the clip region, |
| 540 | * can have as many as eight sides, emitted as a triangle-fan. |
| 541 | * The first vertex in the triangle fan can be chosen arbitrarily, |
| 542 | * since the area is guaranteed to be convex. |
| 543 | * |
| 544 | * If a corner of the transformed surface falls outside of the |
| 545 | * clip region, instead of emitting one vertex for the corner |
| 546 | * of the surface, up to two are emitted for two corresponding |
| 547 | * intersection point(s) between the surface and the clip region. |
| 548 | * |
| 549 | * To do this, we first calculate the (up to eight) points that |
| 550 | * form the intersection of the clip rect and the transformed |
| 551 | * surface. |
| 552 | */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 553 | n = calculate_edges(ev, rect, surf_rect, ex, ey); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 554 | if (n < 3) |
| 555 | continue; |
| 556 | |
| 557 | /* emit edge points: */ |
| 558 | for (k = 0; k < n; k++) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 559 | weston_view_from_global_float(ev, ex[k], ey[k], |
| 560 | &sx, &sy); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 561 | /* position: */ |
| 562 | *(v++) = ex[k]; |
| 563 | *(v++) = ey[k]; |
| 564 | /* texcoord: */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 565 | weston_surface_to_buffer_float(ev->surface, |
| 566 | sx, sy, |
Ander Conselvan de Oliveira | 0396ba2 | 2012-11-28 17:10:26 +0200 | [diff] [blame] | 567 | &bx, &by); |
| 568 | *(v++) = bx * inv_width; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 569 | if (gs->y_inverted) { |
| 570 | *(v++) = by * inv_height; |
| 571 | } else { |
| 572 | *(v++) = (gs->height - by) * inv_height; |
| 573 | } |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | vtxcnt[nvtx++] = n; |
| 577 | } |
| 578 | } |
| 579 | |
Derek Foreman | f818098 | 2014-10-16 16:37:02 -0500 | [diff] [blame] | 580 | if (used_band_compression) |
| 581 | free(rects); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 582 | return nvtx; |
| 583 | } |
| 584 | |
| 585 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 586 | triangle_fan_debug(struct weston_view *view, int first, int count) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 587 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 588 | struct weston_compositor *compositor = view->surface->compositor; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 589 | struct gl_renderer *gr = get_renderer(compositor); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 590 | int i; |
| 591 | GLushort *buffer; |
| 592 | GLushort *index; |
| 593 | int nelems; |
| 594 | static int color_idx = 0; |
| 595 | static const GLfloat color[][4] = { |
| 596 | { 1.0, 0.0, 0.0, 1.0 }, |
| 597 | { 0.0, 1.0, 0.0, 1.0 }, |
| 598 | { 0.0, 0.0, 1.0, 1.0 }, |
| 599 | { 1.0, 1.0, 1.0, 1.0 }, |
| 600 | }; |
| 601 | |
| 602 | nelems = (count - 1 + count - 2) * 2; |
| 603 | |
| 604 | buffer = malloc(sizeof(GLushort) * nelems); |
| 605 | index = buffer; |
| 606 | |
| 607 | for (i = 1; i < count; i++) { |
| 608 | *index++ = first; |
| 609 | *index++ = first + i; |
| 610 | } |
| 611 | |
| 612 | for (i = 2; i < count; i++) { |
| 613 | *index++ = first + i - 1; |
| 614 | *index++ = first + i; |
| 615 | } |
| 616 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 617 | glUseProgram(gr->solid_shader.program); |
| 618 | glUniform4fv(gr->solid_shader.color_uniform, 1, |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 619 | color[color_idx++ % ARRAY_LENGTH(color)]); |
| 620 | glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer); |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 621 | glUseProgram(gr->current_shader->program); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 622 | free(buffer); |
| 623 | } |
| 624 | |
| 625 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 626 | repaint_region(struct weston_view *ev, pixman_region32_t *region, |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 627 | pixman_region32_t *surf_region) |
| 628 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 629 | struct weston_compositor *ec = ev->surface->compositor; |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 630 | struct gl_renderer *gr = get_renderer(ec); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 631 | GLfloat *v; |
| 632 | unsigned int *vtxcnt; |
| 633 | int i, first, nfans; |
| 634 | |
| 635 | /* The final region to be painted is the intersection of |
| 636 | * 'region' and 'surf_region'. However, 'region' is in the global |
| 637 | * coordinates, and 'surf_region' is in the surface-local |
| 638 | * coordinates. texture_region() will iterate over all pairs of |
| 639 | * rectangles from both regions, compute the intersection |
| 640 | * polygon for each pair, and store it as a triangle fan if |
Bryce Harrington | ea8fb94 | 2016-01-13 18:48:56 -0800 | [diff] [blame] | 641 | * it has a non-zero area (at least 3 vertices, actually). |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 642 | */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 643 | nfans = texture_region(ev, region, surf_region); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 644 | |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 645 | v = gr->vertices.data; |
| 646 | vtxcnt = gr->vtxcnt.data; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 647 | |
| 648 | /* position: */ |
| 649 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 650 | glEnableVertexAttribArray(0); |
| 651 | |
| 652 | /* texcoord: */ |
| 653 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
| 654 | glEnableVertexAttribArray(1); |
| 655 | |
| 656 | for (i = 0, first = 0; i < nfans; i++) { |
| 657 | glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]); |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 658 | if (gr->fan_debug) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 659 | triangle_fan_debug(ev, first, vtxcnt[i]); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 660 | first += vtxcnt[i]; |
| 661 | } |
| 662 | |
| 663 | glDisableVertexAttribArray(1); |
| 664 | glDisableVertexAttribArray(0); |
| 665 | |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 666 | gr->vertices.size = 0; |
| 667 | gr->vtxcnt.size = 0; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 668 | } |
| 669 | |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 670 | static int |
| 671 | use_output(struct weston_output *output) |
| 672 | { |
| 673 | static int errored; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 674 | struct gl_output_state *go = get_output_state(output); |
| 675 | struct gl_renderer *gr = get_renderer(output->compositor); |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 676 | EGLBoolean ret; |
| 677 | |
| 678 | ret = eglMakeCurrent(gr->egl_display, go->egl_surface, |
| 679 | go->egl_surface, gr->egl_context); |
| 680 | |
| 681 | if (ret == EGL_FALSE) { |
| 682 | if (errored) |
| 683 | return -1; |
| 684 | errored = 1; |
| 685 | weston_log("Failed to make EGL context current.\n"); |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 686 | gl_renderer_print_egl_error_state(); |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 693 | static int |
| 694 | shader_init(struct gl_shader *shader, struct gl_renderer *gr, |
| 695 | const char *vertex_source, const char *fragment_source); |
| 696 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 697 | static void |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 698 | use_shader(struct gl_renderer *gr, struct gl_shader *shader) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 699 | { |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 700 | if (!shader->program) { |
| 701 | int ret; |
| 702 | |
| 703 | ret = shader_init(shader, gr, |
| 704 | shader->vertex_source, |
| 705 | shader->fragment_source); |
| 706 | |
| 707 | if (ret < 0) |
| 708 | weston_log("warning: failed to compile shader\n"); |
| 709 | } |
| 710 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 711 | if (gr->current_shader == shader) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 712 | return; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 713 | glUseProgram(shader->program); |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 714 | gr->current_shader = shader; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 718 | shader_uniforms(struct gl_shader *shader, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 719 | struct weston_view *view, |
| 720 | struct weston_output *output) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 721 | { |
| 722 | int i; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 723 | struct gl_surface_state *gs = get_surface_state(view->surface); |
Jason Ekstrand | fb23df7 | 2014-10-16 10:55:21 -0500 | [diff] [blame] | 724 | struct gl_output_state *go = get_output_state(output); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 725 | |
| 726 | glUniformMatrix4fv(shader->proj_uniform, |
Jason Ekstrand | fb23df7 | 2014-10-16 10:55:21 -0500 | [diff] [blame] | 727 | 1, GL_FALSE, go->output_matrix.d); |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 728 | glUniform4fv(shader->color_uniform, 1, gs->color); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 729 | glUniform1f(shader->alpha_uniform, view->alpha); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 730 | |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 731 | for (i = 0; i < gs->num_textures; i++) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 732 | glUniform1i(shader->tex_uniforms[i], i); |
| 733 | } |
| 734 | |
| 735 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 736 | draw_view(struct weston_view *ev, struct weston_output *output, |
| 737 | pixman_region32_t *damage) /* in global coordinates */ |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 738 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 739 | struct weston_compositor *ec = ev->surface->compositor; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 740 | struct gl_renderer *gr = get_renderer(ec); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 741 | struct gl_surface_state *gs = get_surface_state(ev->surface); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 742 | /* repaint bounding region in global coordinates: */ |
| 743 | pixman_region32_t repaint; |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 744 | /* opaque region in surface coordinates: */ |
| 745 | pixman_region32_t surface_opaque; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 746 | /* non-opaque region in surface coordinates: */ |
| 747 | pixman_region32_t surface_blend; |
| 748 | GLint filter; |
| 749 | int i; |
| 750 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 751 | /* In case of a runtime switch of renderers, we may not have received |
| 752 | * an attach for this surface since the switch. In that case we don't |
| 753 | * have a valid buffer or a proper shader set up so skip rendering. */ |
| 754 | if (!gs->shader) |
| 755 | return; |
| 756 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 757 | pixman_region32_init(&repaint); |
| 758 | pixman_region32_intersect(&repaint, |
Pekka Paalanen | 25c0ca5 | 2015-02-19 11:15:33 +0200 | [diff] [blame] | 759 | &ev->transform.boundingbox, damage); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 760 | pixman_region32_subtract(&repaint, &repaint, &ev->clip); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 761 | |
| 762 | if (!pixman_region32_not_empty(&repaint)) |
| 763 | goto out; |
| 764 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 765 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 766 | |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 767 | if (gr->fan_debug) { |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 768 | use_shader(gr, &gr->solid_shader); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 769 | shader_uniforms(&gr->solid_shader, ev, output); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 770 | } |
| 771 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 772 | use_shader(gr, gs->shader); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 773 | shader_uniforms(gs->shader, ev, output); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 774 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 775 | if (ev->transform.enabled || output->zoom.active || |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 776 | output->current_scale != ev->surface->buffer_viewport.buffer.scale) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 777 | filter = GL_LINEAR; |
| 778 | else |
| 779 | filter = GL_NEAREST; |
| 780 | |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 781 | for (i = 0; i < gs->num_textures; i++) { |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 782 | glActiveTexture(GL_TEXTURE0 + i); |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 783 | glBindTexture(gs->target, gs->textures[i]); |
| 784 | glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, filter); |
| 785 | glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, filter); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* blended region is whole surface minus opaque region: */ |
| 789 | pixman_region32_init_rect(&surface_blend, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 790 | ev->surface->width, ev->surface->height); |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 791 | if (ev->geometry.scissor_enabled) |
| 792 | pixman_region32_intersect(&surface_blend, &surface_blend, |
| 793 | &ev->geometry.scissor); |
| 794 | pixman_region32_subtract(&surface_blend, &surface_blend, |
| 795 | &ev->surface->opaque); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 796 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 797 | /* XXX: Should we be using ev->transform.opaque here? */ |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 798 | pixman_region32_init(&surface_opaque); |
| 799 | if (ev->geometry.scissor_enabled) |
| 800 | pixman_region32_intersect(&surface_opaque, |
| 801 | &ev->surface->opaque, |
| 802 | &ev->geometry.scissor); |
| 803 | else |
| 804 | pixman_region32_copy(&surface_opaque, &ev->surface->opaque); |
| 805 | |
| 806 | if (pixman_region32_not_empty(&surface_opaque)) { |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 807 | if (gs->shader == &gr->texture_shader_rgba) { |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 808 | /* Special case for RGBA textures with possibly |
| 809 | * bad data in alpha channel: use the shader |
| 810 | * that forces texture alpha = 1.0. |
| 811 | * Xwayland surfaces need this. |
| 812 | */ |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 813 | use_shader(gr, &gr->texture_shader_rgbx); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 814 | shader_uniforms(&gr->texture_shader_rgbx, ev, output); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 815 | } |
| 816 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 817 | if (ev->alpha < 1.0) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 818 | glEnable(GL_BLEND); |
| 819 | else |
| 820 | glDisable(GL_BLEND); |
| 821 | |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 822 | repaint_region(ev, &repaint, &surface_opaque); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | if (pixman_region32_not_empty(&surface_blend)) { |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 826 | use_shader(gr, gs->shader); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 827 | glEnable(GL_BLEND); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 828 | repaint_region(ev, &repaint, &surface_blend); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | pixman_region32_fini(&surface_blend); |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 832 | pixman_region32_fini(&surface_opaque); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 833 | |
| 834 | out: |
| 835 | pixman_region32_fini(&repaint); |
| 836 | } |
| 837 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 838 | static void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 839 | repaint_views(struct weston_output *output, pixman_region32_t *damage) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 840 | { |
| 841 | struct weston_compositor *compositor = output->compositor; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 842 | struct weston_view *view; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 843 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 844 | wl_list_for_each_reverse(view, &compositor->view_list, link) |
| 845 | if (view->plane == &compositor->primary_plane) |
| 846 | draw_view(view, output, damage); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 849 | static void |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 850 | draw_output_border_texture(struct gl_output_state *go, |
| 851 | enum gl_renderer_border_side side, |
| 852 | int32_t x, int32_t y, |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 853 | int32_t width, int32_t height) |
| 854 | { |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 855 | struct gl_border_image *img = &go->borders[side]; |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 856 | static GLushort indices [] = { 0, 1, 3, 3, 1, 2 }; |
| 857 | |
| 858 | if (!img->data) { |
| 859 | if (img->tex) { |
| 860 | glDeleteTextures(1, &img->tex); |
| 861 | img->tex = 0; |
| 862 | } |
| 863 | |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | if (!img->tex) { |
| 868 | glGenTextures(1, &img->tex); |
| 869 | glBindTexture(GL_TEXTURE_2D, img->tex); |
| 870 | |
| 871 | glTexParameteri(GL_TEXTURE_2D, |
| 872 | GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 873 | glTexParameteri(GL_TEXTURE_2D, |
| 874 | GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 875 | glTexParameteri(GL_TEXTURE_2D, |
| 876 | GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 877 | glTexParameteri(GL_TEXTURE_2D, |
| 878 | GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 879 | } else { |
| 880 | glBindTexture(GL_TEXTURE_2D, img->tex); |
| 881 | } |
| 882 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 883 | if (go->border_status & (1 << side)) { |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 884 | glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); |
| 885 | glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); |
| 886 | glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 887 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 888 | img->tex_width, img->height, 0, |
| 889 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->data); |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | GLfloat texcoord[] = { |
| 893 | 0.0f, 0.0f, |
| 894 | (GLfloat)img->width / (GLfloat)img->tex_width, 0.0f, |
| 895 | (GLfloat)img->width / (GLfloat)img->tex_width, 1.0f, |
| 896 | 0.0f, 1.0f, |
| 897 | }; |
| 898 | |
| 899 | GLfloat verts[] = { |
| 900 | x, y, |
| 901 | x + width, y, |
| 902 | x + width, y + height, |
| 903 | x, y + height |
| 904 | }; |
| 905 | |
| 906 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 907 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord); |
| 908 | glEnableVertexAttribArray(0); |
| 909 | glEnableVertexAttribArray(1); |
| 910 | |
| 911 | glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); |
| 912 | |
| 913 | glDisableVertexAttribArray(1); |
| 914 | glDisableVertexAttribArray(0); |
| 915 | } |
| 916 | |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 917 | static int |
| 918 | output_has_borders(struct weston_output *output) |
| 919 | { |
| 920 | struct gl_output_state *go = get_output_state(output); |
| 921 | |
| 922 | return go->borders[GL_RENDERER_BORDER_TOP].data || |
| 923 | go->borders[GL_RENDERER_BORDER_RIGHT].data || |
| 924 | go->borders[GL_RENDERER_BORDER_BOTTOM].data || |
| 925 | go->borders[GL_RENDERER_BORDER_LEFT].data; |
| 926 | } |
| 927 | |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 928 | static void |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 929 | draw_output_borders(struct weston_output *output, |
| 930 | enum gl_border_status border_status) |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 931 | { |
| 932 | struct gl_output_state *go = get_output_state(output); |
| 933 | struct gl_renderer *gr = get_renderer(output->compositor); |
| 934 | struct gl_shader *shader = &gr->texture_shader_rgba; |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 935 | struct gl_border_image *top, *bottom, *left, *right; |
| 936 | struct weston_matrix matrix; |
| 937 | int full_width, full_height; |
| 938 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 939 | if (border_status == BORDER_STATUS_CLEAN) |
| 940 | return; /* Clean. Nothing to do. */ |
| 941 | |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 942 | top = &go->borders[GL_RENDERER_BORDER_TOP]; |
| 943 | bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM]; |
| 944 | left = &go->borders[GL_RENDERER_BORDER_LEFT]; |
| 945 | right = &go->borders[GL_RENDERER_BORDER_RIGHT]; |
| 946 | |
| 947 | full_width = output->current_mode->width + left->width + right->width; |
| 948 | full_height = output->current_mode->height + top->height + bottom->height; |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 949 | |
| 950 | glDisable(GL_BLEND); |
| 951 | use_shader(gr, shader); |
| 952 | |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 953 | glViewport(0, 0, full_width, full_height); |
| 954 | |
| 955 | weston_matrix_init(&matrix); |
| 956 | weston_matrix_translate(&matrix, -full_width/2.0, -full_height/2.0, 0); |
| 957 | weston_matrix_scale(&matrix, 2.0/full_width, -2.0/full_height, 1); |
| 958 | glUniformMatrix4fv(shader->proj_uniform, 1, GL_FALSE, matrix.d); |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 959 | |
| 960 | glUniform1i(shader->tex_uniforms[0], 0); |
| 961 | glUniform1f(shader->alpha_uniform, 1); |
| 962 | glActiveTexture(GL_TEXTURE0); |
| 963 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 964 | if (border_status & BORDER_TOP_DIRTY) |
| 965 | draw_output_border_texture(go, GL_RENDERER_BORDER_TOP, |
| 966 | 0, 0, |
| 967 | full_width, top->height); |
| 968 | if (border_status & BORDER_LEFT_DIRTY) |
| 969 | draw_output_border_texture(go, GL_RENDERER_BORDER_LEFT, |
| 970 | 0, top->height, |
| 971 | left->width, output->current_mode->height); |
| 972 | if (border_status & BORDER_RIGHT_DIRTY) |
| 973 | draw_output_border_texture(go, GL_RENDERER_BORDER_RIGHT, |
| 974 | full_width - right->width, top->height, |
| 975 | right->width, output->current_mode->height); |
| 976 | if (border_status & BORDER_BOTTOM_DIRTY) |
| 977 | draw_output_border_texture(go, GL_RENDERER_BORDER_BOTTOM, |
| 978 | 0, full_height - bottom->height, |
| 979 | full_width, bottom->height); |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 980 | } |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 981 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 982 | static void |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 983 | output_get_border_damage(struct weston_output *output, |
| 984 | enum gl_border_status border_status, |
| 985 | pixman_region32_t *damage) |
| 986 | { |
| 987 | struct gl_output_state *go = get_output_state(output); |
| 988 | struct gl_border_image *top, *bottom, *left, *right; |
| 989 | int full_width, full_height; |
| 990 | |
| 991 | if (border_status == BORDER_STATUS_CLEAN) |
| 992 | return; /* Clean. Nothing to do. */ |
| 993 | |
| 994 | top = &go->borders[GL_RENDERER_BORDER_TOP]; |
| 995 | bottom = &go->borders[GL_RENDERER_BORDER_BOTTOM]; |
| 996 | left = &go->borders[GL_RENDERER_BORDER_LEFT]; |
| 997 | right = &go->borders[GL_RENDERER_BORDER_RIGHT]; |
| 998 | |
| 999 | full_width = output->current_mode->width + left->width + right->width; |
| 1000 | full_height = output->current_mode->height + top->height + bottom->height; |
| 1001 | if (border_status & BORDER_TOP_DIRTY) |
| 1002 | pixman_region32_union_rect(damage, damage, |
| 1003 | 0, 0, |
| 1004 | full_width, top->height); |
| 1005 | if (border_status & BORDER_LEFT_DIRTY) |
| 1006 | pixman_region32_union_rect(damage, damage, |
| 1007 | 0, top->height, |
| 1008 | left->width, output->current_mode->height); |
| 1009 | if (border_status & BORDER_RIGHT_DIRTY) |
| 1010 | pixman_region32_union_rect(damage, damage, |
| 1011 | full_width - right->width, top->height, |
| 1012 | right->width, output->current_mode->height); |
| 1013 | if (border_status & BORDER_BOTTOM_DIRTY) |
| 1014 | pixman_region32_union_rect(damage, damage, |
| 1015 | 0, full_height - bottom->height, |
| 1016 | full_width, bottom->height); |
| 1017 | } |
| 1018 | |
| 1019 | static void |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1020 | output_get_damage(struct weston_output *output, |
| 1021 | pixman_region32_t *buffer_damage, uint32_t *border_damage) |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1022 | { |
| 1023 | struct gl_output_state *go = get_output_state(output); |
| 1024 | struct gl_renderer *gr = get_renderer(output->compositor); |
| 1025 | EGLint buffer_age = 0; |
| 1026 | EGLBoolean ret; |
| 1027 | int i; |
| 1028 | |
| 1029 | if (gr->has_egl_buffer_age) { |
| 1030 | ret = eglQuerySurface(gr->egl_display, go->egl_surface, |
| 1031 | EGL_BUFFER_AGE_EXT, &buffer_age); |
| 1032 | if (ret == EGL_FALSE) { |
| 1033 | weston_log("buffer age query failed.\n"); |
| 1034 | gl_renderer_print_egl_error_state(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1038 | if (buffer_age == 0 || buffer_age - 1 > BUFFER_DAMAGE_COUNT) { |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1039 | pixman_region32_copy(buffer_damage, &output->region); |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1040 | *border_damage = BORDER_ALL_DIRTY; |
| 1041 | } else { |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1042 | for (i = 0; i < buffer_age - 1; i++) |
Derek Foreman | 4c58266 | 2014-10-09 18:39:44 -0500 | [diff] [blame] | 1043 | *border_damage |= go->border_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]; |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1044 | |
| 1045 | if (*border_damage & BORDER_SIZE_CHANGED) { |
| 1046 | /* If we've had a resize, we have to do a full |
| 1047 | * repaint. */ |
| 1048 | *border_damage |= BORDER_ALL_DIRTY; |
| 1049 | pixman_region32_copy(buffer_damage, &output->region); |
| 1050 | } else { |
| 1051 | for (i = 0; i < buffer_age - 1; i++) |
| 1052 | pixman_region32_union(buffer_damage, |
| 1053 | buffer_damage, |
Derek Foreman | 4c58266 | 2014-10-09 18:39:44 -0500 | [diff] [blame] | 1054 | &go->buffer_damage[(go->buffer_damage_index + i) % BUFFER_DAMAGE_COUNT]); |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1055 | } |
| 1056 | } |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static void |
| 1060 | output_rotate_damage(struct weston_output *output, |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1061 | pixman_region32_t *output_damage, |
| 1062 | enum gl_border_status border_status) |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1063 | { |
| 1064 | struct gl_output_state *go = get_output_state(output); |
| 1065 | struct gl_renderer *gr = get_renderer(output->compositor); |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1066 | |
| 1067 | if (!gr->has_egl_buffer_age) |
| 1068 | return; |
| 1069 | |
Derek Foreman | 4c58266 | 2014-10-09 18:39:44 -0500 | [diff] [blame] | 1070 | go->buffer_damage_index += BUFFER_DAMAGE_COUNT - 1; |
| 1071 | go->buffer_damage_index %= BUFFER_DAMAGE_COUNT; |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1072 | |
Derek Foreman | 4c58266 | 2014-10-09 18:39:44 -0500 | [diff] [blame] | 1073 | pixman_region32_copy(&go->buffer_damage[go->buffer_damage_index], output_damage); |
| 1074 | go->border_damage[go->buffer_damage_index] = border_status; |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1077 | /* NOTE: We now allow falling back to ARGB gl visuals when XRGB is |
| 1078 | * unavailable, so we're assuming the background has no transparency |
| 1079 | * and that everything with a blend, like drop shadows, will have something |
| 1080 | * opaque (like the background) drawn underneath it. |
| 1081 | * |
| 1082 | * Depending on the underlying hardware, violating that assumption could |
| 1083 | * result in seeing through to another display plane. |
| 1084 | */ |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1085 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1086 | gl_renderer_repaint_output(struct weston_output *output, |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1087 | pixman_region32_t *output_damage) |
| 1088 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1089 | struct gl_output_state *go = get_output_state(output); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1090 | struct weston_compositor *compositor = output->compositor; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1091 | struct gl_renderer *gr = get_renderer(compositor); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1092 | EGLBoolean ret; |
| 1093 | static int errored; |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 1094 | int i, nrects, buffer_height; |
| 1095 | EGLint *egl_damage, *d; |
| 1096 | pixman_box32_t *rects; |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1097 | pixman_region32_t buffer_damage, total_damage; |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1098 | enum gl_border_status border_damage = BORDER_STATUS_CLEAN; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1099 | |
Jason Ekstrand | ae0c6e3 | 2014-10-16 10:55:20 -0500 | [diff] [blame] | 1100 | if (use_output(output) < 0) |
| 1101 | return; |
| 1102 | |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 1103 | /* Calculate the viewport */ |
| 1104 | glViewport(go->borders[GL_RENDERER_BORDER_LEFT].width, |
| 1105 | go->borders[GL_RENDERER_BORDER_BOTTOM].height, |
| 1106 | output->current_mode->width, |
| 1107 | output->current_mode->height); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1108 | |
Jason Ekstrand | fb23df7 | 2014-10-16 10:55:21 -0500 | [diff] [blame] | 1109 | /* Calculate the global GL matrix */ |
| 1110 | go->output_matrix = output->matrix; |
| 1111 | weston_matrix_translate(&go->output_matrix, |
| 1112 | -(output->current_mode->width / 2.0), |
| 1113 | -(output->current_mode->height / 2.0), 0); |
| 1114 | weston_matrix_scale(&go->output_matrix, |
| 1115 | 2.0 / output->current_mode->width, |
| 1116 | -2.0 / output->current_mode->height, 1); |
| 1117 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1118 | /* if debugging, redraw everything outside the damage to clean up |
| 1119 | * debug lines from the previous draw on this buffer: |
| 1120 | */ |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 1121 | if (gr->fan_debug) { |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1122 | pixman_region32_t undamaged; |
| 1123 | pixman_region32_init(&undamaged); |
| 1124 | pixman_region32_subtract(&undamaged, &output->region, |
| 1125 | output_damage); |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 1126 | gr->fan_debug = 0; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1127 | repaint_views(output, &undamaged); |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 1128 | gr->fan_debug = 1; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1129 | pixman_region32_fini(&undamaged); |
| 1130 | } |
| 1131 | |
Ander Conselvan de Oliveira | b605c06 | 2013-03-05 17:30:28 +0200 | [diff] [blame] | 1132 | pixman_region32_init(&total_damage); |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1133 | pixman_region32_init(&buffer_damage); |
| 1134 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1135 | output_get_damage(output, &buffer_damage, &border_damage); |
| 1136 | output_rotate_damage(output, output_damage, go->border_status); |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1137 | |
| 1138 | pixman_region32_union(&total_damage, &buffer_damage, output_damage); |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1139 | border_damage |= go->border_status; |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 1140 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1141 | repaint_views(output, &total_damage); |
Ander Conselvan de Oliveira | b605c06 | 2013-03-05 17:30:28 +0200 | [diff] [blame] | 1142 | |
| 1143 | pixman_region32_fini(&total_damage); |
Ander Conselvan de Oliveira | 1c169ff | 2013-03-05 17:30:30 +0200 | [diff] [blame] | 1144 | pixman_region32_fini(&buffer_damage); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1145 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1146 | draw_output_borders(output, border_damage); |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 1147 | |
Ander Conselvan de Oliveira | b8fcca9 | 2012-11-16 17:23:52 +0200 | [diff] [blame] | 1148 | pixman_region32_copy(&output->previous_damage, output_damage); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1149 | wl_signal_emit(&output->frame_signal, output); |
| 1150 | |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 1151 | if (gr->swap_buffers_with_damage) { |
| 1152 | pixman_region32_init(&buffer_damage); |
| 1153 | weston_transformed_region(output->width, output->height, |
| 1154 | output->transform, |
| 1155 | output->current_scale, |
| 1156 | output_damage, &buffer_damage); |
| 1157 | |
| 1158 | if (output_has_borders(output)) { |
| 1159 | pixman_region32_translate(&buffer_damage, |
| 1160 | go->borders[GL_RENDERER_BORDER_LEFT].width, |
| 1161 | go->borders[GL_RENDERER_BORDER_TOP].height); |
| 1162 | output_get_border_damage(output, go->border_status, |
| 1163 | &buffer_damage); |
| 1164 | } |
| 1165 | |
| 1166 | rects = pixman_region32_rectangles(&buffer_damage, &nrects); |
| 1167 | egl_damage = malloc(nrects * 4 * sizeof(EGLint)); |
| 1168 | |
| 1169 | buffer_height = go->borders[GL_RENDERER_BORDER_TOP].height + |
| 1170 | output->current_mode->height + |
| 1171 | go->borders[GL_RENDERER_BORDER_BOTTOM].height; |
| 1172 | |
| 1173 | d = egl_damage; |
| 1174 | for (i = 0; i < nrects; ++i) { |
| 1175 | *d++ = rects[i].x1; |
| 1176 | *d++ = buffer_height - rects[i].y2; |
| 1177 | *d++ = rects[i].x2 - rects[i].x1; |
| 1178 | *d++ = rects[i].y2 - rects[i].y1; |
| 1179 | } |
| 1180 | ret = gr->swap_buffers_with_damage(gr->egl_display, |
| 1181 | go->egl_surface, |
| 1182 | egl_damage, nrects); |
| 1183 | free(egl_damage); |
| 1184 | pixman_region32_fini(&buffer_damage); |
| 1185 | } else { |
| 1186 | ret = eglSwapBuffers(gr->egl_display, go->egl_surface); |
| 1187 | } |
Jason Ekstrand | 8e96f9e | 2014-02-04 21:36:39 -0600 | [diff] [blame] | 1188 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1189 | if (ret == EGL_FALSE && !errored) { |
| 1190 | errored = 1; |
| 1191 | weston_log("Failed in eglSwapBuffers.\n"); |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 1192 | gl_renderer_print_egl_error_state(); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1193 | } |
| 1194 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 1195 | go->border_status = BORDER_STATUS_CLEAN; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1196 | } |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1197 | |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 1198 | static int |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1199 | gl_renderer_read_pixels(struct weston_output *output, |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 1200 | pixman_format_code_t format, void *pixels, |
| 1201 | uint32_t x, uint32_t y, |
| 1202 | uint32_t width, uint32_t height) |
| 1203 | { |
| 1204 | GLenum gl_format; |
Jason Ekstrand | 701f636 | 2014-04-02 19:53:59 -0500 | [diff] [blame] | 1205 | struct gl_output_state *go = get_output_state(output); |
| 1206 | |
| 1207 | x += go->borders[GL_RENDERER_BORDER_LEFT].width; |
| 1208 | y += go->borders[GL_RENDERER_BORDER_BOTTOM].height; |
John Kåre Alsaker | a95b2d6 | 2012-11-13 19:10:21 +0100 | [diff] [blame] | 1209 | |
| 1210 | switch (format) { |
| 1211 | case PIXMAN_a8r8g8b8: |
| 1212 | gl_format = GL_BGRA_EXT; |
| 1213 | break; |
| 1214 | case PIXMAN_a8b8g8r8: |
| 1215 | gl_format = GL_RGBA; |
| 1216 | break; |
| 1217 | default: |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | if (use_output(output) < 0) |
| 1222 | return -1; |
| 1223 | |
| 1224 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 1225 | glReadPixels(x, y, width, height, gl_format, |
| 1226 | GL_UNSIGNED_BYTE, pixels); |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1231 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1232 | gl_renderer_flush_damage(struct weston_surface *surface) |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1233 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1234 | struct gl_renderer *gr = get_renderer(surface->compositor); |
| 1235 | struct gl_surface_state *gs = get_surface_state(surface); |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1236 | struct weston_buffer *buffer = gs->buffer_ref.buffer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1237 | struct weston_view *view; |
Derek Foreman | 9774679 | 2015-11-18 16:32:28 -0600 | [diff] [blame] | 1238 | bool texture_used; |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1239 | pixman_box32_t *rectangles; |
| 1240 | void *data; |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1241 | int i, j, n; |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1242 | |
Pekka Paalanen | 81ee3f5 | 2012-12-04 15:58:16 +0200 | [diff] [blame] | 1243 | pixman_region32_union(&gs->texture_damage, |
| 1244 | &gs->texture_damage, &surface->damage); |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1245 | |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 1246 | if (!buffer) |
| 1247 | return; |
| 1248 | |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1249 | /* Avoid upload, if the texture won't be used this time. |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 1250 | * We still accumulate the damage in texture_damage, and |
| 1251 | * hold the reference to the buffer, in case the surface |
| 1252 | * migrates back to the primary plane. |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1253 | */ |
Derek Foreman | 9774679 | 2015-11-18 16:32:28 -0600 | [diff] [blame] | 1254 | texture_used = false; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1255 | wl_list_for_each(view, &surface->views, surface_link) { |
| 1256 | if (view->plane == &surface->compositor->primary_plane) { |
Derek Foreman | 9774679 | 2015-11-18 16:32:28 -0600 | [diff] [blame] | 1257 | texture_used = true; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1258 | break; |
| 1259 | } |
| 1260 | } |
| 1261 | if (!texture_used) |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1262 | return; |
| 1263 | |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1264 | if (!pixman_region32_not_empty(&gs->texture_damage) && |
| 1265 | !gs->needs_full_upload) |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 1266 | goto done; |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1267 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1268 | data = wl_shm_buffer_get_data(buffer->shm_buffer); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1269 | |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 1270 | if (!gr->has_unpack_subimage) { |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1271 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1272 | for (j = 0; j < gs->num_textures; j++) { |
| 1273 | glBindTexture(GL_TEXTURE_2D, gs->textures[j]); |
| 1274 | glTexImage2D(GL_TEXTURE_2D, 0, |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1275 | gs->gl_format[j], |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1276 | gs->pitch / gs->hsub[j], |
| 1277 | buffer->height / gs->vsub[j], |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1278 | 0, |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1279 | gs->gl_format[j], |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1280 | gs->gl_pixel_type, |
| 1281 | data + gs->offset[j]); |
| 1282 | } |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1283 | wl_shm_buffer_end_access(buffer->shm_buffer); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1284 | |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1285 | goto done; |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1286 | } |
| 1287 | |
Kristian Høgsberg | ce7a5d8 | 2013-08-07 09:55:07 -0700 | [diff] [blame] | 1288 | glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, gs->pitch); |
Ander Conselvan de Oliveira | 6be5f43 | 2013-06-07 16:52:45 +0300 | [diff] [blame] | 1289 | |
| 1290 | if (gs->needs_full_upload) { |
Kristian Høgsberg | ce7a5d8 | 2013-08-07 09:55:07 -0700 | [diff] [blame] | 1291 | glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); |
| 1292 | glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1293 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1294 | for (j = 0; j < gs->num_textures; j++) { |
| 1295 | glBindTexture(GL_TEXTURE_2D, gs->textures[j]); |
| 1296 | glTexImage2D(GL_TEXTURE_2D, 0, |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1297 | gs->gl_format[j], |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1298 | gs->pitch / gs->hsub[j], |
| 1299 | buffer->height / gs->vsub[j], |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1300 | 0, |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1301 | gs->gl_format[j], |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1302 | gs->gl_pixel_type, |
| 1303 | data + gs->offset[j]); |
| 1304 | } |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1305 | wl_shm_buffer_end_access(buffer->shm_buffer); |
Ander Conselvan de Oliveira | 6be5f43 | 2013-06-07 16:52:45 +0300 | [diff] [blame] | 1306 | goto done; |
| 1307 | } |
| 1308 | |
Pekka Paalanen | 81ee3f5 | 2012-12-04 15:58:16 +0200 | [diff] [blame] | 1309 | rectangles = pixman_region32_rectangles(&gs->texture_damage, &n); |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1310 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1311 | for (i = 0; i < n; i++) { |
Ander Conselvan de Oliveira | 0396ba2 | 2012-11-28 17:10:26 +0200 | [diff] [blame] | 1312 | pixman_box32_t r; |
| 1313 | |
| 1314 | r = weston_surface_to_buffer_rect(surface, rectangles[i]); |
| 1315 | |
Kristian Høgsberg | ce7a5d8 | 2013-08-07 09:55:07 -0700 | [diff] [blame] | 1316 | glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, r.x1); |
| 1317 | glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, r.y1); |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1318 | for (j = 0; j < gs->num_textures; j++) { |
| 1319 | glBindTexture(GL_TEXTURE_2D, gs->textures[j]); |
| 1320 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1321 | r.x1 / gs->hsub[j], |
| 1322 | r.y1 / gs->vsub[j], |
| 1323 | (r.x2 - r.x1) / gs->hsub[j], |
| 1324 | (r.y2 - r.y1) / gs->vsub[j], |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1325 | gs->gl_format[j], |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1326 | gs->gl_pixel_type, |
| 1327 | data + gs->offset[j]); |
| 1328 | } |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1329 | } |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1330 | wl_shm_buffer_end_access(buffer->shm_buffer); |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 1331 | |
| 1332 | done: |
Pekka Paalanen | 81ee3f5 | 2012-12-04 15:58:16 +0200 | [diff] [blame] | 1333 | pixman_region32_fini(&gs->texture_damage); |
| 1334 | pixman_region32_init(&gs->texture_damage); |
Derek Foreman | 4c11fe7 | 2015-11-18 16:32:27 -0600 | [diff] [blame] | 1335 | gs->needs_full_upload = false; |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 1336 | |
| 1337 | weston_buffer_reference(&gs->buffer_ref, NULL); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 1338 | } |
| 1339 | |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1340 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1341 | ensure_textures(struct gl_surface_state *gs, int num_textures) |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1342 | { |
| 1343 | int i; |
| 1344 | |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 1345 | if (num_textures <= gs->num_textures) |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1346 | return; |
| 1347 | |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 1348 | for (i = gs->num_textures; i < num_textures; i++) { |
| 1349 | glGenTextures(1, &gs->textures[i]); |
| 1350 | glBindTexture(gs->target, gs->textures[i]); |
| 1351 | glTexParameteri(gs->target, |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1352 | GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 1353 | glTexParameteri(gs->target, |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1354 | GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 1355 | } |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 1356 | gs->num_textures = num_textures; |
| 1357 | glBindTexture(gs->target, 0); |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1360 | static void |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1361 | gl_renderer_attach_shm(struct weston_surface *es, struct weston_buffer *buffer, |
| 1362 | struct wl_shm_buffer *shm_buffer) |
| 1363 | { |
| 1364 | struct weston_compositor *ec = es->compositor; |
| 1365 | struct gl_renderer *gr = get_renderer(ec); |
| 1366 | struct gl_surface_state *gs = get_surface_state(es); |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1367 | GLenum gl_format[3] = {0, 0, 0}; |
| 1368 | GLenum gl_pixel_type; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1369 | int pitch; |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1370 | int num_planes; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1371 | |
| 1372 | buffer->shm_buffer = shm_buffer; |
| 1373 | buffer->width = wl_shm_buffer_get_width(shm_buffer); |
| 1374 | buffer->height = wl_shm_buffer_get_height(shm_buffer); |
| 1375 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1376 | num_planes = 1; |
| 1377 | gs->offset[0] = 0; |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1378 | gs->hsub[0] = 1; |
| 1379 | gs->vsub[0] = 1; |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1380 | |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1381 | switch (wl_shm_buffer_get_format(shm_buffer)) { |
| 1382 | case WL_SHM_FORMAT_XRGB8888: |
| 1383 | gs->shader = &gr->texture_shader_rgbx; |
| 1384 | pitch = wl_shm_buffer_get_stride(shm_buffer) / 4; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1385 | gl_format[0] = GL_BGRA_EXT; |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1386 | gl_pixel_type = GL_UNSIGNED_BYTE; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1387 | break; |
| 1388 | case WL_SHM_FORMAT_ARGB8888: |
| 1389 | gs->shader = &gr->texture_shader_rgba; |
| 1390 | pitch = wl_shm_buffer_get_stride(shm_buffer) / 4; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1391 | gl_format[0] = GL_BGRA_EXT; |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1392 | gl_pixel_type = GL_UNSIGNED_BYTE; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1393 | break; |
| 1394 | case WL_SHM_FORMAT_RGB565: |
| 1395 | gs->shader = &gr->texture_shader_rgbx; |
| 1396 | pitch = wl_shm_buffer_get_stride(shm_buffer) / 2; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1397 | gl_format[0] = GL_RGB; |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1398 | gl_pixel_type = GL_UNSIGNED_SHORT_5_6_5; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1399 | break; |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1400 | case WL_SHM_FORMAT_YUV420: |
| 1401 | gs->shader = &gr->texture_shader_y_u_v; |
| 1402 | pitch = wl_shm_buffer_get_stride(shm_buffer); |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1403 | gl_pixel_type = GL_UNSIGNED_BYTE; |
| 1404 | num_planes = 3; |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1405 | gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) * |
| 1406 | (buffer->height / gs->vsub[0]); |
| 1407 | gs->hsub[1] = 2; |
| 1408 | gs->vsub[1] = 2; |
| 1409 | gs->offset[2] = gs->offset[1] + (pitch / gs->hsub[1]) * |
| 1410 | (buffer->height / gs->vsub[1]); |
| 1411 | gs->hsub[2] = 2; |
| 1412 | gs->vsub[2] = 2; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1413 | if (gr->has_gl_texture_rg) { |
| 1414 | gl_format[0] = GL_R8_EXT; |
| 1415 | gl_format[1] = GL_R8_EXT; |
| 1416 | gl_format[2] = GL_R8_EXT; |
| 1417 | } else { |
| 1418 | gl_format[0] = GL_LUMINANCE; |
| 1419 | gl_format[1] = GL_LUMINANCE; |
| 1420 | gl_format[2] = GL_LUMINANCE; |
| 1421 | } |
| 1422 | break; |
| 1423 | case WL_SHM_FORMAT_NV12: |
| 1424 | gs->shader = &gr->texture_shader_y_xuxv; |
| 1425 | pitch = wl_shm_buffer_get_stride(shm_buffer); |
| 1426 | gl_pixel_type = GL_UNSIGNED_BYTE; |
| 1427 | num_planes = 2; |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1428 | gs->offset[1] = gs->offset[0] + (pitch / gs->hsub[0]) * |
| 1429 | (buffer->height / gs->vsub[0]); |
| 1430 | gs->hsub[1] = 2; |
| 1431 | gs->vsub[1] = 2; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1432 | if (gr->has_gl_texture_rg) { |
| 1433 | gl_format[0] = GL_R8_EXT; |
| 1434 | gl_format[1] = GL_RG8_EXT; |
| 1435 | } else { |
| 1436 | gl_format[0] = GL_LUMINANCE; |
| 1437 | gl_format[1] = GL_LUMINANCE_ALPHA; |
| 1438 | } |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1439 | break; |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 1440 | case WL_SHM_FORMAT_YUYV: |
| 1441 | gs->shader = &gr->texture_shader_y_xuxv; |
| 1442 | pitch = wl_shm_buffer_get_stride(shm_buffer) / 2; |
| 1443 | gl_pixel_type = GL_UNSIGNED_BYTE; |
| 1444 | num_planes = 2; |
| 1445 | gs->hsub[1] = 2; |
| 1446 | gs->vsub[1] = 1; |
| 1447 | if (gr->has_gl_texture_rg) |
| 1448 | gl_format[0] = GL_RG8_EXT; |
| 1449 | else |
| 1450 | gl_format[0] = GL_LUMINANCE_ALPHA; |
| 1451 | gl_format[1] = GL_BGRA_EXT; |
| 1452 | break; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1453 | default: |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1454 | weston_log("warning: unknown shm buffer format: %08x\n", |
| 1455 | wl_shm_buffer_get_format(shm_buffer)); |
| 1456 | return; |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1459 | /* Only allocate a texture if it doesn't match existing one. |
| 1460 | * If a switch from DRM allocated buffer to a SHM buffer is |
| 1461 | * happening, we need to allocate a new texture buffer. */ |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1462 | if (pitch != gs->pitch || |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1463 | buffer->height != gs->height || |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1464 | gl_format[0] != gs->gl_format[0] || |
| 1465 | gl_format[1] != gs->gl_format[1] || |
| 1466 | gl_format[2] != gs->gl_format[2] || |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1467 | gl_pixel_type != gs->gl_pixel_type || |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1468 | gs->buffer_type != BUFFER_TYPE_SHM) { |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 1469 | gs->pitch = pitch; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1470 | gs->height = buffer->height; |
| 1471 | gs->target = GL_TEXTURE_2D; |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 1472 | gs->gl_format[0] = gl_format[0]; |
| 1473 | gs->gl_format[1] = gl_format[1]; |
| 1474 | gs->gl_format[2] = gl_format[2]; |
Neil Roberts | 4d085e7 | 2014-04-07 15:01:01 +0100 | [diff] [blame] | 1475 | gs->gl_pixel_type = gl_pixel_type; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1476 | gs->buffer_type = BUFFER_TYPE_SHM; |
Derek Foreman | 4c11fe7 | 2015-11-18 16:32:27 -0600 | [diff] [blame] | 1477 | gs->needs_full_upload = true; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 1478 | gs->y_inverted = 1; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1479 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 1480 | gs->surface = es; |
| 1481 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 1482 | ensure_textures(gs, num_planes); |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1483 | } |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static void |
| 1487 | gl_renderer_attach_egl(struct weston_surface *es, struct weston_buffer *buffer, |
| 1488 | uint32_t format) |
| 1489 | { |
| 1490 | struct weston_compositor *ec = es->compositor; |
| 1491 | struct gl_renderer *gr = get_renderer(ec); |
| 1492 | struct gl_surface_state *gs = get_surface_state(es); |
| 1493 | EGLint attribs[3]; |
| 1494 | int i, num_planes; |
| 1495 | |
| 1496 | buffer->legacy_buffer = (struct wl_buffer *)buffer->resource; |
| 1497 | gr->query_buffer(gr->egl_display, buffer->legacy_buffer, |
| 1498 | EGL_WIDTH, &buffer->width); |
| 1499 | gr->query_buffer(gr->egl_display, buffer->legacy_buffer, |
| 1500 | EGL_HEIGHT, &buffer->height); |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 1501 | gr->query_buffer(gr->egl_display, buffer->legacy_buffer, |
| 1502 | EGL_WAYLAND_Y_INVERTED_WL, &buffer->y_inverted); |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1503 | |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 1504 | for (i = 0; i < gs->num_images; i++) { |
| 1505 | egl_image_unref(gs->images[i]); |
| 1506 | gs->images[i] = NULL; |
| 1507 | } |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1508 | gs->num_images = 0; |
| 1509 | gs->target = GL_TEXTURE_2D; |
| 1510 | switch (format) { |
| 1511 | case EGL_TEXTURE_RGB: |
| 1512 | case EGL_TEXTURE_RGBA: |
| 1513 | default: |
| 1514 | num_planes = 1; |
| 1515 | gs->shader = &gr->texture_shader_rgba; |
| 1516 | break; |
| 1517 | case EGL_TEXTURE_EXTERNAL_WL: |
| 1518 | num_planes = 1; |
| 1519 | gs->target = GL_TEXTURE_EXTERNAL_OES; |
| 1520 | gs->shader = &gr->texture_shader_egl_external; |
| 1521 | break; |
| 1522 | case EGL_TEXTURE_Y_UV_WL: |
| 1523 | num_planes = 2; |
| 1524 | gs->shader = &gr->texture_shader_y_uv; |
| 1525 | break; |
| 1526 | case EGL_TEXTURE_Y_U_V_WL: |
| 1527 | num_planes = 3; |
| 1528 | gs->shader = &gr->texture_shader_y_u_v; |
| 1529 | break; |
| 1530 | case EGL_TEXTURE_Y_XUXV_WL: |
| 1531 | num_planes = 2; |
| 1532 | gs->shader = &gr->texture_shader_y_xuxv; |
| 1533 | break; |
| 1534 | } |
| 1535 | |
| 1536 | ensure_textures(gs, num_planes); |
| 1537 | for (i = 0; i < num_planes; i++) { |
| 1538 | attribs[0] = EGL_WAYLAND_PLANE_WL; |
| 1539 | attribs[1] = i; |
| 1540 | attribs[2] = EGL_NONE; |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 1541 | gs->images[i] = egl_image_create(gr, |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1542 | EGL_WAYLAND_BUFFER_WL, |
| 1543 | buffer->legacy_buffer, |
| 1544 | attribs); |
| 1545 | if (!gs->images[i]) { |
| 1546 | weston_log("failed to create img for plane %d\n", i); |
| 1547 | continue; |
| 1548 | } |
| 1549 | gs->num_images++; |
| 1550 | |
| 1551 | glActiveTexture(GL_TEXTURE0 + i); |
| 1552 | glBindTexture(gs->target, gs->textures[i]); |
| 1553 | gr->image_target_texture_2d(gs->target, |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 1554 | gs->images[i]->image); |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | gs->pitch = buffer->width; |
| 1558 | gs->height = buffer->height; |
| 1559 | gs->buffer_type = BUFFER_TYPE_EGL; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 1560 | gs->y_inverted = buffer->y_inverted; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | static void |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1564 | gl_renderer_destroy_dmabuf(struct linux_dmabuf_buffer *dmabuf) |
| 1565 | { |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1566 | struct dmabuf_image *image = dmabuf->user_data; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1567 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1568 | dmabuf_image_destroy(image); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static struct egl_image * |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1572 | import_simple_dmabuf(struct gl_renderer *gr, |
| 1573 | struct dmabuf_attributes *attributes) |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1574 | { |
| 1575 | struct egl_image *image; |
| 1576 | EGLint attribs[30]; |
| 1577 | int atti = 0; |
| 1578 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1579 | /* This requires the Mesa commit in |
| 1580 | * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or |
| 1581 | * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652). |
| 1582 | * Otherwise Mesa closes the fd behind our back and re-importing |
| 1583 | * will fail. |
| 1584 | * https://bugs.freedesktop.org/show_bug.cgi?id=76188 |
| 1585 | */ |
| 1586 | |
| 1587 | attribs[atti++] = EGL_WIDTH; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1588 | attribs[atti++] = attributes->width; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1589 | attribs[atti++] = EGL_HEIGHT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1590 | attribs[atti++] = attributes->height; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1591 | attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1592 | attribs[atti++] = attributes->format; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1593 | /* XXX: Add modifier here when supported */ |
| 1594 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1595 | if (attributes->n_planes > 0) { |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1596 | attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1597 | attribs[atti++] = attributes->fd[0]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1598 | attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1599 | attribs[atti++] = attributes->offset[0]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1600 | attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1601 | attribs[atti++] = attributes->stride[0]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1602 | } |
| 1603 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1604 | if (attributes->n_planes > 1) { |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1605 | attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1606 | attribs[atti++] = attributes->fd[1]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1607 | attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1608 | attribs[atti++] = attributes->offset[1]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1609 | attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1610 | attribs[atti++] = attributes->stride[1]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1611 | } |
| 1612 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1613 | if (attributes->n_planes > 2) { |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1614 | attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1615 | attribs[atti++] = attributes->fd[2]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1616 | attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1617 | attribs[atti++] = attributes->offset[2]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1618 | attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1619 | attribs[atti++] = attributes->stride[2]; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | attribs[atti++] = EGL_NONE; |
| 1623 | |
| 1624 | image = egl_image_create(gr, EGL_LINUX_DMA_BUF_EXT, NULL, |
| 1625 | attribs); |
| 1626 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1627 | return image; |
| 1628 | } |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1629 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1630 | /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate |
| 1631 | * some of the definitions here so that building Weston won't require |
| 1632 | * bleeding-edge kernel headers. |
| 1633 | */ |
| 1634 | #ifndef DRM_FORMAT_R8 |
| 1635 | #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */ |
| 1636 | #endif |
| 1637 | |
| 1638 | #ifndef DRM_FORMAT_GR88 |
| 1639 | #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */ |
| 1640 | #endif |
| 1641 | |
| 1642 | struct yuv_format_descriptor yuv_formats[] = { |
| 1643 | { |
| 1644 | .format = DRM_FORMAT_YUYV, |
| 1645 | .input_planes = 1, |
| 1646 | .output_planes = 2, |
| 1647 | .texture_type = EGL_TEXTURE_Y_XUXV_WL, |
| 1648 | {{ |
| 1649 | .width_divisor = 1, |
| 1650 | .height_divisor = 1, |
| 1651 | .format = DRM_FORMAT_GR88, |
| 1652 | .plane_index = 0 |
| 1653 | }, { |
| 1654 | .width_divisor = 2, |
| 1655 | .height_divisor = 1, |
| 1656 | .format = DRM_FORMAT_ARGB8888, |
| 1657 | .plane_index = 0 |
| 1658 | }} |
| 1659 | }, { |
| 1660 | .format = DRM_FORMAT_NV12, |
| 1661 | .input_planes = 2, |
| 1662 | .output_planes = 2, |
| 1663 | .texture_type = EGL_TEXTURE_Y_UV_WL, |
| 1664 | {{ |
| 1665 | .width_divisor = 1, |
| 1666 | .height_divisor = 1, |
| 1667 | .format = DRM_FORMAT_R8, |
| 1668 | .plane_index = 0 |
| 1669 | }, { |
| 1670 | .width_divisor = 2, |
| 1671 | .height_divisor = 2, |
| 1672 | .format = DRM_FORMAT_GR88, |
| 1673 | .plane_index = 1 |
| 1674 | }} |
| 1675 | }, { |
| 1676 | .format = DRM_FORMAT_YUV420, |
| 1677 | .input_planes = 3, |
| 1678 | .output_planes = 3, |
| 1679 | .texture_type = EGL_TEXTURE_Y_U_V_WL, |
| 1680 | {{ |
| 1681 | .width_divisor = 1, |
| 1682 | .height_divisor = 1, |
| 1683 | .format = DRM_FORMAT_R8, |
| 1684 | .plane_index = 0 |
| 1685 | }, { |
| 1686 | .width_divisor = 2, |
| 1687 | .height_divisor = 2, |
| 1688 | .format = DRM_FORMAT_R8, |
| 1689 | .plane_index = 1 |
| 1690 | }, { |
| 1691 | .width_divisor = 2, |
| 1692 | .height_divisor = 2, |
| 1693 | .format = DRM_FORMAT_R8, |
| 1694 | .plane_index = 2 |
| 1695 | }} |
Matthias Treydte | aca3ffb | 2016-07-25 12:15:41 +0200 | [diff] [blame] | 1696 | }, { |
| 1697 | .format = DRM_FORMAT_YUV444, |
| 1698 | .input_planes = 3, |
| 1699 | .output_planes = 3, |
| 1700 | .texture_type = EGL_TEXTURE_Y_U_V_WL, |
| 1701 | {{ |
| 1702 | .width_divisor = 1, |
| 1703 | .height_divisor = 1, |
| 1704 | .format = DRM_FORMAT_R8, |
| 1705 | .plane_index = 0 |
| 1706 | }, { |
| 1707 | .width_divisor = 1, |
| 1708 | .height_divisor = 1, |
| 1709 | .format = DRM_FORMAT_R8, |
| 1710 | .plane_index = 1 |
| 1711 | }, { |
| 1712 | .width_divisor = 1, |
| 1713 | .height_divisor = 1, |
| 1714 | .format = DRM_FORMAT_R8, |
| 1715 | .plane_index = 2 |
| 1716 | }} |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1717 | } |
| 1718 | }; |
| 1719 | |
| 1720 | static struct egl_image * |
| 1721 | import_dmabuf_single_plane(struct gl_renderer *gr, |
| 1722 | const struct dmabuf_attributes *attributes, |
| 1723 | struct yuv_plane_descriptor *descriptor) |
| 1724 | { |
| 1725 | struct dmabuf_attributes plane; |
| 1726 | struct egl_image *image; |
| 1727 | char fmt[4]; |
| 1728 | |
| 1729 | plane.width = attributes->width / descriptor->width_divisor; |
| 1730 | plane.height = attributes->height / descriptor->height_divisor; |
| 1731 | plane.format = descriptor->format; |
| 1732 | plane.n_planes = 1; |
| 1733 | plane.fd[0] = attributes->fd[descriptor->plane_index]; |
| 1734 | plane.offset[0] = attributes->offset[descriptor->plane_index]; |
| 1735 | plane.stride[0] = attributes->stride[descriptor->plane_index]; |
| 1736 | plane.modifier[0] = attributes->modifier[descriptor->plane_index]; |
| 1737 | |
| 1738 | image = import_simple_dmabuf(gr, &plane); |
| 1739 | if (!image) { |
| 1740 | weston_log("Failed to import plane %d as %.4s\n", |
| 1741 | descriptor->plane_index, |
| 1742 | dump_format(descriptor->format, fmt)); |
| 1743 | return NULL; |
| 1744 | } |
| 1745 | |
| 1746 | return image; |
| 1747 | } |
| 1748 | |
| 1749 | static bool |
| 1750 | import_yuv_dmabuf(struct gl_renderer *gr, |
| 1751 | struct dmabuf_image *image) |
| 1752 | { |
| 1753 | unsigned i; |
| 1754 | int j; |
| 1755 | int ret; |
| 1756 | struct yuv_format_descriptor *format = NULL; |
| 1757 | struct dmabuf_attributes *attributes = &image->dmabuf->attributes; |
| 1758 | char fmt[4]; |
| 1759 | |
| 1760 | for (i = 0; i < ARRAY_LENGTH(yuv_formats); ++i) { |
| 1761 | if (yuv_formats[i].format == attributes->format) { |
| 1762 | format = &yuv_formats[i]; |
| 1763 | break; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | if (!format) { |
| 1768 | weston_log("Error during import, and no known conversion for format " |
| 1769 | "%.4s in the renderer", |
| 1770 | dump_format(attributes->format, fmt)); |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | if (attributes->n_planes != format->input_planes) { |
| 1775 | weston_log("%.4s dmabuf must contain %d plane%s (%d provided)", |
| 1776 | dump_format(format->format, fmt), |
| 1777 | format->input_planes, |
| 1778 | (format->input_planes > 1) ? "s" : "", |
| 1779 | attributes->n_planes); |
| 1780 | return false; |
| 1781 | } |
| 1782 | |
| 1783 | for (j = 0; j < format->output_planes; ++j) { |
| 1784 | image->images[j] = import_dmabuf_single_plane(gr, attributes, |
| 1785 | &format->plane[j]); |
| 1786 | if (!image->images[j]) { |
| 1787 | while (j) { |
| 1788 | ret = egl_image_unref(image->images[--j]); |
| 1789 | assert(ret == 0); |
| 1790 | } |
| 1791 | return false; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | image->num_images = format->output_planes; |
| 1796 | |
| 1797 | switch (format->texture_type) { |
| 1798 | case EGL_TEXTURE_Y_XUXV_WL: |
| 1799 | image->shader = &gr->texture_shader_y_xuxv; |
| 1800 | break; |
| 1801 | case EGL_TEXTURE_Y_UV_WL: |
| 1802 | image->shader = &gr->texture_shader_y_uv; |
| 1803 | break; |
| 1804 | case EGL_TEXTURE_Y_U_V_WL: |
| 1805 | image->shader = &gr->texture_shader_y_u_v; |
| 1806 | break; |
| 1807 | default: |
| 1808 | assert(false); |
| 1809 | } |
| 1810 | |
| 1811 | return true; |
| 1812 | } |
| 1813 | |
| 1814 | static GLenum |
| 1815 | choose_texture_target(struct dmabuf_attributes *attributes) |
| 1816 | { |
| 1817 | if (attributes->n_planes > 1) |
| 1818 | return GL_TEXTURE_EXTERNAL_OES; |
| 1819 | |
| 1820 | switch (attributes->format & ~DRM_FORMAT_BIG_ENDIAN) { |
| 1821 | case DRM_FORMAT_YUYV: |
| 1822 | case DRM_FORMAT_YVYU: |
| 1823 | case DRM_FORMAT_UYVY: |
| 1824 | case DRM_FORMAT_VYUY: |
| 1825 | case DRM_FORMAT_AYUV: |
| 1826 | return GL_TEXTURE_EXTERNAL_OES; |
| 1827 | default: |
| 1828 | return GL_TEXTURE_2D; |
| 1829 | } |
| 1830 | } |
| 1831 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1832 | static struct dmabuf_image * |
| 1833 | import_dmabuf(struct gl_renderer *gr, |
| 1834 | struct linux_dmabuf_buffer *dmabuf) |
| 1835 | { |
| 1836 | struct egl_image *egl_image; |
| 1837 | struct dmabuf_image *image; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1838 | |
| 1839 | image = dmabuf_image_create(); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1840 | image->dmabuf = dmabuf; |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1841 | |
| 1842 | egl_image = import_simple_dmabuf(gr, &dmabuf->attributes); |
| 1843 | if (egl_image) { |
| 1844 | image->num_images = 1; |
| 1845 | image->images[0] = egl_image; |
| 1846 | image->import_type = IMPORT_TYPE_DIRECT; |
| 1847 | image->target = choose_texture_target(&dmabuf->attributes); |
| 1848 | |
| 1849 | switch (image->target) { |
| 1850 | case GL_TEXTURE_2D: |
| 1851 | image->shader = &gr->texture_shader_rgba; |
| 1852 | break; |
| 1853 | default: |
| 1854 | image->shader = &gr->texture_shader_egl_external; |
| 1855 | } |
| 1856 | } else { |
| 1857 | if (!import_yuv_dmabuf(gr, image)) { |
| 1858 | dmabuf_image_destroy(image); |
| 1859 | return NULL; |
| 1860 | } |
| 1861 | image->import_type = IMPORT_TYPE_GL_CONVERSION; |
| 1862 | image->target = GL_TEXTURE_2D; |
| 1863 | } |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1864 | |
| 1865 | return image; |
| 1866 | } |
| 1867 | |
| 1868 | static bool |
| 1869 | gl_renderer_import_dmabuf(struct weston_compositor *ec, |
| 1870 | struct linux_dmabuf_buffer *dmabuf) |
| 1871 | { |
| 1872 | struct gl_renderer *gr = get_renderer(ec); |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1873 | struct dmabuf_image *image; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1874 | int i; |
| 1875 | |
| 1876 | assert(gr->has_dmabuf_import); |
| 1877 | |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1878 | for (i = 0; i < dmabuf->attributes.n_planes; i++) { |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1879 | /* EGL import does not have modifiers */ |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1880 | if (dmabuf->attributes.modifier[i] != 0) |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1881 | return false; |
| 1882 | } |
| 1883 | |
| 1884 | /* reject all flags we do not recognize or handle */ |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1885 | if (dmabuf->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT) |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1886 | return false; |
| 1887 | |
| 1888 | image = import_dmabuf(gr, dmabuf); |
| 1889 | if (!image) |
| 1890 | return false; |
| 1891 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1892 | wl_list_insert(&gr->dmabuf_images, &image->link); |
| 1893 | linux_dmabuf_buffer_set_user_data(dmabuf, image, |
| 1894 | gl_renderer_destroy_dmabuf); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1895 | |
| 1896 | return true; |
| 1897 | } |
| 1898 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1899 | static bool |
| 1900 | import_known_dmabuf(struct gl_renderer *gr, |
| 1901 | struct dmabuf_image *image) |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1902 | { |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1903 | switch (image->import_type) { |
| 1904 | case IMPORT_TYPE_DIRECT: |
| 1905 | image->images[0] = import_simple_dmabuf(gr, &image->dmabuf->attributes); |
| 1906 | if (!image->images[0]) |
| 1907 | return false; |
| 1908 | break; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1909 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1910 | case IMPORT_TYPE_GL_CONVERSION: |
| 1911 | if (!import_yuv_dmabuf(gr, image)) |
| 1912 | return false; |
| 1913 | break; |
| 1914 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1915 | default: |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1916 | weston_log("Invalid import type for dmabuf\n"); |
| 1917 | return false; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1918 | } |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1919 | |
| 1920 | return true; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static void |
| 1924 | gl_renderer_attach_dmabuf(struct weston_surface *surface, |
| 1925 | struct weston_buffer *buffer, |
| 1926 | struct linux_dmabuf_buffer *dmabuf) |
| 1927 | { |
| 1928 | struct gl_renderer *gr = get_renderer(surface->compositor); |
| 1929 | struct gl_surface_state *gs = get_surface_state(surface); |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1930 | struct dmabuf_image *image; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1931 | int i; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1932 | int ret; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1933 | |
| 1934 | if (!gr->has_dmabuf_import) { |
| 1935 | linux_dmabuf_buffer_send_server_error(dmabuf, |
| 1936 | "EGL dmabuf import not supported"); |
| 1937 | return; |
| 1938 | } |
| 1939 | |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1940 | buffer->width = dmabuf->attributes.width; |
| 1941 | buffer->height = dmabuf->attributes.height; |
Pekka Paalanen | 319397e | 2016-07-04 16:25:16 +0300 | [diff] [blame] | 1942 | |
| 1943 | /* |
| 1944 | * GL-renderer uses the OpenGL convention of texture coordinates, where |
| 1945 | * the origin is at bottom-left. Because dmabuf buffers have the origin |
| 1946 | * at top-left, we must invert the Y_INVERT flag to get the image right. |
| 1947 | */ |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1948 | buffer->y_inverted = |
Pekka Paalanen | 319397e | 2016-07-04 16:25:16 +0300 | [diff] [blame] | 1949 | !(dmabuf->attributes.flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1950 | |
| 1951 | for (i = 0; i < gs->num_images; i++) |
| 1952 | egl_image_unref(gs->images[i]); |
| 1953 | gs->num_images = 0; |
| 1954 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1955 | /* |
| 1956 | * We try to always hold an imported EGLImage from the dmabuf |
| 1957 | * to prevent the client from preventing re-imports. But, we also |
| 1958 | * need to re-import every time the contents may change because |
| 1959 | * GL driver's caching may need flushing. |
| 1960 | * |
| 1961 | * Here we release the cache reference which has to be final. |
| 1962 | */ |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1963 | image = linux_dmabuf_buffer_get_user_data(dmabuf); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1964 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1965 | /* The dmabuf_image should have been created during the import */ |
| 1966 | assert(image != NULL); |
| 1967 | |
| 1968 | for (i = 0; i < image->num_images; ++i) { |
| 1969 | ret = egl_image_unref(image->images[i]); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1970 | assert(ret == 0); |
| 1971 | } |
| 1972 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1973 | if (!import_known_dmabuf(gr, image)) { |
| 1974 | linux_dmabuf_buffer_send_server_error(dmabuf, "EGL dmabuf import failed"); |
| 1975 | return; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1976 | } |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1977 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1978 | gs->num_images = image->num_images; |
| 1979 | for (i = 0; i < gs->num_images; ++i) |
| 1980 | gs->images[i] = egl_image_ref(image->images[i]); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1981 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1982 | gs->target = image->target; |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 1983 | ensure_textures(gs, gs->num_images); |
| 1984 | for (i = 0; i < gs->num_images; ++i) { |
| 1985 | glActiveTexture(GL_TEXTURE0 + i); |
| 1986 | glBindTexture(gs->target, gs->textures[i]); |
| 1987 | gr->image_target_texture_2d(gs->target, gs->images[i]->image); |
| 1988 | } |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1989 | |
Emmanuel Gil Peyrot | bc35fda | 2016-01-11 19:04:35 +0000 | [diff] [blame] | 1990 | gs->shader = image->shader; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 1991 | gs->pitch = buffer->width; |
| 1992 | gs->height = buffer->height; |
| 1993 | gs->buffer_type = BUFFER_TYPE_EGL; |
| 1994 | gs->y_inverted = buffer->y_inverted; |
| 1995 | } |
| 1996 | |
| 1997 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 1998 | gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 1999 | { |
| 2000 | struct weston_compositor *ec = es->compositor; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2001 | struct gl_renderer *gr = get_renderer(ec); |
| 2002 | struct gl_surface_state *gs = get_surface_state(es); |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 2003 | struct wl_shm_buffer *shm_buffer; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2004 | struct linux_dmabuf_buffer *dmabuf; |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 2005 | EGLint format; |
| 2006 | int i; |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2007 | |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 2008 | weston_buffer_reference(&gs->buffer_ref, buffer); |
| 2009 | |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2010 | if (!buffer) { |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 2011 | for (i = 0; i < gs->num_images; i++) { |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 2012 | egl_image_unref(gs->images[i]); |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 2013 | gs->images[i] = NULL; |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2014 | } |
John Kåre Alsaker | 75cc571 | 2012-11-13 19:10:26 +0100 | [diff] [blame] | 2015 | gs->num_images = 0; |
| 2016 | glDeleteTextures(gs->num_textures, gs->textures); |
| 2017 | gs->num_textures = 0; |
Ander Conselvan de Oliveira | a9bf161 | 2013-06-07 16:52:44 +0300 | [diff] [blame] | 2018 | gs->buffer_type = BUFFER_TYPE_NULL; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 2019 | gs->y_inverted = 1; |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2020 | return; |
| 2021 | } |
| 2022 | |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 2023 | shm_buffer = wl_shm_buffer_get(buffer->resource); |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 2024 | |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 2025 | if (shm_buffer) |
| 2026 | gl_renderer_attach_shm(es, buffer, shm_buffer); |
Vincent Abriou | 9c526e0 | 2016-11-03 11:15:32 +0100 | [diff] [blame] | 2027 | else if (gr->has_bind_display && |
| 2028 | gr->query_buffer(gr->egl_display, (void *)buffer->resource, |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 2029 | EGL_TEXTURE_FORMAT, &format)) |
| 2030 | gl_renderer_attach_egl(es, buffer, format); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2031 | else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource))) |
| 2032 | gl_renderer_attach_dmabuf(es, buffer, dmabuf); |
Ander Conselvan de Oliveira | 047e9b9 | 2013-06-07 16:52:46 +0300 | [diff] [blame] | 2033 | else { |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2034 | weston_log("unhandled buffer type!\n"); |
Pekka Paalanen | fb003d3 | 2012-12-04 15:58:13 +0200 | [diff] [blame] | 2035 | weston_buffer_reference(&gs->buffer_ref, NULL); |
Ander Conselvan de Oliveira | a9bf161 | 2013-06-07 16:52:44 +0300 | [diff] [blame] | 2036 | gs->buffer_type = BUFFER_TYPE_NULL; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 2037 | gs->y_inverted = 1; |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Kristian Høgsberg | 4226385 | 2012-09-06 21:59:29 -0400 | [diff] [blame] | 2041 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2042 | gl_renderer_surface_set_color(struct weston_surface *surface, |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2043 | float red, float green, float blue, float alpha) |
| 2044 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2045 | struct gl_surface_state *gs = get_surface_state(surface); |
| 2046 | struct gl_renderer *gr = get_renderer(surface->compositor); |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2047 | |
| 2048 | gs->color[0] = red; |
| 2049 | gs->color[1] = green; |
| 2050 | gs->color[2] = blue; |
| 2051 | gs->color[3] = alpha; |
Pekka Paalanen | aeb917e | 2015-02-09 13:56:56 +0200 | [diff] [blame] | 2052 | gs->buffer_type = BUFFER_TYPE_SOLID; |
| 2053 | gs->pitch = 1; |
| 2054 | gs->height = 1; |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2055 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 2056 | gs->shader = &gr->solid_shader; |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2059 | static void |
Pekka Paalanen | eb35cbe | 2015-02-09 13:37:27 +0200 | [diff] [blame] | 2060 | gl_renderer_surface_get_content_size(struct weston_surface *surface, |
| 2061 | int *width, int *height) |
| 2062 | { |
| 2063 | struct gl_surface_state *gs = get_surface_state(surface); |
| 2064 | |
| 2065 | if (gs->buffer_type == BUFFER_TYPE_NULL) { |
| 2066 | *width = 0; |
| 2067 | *height = 0; |
| 2068 | } else { |
| 2069 | *width = gs->pitch; |
| 2070 | *height = gs->height; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | static uint32_t |
| 2075 | pack_color(pixman_format_code_t format, float *c) |
| 2076 | { |
| 2077 | uint8_t r = round(c[0] * 255.0f); |
| 2078 | uint8_t g = round(c[1] * 255.0f); |
| 2079 | uint8_t b = round(c[2] * 255.0f); |
| 2080 | uint8_t a = round(c[3] * 255.0f); |
| 2081 | |
| 2082 | switch (format) { |
| 2083 | case PIXMAN_a8b8g8r8: |
| 2084 | return (a << 24) | (b << 16) | (g << 8) | r; |
| 2085 | default: |
| 2086 | assert(0); |
| 2087 | return 0; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | static int |
| 2092 | gl_renderer_surface_copy_content(struct weston_surface *surface, |
| 2093 | void *target, size_t size, |
| 2094 | int src_x, int src_y, |
| 2095 | int width, int height) |
| 2096 | { |
| 2097 | static const GLfloat verts[4 * 2] = { |
| 2098 | 0.0f, 0.0f, |
| 2099 | 1.0f, 0.0f, |
| 2100 | 1.0f, 1.0f, |
| 2101 | 0.0f, 1.0f |
| 2102 | }; |
| 2103 | static const GLfloat projmat_normal[16] = { /* transpose */ |
| 2104 | 2.0f, 0.0f, 0.0f, 0.0f, |
| 2105 | 0.0f, 2.0f, 0.0f, 0.0f, |
| 2106 | 0.0f, 0.0f, 1.0f, 0.0f, |
| 2107 | -1.0f, -1.0f, 0.0f, 1.0f |
| 2108 | }; |
| 2109 | static const GLfloat projmat_yinvert[16] = { /* transpose */ |
| 2110 | 2.0f, 0.0f, 0.0f, 0.0f, |
| 2111 | 0.0f, -2.0f, 0.0f, 0.0f, |
| 2112 | 0.0f, 0.0f, 1.0f, 0.0f, |
| 2113 | -1.0f, 1.0f, 0.0f, 1.0f |
| 2114 | }; |
| 2115 | const pixman_format_code_t format = PIXMAN_a8b8g8r8; |
| 2116 | const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */ |
| 2117 | const GLenum gl_format = GL_RGBA; /* PIXMAN_a8b8g8r8 little-endian */ |
| 2118 | struct gl_renderer *gr = get_renderer(surface->compositor); |
| 2119 | struct gl_surface_state *gs = get_surface_state(surface); |
| 2120 | int cw, ch; |
| 2121 | GLuint fbo; |
| 2122 | GLuint tex; |
| 2123 | GLenum status; |
| 2124 | const GLfloat *proj; |
| 2125 | int i; |
| 2126 | |
| 2127 | gl_renderer_surface_get_content_size(surface, &cw, &ch); |
| 2128 | |
| 2129 | switch (gs->buffer_type) { |
| 2130 | case BUFFER_TYPE_NULL: |
| 2131 | return -1; |
| 2132 | case BUFFER_TYPE_SOLID: |
| 2133 | *(uint32_t *)target = pack_color(format, gs->color); |
| 2134 | return 0; |
| 2135 | case BUFFER_TYPE_SHM: |
| 2136 | gl_renderer_flush_damage(surface); |
| 2137 | /* fall through */ |
| 2138 | case BUFFER_TYPE_EGL: |
| 2139 | break; |
| 2140 | } |
| 2141 | |
| 2142 | glGenTextures(1, &tex); |
| 2143 | glBindTexture(GL_TEXTURE_2D, tex); |
| 2144 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch, |
| 2145 | 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 2146 | glBindTexture(GL_TEXTURE_2D, 0); |
| 2147 | |
| 2148 | glGenFramebuffers(1, &fbo); |
| 2149 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 2150 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 2151 | GL_TEXTURE_2D, tex, 0); |
| 2152 | |
| 2153 | status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 2154 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 2155 | weston_log("%s: fbo error: %#x\n", __func__, status); |
| 2156 | glDeleteFramebuffers(1, &fbo); |
| 2157 | glDeleteTextures(1, &tex); |
| 2158 | return -1; |
| 2159 | } |
| 2160 | |
| 2161 | glViewport(0, 0, cw, ch); |
| 2162 | glDisable(GL_BLEND); |
| 2163 | use_shader(gr, gs->shader); |
| 2164 | if (gs->y_inverted) |
| 2165 | proj = projmat_normal; |
| 2166 | else |
| 2167 | proj = projmat_yinvert; |
| 2168 | |
| 2169 | glUniformMatrix4fv(gs->shader->proj_uniform, 1, GL_FALSE, proj); |
| 2170 | glUniform1f(gs->shader->alpha_uniform, 1.0f); |
| 2171 | |
| 2172 | for (i = 0; i < gs->num_textures; i++) { |
| 2173 | glUniform1i(gs->shader->tex_uniforms[i], i); |
| 2174 | |
| 2175 | glActiveTexture(GL_TEXTURE0 + i); |
| 2176 | glBindTexture(gs->target, gs->textures[i]); |
| 2177 | glTexParameteri(gs->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 2178 | glTexParameteri(gs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 2179 | } |
| 2180 | |
| 2181 | /* position: */ |
| 2182 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 2183 | glEnableVertexAttribArray(0); |
| 2184 | |
| 2185 | /* texcoord: */ |
| 2186 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 2187 | glEnableVertexAttribArray(1); |
| 2188 | |
| 2189 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2190 | |
| 2191 | glDisableVertexAttribArray(1); |
| 2192 | glDisableVertexAttribArray(0); |
| 2193 | |
| 2194 | glPixelStorei(GL_PACK_ALIGNMENT, bytespp); |
| 2195 | glReadPixels(src_x, src_y, width, height, gl_format, |
| 2196 | GL_UNSIGNED_BYTE, target); |
| 2197 | |
| 2198 | glDeleteFramebuffers(1, &fbo); |
| 2199 | glDeleteTextures(1, &tex); |
| 2200 | |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
| 2204 | static void |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2205 | surface_state_destroy(struct gl_surface_state *gs, struct gl_renderer *gr) |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2206 | { |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2207 | int i; |
| 2208 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2209 | wl_list_remove(&gs->surface_destroy_listener.link); |
| 2210 | wl_list_remove(&gs->renderer_destroy_listener.link); |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2211 | |
| 2212 | gs->surface->renderer_state = NULL; |
| 2213 | |
| 2214 | glDeleteTextures(gs->num_textures, gs->textures); |
| 2215 | |
| 2216 | for (i = 0; i < gs->num_images; i++) |
Louis-Francis Ratté-Boulianne | 534defd | 2015-06-08 16:37:05 +0300 | [diff] [blame] | 2217 | egl_image_unref(gs->images[i]); |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2218 | |
| 2219 | weston_buffer_reference(&gs->buffer_ref, NULL); |
| 2220 | pixman_region32_fini(&gs->texture_damage); |
| 2221 | free(gs); |
| 2222 | } |
| 2223 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2224 | static void |
| 2225 | surface_state_handle_surface_destroy(struct wl_listener *listener, void *data) |
| 2226 | { |
| 2227 | struct gl_surface_state *gs; |
| 2228 | struct gl_renderer *gr; |
| 2229 | |
| 2230 | gs = container_of(listener, struct gl_surface_state, |
| 2231 | surface_destroy_listener); |
| 2232 | |
| 2233 | gr = get_renderer(gs->surface->compositor); |
| 2234 | |
| 2235 | surface_state_destroy(gs, gr); |
| 2236 | } |
| 2237 | |
| 2238 | static void |
| 2239 | surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data) |
| 2240 | { |
| 2241 | struct gl_surface_state *gs; |
| 2242 | struct gl_renderer *gr; |
| 2243 | |
| 2244 | gr = data; |
| 2245 | |
| 2246 | gs = container_of(listener, struct gl_surface_state, |
| 2247 | renderer_destroy_listener); |
| 2248 | |
| 2249 | surface_state_destroy(gs, gr); |
| 2250 | } |
| 2251 | |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2252 | static int |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2253 | gl_renderer_create_surface(struct weston_surface *surface) |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2254 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2255 | struct gl_surface_state *gs; |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2256 | struct gl_renderer *gr = get_renderer(surface->compositor); |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2257 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 2258 | gs = zalloc(sizeof *gs); |
| 2259 | if (gs == NULL) |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2260 | return -1; |
| 2261 | |
Pekka Paalanen | 68033ac | 2012-12-04 15:58:15 +0200 | [diff] [blame] | 2262 | /* A buffer is never attached to solid color surfaces, yet |
| 2263 | * they still go through texcoord computations. Do not divide |
| 2264 | * by zero there. |
| 2265 | */ |
| 2266 | gs->pitch = 1; |
Stanislav Vorobiov | bfbb8e5 | 2013-08-29 11:36:44 +0400 | [diff] [blame] | 2267 | gs->y_inverted = 1; |
Pekka Paalanen | 68033ac | 2012-12-04 15:58:15 +0200 | [diff] [blame] | 2268 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2269 | gs->surface = surface; |
| 2270 | |
Pekka Paalanen | 81ee3f5 | 2012-12-04 15:58:16 +0200 | [diff] [blame] | 2271 | pixman_region32_init(&gs->texture_damage); |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2272 | surface->renderer_state = gs; |
| 2273 | |
Ander Conselvan de Oliveira | aa398ae | 2013-10-25 16:26:33 +0300 | [diff] [blame] | 2274 | gs->surface_destroy_listener.notify = |
| 2275 | surface_state_handle_surface_destroy; |
| 2276 | wl_signal_add(&surface->destroy_signal, |
| 2277 | &gs->surface_destroy_listener); |
| 2278 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2279 | gs->renderer_destroy_listener.notify = |
| 2280 | surface_state_handle_renderer_destroy; |
| 2281 | wl_signal_add(&gr->destroy_signal, |
| 2282 | &gs->renderer_destroy_listener); |
| 2283 | |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 2284 | if (surface->buffer_ref.buffer) { |
| 2285 | gl_renderer_attach(surface, surface->buffer_ref.buffer); |
| 2286 | gl_renderer_flush_damage(surface); |
| 2287 | } |
| 2288 | |
John Kåre Alsaker | 878f449 | 2012-11-13 19:10:23 +0100 | [diff] [blame] | 2289 | return 0; |
| 2290 | } |
| 2291 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2292 | static const char vertex_shader[] = |
| 2293 | "uniform mat4 proj;\n" |
| 2294 | "attribute vec2 position;\n" |
| 2295 | "attribute vec2 texcoord;\n" |
| 2296 | "varying vec2 v_texcoord;\n" |
| 2297 | "void main()\n" |
| 2298 | "{\n" |
| 2299 | " gl_Position = proj * vec4(position, 0.0, 1.0);\n" |
| 2300 | " v_texcoord = texcoord;\n" |
| 2301 | "}\n"; |
| 2302 | |
| 2303 | /* Declare common fragment shader uniforms */ |
| 2304 | #define FRAGMENT_CONVERT_YUV \ |
| 2305 | " y *= alpha;\n" \ |
| 2306 | " u *= alpha;\n" \ |
| 2307 | " v *= alpha;\n" \ |
| 2308 | " gl_FragColor.r = y + 1.59602678 * v;\n" \ |
| 2309 | " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \ |
| 2310 | " gl_FragColor.b = y + 2.01723214 * u;\n" \ |
| 2311 | " gl_FragColor.a = alpha;\n" |
| 2312 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2313 | static const char fragment_debug[] = |
| 2314 | " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n"; |
| 2315 | |
| 2316 | static const char fragment_brace[] = |
| 2317 | "}\n"; |
| 2318 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2319 | static const char texture_fragment_shader_rgba[] = |
| 2320 | "precision mediump float;\n" |
| 2321 | "varying vec2 v_texcoord;\n" |
| 2322 | "uniform sampler2D tex;\n" |
| 2323 | "uniform float alpha;\n" |
| 2324 | "void main()\n" |
| 2325 | "{\n" |
| 2326 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2327 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2328 | |
| 2329 | static const char texture_fragment_shader_rgbx[] = |
| 2330 | "precision mediump float;\n" |
| 2331 | "varying vec2 v_texcoord;\n" |
| 2332 | "uniform sampler2D tex;\n" |
| 2333 | "uniform float alpha;\n" |
| 2334 | "void main()\n" |
| 2335 | "{\n" |
| 2336 | " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;" |
| 2337 | " gl_FragColor.a = alpha;\n" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2338 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2339 | |
| 2340 | static const char texture_fragment_shader_egl_external[] = |
| 2341 | "#extension GL_OES_EGL_image_external : require\n" |
| 2342 | "precision mediump float;\n" |
| 2343 | "varying vec2 v_texcoord;\n" |
| 2344 | "uniform samplerExternalOES tex;\n" |
| 2345 | "uniform float alpha;\n" |
| 2346 | "void main()\n" |
| 2347 | "{\n" |
| 2348 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2349 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2350 | |
| 2351 | static const char texture_fragment_shader_y_uv[] = |
| 2352 | "precision mediump float;\n" |
| 2353 | "uniform sampler2D tex;\n" |
| 2354 | "uniform sampler2D tex1;\n" |
| 2355 | "varying vec2 v_texcoord;\n" |
| 2356 | "uniform float alpha;\n" |
| 2357 | "void main() {\n" |
| 2358 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 2359 | " float u = texture2D(tex1, v_texcoord).r - 0.5;\n" |
| 2360 | " float v = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 2361 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2362 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2363 | |
| 2364 | static const char texture_fragment_shader_y_u_v[] = |
| 2365 | "precision mediump float;\n" |
| 2366 | "uniform sampler2D tex;\n" |
| 2367 | "uniform sampler2D tex1;\n" |
| 2368 | "uniform sampler2D tex2;\n" |
| 2369 | "varying vec2 v_texcoord;\n" |
| 2370 | "uniform float alpha;\n" |
| 2371 | "void main() {\n" |
| 2372 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 2373 | " float u = texture2D(tex1, v_texcoord).x - 0.5;\n" |
| 2374 | " float v = texture2D(tex2, v_texcoord).x - 0.5;\n" |
| 2375 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2376 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2377 | |
| 2378 | static const char texture_fragment_shader_y_xuxv[] = |
| 2379 | "precision mediump float;\n" |
| 2380 | "uniform sampler2D tex;\n" |
| 2381 | "uniform sampler2D tex1;\n" |
| 2382 | "varying vec2 v_texcoord;\n" |
| 2383 | "uniform float alpha;\n" |
| 2384 | "void main() {\n" |
| 2385 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 2386 | " float u = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 2387 | " float v = texture2D(tex1, v_texcoord).a - 0.5;\n" |
| 2388 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2389 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2390 | |
| 2391 | static const char solid_fragment_shader[] = |
| 2392 | "precision mediump float;\n" |
| 2393 | "uniform vec4 color;\n" |
| 2394 | "uniform float alpha;\n" |
| 2395 | "void main()\n" |
| 2396 | "{\n" |
| 2397 | " gl_FragColor = alpha * color\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2398 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2399 | |
| 2400 | static int |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2401 | compile_shader(GLenum type, int count, const char **sources) |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2402 | { |
| 2403 | GLuint s; |
| 2404 | char msg[512]; |
| 2405 | GLint status; |
| 2406 | |
| 2407 | s = glCreateShader(type); |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2408 | glShaderSource(s, count, sources, NULL); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2409 | glCompileShader(s); |
| 2410 | glGetShaderiv(s, GL_COMPILE_STATUS, &status); |
| 2411 | if (!status) { |
| 2412 | glGetShaderInfoLog(s, sizeof msg, NULL, msg); |
| 2413 | weston_log("shader info: %s\n", msg); |
| 2414 | return GL_NONE; |
| 2415 | } |
| 2416 | |
| 2417 | return s; |
| 2418 | } |
| 2419 | |
| 2420 | static int |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 2421 | shader_init(struct gl_shader *shader, struct gl_renderer *renderer, |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2422 | const char *vertex_source, const char *fragment_source) |
| 2423 | { |
| 2424 | char msg[512]; |
| 2425 | GLint status; |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2426 | int count; |
| 2427 | const char *sources[3]; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2428 | |
| 2429 | shader->vertex_shader = |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2430 | compile_shader(GL_VERTEX_SHADER, 1, &vertex_source); |
| 2431 | |
| 2432 | if (renderer->fragment_shader_debug) { |
| 2433 | sources[0] = fragment_source; |
| 2434 | sources[1] = fragment_debug; |
| 2435 | sources[2] = fragment_brace; |
| 2436 | count = 3; |
| 2437 | } else { |
| 2438 | sources[0] = fragment_source; |
| 2439 | sources[1] = fragment_brace; |
| 2440 | count = 2; |
| 2441 | } |
| 2442 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2443 | shader->fragment_shader = |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2444 | compile_shader(GL_FRAGMENT_SHADER, count, sources); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2445 | |
| 2446 | shader->program = glCreateProgram(); |
| 2447 | glAttachShader(shader->program, shader->vertex_shader); |
| 2448 | glAttachShader(shader->program, shader->fragment_shader); |
| 2449 | glBindAttribLocation(shader->program, 0, "position"); |
| 2450 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 2451 | |
| 2452 | glLinkProgram(shader->program); |
| 2453 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
| 2454 | if (!status) { |
| 2455 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
| 2456 | weston_log("link info: %s\n", msg); |
| 2457 | return -1; |
| 2458 | } |
| 2459 | |
| 2460 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 2461 | shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex"); |
| 2462 | shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1"); |
| 2463 | shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2"); |
| 2464 | shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha"); |
| 2465 | shader->color_uniform = glGetUniformLocation(shader->program, "color"); |
| 2466 | |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2471 | shader_release(struct gl_shader *shader) |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 2472 | { |
| 2473 | glDeleteShader(shader->vertex_shader); |
| 2474 | glDeleteShader(shader->fragment_shader); |
| 2475 | glDeleteProgram(shader->program); |
| 2476 | |
| 2477 | shader->vertex_shader = 0; |
| 2478 | shader->fragment_shader = 0; |
| 2479 | shader->program = 0; |
| 2480 | } |
| 2481 | |
| 2482 | static void |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 2483 | log_extensions(const char *name, const char *extensions) |
| 2484 | { |
| 2485 | const char *p, *end; |
| 2486 | int l; |
| 2487 | int len; |
| 2488 | |
| 2489 | l = weston_log("%s:", name); |
| 2490 | p = extensions; |
| 2491 | while (*p) { |
| 2492 | end = strchrnul(p, ' '); |
| 2493 | len = end - p; |
| 2494 | if (l + len > 78) |
| 2495 | l = weston_log_continue("\n" STAMP_SPACE "%.*s", |
| 2496 | len, p); |
| 2497 | else |
| 2498 | l += weston_log_continue(" %.*s", len, p); |
| 2499 | for (p = end; isspace(*p); p++) |
| 2500 | ; |
| 2501 | } |
| 2502 | weston_log_continue("\n"); |
| 2503 | } |
| 2504 | |
| 2505 | static void |
| 2506 | log_egl_gl_info(EGLDisplay egldpy) |
| 2507 | { |
| 2508 | const char *str; |
| 2509 | |
| 2510 | str = eglQueryString(egldpy, EGL_VERSION); |
| 2511 | weston_log("EGL version: %s\n", str ? str : "(null)"); |
| 2512 | |
| 2513 | str = eglQueryString(egldpy, EGL_VENDOR); |
| 2514 | weston_log("EGL vendor: %s\n", str ? str : "(null)"); |
| 2515 | |
| 2516 | str = eglQueryString(egldpy, EGL_CLIENT_APIS); |
| 2517 | weston_log("EGL client APIs: %s\n", str ? str : "(null)"); |
| 2518 | |
| 2519 | str = eglQueryString(egldpy, EGL_EXTENSIONS); |
| 2520 | log_extensions("EGL extensions", str ? str : "(null)"); |
| 2521 | |
| 2522 | str = (char *)glGetString(GL_VERSION); |
| 2523 | weston_log("GL version: %s\n", str ? str : "(null)"); |
| 2524 | |
| 2525 | str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION); |
| 2526 | weston_log("GLSL version: %s\n", str ? str : "(null)"); |
| 2527 | |
| 2528 | str = (char *)glGetString(GL_VENDOR); |
| 2529 | weston_log("GL vendor: %s\n", str ? str : "(null)"); |
| 2530 | |
| 2531 | str = (char *)glGetString(GL_RENDERER); |
| 2532 | weston_log("GL renderer: %s\n", str ? str : "(null)"); |
| 2533 | |
| 2534 | str = (char *)glGetString(GL_EXTENSIONS); |
| 2535 | log_extensions("GL extensions", str ? str : "(null)"); |
| 2536 | } |
| 2537 | |
Pekka Paalanen | 9c3fe25 | 2012-10-24 09:43:05 +0300 | [diff] [blame] | 2538 | static void |
| 2539 | log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig) |
| 2540 | { |
| 2541 | EGLint r, g, b, a; |
| 2542 | |
| 2543 | weston_log("Chosen EGL config details:\n"); |
| 2544 | |
| 2545 | weston_log_continue(STAMP_SPACE "RGBA bits"); |
| 2546 | if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) && |
| 2547 | eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) && |
| 2548 | eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) && |
| 2549 | eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a)) |
| 2550 | weston_log_continue(": %d %d %d %d\n", r, g, b, a); |
| 2551 | else |
| 2552 | weston_log_continue(" unknown\n"); |
| 2553 | |
| 2554 | weston_log_continue(STAMP_SPACE "swap interval range"); |
| 2555 | if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) && |
| 2556 | eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b)) |
| 2557 | weston_log_continue(": %d - %d\n", a, b); |
| 2558 | else |
| 2559 | weston_log_continue(" unknown\n"); |
| 2560 | } |
| 2561 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2562 | static int |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2563 | match_config_to_visual(EGLDisplay egl_display, |
| 2564 | EGLint visual_id, |
| 2565 | EGLConfig *configs, |
| 2566 | int count) |
| 2567 | { |
| 2568 | int i; |
| 2569 | |
| 2570 | for (i = 0; i < count; ++i) { |
| 2571 | EGLint id; |
| 2572 | |
| 2573 | if (!eglGetConfigAttrib(egl_display, |
| 2574 | configs[i], EGL_NATIVE_VISUAL_ID, |
| 2575 | &id)) |
| 2576 | continue; |
| 2577 | |
| 2578 | if (id == visual_id) |
| 2579 | return i; |
| 2580 | } |
| 2581 | |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2582 | return -1; |
| 2583 | } |
| 2584 | |
| 2585 | static int |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2586 | egl_choose_config(struct gl_renderer *gr, const EGLint *attribs, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2587 | const EGLint *visual_id, const int n_ids, |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2588 | EGLConfig *config_out) |
| 2589 | { |
| 2590 | EGLint count = 0; |
| 2591 | EGLint matched = 0; |
| 2592 | EGLConfig *configs; |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2593 | int i, config_index = -1; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2594 | |
Derek Foreman | a7e1991 | 2015-05-20 14:57:58 -0500 | [diff] [blame] | 2595 | if (!eglGetConfigs(gr->egl_display, NULL, 0, &count) || count < 1) { |
| 2596 | weston_log("No EGL configs to choose from.\n"); |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2597 | return -1; |
Derek Foreman | a7e1991 | 2015-05-20 14:57:58 -0500 | [diff] [blame] | 2598 | } |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2599 | configs = calloc(count, sizeof *configs); |
| 2600 | if (!configs) |
| 2601 | return -1; |
| 2602 | |
| 2603 | if (!eglChooseConfig(gr->egl_display, attribs, configs, |
Derek Foreman | a7e1991 | 2015-05-20 14:57:58 -0500 | [diff] [blame] | 2604 | count, &matched) || !matched) { |
| 2605 | weston_log("No EGL configs with appropriate attributes.\n"); |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2606 | goto out; |
Derek Foreman | a7e1991 | 2015-05-20 14:57:58 -0500 | [diff] [blame] | 2607 | } |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2608 | |
Miguel A. Vico | 684c9f4 | 2016-05-18 17:48:47 +0200 | [diff] [blame] | 2609 | if (!visual_id || n_ids == 0) |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2610 | config_index = 0; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2611 | |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2612 | for (i = 0; config_index == -1 && i < n_ids; i++) |
| 2613 | config_index = match_config_to_visual(gr->egl_display, |
| 2614 | visual_id[i], |
| 2615 | configs, |
| 2616 | matched); |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2617 | |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2618 | if (config_index != -1) |
| 2619 | *config_out = configs[config_index]; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2620 | |
| 2621 | out: |
| 2622 | free(configs); |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2623 | if (config_index == -1) |
| 2624 | return -1; |
| 2625 | |
Derek Foreman | a7e1991 | 2015-05-20 14:57:58 -0500 | [diff] [blame] | 2626 | if (i > 1) |
| 2627 | weston_log("Unable to use first choice EGL config with id" |
| 2628 | " 0x%x, succeeded with alternate id 0x%x.\n", |
| 2629 | visual_id[0], visual_id[i - 1]); |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2630 | return 0; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 2633 | static void |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 2634 | gl_renderer_output_set_border(struct weston_output *output, |
| 2635 | enum gl_renderer_border_side side, |
| 2636 | int32_t width, int32_t height, |
| 2637 | int32_t tex_width, unsigned char *data) |
| 2638 | { |
| 2639 | struct gl_output_state *go = get_output_state(output); |
| 2640 | |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 2641 | if (go->borders[side].width != width || |
| 2642 | go->borders[side].height != height) |
| 2643 | /* In this case, we have to blow everything and do a full |
| 2644 | * repaint. */ |
| 2645 | go->border_status |= BORDER_SIZE_CHANGED | BORDER_ALL_DIRTY; |
| 2646 | |
| 2647 | if (data == NULL) { |
| 2648 | width = 0; |
| 2649 | height = 0; |
| 2650 | } |
| 2651 | |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 2652 | go->borders[side].width = width; |
| 2653 | go->borders[side].height = height; |
| 2654 | go->borders[side].tex_width = tex_width; |
| 2655 | go->borders[side].data = data; |
Jason Ekstrand | e5512d4 | 2014-02-04 21:36:38 -0600 | [diff] [blame] | 2656 | go->border_status |= 1 << side; |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 2657 | } |
| 2658 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2659 | static int |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2660 | gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2661 | |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2662 | static EGLSurface |
| 2663 | gl_renderer_create_window_surface(struct gl_renderer *gr, |
| 2664 | EGLNativeWindowType window_for_legacy, |
| 2665 | void *window_for_platform, |
| 2666 | const EGLint *config_attribs, |
| 2667 | const EGLint *visual_id, |
| 2668 | int n_ids) |
| 2669 | { |
| 2670 | EGLSurface egl_surface = EGL_NO_SURFACE; |
| 2671 | EGLConfig egl_config; |
| 2672 | |
| 2673 | if (egl_choose_config(gr, config_attribs, visual_id, |
| 2674 | n_ids, &egl_config) == -1) { |
| 2675 | weston_log("failed to choose EGL config for output\n"); |
| 2676 | return EGL_NO_SURFACE; |
| 2677 | } |
| 2678 | |
| 2679 | if (egl_config != gr->egl_config && |
| 2680 | !gr->has_configless_context) { |
| 2681 | weston_log("attempted to use a different EGL config for an " |
Armin Krezović | e3bfee1 | 2016-09-30 14:27:38 +0200 | [diff] [blame] | 2682 | "output but EGL_KHR_no_config_context or " |
| 2683 | "EGL_MESA_configless_context is not supported\n"); |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2684 | return EGL_NO_SURFACE; |
| 2685 | } |
| 2686 | |
| 2687 | log_egl_config_info(gr->egl_display, egl_config); |
| 2688 | |
| 2689 | if (gr->create_platform_window) |
| 2690 | egl_surface = gr->create_platform_window(gr->egl_display, |
| 2691 | egl_config, |
| 2692 | window_for_platform, |
| 2693 | NULL); |
| 2694 | else |
| 2695 | egl_surface = eglCreateWindowSurface(gr->egl_display, |
| 2696 | egl_config, |
| 2697 | window_for_legacy, NULL); |
| 2698 | |
| 2699 | return egl_surface; |
| 2700 | } |
| 2701 | |
| 2702 | static int |
| 2703 | gl_renderer_output_create(struct weston_output *output, |
| 2704 | EGLSurface surface) |
| 2705 | { |
| 2706 | struct gl_output_state *go; |
| 2707 | int i; |
| 2708 | |
| 2709 | go = zalloc(sizeof *go); |
| 2710 | if (go == NULL) |
| 2711 | return -1; |
| 2712 | |
| 2713 | go->egl_surface = surface; |
| 2714 | |
| 2715 | for (i = 0; i < BUFFER_DAMAGE_COUNT; i++) |
| 2716 | pixman_region32_init(&go->buffer_damage[i]); |
| 2717 | |
| 2718 | output->renderer_state = go; |
| 2719 | |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2723 | static int |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 2724 | gl_renderer_output_window_create(struct weston_output *output, |
| 2725 | EGLNativeWindowType window_for_legacy, |
| 2726 | void *window_for_platform, |
Miguel A. Vico | 4057cd9 | 2016-05-18 17:44:22 +0200 | [diff] [blame] | 2727 | const EGLint *config_attribs, |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 2728 | const EGLint *visual_id, |
| 2729 | int n_ids) |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2730 | { |
| 2731 | struct weston_compositor *ec = output->compositor; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2732 | struct gl_renderer *gr = get_renderer(ec); |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2733 | EGLSurface egl_surface = EGL_NO_SURFACE; |
| 2734 | int ret = 0; |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2735 | |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2736 | egl_surface = gl_renderer_create_window_surface(gr, |
| 2737 | window_for_legacy, |
| 2738 | window_for_platform, |
| 2739 | config_attribs, |
| 2740 | visual_id, n_ids); |
| 2741 | if (egl_surface == EGL_NO_SURFACE) { |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2742 | weston_log("failed to create egl surface\n"); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2743 | return -1; |
| 2744 | } |
| 2745 | |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2746 | ret = gl_renderer_output_create(output, egl_surface); |
| 2747 | if (ret < 0) |
Emil Velikov | 0263955 | 2016-11-14 17:08:17 +0000 | [diff] [blame] | 2748 | weston_platform_destroy_egl_surface(gr->egl_display, egl_surface); |
Ander Conselvan de Oliveira | b8fcca9 | 2012-11-16 17:23:52 +0200 | [diff] [blame] | 2749 | |
Miguel A. Vico | 967b6bc | 2016-05-18 17:50:54 +0200 | [diff] [blame] | 2750 | return ret; |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2753 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2754 | gl_renderer_output_destroy(struct weston_output *output) |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2755 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2756 | struct gl_renderer *gr = get_renderer(output->compositor); |
| 2757 | struct gl_output_state *go = get_output_state(output); |
Ander Conselvan de Oliveira | b8fcca9 | 2012-11-16 17:23:52 +0200 | [diff] [blame] | 2758 | int i; |
| 2759 | |
| 2760 | for (i = 0; i < 2; i++) |
| 2761 | pixman_region32_fini(&go->buffer_damage[i]); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2762 | |
Emil Velikov | 0263955 | 2016-11-14 17:08:17 +0000 | [diff] [blame] | 2763 | weston_platform_destroy_egl_surface(gr->egl_display, go->egl_surface); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2764 | |
| 2765 | free(go); |
| 2766 | } |
| 2767 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2768 | static EGLSurface |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2769 | gl_renderer_output_surface(struct weston_output *output) |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2770 | { |
| 2771 | return get_output_state(output)->egl_surface; |
| 2772 | } |
| 2773 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 2774 | static void |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2775 | gl_renderer_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 2776 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2777 | struct gl_renderer *gr = get_renderer(ec); |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 2778 | struct dmabuf_image *image, *next; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2779 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 2780 | wl_signal_emit(&gr->destroy_signal, gr); |
| 2781 | |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 2782 | if (gr->has_bind_display) |
| 2783 | gr->unbind_display(gr->egl_display, ec->wl_display); |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2784 | |
| 2785 | /* Work around crash in egl_dri2.c's dri2_make_current() - when does this apply? */ |
| 2786 | eglMakeCurrent(gr->egl_display, |
| 2787 | EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 2788 | EGL_NO_CONTEXT); |
| 2789 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2790 | |
Emmanuel Gil Peyrot | b805350 | 2016-01-11 19:04:34 +0000 | [diff] [blame] | 2791 | wl_list_for_each_safe(image, next, &gr->dmabuf_images, link) |
| 2792 | dmabuf_image_destroy(image); |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2793 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 2794 | if (gr->dummy_surface != EGL_NO_SURFACE) |
Emil Velikov | 0263955 | 2016-11-14 17:08:17 +0000 | [diff] [blame] | 2795 | weston_platform_destroy_egl_surface(gr->egl_display, |
| 2796 | gr->dummy_surface); |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 2797 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2798 | eglTerminate(gr->egl_display); |
| 2799 | eglReleaseThread(); |
Scott Moreau | 976a050 | 2013-03-07 10:15:17 -0700 | [diff] [blame] | 2800 | |
Armin Krezović | 36d699a | 2016-08-05 15:28:30 +0200 | [diff] [blame] | 2801 | wl_list_remove(&gr->output_destroy_listener.link); |
| 2802 | |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 2803 | wl_array_release(&gr->vertices); |
Kristian Høgsberg | 7b9195f | 2013-05-08 22:38:05 -0400 | [diff] [blame] | 2804 | wl_array_release(&gr->vtxcnt); |
| 2805 | |
Mariusz Ceier | cbb9158 | 2014-02-08 20:11:24 +0100 | [diff] [blame] | 2806 | if (gr->fragment_binding) |
| 2807 | weston_binding_destroy(gr->fragment_binding); |
| 2808 | if (gr->fan_binding) |
| 2809 | weston_binding_destroy(gr->fan_binding); |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 2810 | |
Scott Moreau | 976a050 | 2013-03-07 10:15:17 -0700 | [diff] [blame] | 2811 | free(gr); |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2812 | } |
| 2813 | |
Pekka Paalanen | 8b69d03 | 2015-04-08 17:02:22 +0300 | [diff] [blame] | 2814 | static void |
| 2815 | renderer_setup_egl_client_extensions(struct gl_renderer *gr) |
| 2816 | { |
| 2817 | const char *extensions; |
| 2818 | |
| 2819 | extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
| 2820 | if (!extensions) { |
| 2821 | weston_log("Retrieving EGL client extension string failed.\n"); |
| 2822 | return; |
| 2823 | } |
| 2824 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2825 | if (weston_check_egl_extension(extensions, "EGL_EXT_platform_base")) |
Pekka Paalanen | 8b69d03 | 2015-04-08 17:02:22 +0300 | [diff] [blame] | 2826 | gr->create_platform_window = |
| 2827 | (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"); |
| 2828 | else |
| 2829 | weston_log("warning: EGL_EXT_platform_base not supported.\n"); |
| 2830 | } |
| 2831 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2832 | static int |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2833 | gl_renderer_setup_egl_extensions(struct weston_compositor *ec) |
| 2834 | { |
Emil Velikov | 43cea54 | 2016-11-14 16:03:45 +0000 | [diff] [blame] | 2835 | static const struct { |
| 2836 | char *extension, *entrypoint; |
| 2837 | } swap_damage_ext_to_entrypoint[] = { |
| 2838 | { |
| 2839 | .extension = "EGL_EXT_swap_buffers_with_damage", |
| 2840 | .entrypoint = "eglSwapBuffersWithDamageEXT", |
| 2841 | }, |
| 2842 | { |
| 2843 | .extension = "EGL_KHR_swap_buffers_with_damage", |
| 2844 | .entrypoint = "eglSwapBuffersWithDamageKHR", |
| 2845 | }, |
| 2846 | }; |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2847 | struct gl_renderer *gr = get_renderer(ec); |
| 2848 | const char *extensions; |
| 2849 | EGLBoolean ret; |
| 2850 | |
| 2851 | gr->create_image = (void *) eglGetProcAddress("eglCreateImageKHR"); |
| 2852 | gr->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR"); |
| 2853 | gr->bind_display = |
| 2854 | (void *) eglGetProcAddress("eglBindWaylandDisplayWL"); |
| 2855 | gr->unbind_display = |
| 2856 | (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL"); |
| 2857 | gr->query_buffer = |
| 2858 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 2859 | |
| 2860 | extensions = |
| 2861 | (const char *) eglQueryString(gr->egl_display, EGL_EXTENSIONS); |
| 2862 | if (!extensions) { |
| 2863 | weston_log("Retrieving EGL extension string failed.\n"); |
| 2864 | return -1; |
| 2865 | } |
| 2866 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2867 | if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display")) |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2868 | gr->has_bind_display = 1; |
| 2869 | if (gr->has_bind_display) { |
| 2870 | ret = gr->bind_display(gr->egl_display, ec->wl_display); |
| 2871 | if (!ret) |
| 2872 | gr->has_bind_display = 0; |
| 2873 | } |
| 2874 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2875 | if (weston_check_egl_extension(extensions, "EGL_EXT_buffer_age")) |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2876 | gr->has_egl_buffer_age = 1; |
| 2877 | else |
| 2878 | weston_log("warning: EGL_EXT_buffer_age not supported. " |
| 2879 | "Performance could be affected.\n"); |
| 2880 | |
Emil Velikov | 43cea54 | 2016-11-14 16:03:45 +0000 | [diff] [blame] | 2881 | for (unsigned i = 0; i < ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) { |
| 2882 | if (weston_check_egl_extension(extensions, |
| 2883 | swap_damage_ext_to_entrypoint[i].extension)) { |
| 2884 | gr->swap_buffers_with_damage = |
| 2885 | (void *) eglGetProcAddress( |
| 2886 | swap_damage_ext_to_entrypoint[i].entrypoint); |
| 2887 | break; |
| 2888 | } |
| 2889 | } |
| 2890 | if (!gr->swap_buffers_with_damage) |
| 2891 | weston_log("warning: neither %s or %s is supported. " |
| 2892 | "Performance could be affected.\n", |
| 2893 | swap_damage_ext_to_entrypoint[0].extension, |
| 2894 | swap_damage_ext_to_entrypoint[1].extension); |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2895 | |
Armin Krezović | e3bfee1 | 2016-09-30 14:27:38 +0200 | [diff] [blame] | 2896 | if (weston_check_egl_extension(extensions, "EGL_KHR_no_config_context") || |
| 2897 | weston_check_egl_extension(extensions, "EGL_MESA_configless_context")) |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2898 | gr->has_configless_context = 1; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2899 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2900 | if (weston_check_egl_extension(extensions, "EGL_KHR_surfaceless_context")) |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 2901 | gr->has_surfaceless_context = 1; |
| 2902 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2903 | if (weston_check_egl_extension(extensions, "EGL_EXT_image_dma_buf_import")) |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2904 | gr->has_dmabuf_import = 1; |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 2905 | |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 2906 | if (weston_check_egl_extension(extensions, "GL_EXT_texture_rg")) |
| 2907 | gr->has_gl_texture_rg = 1; |
| 2908 | |
Pekka Paalanen | 8b69d03 | 2015-04-08 17:02:22 +0300 | [diff] [blame] | 2909 | renderer_setup_egl_client_extensions(gr); |
| 2910 | |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 2911 | return 0; |
| 2912 | } |
| 2913 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2914 | static const EGLint gl_renderer_opaque_attribs[] = { |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2915 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 2916 | EGL_RED_SIZE, 1, |
| 2917 | EGL_GREEN_SIZE, 1, |
| 2918 | EGL_BLUE_SIZE, 1, |
| 2919 | EGL_ALPHA_SIZE, 0, |
| 2920 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 2921 | EGL_NONE |
| 2922 | }; |
| 2923 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2924 | static const EGLint gl_renderer_alpha_attribs[] = { |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2925 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 2926 | EGL_RED_SIZE, 1, |
| 2927 | EGL_GREEN_SIZE, 1, |
| 2928 | EGL_BLUE_SIZE, 1, |
| 2929 | EGL_ALPHA_SIZE, 1, |
| 2930 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 2931 | EGL_NONE |
| 2932 | }; |
| 2933 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 2934 | |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2935 | /** Checks whether a platform EGL client extension is supported |
| 2936 | * |
| 2937 | * \param ec The weston compositor |
| 2938 | * \param extension_suffix The EGL client extension suffix |
| 2939 | * \return 1 if supported, 0 if using fallbacks, -1 unsupported |
| 2940 | * |
| 2941 | * This function checks whether a specific platform_* extension is supported |
| 2942 | * by EGL. |
| 2943 | * |
| 2944 | * The extension suffix should be the suffix of the platform extension (that |
| 2945 | * specifies a <platform> argument as defined in EGL_EXT_platform_base). For |
| 2946 | * example, passing "foo" will check whether either "EGL_KHR_platform_foo", |
| 2947 | * "EGL_EXT_platform_foo", or "EGL_MESA_platform_foo" is supported. |
| 2948 | * |
| 2949 | * The return value is 1: |
| 2950 | * - if the supplied EGL client extension is supported. |
| 2951 | * The return value is 0: |
| 2952 | * - if the platform_base client extension isn't supported so will |
| 2953 | * fallback to eglGetDisplay and friends. |
| 2954 | * The return value is -1: |
| 2955 | * - if the supplied EGL client extension is not supported. |
| 2956 | */ |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2957 | static int |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2958 | gl_renderer_supports(struct weston_compositor *ec, |
| 2959 | const char *extension_suffix) |
| 2960 | { |
| 2961 | static const char *extensions = NULL; |
| 2962 | char s[64]; |
| 2963 | |
| 2964 | if (!extensions) { |
| 2965 | extensions = (const char *) eglQueryString( |
| 2966 | EGL_NO_DISPLAY, EGL_EXTENSIONS); |
| 2967 | |
| 2968 | if (!extensions) |
| 2969 | return 0; |
| 2970 | |
| 2971 | log_extensions("EGL client extensions", |
| 2972 | extensions); |
| 2973 | } |
| 2974 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2975 | if (!weston_check_egl_extension(extensions, "EGL_EXT_platform_base")) |
Pekka Paalanen | f282454 | 2015-04-08 17:02:21 +0300 | [diff] [blame] | 2976 | return 0; |
| 2977 | |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2978 | snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix); |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2979 | if (weston_check_egl_extension(extensions, s)) |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2980 | return 1; |
| 2981 | |
| 2982 | snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix); |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2983 | if (weston_check_egl_extension(extensions, s)) |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2984 | return 1; |
| 2985 | |
| 2986 | snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix); |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 2987 | if (weston_check_egl_extension(extensions, s)) |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2988 | return 1; |
| 2989 | |
Pekka Paalanen | f282454 | 2015-04-08 17:02:21 +0300 | [diff] [blame] | 2990 | /* at this point we definitely have some platform extensions but |
| 2991 | * haven't found the supplied platform, so chances are it's |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 2992 | * not supported. */ |
| 2993 | |
| 2994 | return -1; |
| 2995 | } |
| 2996 | |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 2997 | static const char * |
| 2998 | platform_to_extension(EGLenum platform) |
| 2999 | { |
| 3000 | switch (platform) { |
| 3001 | case EGL_PLATFORM_GBM_KHR: |
| 3002 | return "gbm"; |
| 3003 | case EGL_PLATFORM_WAYLAND_KHR: |
| 3004 | return "wayland"; |
| 3005 | case EGL_PLATFORM_X11_KHR: |
| 3006 | return "x11"; |
| 3007 | default: |
| 3008 | assert(0 && "bad EGL platform enum"); |
| 3009 | } |
| 3010 | } |
| 3011 | |
Armin Krezović | 36d699a | 2016-08-05 15:28:30 +0200 | [diff] [blame] | 3012 | static void |
| 3013 | output_handle_destroy(struct wl_listener *listener, void *data) |
| 3014 | { |
| 3015 | struct gl_renderer *gr; |
| 3016 | struct weston_output *output = data; |
| 3017 | |
| 3018 | gr = container_of(listener, struct gl_renderer, |
| 3019 | output_destroy_listener); |
| 3020 | |
| 3021 | if (wl_list_empty(&output->compositor->output_list)) |
| 3022 | eglMakeCurrent(gr->egl_display, gr->dummy_surface, |
| 3023 | gr->dummy_surface, gr->egl_context); |
| 3024 | } |
| 3025 | |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 3026 | static int |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 3027 | gl_renderer_create_pbuffer_surface(struct gl_renderer *gr) { |
| 3028 | EGLConfig pbuffer_config; |
| 3029 | |
| 3030 | static const EGLint pbuffer_config_attribs[] = { |
| 3031 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 3032 | EGL_RED_SIZE, 1, |
| 3033 | EGL_GREEN_SIZE, 1, |
| 3034 | EGL_BLUE_SIZE, 1, |
| 3035 | EGL_ALPHA_SIZE, 0, |
| 3036 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 3037 | EGL_NONE |
| 3038 | }; |
| 3039 | |
| 3040 | static const EGLint pbuffer_attribs[] = { |
| 3041 | EGL_WIDTH, 10, |
| 3042 | EGL_HEIGHT, 10, |
| 3043 | EGL_NONE |
| 3044 | }; |
| 3045 | |
| 3046 | if (egl_choose_config(gr, pbuffer_config_attribs, NULL, 0, &pbuffer_config) < 0) { |
| 3047 | weston_log("failed to choose EGL config for PbufferSurface"); |
| 3048 | return -1; |
| 3049 | } |
| 3050 | |
| 3051 | gr->dummy_surface = eglCreatePbufferSurface(gr->egl_display, |
| 3052 | pbuffer_config, |
| 3053 | pbuffer_attribs); |
| 3054 | |
| 3055 | if (gr->dummy_surface == EGL_NO_SURFACE) { |
| 3056 | weston_log("failed to create PbufferSurface\n"); |
| 3057 | return -1; |
| 3058 | } |
| 3059 | |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | static int |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3064 | gl_renderer_display_create(struct weston_compositor *ec, EGLenum platform, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3065 | void *native_window, const EGLint *platform_attribs, |
| 3066 | const EGLint *config_attribs, const EGLint *visual_id, int n_ids) |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3067 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3068 | struct gl_renderer *gr; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3069 | EGLint major, minor; |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3070 | int supports = 0; |
| 3071 | |
| 3072 | if (platform) { |
| 3073 | supports = gl_renderer_supports( |
| 3074 | ec, platform_to_extension(platform)); |
| 3075 | if (supports < 0) |
| 3076 | return -1; |
| 3077 | } |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3078 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 3079 | gr = zalloc(sizeof *gr); |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3080 | if (gr == NULL) |
| 3081 | return -1; |
| 3082 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3083 | gr->base.read_pixels = gl_renderer_read_pixels; |
| 3084 | gr->base.repaint_output = gl_renderer_repaint_output; |
| 3085 | gr->base.flush_damage = gl_renderer_flush_damage; |
| 3086 | gr->base.attach = gl_renderer_attach; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3087 | gr->base.surface_set_color = gl_renderer_surface_set_color; |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 3088 | gr->base.destroy = gl_renderer_destroy; |
Pekka Paalanen | eb35cbe | 2015-02-09 13:37:27 +0200 | [diff] [blame] | 3089 | gr->base.surface_get_content_size = |
| 3090 | gl_renderer_surface_get_content_size; |
| 3091 | gr->base.surface_copy_content = gl_renderer_surface_copy_content; |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3092 | gr->egl_display = NULL; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3093 | |
Jonny Lamb | f1ec506 | 2015-03-24 13:12:05 +0100 | [diff] [blame] | 3094 | /* extension_suffix is supported */ |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3095 | if (supports) { |
| 3096 | if (!get_platform_display) { |
| 3097 | get_platform_display = (void *) eglGetProcAddress( |
| 3098 | "eglGetPlatformDisplayEXT"); |
| 3099 | } |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 3100 | |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3101 | /* also wrap this in the supports check because |
| 3102 | * eglGetProcAddress can return non-NULL and still not |
| 3103 | * support the feature at runtime, so ensure the |
| 3104 | * appropriate extension checks have been done. */ |
| 3105 | if (get_platform_display && platform) { |
| 3106 | gr->egl_display = get_platform_display(platform, |
| 3107 | native_window, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 3108 | platform_attribs); |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3109 | } |
| 3110 | } |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 3111 | |
| 3112 | if (!gr->egl_display) { |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 3113 | weston_log("warning: either no EGL_EXT_platform_base " |
| 3114 | "support or specific platform support; " |
| 3115 | "falling back to eglGetDisplay.\n"); |
| 3116 | gr->egl_display = eglGetDisplay(native_window); |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 3117 | } |
Jonny Lamb | 70eba3f | 2015-03-20 15:26:50 +0100 | [diff] [blame] | 3118 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3119 | if (gr->egl_display == EGL_NO_DISPLAY) { |
| 3120 | weston_log("failed to create display\n"); |
Derek Foreman | 066ca0c | 2015-06-11 12:14:45 -0500 | [diff] [blame] | 3121 | goto fail; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | if (!eglInitialize(gr->egl_display, &major, &minor)) { |
| 3125 | weston_log("failed to initialize display\n"); |
Derek Foreman | 066ca0c | 2015-06-11 12:14:45 -0500 | [diff] [blame] | 3126 | goto fail_with_error; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3127 | } |
| 3128 | |
Miguel A. Vico | 4057cd9 | 2016-05-18 17:44:22 +0200 | [diff] [blame] | 3129 | if (egl_choose_config(gr, config_attribs, visual_id, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 3130 | n_ids, &gr->egl_config) < 0) { |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3131 | weston_log("failed to choose EGL config\n"); |
Dawid Gajownik | 1a912a9 | 2015-08-21 00:20:54 -0300 | [diff] [blame] | 3132 | goto fail_terminate; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | ec->renderer = &gr->base; |
Pekka Paalanen | 7bb6510 | 2013-05-22 18:03:04 +0300 | [diff] [blame] | 3136 | ec->capabilities |= WESTON_CAP_ROTATION_ANY; |
Pekka Paalanen | 4fc5dd0 | 2013-05-22 18:03:05 +0300 | [diff] [blame] | 3137 | ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP; |
Pekka Paalanen | fa79b1d | 2015-02-18 09:48:59 +0200 | [diff] [blame] | 3138 | ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK; |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3139 | |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 3140 | if (gl_renderer_setup_egl_extensions(ec) < 0) |
Derek Foreman | 066ca0c | 2015-06-11 12:14:45 -0500 | [diff] [blame] | 3141 | goto fail_with_error; |
Neil Roberts | b7f8533 | 2014-03-07 18:05:49 +0000 | [diff] [blame] | 3142 | |
Pekka Paalanen | a352580 | 2014-06-12 16:49:29 +0300 | [diff] [blame] | 3143 | wl_list_init(&gr->dmabuf_images); |
| 3144 | if (gr->has_dmabuf_import) |
| 3145 | gr->base.import_dmabuf = gl_renderer_import_dmabuf; |
| 3146 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 3147 | if (gr->has_surfaceless_context) { |
| 3148 | weston_log("EGL_KHR_surfaceless_context available\n"); |
| 3149 | gr->dummy_surface = EGL_NO_SURFACE; |
| 3150 | } else { |
| 3151 | weston_log("EGL_KHR_surfaceless_context unavailable. " |
| 3152 | "Trying PbufferSurface\n"); |
| 3153 | |
| 3154 | if (gl_renderer_create_pbuffer_surface(gr) < 0) |
| 3155 | goto fail_with_error; |
| 3156 | } |
| 3157 | |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 3158 | wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565); |
Vincent Abriou | fdeefe4 | 2016-10-05 14:54:34 +0200 | [diff] [blame] | 3159 | wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUV420); |
Vincent Abriou | 00a03d2 | 2016-10-05 14:54:35 +0200 | [diff] [blame] | 3160 | wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_NV12); |
Vincent ABRIOU | e5732c7 | 2016-10-20 16:20:11 +0200 | [diff] [blame^] | 3161 | wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_YUYV); |
Tomeu Vizoso | 12072b6 | 2013-08-06 20:05:55 +0200 | [diff] [blame] | 3162 | |
Ander Conselvan de Oliveira | adda00e | 2013-10-25 16:26:34 +0300 | [diff] [blame] | 3163 | wl_signal_init(&gr->destroy_signal); |
| 3164 | |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 3165 | if (gl_renderer_setup(ec, gr->dummy_surface) < 0) { |
| 3166 | if (gr->dummy_surface != EGL_NO_SURFACE) |
Emil Velikov | 0263955 | 2016-11-14 17:08:17 +0000 | [diff] [blame] | 3167 | weston_platform_destroy_egl_surface(gr->egl_display, |
| 3168 | gr->dummy_surface); |
Armin Krezović | 28d240f | 2016-06-23 11:59:35 +0200 | [diff] [blame] | 3169 | goto fail_with_error; |
| 3170 | } |
| 3171 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3172 | return 0; |
| 3173 | |
Derek Foreman | 066ca0c | 2015-06-11 12:14:45 -0500 | [diff] [blame] | 3174 | fail_with_error: |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 3175 | gl_renderer_print_egl_error_state(); |
Dawid Gajownik | 1a912a9 | 2015-08-21 00:20:54 -0300 | [diff] [blame] | 3176 | fail_terminate: |
| 3177 | eglTerminate(gr->egl_display); |
Derek Foreman | 066ca0c | 2015-06-11 12:14:45 -0500 | [diff] [blame] | 3178 | fail: |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3179 | free(gr); |
| 3180 | return -1; |
| 3181 | } |
| 3182 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3183 | static EGLDisplay |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3184 | gl_renderer_display(struct weston_compositor *ec) |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3185 | { |
| 3186 | return get_renderer(ec)->egl_display; |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 3187 | } |
| 3188 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3189 | static int |
| 3190 | compile_shaders(struct weston_compositor *ec) |
| 3191 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3192 | struct gl_renderer *gr = get_renderer(ec); |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 3193 | |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 3194 | gr->texture_shader_rgba.vertex_source = vertex_shader; |
| 3195 | gr->texture_shader_rgba.fragment_source = texture_fragment_shader_rgba; |
| 3196 | |
| 3197 | gr->texture_shader_rgbx.vertex_source = vertex_shader; |
| 3198 | gr->texture_shader_rgbx.fragment_source = texture_fragment_shader_rgbx; |
| 3199 | |
| 3200 | gr->texture_shader_egl_external.vertex_source = vertex_shader; |
| 3201 | gr->texture_shader_egl_external.fragment_source = |
| 3202 | texture_fragment_shader_egl_external; |
| 3203 | |
| 3204 | gr->texture_shader_y_uv.vertex_source = vertex_shader; |
| 3205 | gr->texture_shader_y_uv.fragment_source = texture_fragment_shader_y_uv; |
| 3206 | |
| 3207 | gr->texture_shader_y_u_v.vertex_source = vertex_shader; |
| 3208 | gr->texture_shader_y_u_v.fragment_source = |
| 3209 | texture_fragment_shader_y_u_v; |
| 3210 | |
Ander Conselvan de Oliveira | 41a50ea | 2013-11-27 17:43:51 +0200 | [diff] [blame] | 3211 | gr->texture_shader_y_xuxv.vertex_source = vertex_shader; |
Ander Conselvan de Oliveira | 1ed7324 | 2013-05-17 14:00:40 +0300 | [diff] [blame] | 3212 | gr->texture_shader_y_xuxv.fragment_source = |
| 3213 | texture_fragment_shader_y_xuxv; |
| 3214 | |
| 3215 | gr->solid_shader.vertex_source = vertex_shader; |
| 3216 | gr->solid_shader.fragment_source = solid_fragment_shader; |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3217 | |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
| 3221 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3222 | fragment_debug_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 3223 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3224 | { |
| 3225 | struct weston_compositor *ec = data; |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3226 | struct gl_renderer *gr = get_renderer(ec); |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3227 | struct weston_output *output; |
| 3228 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 3229 | gr->fragment_shader_debug ^= 1; |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3230 | |
John Kåre Alsaker | 4068414 | 2012-11-13 19:10:25 +0100 | [diff] [blame] | 3231 | shader_release(&gr->texture_shader_rgba); |
| 3232 | shader_release(&gr->texture_shader_rgbx); |
| 3233 | shader_release(&gr->texture_shader_egl_external); |
| 3234 | shader_release(&gr->texture_shader_y_uv); |
| 3235 | shader_release(&gr->texture_shader_y_u_v); |
| 3236 | shader_release(&gr->texture_shader_y_xuxv); |
| 3237 | shader_release(&gr->solid_shader); |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3238 | |
Ander Conselvan de Oliveira | 03fb4ef | 2012-12-03 17:08:11 +0200 | [diff] [blame] | 3239 | /* Force use_shader() to call glUseProgram(), since we need to use |
| 3240 | * the recompiled version of the shader. */ |
| 3241 | gr->current_shader = NULL; |
| 3242 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3243 | wl_list_for_each(output, &ec->output_list, link) |
| 3244 | weston_output_damage(output); |
| 3245 | } |
| 3246 | |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 3247 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3248 | fan_debug_repaint_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 3249 | uint32_t key, void *data) |
Kristian Høgsberg | 8799d41 | 2013-05-07 10:50:09 -0400 | [diff] [blame] | 3250 | { |
| 3251 | struct weston_compositor *compositor = data; |
| 3252 | struct gl_renderer *gr = get_renderer(compositor); |
| 3253 | |
| 3254 | gr->fan_debug = !gr->fan_debug; |
| 3255 | weston_compositor_damage_all(compositor); |
| 3256 | } |
| 3257 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 3258 | static int |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3259 | gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface) |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3260 | { |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 3261 | struct gl_renderer *gr = get_renderer(ec); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3262 | const char *extensions; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 3263 | EGLConfig context_config; |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3264 | EGLBoolean ret; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3265 | |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3266 | static const EGLint context_attribs[] = { |
| 3267 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 3268 | EGL_NONE |
| 3269 | }; |
| 3270 | |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3271 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 3272 | weston_log("failed to bind EGL_OPENGL_ES_API\n"); |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 3273 | gl_renderer_print_egl_error_state(); |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3274 | return -1; |
| 3275 | } |
Pekka Paalanen | 9c3fe25 | 2012-10-24 09:43:05 +0300 | [diff] [blame] | 3276 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 3277 | context_config = gr->egl_config; |
Pekka Paalanen | 9c3fe25 | 2012-10-24 09:43:05 +0300 | [diff] [blame] | 3278 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 3279 | if (gr->has_configless_context) |
Armin Krezović | e3bfee1 | 2016-09-30 14:27:38 +0200 | [diff] [blame] | 3280 | context_config = EGL_NO_CONFIG_KHR; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 3281 | |
| 3282 | gr->egl_context = eglCreateContext(gr->egl_display, context_config, |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3283 | EGL_NO_CONTEXT, context_attribs); |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3284 | if (gr->egl_context == NULL) { |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3285 | weston_log("failed to create context\n"); |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 3286 | gl_renderer_print_egl_error_state(); |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 3287 | return -1; |
| 3288 | } |
| 3289 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3290 | ret = eglMakeCurrent(gr->egl_display, egl_surface, |
| 3291 | egl_surface, gr->egl_context); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3292 | if (ret == EGL_FALSE) { |
| 3293 | weston_log("Failed to make EGL context current.\n"); |
Pekka Paalanen | 326529f | 2012-11-27 12:25:25 +0200 | [diff] [blame] | 3294 | gl_renderer_print_egl_error_state(); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3295 | return -1; |
| 3296 | } |
| 3297 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3298 | log_egl_gl_info(gr->egl_display); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3299 | |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 3300 | gr->image_target_texture_2d = |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3301 | (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES"); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3302 | |
| 3303 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 3304 | if (!extensions) { |
| 3305 | weston_log("Retrieving GL extension string failed.\n"); |
| 3306 | return -1; |
| 3307 | } |
| 3308 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 3309 | if (!weston_check_egl_extension(extensions, "GL_EXT_texture_format_BGRA8888")) { |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3310 | weston_log("GL_EXT_texture_format_BGRA8888 not available\n"); |
| 3311 | return -1; |
| 3312 | } |
| 3313 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 3314 | if (weston_check_egl_extension(extensions, "GL_EXT_read_format_bgra")) |
John Kåre Alsaker | f9e710b | 2012-11-13 19:10:22 +0100 | [diff] [blame] | 3315 | ec->read_format = PIXMAN_a8r8g8b8; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3316 | else |
John Kåre Alsaker | f9e710b | 2012-11-13 19:10:22 +0100 | [diff] [blame] | 3317 | ec->read_format = PIXMAN_a8b8g8r8; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3318 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 3319 | if (weston_check_egl_extension(extensions, "GL_EXT_unpack_subimage")) |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 3320 | gr->has_unpack_subimage = 1; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3321 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 3322 | if (weston_check_egl_extension(extensions, "GL_OES_EGL_image_external")) |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 3323 | gr->has_egl_image_external = 1; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3324 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3325 | glActiveTexture(GL_TEXTURE0); |
| 3326 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3327 | if (compile_shaders(ec)) |
| 3328 | return -1; |
| 3329 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 3330 | gr->fragment_binding = |
| 3331 | weston_compositor_add_debug_binding(ec, KEY_S, |
| 3332 | fragment_debug_binding, |
| 3333 | ec); |
| 3334 | gr->fan_binding = |
| 3335 | weston_compositor_add_debug_binding(ec, KEY_F, |
| 3336 | fan_debug_repaint_binding, |
| 3337 | ec); |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 3338 | |
Armin Krezović | 36d699a | 2016-08-05 15:28:30 +0200 | [diff] [blame] | 3339 | gr->output_destroy_listener.notify = output_handle_destroy; |
| 3340 | wl_signal_add(&ec->output_destroyed_signal, |
| 3341 | &gr->output_destroy_listener); |
| 3342 | |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 3343 | weston_log("GL ES 2 renderer features:\n"); |
| 3344 | weston_log_continue(STAMP_SPACE "read-back format: %s\n", |
Pekka Paalanen | fe4eacf | 2013-01-10 16:50:42 +0200 | [diff] [blame] | 3345 | ec->read_format == PIXMAN_a8r8g8b8 ? "BGRA" : "RGBA"); |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 3346 | weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n", |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 3347 | gr->has_unpack_subimage ? "yes" : "no"); |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 3348 | weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n", |
John Kåre Alsaker | 320711d | 2012-11-13 19:10:27 +0100 | [diff] [blame] | 3349 | gr->has_bind_display ? "yes" : "no"); |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 3350 | |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 3351 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 3352 | return 0; |
| 3353 | } |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3354 | |
| 3355 | WL_EXPORT struct gl_renderer_interface gl_renderer_interface = { |
| 3356 | .opaque_attribs = gl_renderer_opaque_attribs, |
| 3357 | .alpha_attribs = gl_renderer_alpha_attribs, |
| 3358 | |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 3359 | .display_create = gl_renderer_display_create, |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3360 | .display = gl_renderer_display, |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 3361 | .output_window_create = gl_renderer_output_window_create, |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3362 | .output_destroy = gl_renderer_output_destroy, |
| 3363 | .output_surface = gl_renderer_output_surface, |
Jason Ekstrand | 0b61bf4 | 2013-10-27 22:24:54 -0500 | [diff] [blame] | 3364 | .output_set_border = gl_renderer_output_set_border, |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 3365 | .print_egl_error_state = gl_renderer_print_egl_error_state |
| 3366 | }; |