Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 23 | #define _GNU_SOURCE |
| 24 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <ctype.h> |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 28 | #include <float.h> |
| 29 | #include <assert.h> |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 30 | #include <linux/input.h> |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 31 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 32 | #include "compositor.h" |
| 33 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 34 | struct gles2_renderer { |
| 35 | struct weston_renderer base; |
| 36 | int fragment_shader_debug; |
| 37 | }; |
| 38 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 39 | struct gles2_output_state { |
| 40 | EGLSurface egl_surface; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | static inline struct gles2_output_state * |
| 45 | get_output_state(struct weston_output *output) |
| 46 | { |
| 47 | return (struct gles2_output_state *)output->renderer_state; |
| 48 | } |
| 49 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 50 | static const char * |
| 51 | egl_error_string(EGLint code) |
| 52 | { |
| 53 | #define MYERRCODE(x) case x: return #x; |
| 54 | switch (code) { |
| 55 | MYERRCODE(EGL_SUCCESS) |
| 56 | MYERRCODE(EGL_NOT_INITIALIZED) |
| 57 | MYERRCODE(EGL_BAD_ACCESS) |
| 58 | MYERRCODE(EGL_BAD_ALLOC) |
| 59 | MYERRCODE(EGL_BAD_ATTRIBUTE) |
| 60 | MYERRCODE(EGL_BAD_CONTEXT) |
| 61 | MYERRCODE(EGL_BAD_CONFIG) |
| 62 | MYERRCODE(EGL_BAD_CURRENT_SURFACE) |
| 63 | MYERRCODE(EGL_BAD_DISPLAY) |
| 64 | MYERRCODE(EGL_BAD_SURFACE) |
| 65 | MYERRCODE(EGL_BAD_MATCH) |
| 66 | MYERRCODE(EGL_BAD_PARAMETER) |
| 67 | MYERRCODE(EGL_BAD_NATIVE_PIXMAP) |
| 68 | MYERRCODE(EGL_BAD_NATIVE_WINDOW) |
| 69 | MYERRCODE(EGL_CONTEXT_LOST) |
| 70 | default: |
| 71 | return "unknown"; |
| 72 | } |
| 73 | #undef MYERRCODE |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | print_egl_error_state(void) |
| 78 | { |
| 79 | EGLint code; |
| 80 | |
| 81 | code = eglGetError(); |
| 82 | weston_log("EGL error state: %s (0x%04lx)\n", |
| 83 | egl_error_string(code), (long)code); |
| 84 | } |
| 85 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 86 | struct polygon8 { |
| 87 | GLfloat x[8]; |
| 88 | GLfloat y[8]; |
| 89 | int n; |
| 90 | }; |
| 91 | |
| 92 | struct clip_context { |
| 93 | struct { |
| 94 | GLfloat x; |
| 95 | GLfloat y; |
| 96 | } prev; |
| 97 | |
| 98 | struct { |
| 99 | GLfloat x1, y1; |
| 100 | GLfloat x2, y2; |
| 101 | } clip; |
| 102 | |
| 103 | struct { |
| 104 | GLfloat *x; |
| 105 | GLfloat *y; |
| 106 | } vertices; |
| 107 | }; |
| 108 | |
| 109 | static GLfloat |
| 110 | float_difference(GLfloat a, GLfloat b) |
| 111 | { |
| 112 | /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */ |
| 113 | static const GLfloat max_diff = 4.0f * FLT_MIN; |
| 114 | static const GLfloat max_rel_diff = 4.0e-5; |
| 115 | GLfloat diff = a - b; |
| 116 | GLfloat adiff = fabsf(diff); |
| 117 | |
| 118 | if (adiff <= max_diff) |
| 119 | return 0.0f; |
| 120 | |
| 121 | a = fabsf(a); |
| 122 | b = fabsf(b); |
| 123 | if (adiff <= (a > b ? a : b) * max_rel_diff) |
| 124 | return 0.0f; |
| 125 | |
| 126 | return diff; |
| 127 | } |
| 128 | |
| 129 | /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg. |
| 130 | * Compute the y coordinate of the intersection. |
| 131 | */ |
| 132 | static GLfloat |
| 133 | clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y, |
| 134 | GLfloat x_arg) |
| 135 | { |
| 136 | GLfloat a; |
| 137 | GLfloat diff = float_difference(p1x, p2x); |
| 138 | |
| 139 | /* Practically vertical line segment, yet the end points have already |
| 140 | * been determined to be on different sides of the line. Therefore |
| 141 | * the line segment is part of the line and intersects everywhere. |
| 142 | * Return the end point, so we use the whole line segment. |
| 143 | */ |
| 144 | if (diff == 0.0f) |
| 145 | return p2y; |
| 146 | |
| 147 | a = (x_arg - p2x) / diff; |
| 148 | return p2y + (p1y - p2y) * a; |
| 149 | } |
| 150 | |
| 151 | /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg. |
| 152 | * Compute the x coordinate of the intersection. |
| 153 | */ |
| 154 | static GLfloat |
| 155 | clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y, |
| 156 | GLfloat y_arg) |
| 157 | { |
| 158 | GLfloat a; |
| 159 | GLfloat diff = float_difference(p1y, p2y); |
| 160 | |
| 161 | /* Practically horizontal line segment, yet the end points have already |
| 162 | * been determined to be on different sides of the line. Therefore |
| 163 | * the line segment is part of the line and intersects everywhere. |
| 164 | * Return the end point, so we use the whole line segment. |
| 165 | */ |
| 166 | if (diff == 0.0f) |
| 167 | return p2x; |
| 168 | |
| 169 | a = (y_arg - p2y) / diff; |
| 170 | return p2x + (p1x - p2x) * a; |
| 171 | } |
| 172 | |
| 173 | enum path_transition { |
| 174 | PATH_TRANSITION_OUT_TO_OUT = 0, |
| 175 | PATH_TRANSITION_OUT_TO_IN = 1, |
| 176 | PATH_TRANSITION_IN_TO_OUT = 2, |
| 177 | PATH_TRANSITION_IN_TO_IN = 3, |
| 178 | }; |
| 179 | |
| 180 | static void |
| 181 | clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 182 | { |
| 183 | *ctx->vertices.x++ = x; |
| 184 | *ctx->vertices.y++ = y; |
| 185 | } |
| 186 | |
| 187 | static enum path_transition |
| 188 | path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 189 | { |
| 190 | return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1); |
| 191 | } |
| 192 | |
| 193 | static enum path_transition |
| 194 | path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 195 | { |
| 196 | return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2); |
| 197 | } |
| 198 | |
| 199 | static enum path_transition |
| 200 | path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 201 | { |
| 202 | return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1); |
| 203 | } |
| 204 | |
| 205 | static enum path_transition |
| 206 | path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 207 | { |
| 208 | return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2); |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | clip_polygon_leftright(struct clip_context *ctx, |
| 213 | enum path_transition transition, |
| 214 | GLfloat x, GLfloat y, GLfloat clip_x) |
| 215 | { |
| 216 | GLfloat yi; |
| 217 | |
| 218 | switch (transition) { |
| 219 | case PATH_TRANSITION_IN_TO_IN: |
| 220 | clip_append_vertex(ctx, x, y); |
| 221 | break; |
| 222 | case PATH_TRANSITION_IN_TO_OUT: |
| 223 | yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x); |
| 224 | clip_append_vertex(ctx, clip_x, yi); |
| 225 | break; |
| 226 | case PATH_TRANSITION_OUT_TO_IN: |
| 227 | yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x); |
| 228 | clip_append_vertex(ctx, clip_x, yi); |
| 229 | clip_append_vertex(ctx, x, y); |
| 230 | break; |
| 231 | case PATH_TRANSITION_OUT_TO_OUT: |
| 232 | /* nothing */ |
| 233 | break; |
| 234 | default: |
| 235 | assert(0 && "bad enum path_transition"); |
| 236 | } |
| 237 | |
| 238 | ctx->prev.x = x; |
| 239 | ctx->prev.y = y; |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | clip_polygon_topbottom(struct clip_context *ctx, |
| 244 | enum path_transition transition, |
| 245 | GLfloat x, GLfloat y, GLfloat clip_y) |
| 246 | { |
| 247 | GLfloat xi; |
| 248 | |
| 249 | switch (transition) { |
| 250 | case PATH_TRANSITION_IN_TO_IN: |
| 251 | clip_append_vertex(ctx, x, y); |
| 252 | break; |
| 253 | case PATH_TRANSITION_IN_TO_OUT: |
| 254 | xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y); |
| 255 | clip_append_vertex(ctx, xi, clip_y); |
| 256 | break; |
| 257 | case PATH_TRANSITION_OUT_TO_IN: |
| 258 | xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y); |
| 259 | clip_append_vertex(ctx, xi, clip_y); |
| 260 | clip_append_vertex(ctx, x, y); |
| 261 | break; |
| 262 | case PATH_TRANSITION_OUT_TO_OUT: |
| 263 | /* nothing */ |
| 264 | break; |
| 265 | default: |
| 266 | assert(0 && "bad enum path_transition"); |
| 267 | } |
| 268 | |
| 269 | ctx->prev.x = x; |
| 270 | ctx->prev.y = y; |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src, |
| 275 | GLfloat *dst_x, GLfloat *dst_y) |
| 276 | { |
| 277 | ctx->prev.x = src->x[src->n - 1]; |
| 278 | ctx->prev.y = src->y[src->n - 1]; |
| 279 | ctx->vertices.x = dst_x; |
| 280 | ctx->vertices.y = dst_y; |
| 281 | } |
| 282 | |
| 283 | static int |
| 284 | clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src, |
| 285 | GLfloat *dst_x, GLfloat *dst_y) |
| 286 | { |
| 287 | enum path_transition trans; |
| 288 | int i; |
| 289 | |
| 290 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 291 | for (i = 0; i < src->n; i++) { |
| 292 | trans = path_transition_left_edge(ctx, src->x[i], src->y[i]); |
| 293 | clip_polygon_leftright(ctx, trans, src->x[i], src->y[i], |
| 294 | ctx->clip.x1); |
| 295 | } |
| 296 | return ctx->vertices.x - dst_x; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src, |
| 301 | GLfloat *dst_x, GLfloat *dst_y) |
| 302 | { |
| 303 | enum path_transition trans; |
| 304 | int i; |
| 305 | |
| 306 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 307 | for (i = 0; i < src->n; i++) { |
| 308 | trans = path_transition_right_edge(ctx, src->x[i], src->y[i]); |
| 309 | clip_polygon_leftright(ctx, trans, src->x[i], src->y[i], |
| 310 | ctx->clip.x2); |
| 311 | } |
| 312 | return ctx->vertices.x - dst_x; |
| 313 | } |
| 314 | |
| 315 | static int |
| 316 | clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src, |
| 317 | GLfloat *dst_x, GLfloat *dst_y) |
| 318 | { |
| 319 | enum path_transition trans; |
| 320 | int i; |
| 321 | |
| 322 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 323 | for (i = 0; i < src->n; i++) { |
| 324 | trans = path_transition_top_edge(ctx, src->x[i], src->y[i]); |
| 325 | clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i], |
| 326 | ctx->clip.y1); |
| 327 | } |
| 328 | return ctx->vertices.x - dst_x; |
| 329 | } |
| 330 | |
| 331 | static int |
| 332 | clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src, |
| 333 | GLfloat *dst_x, GLfloat *dst_y) |
| 334 | { |
| 335 | enum path_transition trans; |
| 336 | int i; |
| 337 | |
| 338 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 339 | for (i = 0; i < src->n; i++) { |
| 340 | trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]); |
| 341 | clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i], |
| 342 | ctx->clip.y2); |
| 343 | } |
| 344 | return ctx->vertices.x - dst_x; |
| 345 | } |
| 346 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 347 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 348 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 349 | #define clip(x, a, b) min(max(x, a), b) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 350 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 351 | /* |
| 352 | * Compute the boundary vertices of the intersection of the global coordinate |
| 353 | * aligned rectangle 'rect', and an arbitrary quadrilateral produced from |
| 354 | * 'surf_rect' when transformed from surface coordinates into global coordinates. |
| 355 | * The vertices are written to 'ex' and 'ey', and the return value is the |
| 356 | * number of vertices. Vertices are produced in clockwise winding order. |
| 357 | * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero |
| 358 | * polygon area. |
| 359 | */ |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 360 | static int |
| 361 | calculate_edges(struct weston_surface *es, pixman_box32_t *rect, |
| 362 | pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey) |
| 363 | { |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 364 | struct polygon8 polygon; |
| 365 | struct clip_context ctx; |
| 366 | int i, n; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 367 | GLfloat min_x, max_x, min_y, max_y; |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 368 | struct polygon8 surf = { |
| 369 | { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 }, |
| 370 | { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 }, |
| 371 | 4 |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 372 | }; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 373 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 374 | ctx.clip.x1 = rect->x1; |
| 375 | ctx.clip.y1 = rect->y1; |
| 376 | ctx.clip.x2 = rect->x2; |
| 377 | ctx.clip.y2 = rect->y2; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 378 | |
| 379 | /* transform surface to screen space: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 380 | for (i = 0; i < surf.n; i++) |
| 381 | weston_surface_to_global_float(es, surf.x[i], surf.y[i], |
| 382 | &surf.x[i], &surf.y[i]); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 383 | |
| 384 | /* find bounding box: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 385 | min_x = max_x = surf.x[0]; |
| 386 | min_y = max_y = surf.y[0]; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 387 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 388 | for (i = 1; i < surf.n; i++) { |
| 389 | min_x = min(min_x, surf.x[i]); |
| 390 | max_x = max(max_x, surf.x[i]); |
| 391 | min_y = min(min_y, surf.y[i]); |
| 392 | max_y = max(max_y, surf.y[i]); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* First, simple bounding box check to discard early transformed |
| 396 | * surface rects that do not intersect with the clip region: |
| 397 | */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 398 | if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) || |
| 399 | (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1)) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 400 | return 0; |
| 401 | |
| 402 | /* Simple case, bounding box edges are parallel to surface edges, |
| 403 | * there will be only four edges. We just need to clip the surface |
| 404 | * vertices to the clip rect bounds: |
| 405 | */ |
| 406 | if (!es->transform.enabled) { |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 407 | for (i = 0; i < surf.n; i++) { |
| 408 | ex[i] = clip(surf.x[i], ctx.clip.x1, ctx.clip.x2); |
| 409 | ey[i] = clip(surf.y[i], ctx.clip.y1, ctx.clip.y2); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 410 | } |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 411 | return surf.n; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 412 | } |
| 413 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 414 | /* Transformed case: use a general polygon clipping algorithm to |
| 415 | * clip the surface rectangle with each side of 'rect'. |
| 416 | * The algorithm is Sutherland-Hodgman, as explained in |
| 417 | * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm |
| 418 | * but without looking at any of that code. |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 419 | */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 420 | polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y); |
| 421 | surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y); |
| 422 | polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y); |
| 423 | surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 424 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 425 | /* Get rid of duplicate vertices */ |
| 426 | ex[0] = surf.x[0]; |
| 427 | ey[0] = surf.y[0]; |
| 428 | n = 1; |
| 429 | for (i = 1; i < surf.n; i++) { |
| 430 | if (float_difference(ex[n - 1], surf.x[i]) == 0.0f && |
| 431 | float_difference(ey[n - 1], surf.y[i]) == 0.0f) |
| 432 | continue; |
| 433 | ex[n] = surf.x[i]; |
| 434 | ey[n] = surf.y[i]; |
| 435 | n++; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 436 | } |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 437 | if (float_difference(ex[n - 1], surf.x[0]) == 0.0f && |
| 438 | float_difference(ey[n - 1], surf.y[0]) == 0.0f) |
| 439 | n--; |
| 440 | |
| 441 | if (n < 3) |
| 442 | return 0; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 443 | |
| 444 | return n; |
| 445 | } |
| 446 | |
| 447 | static int |
| 448 | texture_region(struct weston_surface *es, pixman_region32_t *region, |
| 449 | pixman_region32_t *surf_region) |
| 450 | { |
| 451 | struct weston_compositor *ec = es->compositor; |
| 452 | GLfloat *v, inv_width, inv_height; |
| 453 | unsigned int *vtxcnt, nvtx = 0; |
| 454 | pixman_box32_t *rects, *surf_rects; |
| 455 | int i, j, k, nrects, nsurf; |
| 456 | |
| 457 | rects = pixman_region32_rectangles(region, &nrects); |
| 458 | surf_rects = pixman_region32_rectangles(surf_region, &nsurf); |
| 459 | |
| 460 | /* worst case we can have 8 vertices per rect (ie. clipped into |
| 461 | * an octagon): |
| 462 | */ |
| 463 | v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v); |
| 464 | vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt); |
| 465 | |
| 466 | inv_width = 1.0 / es->pitch; |
| 467 | inv_height = 1.0 / es->geometry.height; |
| 468 | |
| 469 | for (i = 0; i < nrects; i++) { |
| 470 | pixman_box32_t *rect = &rects[i]; |
| 471 | for (j = 0; j < nsurf; j++) { |
| 472 | pixman_box32_t *surf_rect = &surf_rects[j]; |
| 473 | GLfloat sx, sy; |
| 474 | GLfloat ex[8], ey[8]; /* edge points in screen space */ |
| 475 | int n; |
| 476 | |
| 477 | /* The transformed surface, after clipping to the clip region, |
| 478 | * can have as many as eight sides, emitted as a triangle-fan. |
| 479 | * The first vertex in the triangle fan can be chosen arbitrarily, |
| 480 | * since the area is guaranteed to be convex. |
| 481 | * |
| 482 | * If a corner of the transformed surface falls outside of the |
| 483 | * clip region, instead of emitting one vertex for the corner |
| 484 | * of the surface, up to two are emitted for two corresponding |
| 485 | * intersection point(s) between the surface and the clip region. |
| 486 | * |
| 487 | * To do this, we first calculate the (up to eight) points that |
| 488 | * form the intersection of the clip rect and the transformed |
| 489 | * surface. |
| 490 | */ |
| 491 | n = calculate_edges(es, rect, surf_rect, ex, ey); |
| 492 | if (n < 3) |
| 493 | continue; |
| 494 | |
| 495 | /* emit edge points: */ |
| 496 | for (k = 0; k < n; k++) { |
| 497 | weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy); |
| 498 | /* position: */ |
| 499 | *(v++) = ex[k]; |
| 500 | *(v++) = ey[k]; |
| 501 | /* texcoord: */ |
| 502 | *(v++) = sx * inv_width; |
| 503 | *(v++) = sy * inv_height; |
| 504 | } |
| 505 | |
| 506 | vtxcnt[nvtx++] = n; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return nvtx; |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | triangle_fan_debug(struct weston_surface *surface, int first, int count) |
| 515 | { |
| 516 | struct weston_compositor *compositor = surface->compositor; |
| 517 | int i; |
| 518 | GLushort *buffer; |
| 519 | GLushort *index; |
| 520 | int nelems; |
| 521 | static int color_idx = 0; |
| 522 | static const GLfloat color[][4] = { |
| 523 | { 1.0, 0.0, 0.0, 1.0 }, |
| 524 | { 0.0, 1.0, 0.0, 1.0 }, |
| 525 | { 0.0, 0.0, 1.0, 1.0 }, |
| 526 | { 1.0, 1.0, 1.0, 1.0 }, |
| 527 | }; |
| 528 | |
| 529 | nelems = (count - 1 + count - 2) * 2; |
| 530 | |
| 531 | buffer = malloc(sizeof(GLushort) * nelems); |
| 532 | index = buffer; |
| 533 | |
| 534 | for (i = 1; i < count; i++) { |
| 535 | *index++ = first; |
| 536 | *index++ = first + i; |
| 537 | } |
| 538 | |
| 539 | for (i = 2; i < count; i++) { |
| 540 | *index++ = first + i - 1; |
| 541 | *index++ = first + i; |
| 542 | } |
| 543 | |
| 544 | glUseProgram(compositor->solid_shader.program); |
| 545 | glUniform4fv(compositor->solid_shader.color_uniform, 1, |
| 546 | color[color_idx++ % ARRAY_LENGTH(color)]); |
| 547 | glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer); |
| 548 | glUseProgram(compositor->current_shader->program); |
| 549 | free(buffer); |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | repaint_region(struct weston_surface *es, pixman_region32_t *region, |
| 554 | pixman_region32_t *surf_region) |
| 555 | { |
| 556 | struct weston_compositor *ec = es->compositor; |
| 557 | GLfloat *v; |
| 558 | unsigned int *vtxcnt; |
| 559 | int i, first, nfans; |
| 560 | |
| 561 | /* The final region to be painted is the intersection of |
| 562 | * 'region' and 'surf_region'. However, 'region' is in the global |
| 563 | * coordinates, and 'surf_region' is in the surface-local |
| 564 | * coordinates. texture_region() will iterate over all pairs of |
| 565 | * rectangles from both regions, compute the intersection |
| 566 | * polygon for each pair, and store it as a triangle fan if |
| 567 | * it has a non-zero area (at least 3 vertices, actually). |
| 568 | */ |
| 569 | nfans = texture_region(es, region, surf_region); |
| 570 | |
| 571 | v = ec->vertices.data; |
| 572 | vtxcnt = ec->vtxcnt.data; |
| 573 | |
| 574 | /* position: */ |
| 575 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 576 | glEnableVertexAttribArray(0); |
| 577 | |
| 578 | /* texcoord: */ |
| 579 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
| 580 | glEnableVertexAttribArray(1); |
| 581 | |
| 582 | for (i = 0, first = 0; i < nfans; i++) { |
| 583 | glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]); |
| 584 | if (ec->fan_debug) |
| 585 | triangle_fan_debug(es, first, vtxcnt[i]); |
| 586 | first += vtxcnt[i]; |
| 587 | } |
| 588 | |
| 589 | glDisableVertexAttribArray(1); |
| 590 | glDisableVertexAttribArray(0); |
| 591 | |
| 592 | ec->vertices.size = 0; |
| 593 | ec->vtxcnt.size = 0; |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | weston_compositor_use_shader(struct weston_compositor *compositor, |
| 598 | struct weston_shader *shader) |
| 599 | { |
| 600 | if (compositor->current_shader == shader) |
| 601 | return; |
| 602 | |
| 603 | glUseProgram(shader->program); |
| 604 | compositor->current_shader = shader; |
| 605 | } |
| 606 | |
| 607 | static void |
| 608 | weston_shader_uniforms(struct weston_shader *shader, |
| 609 | struct weston_surface *surface, |
| 610 | struct weston_output *output) |
| 611 | { |
| 612 | int i; |
| 613 | |
| 614 | glUniformMatrix4fv(shader->proj_uniform, |
| 615 | 1, GL_FALSE, output->matrix.d); |
| 616 | glUniform4fv(shader->color_uniform, 1, surface->color); |
| 617 | glUniform1f(shader->alpha_uniform, surface->alpha); |
| 618 | |
| 619 | for (i = 0; i < surface->num_textures; i++) |
| 620 | glUniform1i(shader->tex_uniforms[i], i); |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | draw_surface(struct weston_surface *es, struct weston_output *output, |
| 625 | pixman_region32_t *damage) /* in global coordinates */ |
| 626 | { |
| 627 | struct weston_compositor *ec = es->compositor; |
| 628 | /* repaint bounding region in global coordinates: */ |
| 629 | pixman_region32_t repaint; |
| 630 | /* non-opaque region in surface coordinates: */ |
| 631 | pixman_region32_t surface_blend; |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 632 | pixman_region32_t *buffer_damage; |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 633 | GLint filter; |
| 634 | int i; |
| 635 | |
| 636 | pixman_region32_init(&repaint); |
| 637 | pixman_region32_intersect(&repaint, |
| 638 | &es->transform.boundingbox, damage); |
| 639 | pixman_region32_subtract(&repaint, &repaint, &es->clip); |
| 640 | |
| 641 | if (!pixman_region32_not_empty(&repaint)) |
| 642 | goto out; |
| 643 | |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 644 | buffer_damage = &output->buffer_damage[output->current_buffer]; |
| 645 | pixman_region32_subtract(buffer_damage, buffer_damage, &repaint); |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 646 | |
| 647 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 648 | |
| 649 | if (ec->fan_debug) { |
| 650 | weston_compositor_use_shader(ec, &ec->solid_shader); |
| 651 | weston_shader_uniforms(&ec->solid_shader, es, output); |
| 652 | } |
| 653 | |
| 654 | weston_compositor_use_shader(ec, es->shader); |
| 655 | weston_shader_uniforms(es->shader, es, output); |
| 656 | |
| 657 | if (es->transform.enabled || output->zoom.active) |
| 658 | filter = GL_LINEAR; |
| 659 | else |
| 660 | filter = GL_NEAREST; |
| 661 | |
| 662 | for (i = 0; i < es->num_textures; i++) { |
| 663 | glActiveTexture(GL_TEXTURE0 + i); |
| 664 | glBindTexture(es->target, es->textures[i]); |
| 665 | glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter); |
| 666 | glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter); |
| 667 | } |
| 668 | |
| 669 | /* blended region is whole surface minus opaque region: */ |
| 670 | pixman_region32_init_rect(&surface_blend, 0, 0, |
| 671 | es->geometry.width, es->geometry.height); |
| 672 | pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque); |
| 673 | |
| 674 | if (pixman_region32_not_empty(&es->opaque)) { |
| 675 | if (es->shader == &ec->texture_shader_rgba) { |
| 676 | /* Special case for RGBA textures with possibly |
| 677 | * bad data in alpha channel: use the shader |
| 678 | * that forces texture alpha = 1.0. |
| 679 | * Xwayland surfaces need this. |
| 680 | */ |
| 681 | weston_compositor_use_shader(ec, &ec->texture_shader_rgbx); |
| 682 | weston_shader_uniforms(&ec->texture_shader_rgbx, es, output); |
| 683 | } |
| 684 | |
| 685 | if (es->alpha < 1.0) |
| 686 | glEnable(GL_BLEND); |
| 687 | else |
| 688 | glDisable(GL_BLEND); |
| 689 | |
| 690 | repaint_region(es, &repaint, &es->opaque); |
| 691 | } |
| 692 | |
| 693 | if (pixman_region32_not_empty(&surface_blend)) { |
| 694 | weston_compositor_use_shader(ec, es->shader); |
| 695 | glEnable(GL_BLEND); |
| 696 | repaint_region(es, &repaint, &surface_blend); |
| 697 | } |
| 698 | |
| 699 | pixman_region32_fini(&surface_blend); |
| 700 | |
| 701 | out: |
| 702 | pixman_region32_fini(&repaint); |
| 703 | } |
| 704 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 705 | static void |
| 706 | repaint_surfaces(struct weston_output *output, pixman_region32_t *damage) |
| 707 | { |
| 708 | struct weston_compositor *compositor = output->compositor; |
| 709 | struct weston_surface *surface; |
| 710 | |
| 711 | wl_list_for_each_reverse(surface, &compositor->surface_list, link) |
| 712 | if (surface->plane == &compositor->primary_plane) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 713 | draw_surface(surface, output, damage); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 714 | } |
| 715 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 716 | static void |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 717 | gles2_renderer_repaint_output(struct weston_output *output, |
| 718 | pixman_region32_t *output_damage) |
| 719 | { |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 720 | struct gles2_output_state *go = get_output_state(output); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 721 | struct weston_compositor *compositor = output->compositor; |
| 722 | EGLBoolean ret; |
| 723 | static int errored; |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 724 | int32_t width, height, i; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 725 | |
| 726 | width = output->current->width + |
| 727 | output->border.left + output->border.right; |
| 728 | height = output->current->height + |
| 729 | output->border.top + output->border.bottom; |
| 730 | |
| 731 | glViewport(0, 0, width, height); |
| 732 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 733 | ret = eglMakeCurrent(compositor->egl_display, go->egl_surface, |
| 734 | go->egl_surface, compositor->egl_context); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 735 | if (ret == EGL_FALSE) { |
| 736 | if (errored) |
| 737 | return; |
| 738 | errored = 1; |
| 739 | weston_log("Failed to make EGL context current.\n"); |
| 740 | print_egl_error_state(); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | /* if debugging, redraw everything outside the damage to clean up |
| 745 | * debug lines from the previous draw on this buffer: |
| 746 | */ |
| 747 | if (compositor->fan_debug) { |
| 748 | pixman_region32_t undamaged; |
| 749 | pixman_region32_init(&undamaged); |
| 750 | pixman_region32_subtract(&undamaged, &output->region, |
| 751 | output_damage); |
| 752 | compositor->fan_debug = 0; |
| 753 | repaint_surfaces(output, &undamaged); |
| 754 | compositor->fan_debug = 1; |
| 755 | pixman_region32_fini(&undamaged); |
| 756 | } |
| 757 | |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 758 | for (i = 0; i < 2; i++) |
| 759 | pixman_region32_union(&output->buffer_damage[i], |
| 760 | &output->buffer_damage[i], |
| 761 | output_damage); |
| 762 | |
| 763 | pixman_region32_union(output_damage, output_damage, |
| 764 | &output->buffer_damage[output->current_buffer]); |
| 765 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 766 | repaint_surfaces(output, output_damage); |
| 767 | |
| 768 | wl_signal_emit(&output->frame_signal, output); |
| 769 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 770 | ret = eglSwapBuffers(compositor->egl_display, go->egl_surface); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 771 | if (ret == EGL_FALSE && !errored) { |
| 772 | errored = 1; |
| 773 | weston_log("Failed in eglSwapBuffers.\n"); |
| 774 | print_egl_error_state(); |
| 775 | } |
| 776 | |
Ander Conselvan de Oliveira | 8ea818f | 2012-09-14 16:12:03 +0300 | [diff] [blame] | 777 | output->current_buffer ^= 1; |
| 778 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 779 | } |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 780 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 781 | static void |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 782 | gles2_renderer_flush_damage(struct weston_surface *surface) |
| 783 | { |
| 784 | #ifdef GL_UNPACK_ROW_LENGTH |
| 785 | pixman_box32_t *rectangles; |
| 786 | void *data; |
| 787 | int i, n; |
| 788 | #endif |
| 789 | |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 790 | pixman_region32_union(&surface->texture_damage, |
| 791 | &surface->texture_damage, &surface->damage); |
| 792 | |
| 793 | /* Avoid upload, if the texture won't be used this time. |
| 794 | * We still accumulate the damage in texture_damage. |
| 795 | */ |
| 796 | if (surface->plane != &surface->compositor->primary_plane) |
| 797 | return; |
| 798 | |
| 799 | if (!pixman_region32_not_empty(&surface->texture_damage)) |
| 800 | return; |
| 801 | |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 802 | glBindTexture(GL_TEXTURE_2D, surface->textures[0]); |
| 803 | |
| 804 | if (!surface->compositor->has_unpack_subimage) { |
| 805 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 806 | surface->pitch, surface->buffer->height, 0, |
| 807 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 808 | wl_shm_buffer_get_data(surface->buffer)); |
| 809 | |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 810 | goto done; |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | #ifdef GL_UNPACK_ROW_LENGTH |
| 814 | /* Mesa does not define GL_EXT_unpack_subimage */ |
| 815 | glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch); |
| 816 | data = wl_shm_buffer_get_data(surface->buffer); |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 817 | rectangles = pixman_region32_rectangles(&surface->texture_damage, &n); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 818 | for (i = 0; i < n; i++) { |
| 819 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1); |
| 820 | glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1); |
| 821 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 822 | rectangles[i].x1, rectangles[i].y1, |
| 823 | rectangles[i].x2 - rectangles[i].x1, |
| 824 | rectangles[i].y2 - rectangles[i].y1, |
| 825 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, data); |
| 826 | } |
| 827 | #endif |
Pekka Paalanen | bcdd579 | 2012-11-07 12:25:13 +0200 | [diff] [blame] | 828 | |
| 829 | done: |
| 830 | pixman_region32_fini(&surface->texture_damage); |
| 831 | pixman_region32_init(&surface->texture_damage); |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 832 | } |
| 833 | |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 834 | static void |
| 835 | ensure_textures(struct weston_surface *es, int num_textures) |
| 836 | { |
| 837 | int i; |
| 838 | |
| 839 | if (num_textures <= es->num_textures) |
| 840 | return; |
| 841 | |
| 842 | for (i = es->num_textures; i < num_textures; i++) { |
| 843 | glGenTextures(1, &es->textures[i]); |
| 844 | glBindTexture(es->target, es->textures[i]); |
| 845 | glTexParameteri(es->target, |
| 846 | GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 847 | glTexParameteri(es->target, |
| 848 | GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 849 | } |
| 850 | es->num_textures = num_textures; |
| 851 | glBindTexture(es->target, 0); |
| 852 | } |
| 853 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 854 | static void |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 855 | gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer) |
| 856 | { |
| 857 | struct weston_compositor *ec = es->compositor; |
| 858 | EGLint attribs[3], format; |
| 859 | int i, num_planes; |
| 860 | |
| 861 | if (!buffer) { |
| 862 | for (i = 0; i < es->num_images; i++) { |
| 863 | ec->destroy_image(ec->egl_display, es->images[i]); |
| 864 | es->images[i] = NULL; |
| 865 | } |
| 866 | es->num_images = 0; |
| 867 | glDeleteTextures(es->num_textures, es->textures); |
| 868 | es->num_textures = 0; |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | if (wl_buffer_is_shm(buffer)) { |
| 873 | es->pitch = wl_shm_buffer_get_stride(buffer) / 4; |
| 874 | es->target = GL_TEXTURE_2D; |
| 875 | |
| 876 | ensure_textures(es, 1); |
| 877 | glBindTexture(GL_TEXTURE_2D, es->textures[0]); |
| 878 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 879 | es->pitch, buffer->height, 0, |
| 880 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); |
| 881 | if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888) |
| 882 | es->shader = &ec->texture_shader_rgbx; |
| 883 | else |
| 884 | es->shader = &ec->texture_shader_rgba; |
| 885 | } else if (ec->query_buffer(ec->egl_display, buffer, |
| 886 | EGL_TEXTURE_FORMAT, &format)) { |
| 887 | for (i = 0; i < es->num_images; i++) |
| 888 | ec->destroy_image(ec->egl_display, es->images[i]); |
| 889 | es->num_images = 0; |
| 890 | es->target = GL_TEXTURE_2D; |
| 891 | switch (format) { |
| 892 | case EGL_TEXTURE_RGB: |
| 893 | case EGL_TEXTURE_RGBA: |
| 894 | default: |
| 895 | num_planes = 1; |
| 896 | es->shader = &ec->texture_shader_rgba; |
| 897 | break; |
| 898 | case EGL_TEXTURE_EXTERNAL_WL: |
| 899 | num_planes = 1; |
| 900 | es->target = GL_TEXTURE_EXTERNAL_OES; |
| 901 | es->shader = &ec->texture_shader_egl_external; |
| 902 | break; |
| 903 | case EGL_TEXTURE_Y_UV_WL: |
| 904 | num_planes = 2; |
| 905 | es->shader = &ec->texture_shader_y_uv; |
| 906 | break; |
| 907 | case EGL_TEXTURE_Y_U_V_WL: |
| 908 | num_planes = 3; |
| 909 | es->shader = &ec->texture_shader_y_u_v; |
| 910 | break; |
| 911 | case EGL_TEXTURE_Y_XUXV_WL: |
| 912 | num_planes = 2; |
| 913 | es->shader = &ec->texture_shader_y_xuxv; |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | ensure_textures(es, num_planes); |
| 918 | for (i = 0; i < num_planes; i++) { |
| 919 | attribs[0] = EGL_WAYLAND_PLANE_WL; |
| 920 | attribs[1] = i; |
| 921 | attribs[2] = EGL_NONE; |
| 922 | es->images[i] = ec->create_image(ec->egl_display, |
| 923 | NULL, |
| 924 | EGL_WAYLAND_BUFFER_WL, |
| 925 | buffer, attribs); |
| 926 | if (!es->images[i]) { |
| 927 | weston_log("failed to create img for plane %d\n", i); |
| 928 | continue; |
| 929 | } |
| 930 | es->num_images++; |
| 931 | |
| 932 | glActiveTexture(GL_TEXTURE0 + i); |
| 933 | glBindTexture(es->target, es->textures[i]); |
| 934 | ec->image_target_texture_2d(es->target, |
| 935 | es->images[i]); |
| 936 | } |
| 937 | |
| 938 | es->pitch = buffer->width; |
| 939 | } else { |
| 940 | weston_log("unhandled buffer type!\n"); |
| 941 | } |
| 942 | } |
| 943 | |
Kristian Høgsberg | 4226385 | 2012-09-06 21:59:29 -0400 | [diff] [blame] | 944 | static void |
| 945 | gles2_renderer_destroy_surface(struct weston_surface *surface) |
| 946 | { |
| 947 | struct weston_compositor *ec = surface->compositor; |
| 948 | int i; |
| 949 | |
| 950 | glDeleteTextures(surface->num_textures, surface->textures); |
| 951 | |
| 952 | for (i = 0; i < surface->num_images; i++) |
| 953 | ec->destroy_image(ec->egl_display, surface->images[i]); |
| 954 | } |
| 955 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 956 | static const char vertex_shader[] = |
| 957 | "uniform mat4 proj;\n" |
| 958 | "attribute vec2 position;\n" |
| 959 | "attribute vec2 texcoord;\n" |
| 960 | "varying vec2 v_texcoord;\n" |
| 961 | "void main()\n" |
| 962 | "{\n" |
| 963 | " gl_Position = proj * vec4(position, 0.0, 1.0);\n" |
| 964 | " v_texcoord = texcoord;\n" |
| 965 | "}\n"; |
| 966 | |
| 967 | /* Declare common fragment shader uniforms */ |
| 968 | #define FRAGMENT_CONVERT_YUV \ |
| 969 | " y *= alpha;\n" \ |
| 970 | " u *= alpha;\n" \ |
| 971 | " v *= alpha;\n" \ |
| 972 | " gl_FragColor.r = y + 1.59602678 * v;\n" \ |
| 973 | " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \ |
| 974 | " gl_FragColor.b = y + 2.01723214 * u;\n" \ |
| 975 | " gl_FragColor.a = alpha;\n" |
| 976 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 977 | static const char fragment_debug[] = |
| 978 | " gl_FragColor = vec4(0.0, 0.3, 0.0, 0.2) + gl_FragColor * 0.8;\n"; |
| 979 | |
| 980 | static const char fragment_brace[] = |
| 981 | "}\n"; |
| 982 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 983 | static const char texture_fragment_shader_rgba[] = |
| 984 | "precision mediump float;\n" |
| 985 | "varying vec2 v_texcoord;\n" |
| 986 | "uniform sampler2D tex;\n" |
| 987 | "uniform float alpha;\n" |
| 988 | "void main()\n" |
| 989 | "{\n" |
| 990 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 991 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 992 | |
| 993 | static const char texture_fragment_shader_rgbx[] = |
| 994 | "precision mediump float;\n" |
| 995 | "varying vec2 v_texcoord;\n" |
| 996 | "uniform sampler2D tex;\n" |
| 997 | "uniform float alpha;\n" |
| 998 | "void main()\n" |
| 999 | "{\n" |
| 1000 | " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;" |
| 1001 | " gl_FragColor.a = alpha;\n" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1002 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1003 | |
| 1004 | static const char texture_fragment_shader_egl_external[] = |
| 1005 | "#extension GL_OES_EGL_image_external : require\n" |
| 1006 | "precision mediump float;\n" |
| 1007 | "varying vec2 v_texcoord;\n" |
| 1008 | "uniform samplerExternalOES tex;\n" |
| 1009 | "uniform float alpha;\n" |
| 1010 | "void main()\n" |
| 1011 | "{\n" |
| 1012 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1013 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1014 | |
| 1015 | static const char texture_fragment_shader_y_uv[] = |
| 1016 | "precision mediump float;\n" |
| 1017 | "uniform sampler2D tex;\n" |
| 1018 | "uniform sampler2D tex1;\n" |
| 1019 | "varying vec2 v_texcoord;\n" |
| 1020 | "uniform float alpha;\n" |
| 1021 | "void main() {\n" |
| 1022 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 1023 | " float u = texture2D(tex1, v_texcoord).r - 0.5;\n" |
| 1024 | " float v = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 1025 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1026 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1027 | |
| 1028 | static const char texture_fragment_shader_y_u_v[] = |
| 1029 | "precision mediump float;\n" |
| 1030 | "uniform sampler2D tex;\n" |
| 1031 | "uniform sampler2D tex1;\n" |
| 1032 | "uniform sampler2D tex2;\n" |
| 1033 | "varying vec2 v_texcoord;\n" |
| 1034 | "uniform float alpha;\n" |
| 1035 | "void main() {\n" |
| 1036 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 1037 | " float u = texture2D(tex1, v_texcoord).x - 0.5;\n" |
| 1038 | " float v = texture2D(tex2, v_texcoord).x - 0.5;\n" |
| 1039 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1040 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1041 | |
| 1042 | static const char texture_fragment_shader_y_xuxv[] = |
| 1043 | "precision mediump float;\n" |
| 1044 | "uniform sampler2D tex;\n" |
| 1045 | "uniform sampler2D tex1;\n" |
| 1046 | "varying vec2 v_texcoord;\n" |
| 1047 | "uniform float alpha;\n" |
| 1048 | "void main() {\n" |
| 1049 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 1050 | " float u = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 1051 | " float v = texture2D(tex1, v_texcoord).a - 0.5;\n" |
| 1052 | FRAGMENT_CONVERT_YUV |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1053 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1054 | |
| 1055 | static const char solid_fragment_shader[] = |
| 1056 | "precision mediump float;\n" |
| 1057 | "uniform vec4 color;\n" |
| 1058 | "uniform float alpha;\n" |
| 1059 | "void main()\n" |
| 1060 | "{\n" |
| 1061 | " gl_FragColor = alpha * color\n;" |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1062 | ; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1063 | |
| 1064 | static int |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1065 | compile_shader(GLenum type, int count, const char **sources) |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1066 | { |
| 1067 | GLuint s; |
| 1068 | char msg[512]; |
| 1069 | GLint status; |
| 1070 | |
| 1071 | s = glCreateShader(type); |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1072 | glShaderSource(s, count, sources, NULL); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1073 | glCompileShader(s); |
| 1074 | glGetShaderiv(s, GL_COMPILE_STATUS, &status); |
| 1075 | if (!status) { |
| 1076 | glGetShaderInfoLog(s, sizeof msg, NULL, msg); |
| 1077 | weston_log("shader info: %s\n", msg); |
| 1078 | return GL_NONE; |
| 1079 | } |
| 1080 | |
| 1081 | return s; |
| 1082 | } |
| 1083 | |
| 1084 | static int |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1085 | weston_shader_init(struct weston_shader *shader, struct weston_compositor *ec, |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1086 | const char *vertex_source, const char *fragment_source) |
| 1087 | { |
| 1088 | char msg[512]; |
| 1089 | GLint status; |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1090 | int count; |
| 1091 | const char *sources[3]; |
| 1092 | struct gles2_renderer *renderer = |
| 1093 | (struct gles2_renderer *) ec->renderer; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1094 | |
| 1095 | shader->vertex_shader = |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1096 | compile_shader(GL_VERTEX_SHADER, 1, &vertex_source); |
| 1097 | |
| 1098 | if (renderer->fragment_shader_debug) { |
| 1099 | sources[0] = fragment_source; |
| 1100 | sources[1] = fragment_debug; |
| 1101 | sources[2] = fragment_brace; |
| 1102 | count = 3; |
| 1103 | } else { |
| 1104 | sources[0] = fragment_source; |
| 1105 | sources[1] = fragment_brace; |
| 1106 | count = 2; |
| 1107 | } |
| 1108 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1109 | shader->fragment_shader = |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1110 | compile_shader(GL_FRAGMENT_SHADER, count, sources); |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1111 | |
| 1112 | shader->program = glCreateProgram(); |
| 1113 | glAttachShader(shader->program, shader->vertex_shader); |
| 1114 | glAttachShader(shader->program, shader->fragment_shader); |
| 1115 | glBindAttribLocation(shader->program, 0, "position"); |
| 1116 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 1117 | |
| 1118 | glLinkProgram(shader->program); |
| 1119 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
| 1120 | if (!status) { |
| 1121 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
| 1122 | weston_log("link info: %s\n", msg); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 1127 | shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex"); |
| 1128 | shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1"); |
| 1129 | shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2"); |
| 1130 | shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha"); |
| 1131 | shader->color_uniform = glGetUniformLocation(shader->program, "color"); |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | static void |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1137 | weston_shader_release(struct weston_shader *shader) |
| 1138 | { |
| 1139 | glDeleteShader(shader->vertex_shader); |
| 1140 | glDeleteShader(shader->fragment_shader); |
| 1141 | glDeleteProgram(shader->program); |
| 1142 | |
| 1143 | shader->vertex_shader = 0; |
| 1144 | shader->fragment_shader = 0; |
| 1145 | shader->program = 0; |
| 1146 | } |
| 1147 | |
| 1148 | static void |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1149 | log_extensions(const char *name, const char *extensions) |
| 1150 | { |
| 1151 | const char *p, *end; |
| 1152 | int l; |
| 1153 | int len; |
| 1154 | |
| 1155 | l = weston_log("%s:", name); |
| 1156 | p = extensions; |
| 1157 | while (*p) { |
| 1158 | end = strchrnul(p, ' '); |
| 1159 | len = end - p; |
| 1160 | if (l + len > 78) |
| 1161 | l = weston_log_continue("\n" STAMP_SPACE "%.*s", |
| 1162 | len, p); |
| 1163 | else |
| 1164 | l += weston_log_continue(" %.*s", len, p); |
| 1165 | for (p = end; isspace(*p); p++) |
| 1166 | ; |
| 1167 | } |
| 1168 | weston_log_continue("\n"); |
| 1169 | } |
| 1170 | |
| 1171 | static void |
| 1172 | log_egl_gl_info(EGLDisplay egldpy) |
| 1173 | { |
| 1174 | const char *str; |
| 1175 | |
| 1176 | str = eglQueryString(egldpy, EGL_VERSION); |
| 1177 | weston_log("EGL version: %s\n", str ? str : "(null)"); |
| 1178 | |
| 1179 | str = eglQueryString(egldpy, EGL_VENDOR); |
| 1180 | weston_log("EGL vendor: %s\n", str ? str : "(null)"); |
| 1181 | |
| 1182 | str = eglQueryString(egldpy, EGL_CLIENT_APIS); |
| 1183 | weston_log("EGL client APIs: %s\n", str ? str : "(null)"); |
| 1184 | |
| 1185 | str = eglQueryString(egldpy, EGL_EXTENSIONS); |
| 1186 | log_extensions("EGL extensions", str ? str : "(null)"); |
| 1187 | |
| 1188 | str = (char *)glGetString(GL_VERSION); |
| 1189 | weston_log("GL version: %s\n", str ? str : "(null)"); |
| 1190 | |
| 1191 | str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION); |
| 1192 | weston_log("GLSL version: %s\n", str ? str : "(null)"); |
| 1193 | |
| 1194 | str = (char *)glGetString(GL_VENDOR); |
| 1195 | weston_log("GL vendor: %s\n", str ? str : "(null)"); |
| 1196 | |
| 1197 | str = (char *)glGetString(GL_RENDERER); |
| 1198 | weston_log("GL renderer: %s\n", str ? str : "(null)"); |
| 1199 | |
| 1200 | str = (char *)glGetString(GL_EXTENSIONS); |
| 1201 | log_extensions("GL extensions", str ? str : "(null)"); |
| 1202 | } |
| 1203 | |
Pekka Paalanen | 9c3fe25 | 2012-10-24 09:43:05 +0300 | [diff] [blame] | 1204 | static void |
| 1205 | log_egl_config_info(EGLDisplay egldpy, EGLConfig eglconfig) |
| 1206 | { |
| 1207 | EGLint r, g, b, a; |
| 1208 | |
| 1209 | weston_log("Chosen EGL config details:\n"); |
| 1210 | |
| 1211 | weston_log_continue(STAMP_SPACE "RGBA bits"); |
| 1212 | if (eglGetConfigAttrib(egldpy, eglconfig, EGL_RED_SIZE, &r) && |
| 1213 | eglGetConfigAttrib(egldpy, eglconfig, EGL_GREEN_SIZE, &g) && |
| 1214 | eglGetConfigAttrib(egldpy, eglconfig, EGL_BLUE_SIZE, &b) && |
| 1215 | eglGetConfigAttrib(egldpy, eglconfig, EGL_ALPHA_SIZE, &a)) |
| 1216 | weston_log_continue(": %d %d %d %d\n", r, g, b, a); |
| 1217 | else |
| 1218 | weston_log_continue(" unknown\n"); |
| 1219 | |
| 1220 | weston_log_continue(STAMP_SPACE "swap interval range"); |
| 1221 | if (eglGetConfigAttrib(egldpy, eglconfig, EGL_MIN_SWAP_INTERVAL, &a) && |
| 1222 | eglGetConfigAttrib(egldpy, eglconfig, EGL_MAX_SWAP_INTERVAL, &b)) |
| 1223 | weston_log_continue(": %d - %d\n", a, b); |
| 1224 | else |
| 1225 | weston_log_continue(" unknown\n"); |
| 1226 | } |
| 1227 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 1228 | static int |
| 1229 | gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface); |
| 1230 | |
| 1231 | WL_EXPORT int |
| 1232 | gles2_renderer_output_create(struct weston_output *output, |
| 1233 | EGLNativeWindowType window) |
| 1234 | { |
| 1235 | struct weston_compositor *ec = output->compositor; |
| 1236 | struct gles2_output_state *go = calloc(1, sizeof *go); |
| 1237 | |
| 1238 | if (!go) |
| 1239 | return -1; |
| 1240 | |
| 1241 | go->egl_surface = |
| 1242 | eglCreateWindowSurface(ec->egl_display, |
| 1243 | ec->egl_config, |
| 1244 | window, NULL); |
| 1245 | |
| 1246 | if (go->egl_surface == EGL_NO_SURFACE) { |
| 1247 | weston_log("failed to create egl surface\n"); |
| 1248 | free(go); |
| 1249 | return -1; |
| 1250 | } |
| 1251 | |
| 1252 | if (ec->egl_context == NULL) |
| 1253 | if (gles2_renderer_setup(ec, go->egl_surface) < 0) { |
| 1254 | free(go); |
| 1255 | return -1; |
| 1256 | } |
| 1257 | |
| 1258 | output->renderer_state = go; |
| 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | WL_EXPORT void |
| 1264 | gles2_renderer_output_destroy(struct weston_output *output) |
| 1265 | { |
| 1266 | struct gles2_output_state *go = get_output_state(output); |
| 1267 | |
| 1268 | eglDestroySurface(output->compositor->egl_display, go->egl_surface); |
| 1269 | |
| 1270 | free(go); |
| 1271 | } |
| 1272 | |
| 1273 | WL_EXPORT EGLSurface |
| 1274 | gles2_renderer_output_surface(struct weston_output *output) |
| 1275 | { |
| 1276 | return get_output_state(output)->egl_surface; |
| 1277 | } |
| 1278 | |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 1279 | WL_EXPORT void |
| 1280 | gles2_renderer_destroy(struct weston_compositor *ec) |
| 1281 | { |
| 1282 | if (ec->has_bind_display) |
| 1283 | ec->unbind_display(ec->egl_display, ec->wl_display); |
| 1284 | } |
| 1285 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1286 | static int |
| 1287 | compile_shaders(struct weston_compositor *ec) |
| 1288 | { |
| 1289 | if (weston_shader_init(&ec->texture_shader_rgba, ec, |
| 1290 | vertex_shader, texture_fragment_shader_rgba) < 0) |
| 1291 | return -1; |
| 1292 | if (weston_shader_init(&ec->texture_shader_rgbx, ec, |
| 1293 | vertex_shader, texture_fragment_shader_rgbx) < 0) |
| 1294 | return -1; |
| 1295 | if (ec->has_egl_image_external && |
| 1296 | weston_shader_init(&ec->texture_shader_egl_external, ec, |
| 1297 | vertex_shader, texture_fragment_shader_egl_external) < 0) |
| 1298 | return -1; |
| 1299 | if (weston_shader_init(&ec->texture_shader_y_uv, ec, |
| 1300 | vertex_shader, texture_fragment_shader_y_uv) < 0) |
| 1301 | return -1; |
| 1302 | if (weston_shader_init(&ec->texture_shader_y_u_v, ec, |
| 1303 | vertex_shader, texture_fragment_shader_y_u_v) < 0) |
| 1304 | return -1; |
| 1305 | if (weston_shader_init(&ec->texture_shader_y_xuxv, ec, |
| 1306 | vertex_shader, texture_fragment_shader_y_xuxv) < 0) |
| 1307 | return -1; |
| 1308 | if (weston_shader_init(&ec->solid_shader, ec, |
| 1309 | vertex_shader, solid_fragment_shader) < 0) |
| 1310 | return -1; |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static void |
| 1316 | fragment_debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, |
| 1317 | void *data) |
| 1318 | { |
| 1319 | struct weston_compositor *ec = data; |
| 1320 | struct gles2_renderer *renderer = |
| 1321 | (struct gles2_renderer *) ec->renderer; |
| 1322 | struct weston_output *output; |
| 1323 | |
| 1324 | renderer->fragment_shader_debug ^= 1; |
| 1325 | |
| 1326 | weston_shader_release(&ec->texture_shader_rgba); |
| 1327 | weston_shader_release(&ec->texture_shader_rgbx); |
| 1328 | weston_shader_release(&ec->texture_shader_egl_external); |
| 1329 | weston_shader_release(&ec->texture_shader_y_uv); |
| 1330 | weston_shader_release(&ec->texture_shader_y_u_v); |
| 1331 | weston_shader_release(&ec->texture_shader_y_xuxv); |
| 1332 | weston_shader_release(&ec->solid_shader); |
| 1333 | |
| 1334 | compile_shaders(ec); |
| 1335 | |
| 1336 | wl_list_for_each(output, &ec->output_list, link) |
| 1337 | weston_output_damage(output); |
| 1338 | } |
| 1339 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 1340 | static int |
| 1341 | gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface) |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1342 | { |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1343 | struct gles2_renderer *renderer; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1344 | const char *extensions; |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 1345 | EGLBoolean ret; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1346 | |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 1347 | static const EGLint context_attribs[] = { |
| 1348 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 1349 | EGL_NONE |
| 1350 | }; |
| 1351 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1352 | renderer = calloc(1, sizeof *renderer); |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1353 | if (renderer == NULL) |
| 1354 | return -1; |
| 1355 | |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 1356 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 1357 | weston_log("failed to bind EGL_OPENGL_ES_API\n"); |
| 1358 | print_egl_error_state(); |
| 1359 | return -1; |
| 1360 | } |
Pekka Paalanen | 9c3fe25 | 2012-10-24 09:43:05 +0300 | [diff] [blame] | 1361 | |
| 1362 | log_egl_config_info(ec->egl_display, ec->egl_config); |
| 1363 | |
Kristian Høgsberg | 9793fc7 | 2012-09-06 21:07:40 -0400 | [diff] [blame] | 1364 | ec->egl_context = eglCreateContext(ec->egl_display, ec->egl_config, |
| 1365 | EGL_NO_CONTEXT, context_attribs); |
| 1366 | if (ec->egl_context == NULL) { |
| 1367 | weston_log("failed to create context\n"); |
| 1368 | print_egl_error_state(); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame^] | 1372 | ret = eglMakeCurrent(ec->egl_display, egl_surface, |
| 1373 | egl_surface, ec->egl_context); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 1374 | if (ret == EGL_FALSE) { |
| 1375 | weston_log("Failed to make EGL context current.\n"); |
| 1376 | print_egl_error_state(); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1380 | log_egl_gl_info(ec->egl_display); |
| 1381 | |
| 1382 | ec->image_target_texture_2d = |
| 1383 | (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES"); |
| 1384 | ec->image_target_renderbuffer_storage = (void *) |
| 1385 | eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES"); |
| 1386 | ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR"); |
| 1387 | ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR"); |
| 1388 | ec->bind_display = |
| 1389 | (void *) eglGetProcAddress("eglBindWaylandDisplayWL"); |
| 1390 | ec->unbind_display = |
| 1391 | (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL"); |
| 1392 | ec->query_buffer = |
| 1393 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 1394 | |
| 1395 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 1396 | if (!extensions) { |
| 1397 | weston_log("Retrieving GL extension string failed.\n"); |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
| 1401 | if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) { |
| 1402 | weston_log("GL_EXT_texture_format_BGRA8888 not available\n"); |
| 1403 | return -1; |
| 1404 | } |
| 1405 | |
| 1406 | if (strstr(extensions, "GL_EXT_read_format_bgra")) |
| 1407 | ec->read_format = GL_BGRA_EXT; |
| 1408 | else |
| 1409 | ec->read_format = GL_RGBA; |
| 1410 | |
| 1411 | if (strstr(extensions, "GL_EXT_unpack_subimage")) |
| 1412 | ec->has_unpack_subimage = 1; |
| 1413 | |
| 1414 | if (strstr(extensions, "GL_OES_EGL_image_external")) |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1415 | ec->has_egl_image_external = 1; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1416 | |
| 1417 | extensions = |
| 1418 | (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS); |
| 1419 | if (!extensions) { |
| 1420 | weston_log("Retrieving EGL extension string failed.\n"); |
| 1421 | return -1; |
| 1422 | } |
| 1423 | |
| 1424 | if (strstr(extensions, "EGL_WL_bind_wayland_display")) |
| 1425 | ec->has_bind_display = 1; |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 1426 | if (ec->has_bind_display) { |
| 1427 | ret = ec->bind_display(ec->egl_display, ec->wl_display); |
| 1428 | if (!ret) |
| 1429 | ec->has_bind_display = 0; |
| 1430 | } |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1431 | |
| 1432 | glActiveTexture(GL_TEXTURE0); |
| 1433 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1434 | renderer->base.repaint_output = gles2_renderer_repaint_output; |
| 1435 | renderer->base.flush_damage = gles2_renderer_flush_damage; |
| 1436 | renderer->base.attach = gles2_renderer_attach; |
Kristian Høgsberg | 4226385 | 2012-09-06 21:59:29 -0400 | [diff] [blame] | 1437 | renderer->base.destroy_surface = gles2_renderer_destroy_surface; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 1438 | ec->renderer = &renderer->base; |
| 1439 | |
Ander Conselvan de Oliveira | 27508c2 | 2012-11-08 17:20:46 +0200 | [diff] [blame] | 1440 | if (compile_shaders(ec)) |
| 1441 | return -1; |
| 1442 | |
| 1443 | weston_compositor_add_debug_binding(ec, KEY_S, |
| 1444 | fragment_debug_binding, ec); |
| 1445 | |
Pekka Paalanen | 035a032 | 2012-10-24 09:43:06 +0300 | [diff] [blame] | 1446 | weston_log("GL ES 2 renderer features:\n"); |
| 1447 | weston_log_continue(STAMP_SPACE "read-back format: %s\n", |
| 1448 | ec->read_format == GL_BGRA_EXT ? "BGRA" : "RGBA"); |
| 1449 | weston_log_continue(STAMP_SPACE "wl_shm sub-image to texture: %s\n", |
| 1450 | ec->has_unpack_subimage ? "yes" : "no"); |
| 1451 | weston_log_continue(STAMP_SPACE "EGL Wayland extension: %s\n", |
| 1452 | ec->has_bind_display ? "yes" : "no"); |
| 1453 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |