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> |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 28 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 29 | #include "compositor.h" |
| 30 | |
| 31 | static const char * |
| 32 | egl_error_string(EGLint code) |
| 33 | { |
| 34 | #define MYERRCODE(x) case x: return #x; |
| 35 | switch (code) { |
| 36 | MYERRCODE(EGL_SUCCESS) |
| 37 | MYERRCODE(EGL_NOT_INITIALIZED) |
| 38 | MYERRCODE(EGL_BAD_ACCESS) |
| 39 | MYERRCODE(EGL_BAD_ALLOC) |
| 40 | MYERRCODE(EGL_BAD_ATTRIBUTE) |
| 41 | MYERRCODE(EGL_BAD_CONTEXT) |
| 42 | MYERRCODE(EGL_BAD_CONFIG) |
| 43 | MYERRCODE(EGL_BAD_CURRENT_SURFACE) |
| 44 | MYERRCODE(EGL_BAD_DISPLAY) |
| 45 | MYERRCODE(EGL_BAD_SURFACE) |
| 46 | MYERRCODE(EGL_BAD_MATCH) |
| 47 | MYERRCODE(EGL_BAD_PARAMETER) |
| 48 | MYERRCODE(EGL_BAD_NATIVE_PIXMAP) |
| 49 | MYERRCODE(EGL_BAD_NATIVE_WINDOW) |
| 50 | MYERRCODE(EGL_CONTEXT_LOST) |
| 51 | default: |
| 52 | return "unknown"; |
| 53 | } |
| 54 | #undef MYERRCODE |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | print_egl_error_state(void) |
| 59 | { |
| 60 | EGLint code; |
| 61 | |
| 62 | code = eglGetError(); |
| 63 | weston_log("EGL error state: %s (0x%04lx)\n", |
| 64 | egl_error_string(code), (long)code); |
| 65 | } |
| 66 | |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 67 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 68 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 69 | #define clip(x, a, b) min(max(x, a), b) |
| 70 | #define sign(x) ((x) >= 0) |
| 71 | |
| 72 | static int |
| 73 | calculate_edges(struct weston_surface *es, pixman_box32_t *rect, |
| 74 | pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey) |
| 75 | { |
| 76 | int i, n = 0; |
| 77 | GLfloat min_x, max_x, min_y, max_y; |
| 78 | GLfloat x[4] = { |
| 79 | surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1, |
| 80 | }; |
| 81 | GLfloat y[4] = { |
| 82 | surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2, |
| 83 | }; |
| 84 | GLfloat cx1 = rect->x1; |
| 85 | GLfloat cx2 = rect->x2; |
| 86 | GLfloat cy1 = rect->y1; |
| 87 | GLfloat cy2 = rect->y2; |
| 88 | |
| 89 | GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) |
| 90 | { |
| 91 | GLfloat dx = (x1 - x2); |
| 92 | GLfloat dy = (y1 - y2); |
| 93 | return dx * dx + dy * dy; |
| 94 | } |
| 95 | |
| 96 | void append_vertex(GLfloat x, GLfloat y) |
| 97 | { |
| 98 | /* don't emit duplicate vertices: */ |
| 99 | if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y)) |
| 100 | return; |
| 101 | ex[n] = x; |
| 102 | ey[n] = y; |
| 103 | n++; |
| 104 | } |
| 105 | |
| 106 | /* transform surface to screen space: */ |
| 107 | for (i = 0; i < 4; i++) |
| 108 | weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]); |
| 109 | |
| 110 | /* find bounding box: */ |
| 111 | min_x = max_x = x[0]; |
| 112 | min_y = max_y = y[0]; |
| 113 | |
| 114 | for (i = 1; i < 4; i++) { |
| 115 | min_x = min(min_x, x[i]); |
| 116 | max_x = max(max_x, x[i]); |
| 117 | min_y = min(min_y, y[i]); |
| 118 | max_y = max(max_y, y[i]); |
| 119 | } |
| 120 | |
| 121 | /* First, simple bounding box check to discard early transformed |
| 122 | * surface rects that do not intersect with the clip region: |
| 123 | */ |
| 124 | if ((min_x > cx2) || (max_x < cx1) || |
| 125 | (min_y > cy2) || (max_y < cy1)) |
| 126 | return 0; |
| 127 | |
| 128 | /* Simple case, bounding box edges are parallel to surface edges, |
| 129 | * there will be only four edges. We just need to clip the surface |
| 130 | * vertices to the clip rect bounds: |
| 131 | */ |
| 132 | if (!es->transform.enabled) { |
| 133 | for (i = 0; i < 4; i++) { |
| 134 | ex[n] = clip(x[i], cx1, cx2); |
| 135 | ey[n] = clip(y[i], cy1, cy2); |
| 136 | n++; |
| 137 | } |
| 138 | return 4; |
| 139 | } |
| 140 | |
| 141 | /* Hard case, transformation applied. We need to find the vertices |
| 142 | * of the shape that is the intersection of the clip rect and |
| 143 | * transformed surface. This can be anything from 3 to 8 sides. |
| 144 | * |
| 145 | * Observation: all the resulting vertices will be the intersection |
| 146 | * points of the transformed surface and the clip rect, plus the |
| 147 | * vertices of the clip rect which are enclosed by the transformed |
| 148 | * surface and the vertices of the transformed surface which are |
| 149 | * enclosed by the clip rect. |
| 150 | * |
| 151 | * Observation: there will be zero, one, or two resulting vertices |
| 152 | * for each edge of the src rect. |
| 153 | * |
| 154 | * Loop over four edges of the transformed rect: |
| 155 | */ |
| 156 | for (i = 0; i < 4; i++) { |
| 157 | GLfloat x1, y1, x2, y2; |
| 158 | int last_n = n; |
| 159 | |
| 160 | x1 = x[i]; |
| 161 | y1 = y[i]; |
| 162 | |
| 163 | /* if this vertex is contained in the clip rect, use it as-is: */ |
| 164 | if ((cx1 <= x1) && (x1 <= cx2) && |
| 165 | (cy1 <= y1) && (y1 <= cy2)) |
| 166 | append_vertex(x1, y1); |
| 167 | |
| 168 | /* for remaining, we consider the point as part of a line: */ |
| 169 | x2 = x[(i+1) % 4]; |
| 170 | y2 = y[(i+1) % 4]; |
| 171 | |
| 172 | if (x1 == x2) { |
| 173 | append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2)); |
| 174 | append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2)); |
| 175 | } else if (y1 == y2) { |
| 176 | append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2)); |
| 177 | append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2)); |
| 178 | } else { |
| 179 | GLfloat m, c, p; |
| 180 | GLfloat tx[2], ty[2]; |
| 181 | int tn = 0; |
| 182 | |
| 183 | int intersect_horiz(GLfloat y, GLfloat *p) |
| 184 | { |
| 185 | GLfloat x; |
| 186 | |
| 187 | /* if y does not lie between y1 and y2, no |
| 188 | * intersection possible |
| 189 | */ |
| 190 | if (sign(y-y1) == sign(y-y2)) |
| 191 | return 0; |
| 192 | |
| 193 | x = (y - c) / m; |
| 194 | |
| 195 | /* if x does not lie between cx1 and cx2, no |
| 196 | * intersection: |
| 197 | */ |
| 198 | if (sign(x-cx1) == sign(x-cx2)) |
| 199 | return 0; |
| 200 | |
| 201 | *p = x; |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | int intersect_vert(GLfloat x, GLfloat *p) |
| 206 | { |
| 207 | GLfloat y; |
| 208 | |
| 209 | if (sign(x-x1) == sign(x-x2)) |
| 210 | return 0; |
| 211 | |
| 212 | y = m * x + c; |
| 213 | |
| 214 | if (sign(y-cy1) == sign(y-cy2)) |
| 215 | return 0; |
| 216 | |
| 217 | *p = y; |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | /* y = mx + c */ |
| 222 | m = (y2 - y1) / (x2 - x1); |
| 223 | c = y1 - m * x1; |
| 224 | |
| 225 | /* check for up to two intersections with the four edges |
| 226 | * of the clip rect. Note that we don't know the orientation |
| 227 | * of the transformed surface wrt. the clip rect. So if when |
| 228 | * there are two intersection points, we need to put the one |
| 229 | * closest to x1,y1 first: |
| 230 | */ |
| 231 | |
| 232 | /* check top clip rect edge: */ |
| 233 | if (intersect_horiz(cy1, &p)) { |
| 234 | ty[tn] = cy1; |
| 235 | tx[tn] = p; |
| 236 | tn++; |
| 237 | } |
| 238 | |
| 239 | /* check right clip rect edge: */ |
| 240 | if (intersect_vert(cx2, &p)) { |
| 241 | ty[tn] = p; |
| 242 | tx[tn] = cx2; |
| 243 | tn++; |
| 244 | if (tn == 2) |
| 245 | goto edge_check_done; |
| 246 | } |
| 247 | |
| 248 | /* check bottom clip rect edge: */ |
| 249 | if (intersect_horiz(cy2, &p)) { |
| 250 | ty[tn] = cy2; |
| 251 | tx[tn] = p; |
| 252 | tn++; |
| 253 | if (tn == 2) |
| 254 | goto edge_check_done; |
| 255 | } |
| 256 | |
| 257 | /* check left clip rect edge: */ |
| 258 | if (intersect_vert(cx1, &p)) { |
| 259 | ty[tn] = p; |
| 260 | tx[tn] = cx1; |
| 261 | tn++; |
| 262 | } |
| 263 | |
| 264 | edge_check_done: |
| 265 | if (tn == 1) { |
| 266 | append_vertex(tx[0], ty[0]); |
| 267 | } else if (tn == 2) { |
| 268 | if (dist_squared(x1, y1, tx[0], ty[0]) < |
| 269 | dist_squared(x1, y1, tx[1], ty[1])) { |
| 270 | append_vertex(tx[0], ty[0]); |
| 271 | append_vertex(tx[1], ty[1]); |
| 272 | } else { |
| 273 | append_vertex(tx[1], ty[1]); |
| 274 | append_vertex(tx[0], ty[0]); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (n == last_n) { |
| 279 | GLfloat best_x=0, best_y=0; |
| 280 | uint32_t d, best_d = (unsigned int)-1; /* distance squared */ |
| 281 | uint32_t max_d = dist_squared(x2, y2, |
| 282 | x[(i+2) % 4], y[(i+2) % 4]); |
| 283 | |
| 284 | /* if there are no vertices on this line, it could be that |
| 285 | * there is a vertex of the clip rect that is enclosed by |
| 286 | * the transformed surface. Find the vertex of the clip |
| 287 | * rect that is reached by the shortest line perpendicular |
| 288 | * to the current edge, if any. |
| 289 | * |
| 290 | * slope of perpendicular is 1/m, so |
| 291 | * |
| 292 | * cy = -cx/m + c2 |
| 293 | * c2 = cy + cx/m |
| 294 | * |
| 295 | */ |
| 296 | |
| 297 | int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d) |
| 298 | { |
| 299 | GLfloat c2 = cy + cx/m; |
| 300 | GLfloat x = (c2 - c) / (m + 1/m); |
| 301 | |
| 302 | /* if the x position of the intersection of the |
| 303 | * perpendicular with the transformed edge does |
| 304 | * not lie within the bounds of the edge, then |
| 305 | * no intersection: |
| 306 | */ |
| 307 | if (sign(x-x1) == sign(x-x2)) |
| 308 | return 0; |
| 309 | |
| 310 | *d = dist_squared(cx, cy, x, (m * x) + c); |
| 311 | |
| 312 | /* if intersection distance is further away than |
| 313 | * opposite edge of surface region, it is invalid: |
| 314 | */ |
| 315 | if (*d > max_d) |
| 316 | return 0; |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | if (perp_intersect(cx1, cy1, &d)) { |
| 322 | best_x = cx1; |
| 323 | best_y = cy1; |
| 324 | best_d = d; |
| 325 | } |
| 326 | |
| 327 | if (perp_intersect(cx1, cy2, &d) && (d < best_d)) { |
| 328 | best_x = cx1; |
| 329 | best_y = cy2; |
| 330 | best_d = d; |
| 331 | } |
| 332 | |
| 333 | if (perp_intersect(cx2, cy2, &d) && (d < best_d)) { |
| 334 | best_x = cx2; |
| 335 | best_y = cy2; |
| 336 | best_d = d; |
| 337 | } |
| 338 | |
| 339 | if (perp_intersect(cx2, cy1, &d) && (d < best_d)) { |
| 340 | best_x = cx2; |
| 341 | best_y = cy1; |
| 342 | best_d = d; |
| 343 | } |
| 344 | |
| 345 | if (best_d != (unsigned int)-1) // XXX can this happen? |
| 346 | append_vertex(best_x, best_y); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
| 352 | return n; |
| 353 | } |
| 354 | |
| 355 | static int |
| 356 | texture_region(struct weston_surface *es, pixman_region32_t *region, |
| 357 | pixman_region32_t *surf_region) |
| 358 | { |
| 359 | struct weston_compositor *ec = es->compositor; |
| 360 | GLfloat *v, inv_width, inv_height; |
| 361 | unsigned int *vtxcnt, nvtx = 0; |
| 362 | pixman_box32_t *rects, *surf_rects; |
| 363 | int i, j, k, nrects, nsurf; |
| 364 | |
| 365 | rects = pixman_region32_rectangles(region, &nrects); |
| 366 | surf_rects = pixman_region32_rectangles(surf_region, &nsurf); |
| 367 | |
| 368 | /* worst case we can have 8 vertices per rect (ie. clipped into |
| 369 | * an octagon): |
| 370 | */ |
| 371 | v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v); |
| 372 | vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt); |
| 373 | |
| 374 | inv_width = 1.0 / es->pitch; |
| 375 | inv_height = 1.0 / es->geometry.height; |
| 376 | |
| 377 | for (i = 0; i < nrects; i++) { |
| 378 | pixman_box32_t *rect = &rects[i]; |
| 379 | for (j = 0; j < nsurf; j++) { |
| 380 | pixman_box32_t *surf_rect = &surf_rects[j]; |
| 381 | GLfloat sx, sy; |
| 382 | GLfloat ex[8], ey[8]; /* edge points in screen space */ |
| 383 | int n; |
| 384 | |
| 385 | /* The transformed surface, after clipping to the clip region, |
| 386 | * can have as many as eight sides, emitted as a triangle-fan. |
| 387 | * The first vertex in the triangle fan can be chosen arbitrarily, |
| 388 | * since the area is guaranteed to be convex. |
| 389 | * |
| 390 | * If a corner of the transformed surface falls outside of the |
| 391 | * clip region, instead of emitting one vertex for the corner |
| 392 | * of the surface, up to two are emitted for two corresponding |
| 393 | * intersection point(s) between the surface and the clip region. |
| 394 | * |
| 395 | * To do this, we first calculate the (up to eight) points that |
| 396 | * form the intersection of the clip rect and the transformed |
| 397 | * surface. |
| 398 | */ |
| 399 | n = calculate_edges(es, rect, surf_rect, ex, ey); |
| 400 | if (n < 3) |
| 401 | continue; |
| 402 | |
| 403 | /* emit edge points: */ |
| 404 | for (k = 0; k < n; k++) { |
| 405 | weston_surface_from_global_float(es, ex[k], ey[k], &sx, &sy); |
| 406 | /* position: */ |
| 407 | *(v++) = ex[k]; |
| 408 | *(v++) = ey[k]; |
| 409 | /* texcoord: */ |
| 410 | *(v++) = sx * inv_width; |
| 411 | *(v++) = sy * inv_height; |
| 412 | } |
| 413 | |
| 414 | vtxcnt[nvtx++] = n; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return nvtx; |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | triangle_fan_debug(struct weston_surface *surface, int first, int count) |
| 423 | { |
| 424 | struct weston_compositor *compositor = surface->compositor; |
| 425 | int i; |
| 426 | GLushort *buffer; |
| 427 | GLushort *index; |
| 428 | int nelems; |
| 429 | static int color_idx = 0; |
| 430 | static const GLfloat color[][4] = { |
| 431 | { 1.0, 0.0, 0.0, 1.0 }, |
| 432 | { 0.0, 1.0, 0.0, 1.0 }, |
| 433 | { 0.0, 0.0, 1.0, 1.0 }, |
| 434 | { 1.0, 1.0, 1.0, 1.0 }, |
| 435 | }; |
| 436 | |
| 437 | nelems = (count - 1 + count - 2) * 2; |
| 438 | |
| 439 | buffer = malloc(sizeof(GLushort) * nelems); |
| 440 | index = buffer; |
| 441 | |
| 442 | for (i = 1; i < count; i++) { |
| 443 | *index++ = first; |
| 444 | *index++ = first + i; |
| 445 | } |
| 446 | |
| 447 | for (i = 2; i < count; i++) { |
| 448 | *index++ = first + i - 1; |
| 449 | *index++ = first + i; |
| 450 | } |
| 451 | |
| 452 | glUseProgram(compositor->solid_shader.program); |
| 453 | glUniform4fv(compositor->solid_shader.color_uniform, 1, |
| 454 | color[color_idx++ % ARRAY_LENGTH(color)]); |
| 455 | glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer); |
| 456 | glUseProgram(compositor->current_shader->program); |
| 457 | free(buffer); |
| 458 | } |
| 459 | |
| 460 | static void |
| 461 | repaint_region(struct weston_surface *es, pixman_region32_t *region, |
| 462 | pixman_region32_t *surf_region) |
| 463 | { |
| 464 | struct weston_compositor *ec = es->compositor; |
| 465 | GLfloat *v; |
| 466 | unsigned int *vtxcnt; |
| 467 | int i, first, nfans; |
| 468 | |
| 469 | /* The final region to be painted is the intersection of |
| 470 | * 'region' and 'surf_region'. However, 'region' is in the global |
| 471 | * coordinates, and 'surf_region' is in the surface-local |
| 472 | * coordinates. texture_region() will iterate over all pairs of |
| 473 | * rectangles from both regions, compute the intersection |
| 474 | * polygon for each pair, and store it as a triangle fan if |
| 475 | * it has a non-zero area (at least 3 vertices, actually). |
| 476 | */ |
| 477 | nfans = texture_region(es, region, surf_region); |
| 478 | |
| 479 | v = ec->vertices.data; |
| 480 | vtxcnt = ec->vtxcnt.data; |
| 481 | |
| 482 | /* position: */ |
| 483 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); |
| 484 | glEnableVertexAttribArray(0); |
| 485 | |
| 486 | /* texcoord: */ |
| 487 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); |
| 488 | glEnableVertexAttribArray(1); |
| 489 | |
| 490 | for (i = 0, first = 0; i < nfans; i++) { |
| 491 | glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]); |
| 492 | if (ec->fan_debug) |
| 493 | triangle_fan_debug(es, first, vtxcnt[i]); |
| 494 | first += vtxcnt[i]; |
| 495 | } |
| 496 | |
| 497 | glDisableVertexAttribArray(1); |
| 498 | glDisableVertexAttribArray(0); |
| 499 | |
| 500 | ec->vertices.size = 0; |
| 501 | ec->vtxcnt.size = 0; |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | weston_compositor_use_shader(struct weston_compositor *compositor, |
| 506 | struct weston_shader *shader) |
| 507 | { |
| 508 | if (compositor->current_shader == shader) |
| 509 | return; |
| 510 | |
| 511 | glUseProgram(shader->program); |
| 512 | compositor->current_shader = shader; |
| 513 | } |
| 514 | |
| 515 | static void |
| 516 | weston_shader_uniforms(struct weston_shader *shader, |
| 517 | struct weston_surface *surface, |
| 518 | struct weston_output *output) |
| 519 | { |
| 520 | int i; |
| 521 | |
| 522 | glUniformMatrix4fv(shader->proj_uniform, |
| 523 | 1, GL_FALSE, output->matrix.d); |
| 524 | glUniform4fv(shader->color_uniform, 1, surface->color); |
| 525 | glUniform1f(shader->alpha_uniform, surface->alpha); |
| 526 | |
| 527 | for (i = 0; i < surface->num_textures; i++) |
| 528 | glUniform1i(shader->tex_uniforms[i], i); |
| 529 | } |
| 530 | |
| 531 | static void |
| 532 | draw_surface(struct weston_surface *es, struct weston_output *output, |
| 533 | pixman_region32_t *damage) /* in global coordinates */ |
| 534 | { |
| 535 | struct weston_compositor *ec = es->compositor; |
| 536 | /* repaint bounding region in global coordinates: */ |
| 537 | pixman_region32_t repaint; |
| 538 | /* non-opaque region in surface coordinates: */ |
| 539 | pixman_region32_t surface_blend; |
| 540 | GLint filter; |
| 541 | int i; |
| 542 | |
| 543 | pixman_region32_init(&repaint); |
| 544 | pixman_region32_intersect(&repaint, |
| 545 | &es->transform.boundingbox, damage); |
| 546 | pixman_region32_subtract(&repaint, &repaint, &es->clip); |
| 547 | |
| 548 | if (!pixman_region32_not_empty(&repaint)) |
| 549 | goto out; |
| 550 | |
| 551 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 552 | &ec->primary_plane.damage, &repaint); |
| 553 | |
| 554 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 555 | |
| 556 | if (ec->fan_debug) { |
| 557 | weston_compositor_use_shader(ec, &ec->solid_shader); |
| 558 | weston_shader_uniforms(&ec->solid_shader, es, output); |
| 559 | } |
| 560 | |
| 561 | weston_compositor_use_shader(ec, es->shader); |
| 562 | weston_shader_uniforms(es->shader, es, output); |
| 563 | |
| 564 | if (es->transform.enabled || output->zoom.active) |
| 565 | filter = GL_LINEAR; |
| 566 | else |
| 567 | filter = GL_NEAREST; |
| 568 | |
| 569 | for (i = 0; i < es->num_textures; i++) { |
| 570 | glActiveTexture(GL_TEXTURE0 + i); |
| 571 | glBindTexture(es->target, es->textures[i]); |
| 572 | glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter); |
| 573 | glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter); |
| 574 | } |
| 575 | |
| 576 | /* blended region is whole surface minus opaque region: */ |
| 577 | pixman_region32_init_rect(&surface_blend, 0, 0, |
| 578 | es->geometry.width, es->geometry.height); |
| 579 | pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque); |
| 580 | |
| 581 | if (pixman_region32_not_empty(&es->opaque)) { |
| 582 | if (es->shader == &ec->texture_shader_rgba) { |
| 583 | /* Special case for RGBA textures with possibly |
| 584 | * bad data in alpha channel: use the shader |
| 585 | * that forces texture alpha = 1.0. |
| 586 | * Xwayland surfaces need this. |
| 587 | */ |
| 588 | weston_compositor_use_shader(ec, &ec->texture_shader_rgbx); |
| 589 | weston_shader_uniforms(&ec->texture_shader_rgbx, es, output); |
| 590 | } |
| 591 | |
| 592 | if (es->alpha < 1.0) |
| 593 | glEnable(GL_BLEND); |
| 594 | else |
| 595 | glDisable(GL_BLEND); |
| 596 | |
| 597 | repaint_region(es, &repaint, &es->opaque); |
| 598 | } |
| 599 | |
| 600 | if (pixman_region32_not_empty(&surface_blend)) { |
| 601 | weston_compositor_use_shader(ec, es->shader); |
| 602 | glEnable(GL_BLEND); |
| 603 | repaint_region(es, &repaint, &surface_blend); |
| 604 | } |
| 605 | |
| 606 | pixman_region32_fini(&surface_blend); |
| 607 | |
| 608 | out: |
| 609 | pixman_region32_fini(&repaint); |
| 610 | } |
| 611 | |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 612 | static void |
| 613 | repaint_surfaces(struct weston_output *output, pixman_region32_t *damage) |
| 614 | { |
| 615 | struct weston_compositor *compositor = output->compositor; |
| 616 | struct weston_surface *surface; |
| 617 | |
| 618 | wl_list_for_each_reverse(surface, &compositor->surface_list, link) |
| 619 | if (surface->plane == &compositor->primary_plane) |
Kristian Høgsberg | ecf6ede | 2012-09-05 21:59:35 -0400 | [diff] [blame] | 620 | draw_surface(surface, output, damage); |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 621 | } |
| 622 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 623 | static void |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 624 | gles2_renderer_repaint_output(struct weston_output *output, |
| 625 | pixman_region32_t *output_damage) |
| 626 | { |
| 627 | struct weston_compositor *compositor = output->compositor; |
| 628 | EGLBoolean ret; |
| 629 | static int errored; |
| 630 | int32_t width, height; |
| 631 | |
| 632 | width = output->current->width + |
| 633 | output->border.left + output->border.right; |
| 634 | height = output->current->height + |
| 635 | output->border.top + output->border.bottom; |
| 636 | |
| 637 | glViewport(0, 0, width, height); |
| 638 | |
| 639 | ret = eglMakeCurrent(compositor->egl_display, output->egl_surface, |
| 640 | output->egl_surface, compositor->egl_context); |
| 641 | if (ret == EGL_FALSE) { |
| 642 | if (errored) |
| 643 | return; |
| 644 | errored = 1; |
| 645 | weston_log("Failed to make EGL context current.\n"); |
| 646 | print_egl_error_state(); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | /* if debugging, redraw everything outside the damage to clean up |
| 651 | * debug lines from the previous draw on this buffer: |
| 652 | */ |
| 653 | if (compositor->fan_debug) { |
| 654 | pixman_region32_t undamaged; |
| 655 | pixman_region32_init(&undamaged); |
| 656 | pixman_region32_subtract(&undamaged, &output->region, |
| 657 | output_damage); |
| 658 | compositor->fan_debug = 0; |
| 659 | repaint_surfaces(output, &undamaged); |
| 660 | compositor->fan_debug = 1; |
| 661 | pixman_region32_fini(&undamaged); |
| 662 | } |
| 663 | |
| 664 | repaint_surfaces(output, output_damage); |
| 665 | |
| 666 | wl_signal_emit(&output->frame_signal, output); |
| 667 | |
| 668 | ret = eglSwapBuffers(compositor->egl_display, output->egl_surface); |
| 669 | if (ret == EGL_FALSE && !errored) { |
| 670 | errored = 1; |
| 671 | weston_log("Failed in eglSwapBuffers.\n"); |
| 672 | print_egl_error_state(); |
| 673 | } |
| 674 | |
| 675 | } |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 676 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 677 | static void |
Kristian Høgsberg | b1fd2d6 | 2012-09-05 22:13:58 -0400 | [diff] [blame] | 678 | gles2_renderer_flush_damage(struct weston_surface *surface) |
| 679 | { |
| 680 | #ifdef GL_UNPACK_ROW_LENGTH |
| 681 | pixman_box32_t *rectangles; |
| 682 | void *data; |
| 683 | int i, n; |
| 684 | #endif |
| 685 | |
| 686 | glBindTexture(GL_TEXTURE_2D, surface->textures[0]); |
| 687 | |
| 688 | if (!surface->compositor->has_unpack_subimage) { |
| 689 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 690 | surface->pitch, surface->buffer->height, 0, |
| 691 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, |
| 692 | wl_shm_buffer_get_data(surface->buffer)); |
| 693 | |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | #ifdef GL_UNPACK_ROW_LENGTH |
| 698 | /* Mesa does not define GL_EXT_unpack_subimage */ |
| 699 | glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch); |
| 700 | data = wl_shm_buffer_get_data(surface->buffer); |
| 701 | rectangles = pixman_region32_rectangles(&surface->damage, &n); |
| 702 | for (i = 0; i < n; i++) { |
| 703 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1); |
| 704 | glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1); |
| 705 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 706 | rectangles[i].x1, rectangles[i].y1, |
| 707 | rectangles[i].x2 - rectangles[i].x1, |
| 708 | rectangles[i].y2 - rectangles[i].y1, |
| 709 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, data); |
| 710 | } |
| 711 | #endif |
| 712 | } |
| 713 | |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 714 | static void |
| 715 | ensure_textures(struct weston_surface *es, int num_textures) |
| 716 | { |
| 717 | int i; |
| 718 | |
| 719 | if (num_textures <= es->num_textures) |
| 720 | return; |
| 721 | |
| 722 | for (i = es->num_textures; i < num_textures; i++) { |
| 723 | glGenTextures(1, &es->textures[i]); |
| 724 | glBindTexture(es->target, es->textures[i]); |
| 725 | glTexParameteri(es->target, |
| 726 | GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 727 | glTexParameteri(es->target, |
| 728 | GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 729 | } |
| 730 | es->num_textures = num_textures; |
| 731 | glBindTexture(es->target, 0); |
| 732 | } |
| 733 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 734 | static void |
Kristian Høgsberg | b7b77e6 | 2012-09-05 22:38:18 -0400 | [diff] [blame] | 735 | gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer) |
| 736 | { |
| 737 | struct weston_compositor *ec = es->compositor; |
| 738 | EGLint attribs[3], format; |
| 739 | int i, num_planes; |
| 740 | |
| 741 | if (!buffer) { |
| 742 | for (i = 0; i < es->num_images; i++) { |
| 743 | ec->destroy_image(ec->egl_display, es->images[i]); |
| 744 | es->images[i] = NULL; |
| 745 | } |
| 746 | es->num_images = 0; |
| 747 | glDeleteTextures(es->num_textures, es->textures); |
| 748 | es->num_textures = 0; |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | if (wl_buffer_is_shm(buffer)) { |
| 753 | es->pitch = wl_shm_buffer_get_stride(buffer) / 4; |
| 754 | es->target = GL_TEXTURE_2D; |
| 755 | |
| 756 | ensure_textures(es, 1); |
| 757 | glBindTexture(GL_TEXTURE_2D, es->textures[0]); |
| 758 | glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, |
| 759 | es->pitch, buffer->height, 0, |
| 760 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); |
| 761 | if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888) |
| 762 | es->shader = &ec->texture_shader_rgbx; |
| 763 | else |
| 764 | es->shader = &ec->texture_shader_rgba; |
| 765 | } else if (ec->query_buffer(ec->egl_display, buffer, |
| 766 | EGL_TEXTURE_FORMAT, &format)) { |
| 767 | for (i = 0; i < es->num_images; i++) |
| 768 | ec->destroy_image(ec->egl_display, es->images[i]); |
| 769 | es->num_images = 0; |
| 770 | es->target = GL_TEXTURE_2D; |
| 771 | switch (format) { |
| 772 | case EGL_TEXTURE_RGB: |
| 773 | case EGL_TEXTURE_RGBA: |
| 774 | default: |
| 775 | num_planes = 1; |
| 776 | es->shader = &ec->texture_shader_rgba; |
| 777 | break; |
| 778 | case EGL_TEXTURE_EXTERNAL_WL: |
| 779 | num_planes = 1; |
| 780 | es->target = GL_TEXTURE_EXTERNAL_OES; |
| 781 | es->shader = &ec->texture_shader_egl_external; |
| 782 | break; |
| 783 | case EGL_TEXTURE_Y_UV_WL: |
| 784 | num_planes = 2; |
| 785 | es->shader = &ec->texture_shader_y_uv; |
| 786 | break; |
| 787 | case EGL_TEXTURE_Y_U_V_WL: |
| 788 | num_planes = 3; |
| 789 | es->shader = &ec->texture_shader_y_u_v; |
| 790 | break; |
| 791 | case EGL_TEXTURE_Y_XUXV_WL: |
| 792 | num_planes = 2; |
| 793 | es->shader = &ec->texture_shader_y_xuxv; |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | ensure_textures(es, num_planes); |
| 798 | for (i = 0; i < num_planes; i++) { |
| 799 | attribs[0] = EGL_WAYLAND_PLANE_WL; |
| 800 | attribs[1] = i; |
| 801 | attribs[2] = EGL_NONE; |
| 802 | es->images[i] = ec->create_image(ec->egl_display, |
| 803 | NULL, |
| 804 | EGL_WAYLAND_BUFFER_WL, |
| 805 | buffer, attribs); |
| 806 | if (!es->images[i]) { |
| 807 | weston_log("failed to create img for plane %d\n", i); |
| 808 | continue; |
| 809 | } |
| 810 | es->num_images++; |
| 811 | |
| 812 | glActiveTexture(GL_TEXTURE0 + i); |
| 813 | glBindTexture(es->target, es->textures[i]); |
| 814 | ec->image_target_texture_2d(es->target, |
| 815 | es->images[i]); |
| 816 | } |
| 817 | |
| 818 | es->pitch = buffer->width; |
| 819 | } else { |
| 820 | weston_log("unhandled buffer type!\n"); |
| 821 | } |
| 822 | } |
| 823 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 824 | static const char vertex_shader[] = |
| 825 | "uniform mat4 proj;\n" |
| 826 | "attribute vec2 position;\n" |
| 827 | "attribute vec2 texcoord;\n" |
| 828 | "varying vec2 v_texcoord;\n" |
| 829 | "void main()\n" |
| 830 | "{\n" |
| 831 | " gl_Position = proj * vec4(position, 0.0, 1.0);\n" |
| 832 | " v_texcoord = texcoord;\n" |
| 833 | "}\n"; |
| 834 | |
| 835 | /* Declare common fragment shader uniforms */ |
| 836 | #define FRAGMENT_CONVERT_YUV \ |
| 837 | " y *= alpha;\n" \ |
| 838 | " u *= alpha;\n" \ |
| 839 | " v *= alpha;\n" \ |
| 840 | " gl_FragColor.r = y + 1.59602678 * v;\n" \ |
| 841 | " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \ |
| 842 | " gl_FragColor.b = y + 2.01723214 * u;\n" \ |
| 843 | " gl_FragColor.a = alpha;\n" |
| 844 | |
| 845 | static const char texture_fragment_shader_rgba[] = |
| 846 | "precision mediump float;\n" |
| 847 | "varying vec2 v_texcoord;\n" |
| 848 | "uniform sampler2D tex;\n" |
| 849 | "uniform float alpha;\n" |
| 850 | "void main()\n" |
| 851 | "{\n" |
| 852 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
| 853 | "}\n"; |
| 854 | |
| 855 | static const char texture_fragment_shader_rgbx[] = |
| 856 | "precision mediump float;\n" |
| 857 | "varying vec2 v_texcoord;\n" |
| 858 | "uniform sampler2D tex;\n" |
| 859 | "uniform float alpha;\n" |
| 860 | "void main()\n" |
| 861 | "{\n" |
| 862 | " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;" |
| 863 | " gl_FragColor.a = alpha;\n" |
| 864 | "}\n"; |
| 865 | |
| 866 | static const char texture_fragment_shader_egl_external[] = |
| 867 | "#extension GL_OES_EGL_image_external : require\n" |
| 868 | "precision mediump float;\n" |
| 869 | "varying vec2 v_texcoord;\n" |
| 870 | "uniform samplerExternalOES tex;\n" |
| 871 | "uniform float alpha;\n" |
| 872 | "void main()\n" |
| 873 | "{\n" |
| 874 | " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;" |
| 875 | "}\n"; |
| 876 | |
| 877 | static const char texture_fragment_shader_y_uv[] = |
| 878 | "precision mediump float;\n" |
| 879 | "uniform sampler2D tex;\n" |
| 880 | "uniform sampler2D tex1;\n" |
| 881 | "varying vec2 v_texcoord;\n" |
| 882 | "uniform float alpha;\n" |
| 883 | "void main() {\n" |
| 884 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 885 | " float u = texture2D(tex1, v_texcoord).r - 0.5;\n" |
| 886 | " float v = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 887 | FRAGMENT_CONVERT_YUV |
| 888 | "}\n"; |
| 889 | |
| 890 | static const char texture_fragment_shader_y_u_v[] = |
| 891 | "precision mediump float;\n" |
| 892 | "uniform sampler2D tex;\n" |
| 893 | "uniform sampler2D tex1;\n" |
| 894 | "uniform sampler2D tex2;\n" |
| 895 | "varying vec2 v_texcoord;\n" |
| 896 | "uniform float alpha;\n" |
| 897 | "void main() {\n" |
| 898 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 899 | " float u = texture2D(tex1, v_texcoord).x - 0.5;\n" |
| 900 | " float v = texture2D(tex2, v_texcoord).x - 0.5;\n" |
| 901 | FRAGMENT_CONVERT_YUV |
| 902 | "}\n"; |
| 903 | |
| 904 | static const char texture_fragment_shader_y_xuxv[] = |
| 905 | "precision mediump float;\n" |
| 906 | "uniform sampler2D tex;\n" |
| 907 | "uniform sampler2D tex1;\n" |
| 908 | "varying vec2 v_texcoord;\n" |
| 909 | "uniform float alpha;\n" |
| 910 | "void main() {\n" |
| 911 | " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n" |
| 912 | " float u = texture2D(tex1, v_texcoord).g - 0.5;\n" |
| 913 | " float v = texture2D(tex1, v_texcoord).a - 0.5;\n" |
| 914 | FRAGMENT_CONVERT_YUV |
| 915 | "}\n"; |
| 916 | |
| 917 | static const char solid_fragment_shader[] = |
| 918 | "precision mediump float;\n" |
| 919 | "uniform vec4 color;\n" |
| 920 | "uniform float alpha;\n" |
| 921 | "void main()\n" |
| 922 | "{\n" |
| 923 | " gl_FragColor = alpha * color\n;" |
| 924 | "}\n"; |
| 925 | |
| 926 | static int |
| 927 | compile_shader(GLenum type, const char *source) |
| 928 | { |
| 929 | GLuint s; |
| 930 | char msg[512]; |
| 931 | GLint status; |
| 932 | |
| 933 | s = glCreateShader(type); |
| 934 | glShaderSource(s, 1, &source, NULL); |
| 935 | glCompileShader(s); |
| 936 | glGetShaderiv(s, GL_COMPILE_STATUS, &status); |
| 937 | if (!status) { |
| 938 | glGetShaderInfoLog(s, sizeof msg, NULL, msg); |
| 939 | weston_log("shader info: %s\n", msg); |
| 940 | return GL_NONE; |
| 941 | } |
| 942 | |
| 943 | return s; |
| 944 | } |
| 945 | |
| 946 | static int |
| 947 | weston_shader_init(struct weston_shader *shader, |
| 948 | const char *vertex_source, const char *fragment_source) |
| 949 | { |
| 950 | char msg[512]; |
| 951 | GLint status; |
| 952 | |
| 953 | shader->vertex_shader = |
| 954 | compile_shader(GL_VERTEX_SHADER, vertex_source); |
| 955 | shader->fragment_shader = |
| 956 | compile_shader(GL_FRAGMENT_SHADER, fragment_source); |
| 957 | |
| 958 | shader->program = glCreateProgram(); |
| 959 | glAttachShader(shader->program, shader->vertex_shader); |
| 960 | glAttachShader(shader->program, shader->fragment_shader); |
| 961 | glBindAttribLocation(shader->program, 0, "position"); |
| 962 | glBindAttribLocation(shader->program, 1, "texcoord"); |
| 963 | |
| 964 | glLinkProgram(shader->program); |
| 965 | glGetProgramiv(shader->program, GL_LINK_STATUS, &status); |
| 966 | if (!status) { |
| 967 | glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); |
| 968 | weston_log("link info: %s\n", msg); |
| 969 | return -1; |
| 970 | } |
| 971 | |
| 972 | shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); |
| 973 | shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex"); |
| 974 | shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1"); |
| 975 | shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2"); |
| 976 | shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha"); |
| 977 | shader->color_uniform = glGetUniformLocation(shader->program, "color"); |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static void |
| 983 | log_extensions(const char *name, const char *extensions) |
| 984 | { |
| 985 | const char *p, *end; |
| 986 | int l; |
| 987 | int len; |
| 988 | |
| 989 | l = weston_log("%s:", name); |
| 990 | p = extensions; |
| 991 | while (*p) { |
| 992 | end = strchrnul(p, ' '); |
| 993 | len = end - p; |
| 994 | if (l + len > 78) |
| 995 | l = weston_log_continue("\n" STAMP_SPACE "%.*s", |
| 996 | len, p); |
| 997 | else |
| 998 | l += weston_log_continue(" %.*s", len, p); |
| 999 | for (p = end; isspace(*p); p++) |
| 1000 | ; |
| 1001 | } |
| 1002 | weston_log_continue("\n"); |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | log_egl_gl_info(EGLDisplay egldpy) |
| 1007 | { |
| 1008 | const char *str; |
| 1009 | |
| 1010 | str = eglQueryString(egldpy, EGL_VERSION); |
| 1011 | weston_log("EGL version: %s\n", str ? str : "(null)"); |
| 1012 | |
| 1013 | str = eglQueryString(egldpy, EGL_VENDOR); |
| 1014 | weston_log("EGL vendor: %s\n", str ? str : "(null)"); |
| 1015 | |
| 1016 | str = eglQueryString(egldpy, EGL_CLIENT_APIS); |
| 1017 | weston_log("EGL client APIs: %s\n", str ? str : "(null)"); |
| 1018 | |
| 1019 | str = eglQueryString(egldpy, EGL_EXTENSIONS); |
| 1020 | log_extensions("EGL extensions", str ? str : "(null)"); |
| 1021 | |
| 1022 | str = (char *)glGetString(GL_VERSION); |
| 1023 | weston_log("GL version: %s\n", str ? str : "(null)"); |
| 1024 | |
| 1025 | str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION); |
| 1026 | weston_log("GLSL version: %s\n", str ? str : "(null)"); |
| 1027 | |
| 1028 | str = (char *)glGetString(GL_VENDOR); |
| 1029 | weston_log("GL vendor: %s\n", str ? str : "(null)"); |
| 1030 | |
| 1031 | str = (char *)glGetString(GL_RENDERER); |
| 1032 | weston_log("GL renderer: %s\n", str ? str : "(null)"); |
| 1033 | |
| 1034 | str = (char *)glGetString(GL_EXTENSIONS); |
| 1035 | log_extensions("GL extensions", str ? str : "(null)"); |
| 1036 | } |
| 1037 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 1038 | struct gles2_renderer { |
| 1039 | struct weston_renderer base; |
| 1040 | }; |
| 1041 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1042 | WL_EXPORT int |
| 1043 | gles2_renderer_init(struct weston_compositor *ec) |
| 1044 | { |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 1045 | struct gles2_renderer *renderer; |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1046 | const char *extensions; |
| 1047 | int has_egl_image_external = 0; |
| 1048 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 1049 | renderer = malloc(sizeof *renderer); |
| 1050 | if (renderer == NULL) |
| 1051 | return -1; |
| 1052 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1053 | log_egl_gl_info(ec->egl_display); |
| 1054 | |
| 1055 | ec->image_target_texture_2d = |
| 1056 | (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES"); |
| 1057 | ec->image_target_renderbuffer_storage = (void *) |
| 1058 | eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES"); |
| 1059 | ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR"); |
| 1060 | ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR"); |
| 1061 | ec->bind_display = |
| 1062 | (void *) eglGetProcAddress("eglBindWaylandDisplayWL"); |
| 1063 | ec->unbind_display = |
| 1064 | (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL"); |
| 1065 | ec->query_buffer = |
| 1066 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 1067 | |
| 1068 | extensions = (const char *) glGetString(GL_EXTENSIONS); |
| 1069 | if (!extensions) { |
| 1070 | weston_log("Retrieving GL extension string failed.\n"); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
| 1074 | if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) { |
| 1075 | weston_log("GL_EXT_texture_format_BGRA8888 not available\n"); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | |
| 1079 | if (strstr(extensions, "GL_EXT_read_format_bgra")) |
| 1080 | ec->read_format = GL_BGRA_EXT; |
| 1081 | else |
| 1082 | ec->read_format = GL_RGBA; |
| 1083 | |
| 1084 | if (strstr(extensions, "GL_EXT_unpack_subimage")) |
| 1085 | ec->has_unpack_subimage = 1; |
| 1086 | |
| 1087 | if (strstr(extensions, "GL_OES_EGL_image_external")) |
| 1088 | has_egl_image_external = 1; |
| 1089 | |
| 1090 | extensions = |
| 1091 | (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS); |
| 1092 | if (!extensions) { |
| 1093 | weston_log("Retrieving EGL extension string failed.\n"); |
| 1094 | return -1; |
| 1095 | } |
| 1096 | |
| 1097 | if (strstr(extensions, "EGL_WL_bind_wayland_display")) |
| 1098 | ec->has_bind_display = 1; |
| 1099 | if (ec->has_bind_display) |
| 1100 | ec->bind_display(ec->egl_display, ec->wl_display); |
| 1101 | |
| 1102 | glActiveTexture(GL_TEXTURE0); |
| 1103 | |
| 1104 | if (weston_shader_init(&ec->texture_shader_rgba, |
| 1105 | vertex_shader, texture_fragment_shader_rgba) < 0) |
| 1106 | return -1; |
| 1107 | if (weston_shader_init(&ec->texture_shader_rgbx, |
| 1108 | vertex_shader, texture_fragment_shader_rgbx) < 0) |
| 1109 | return -1; |
| 1110 | if (has_egl_image_external && |
| 1111 | weston_shader_init(&ec->texture_shader_egl_external, |
| 1112 | vertex_shader, texture_fragment_shader_egl_external) < 0) |
| 1113 | return -1; |
| 1114 | if (weston_shader_init(&ec->texture_shader_y_uv, |
| 1115 | vertex_shader, texture_fragment_shader_y_uv) < 0) |
| 1116 | return -1; |
| 1117 | if (weston_shader_init(&ec->texture_shader_y_u_v, |
| 1118 | vertex_shader, texture_fragment_shader_y_u_v) < 0) |
| 1119 | return -1; |
| 1120 | if (weston_shader_init(&ec->texture_shader_y_xuxv, |
| 1121 | vertex_shader, texture_fragment_shader_y_xuxv) < 0) |
| 1122 | return -1; |
| 1123 | if (weston_shader_init(&ec->solid_shader, |
| 1124 | vertex_shader, solid_fragment_shader) < 0) |
| 1125 | return -1; |
| 1126 | |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame^] | 1127 | renderer->base.repaint_output = gles2_renderer_repaint_output; |
| 1128 | renderer->base.flush_damage = gles2_renderer_flush_damage; |
| 1129 | renderer->base.attach = gles2_renderer_attach; |
| 1130 | ec->renderer = &renderer->base; |
| 1131 | |
Kristian Høgsberg | 25894fc | 2012-09-05 22:06:26 -0400 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |