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