Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Keith Packard |
| 3 | * Copyright © 2015 Boyan Ding |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <X11/xshmfence.h> |
| 29 | #include <xcb/xcb.h> |
| 30 | #include <xcb/dri3.h> |
| 31 | #include <xcb/present.h> |
| 32 | |
| 33 | #include <X11/Xlib-xcb.h> |
| 34 | |
| 35 | #include "loader_dri3_helper.h" |
| 36 | |
| 37 | /* From xmlpool/options.h, user exposed so should be stable */ |
| 38 | #define DRI_CONF_VBLANK_NEVER 0 |
| 39 | #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1 |
| 40 | #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2 |
| 41 | #define DRI_CONF_VBLANK_ALWAYS_SYNC 3 |
| 42 | |
| 43 | static inline void |
| 44 | dri3_fence_reset(xcb_connection_t *c, struct loader_dri3_buffer *buffer) |
| 45 | { |
| 46 | xshmfence_reset(buffer->shm_fence); |
| 47 | } |
| 48 | |
| 49 | static inline void |
| 50 | dri3_fence_set(struct loader_dri3_buffer *buffer) |
| 51 | { |
| 52 | xshmfence_trigger(buffer->shm_fence); |
| 53 | } |
| 54 | |
| 55 | static inline void |
| 56 | dri3_fence_trigger(xcb_connection_t *c, struct loader_dri3_buffer *buffer) |
| 57 | { |
| 58 | xcb_sync_trigger_fence(c, buffer->sync_fence); |
| 59 | } |
| 60 | |
| 61 | static inline void |
| 62 | dri3_fence_await(xcb_connection_t *c, struct loader_dri3_buffer *buffer) |
| 63 | { |
| 64 | xcb_flush(c); |
| 65 | xshmfence_await(buffer->shm_fence); |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | dri3_update_num_back(struct loader_dri3_drawable *draw) |
| 70 | { |
| 71 | draw->num_back = 1; |
| 72 | if (draw->flipping) { |
| 73 | if (!draw->is_pixmap && |
| 74 | !(draw->present_capabilities & XCB_PRESENT_CAPABILITY_ASYNC)) |
| 75 | draw->num_back++; |
| 76 | draw->num_back++; |
| 77 | } |
| 78 | if (draw->vtable->get_swap_interval(draw) == 0) |
| 79 | draw->num_back++; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | loader_dri3_set_swap_interval(struct loader_dri3_drawable *draw, int interval) |
| 84 | { |
| 85 | interval = draw->vtable->clamp_swap_interval(draw, interval); |
| 86 | draw->vtable->set_swap_interval(draw, interval); |
| 87 | dri3_update_num_back(draw); |
| 88 | } |
| 89 | |
| 90 | /** dri3_free_render_buffer |
| 91 | * |
| 92 | * Free everything associated with one render buffer including pixmap, fence |
| 93 | * stuff and the driver image |
| 94 | */ |
| 95 | static void |
| 96 | dri3_free_render_buffer(struct loader_dri3_drawable *draw, |
| 97 | struct loader_dri3_buffer *buffer) |
| 98 | { |
| 99 | if (buffer->own_pixmap) |
| 100 | xcb_free_pixmap(draw->conn, buffer->pixmap); |
| 101 | xcb_sync_destroy_fence(draw->conn, buffer->sync_fence); |
| 102 | xshmfence_unmap_shm(buffer->shm_fence); |
| 103 | (draw->ext->image->destroyImage)(buffer->image); |
| 104 | if (buffer->linear_buffer) |
| 105 | (draw->ext->image->destroyImage)(buffer->linear_buffer); |
| 106 | free(buffer); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | loader_dri3_drawable_fini(struct loader_dri3_drawable *draw) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | (draw->ext->core->destroyDrawable)(draw->dri_drawable); |
| 115 | |
| 116 | for (i = 0; i < LOADER_DRI3_NUM_BUFFERS; i++) { |
| 117 | if (draw->buffers[i]) |
| 118 | dri3_free_render_buffer(draw, draw->buffers[i]); |
| 119 | } |
| 120 | |
| 121 | if (draw->special_event) |
| 122 | xcb_unregister_for_special_event(draw->conn, draw->special_event); |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | loader_dri3_drawable_init(xcb_connection_t *conn, |
| 127 | xcb_drawable_t drawable, |
| 128 | __DRIscreen *dri_screen, |
| 129 | bool is_different_gpu, |
| 130 | const __DRIconfig *dri_config, |
| 131 | struct loader_dri3_extensions *ext, |
| 132 | struct loader_dri3_vtable *vtable, |
| 133 | struct loader_dri3_drawable *draw) |
| 134 | { |
| 135 | xcb_get_geometry_cookie_t cookie; |
| 136 | xcb_get_geometry_reply_t *reply; |
| 137 | xcb_generic_error_t *error; |
| 138 | GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1; |
| 139 | int swap_interval; |
| 140 | |
| 141 | draw->conn = conn; |
| 142 | draw->ext = ext; |
| 143 | draw->vtable = vtable; |
| 144 | draw->drawable = drawable; |
| 145 | draw->dri_screen = dri_screen; |
| 146 | draw->is_different_gpu = is_different_gpu; |
| 147 | |
| 148 | draw->have_back = 0; |
| 149 | draw->have_fake_front = 0; |
| 150 | draw->first_init = true; |
| 151 | |
| 152 | if (draw->ext->config) |
| 153 | draw->ext->config->configQueryi(draw->dri_screen, |
| 154 | "vblank_mode", &vblank_mode); |
| 155 | |
| 156 | switch (vblank_mode) { |
| 157 | case DRI_CONF_VBLANK_NEVER: |
| 158 | case DRI_CONF_VBLANK_DEF_INTERVAL_0: |
| 159 | swap_interval = 0; |
| 160 | break; |
| 161 | case DRI_CONF_VBLANK_DEF_INTERVAL_1: |
| 162 | case DRI_CONF_VBLANK_ALWAYS_SYNC: |
| 163 | default: |
| 164 | swap_interval = 1; |
| 165 | break; |
| 166 | } |
| 167 | draw->vtable->set_swap_interval(draw, swap_interval); |
| 168 | |
| 169 | dri3_update_num_back(draw); |
| 170 | |
| 171 | /* Create a new drawable */ |
| 172 | draw->dri_drawable = |
| 173 | (draw->ext->image_driver->createNewDrawable)(dri_screen, |
| 174 | dri_config, |
| 175 | draw); |
| 176 | |
| 177 | if (!draw->dri_drawable) |
| 178 | return 1; |
| 179 | |
| 180 | cookie = xcb_get_geometry(draw->conn, draw->drawable); |
| 181 | reply = xcb_get_geometry_reply(draw->conn, cookie, &error); |
| 182 | if (reply == NULL || error != NULL) { |
| 183 | draw->ext->core->destroyDrawable(draw->dri_drawable); |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | draw->width = reply->width; |
| 188 | draw->height = reply->height; |
| 189 | draw->depth = reply->depth; |
| 190 | draw->vtable->set_drawable_size(draw, draw->width, draw->height); |
| 191 | free(reply); |
| 192 | |
| 193 | /* |
| 194 | * Make sure server has the same swap interval we do for the new |
| 195 | * drawable. |
| 196 | */ |
| 197 | loader_dri3_set_swap_interval(draw, swap_interval); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Process one Present event |
| 204 | */ |
| 205 | static void |
| 206 | dri3_handle_present_event(struct loader_dri3_drawable *draw, |
| 207 | xcb_present_generic_event_t *ge) |
| 208 | { |
| 209 | switch (ge->evtype) { |
| 210 | case XCB_PRESENT_CONFIGURE_NOTIFY: { |
| 211 | xcb_present_configure_notify_event_t *ce = (void *) ge; |
| 212 | |
| 213 | draw->width = ce->width; |
| 214 | draw->height = ce->height; |
| 215 | draw->vtable->set_drawable_size(draw, draw->width, draw->height); |
| 216 | break; |
| 217 | } |
| 218 | case XCB_PRESENT_COMPLETE_NOTIFY: { |
| 219 | xcb_present_complete_notify_event_t *ce = (void *) ge; |
| 220 | |
| 221 | /* Compute the processed SBC number from the received 32-bit serial number |
| 222 | * merged with the upper 32-bits of the sent 64-bit serial number while |
| 223 | * checking for wrap. |
| 224 | */ |
| 225 | if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) { |
| 226 | draw->recv_sbc = (draw->send_sbc & 0xffffffff00000000LL) | ce->serial; |
| 227 | if (draw->recv_sbc > draw->send_sbc) |
| 228 | draw->recv_sbc -= 0x100000000; |
| 229 | switch (ce->mode) { |
| 230 | case XCB_PRESENT_COMPLETE_MODE_FLIP: |
| 231 | draw->flipping = true; |
| 232 | break; |
| 233 | case XCB_PRESENT_COMPLETE_MODE_COPY: |
| 234 | draw->flipping = false; |
| 235 | break; |
| 236 | } |
| 237 | dri3_update_num_back(draw); |
| 238 | |
| 239 | if (draw->vtable->show_fps) |
| 240 | draw->vtable->show_fps(draw, ce->ust); |
| 241 | |
| 242 | draw->ust = ce->ust; |
| 243 | draw->msc = ce->msc; |
| 244 | } else { |
| 245 | draw->recv_msc_serial = ce->serial; |
| 246 | draw->notify_ust = ce->ust; |
| 247 | draw->notify_msc = ce->msc; |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | case XCB_PRESENT_EVENT_IDLE_NOTIFY: { |
| 252 | xcb_present_idle_notify_event_t *ie = (void *) ge; |
| 253 | int b; |
| 254 | |
| 255 | for (b = 0; b < sizeof(draw->buffers) / sizeof(draw->buffers[0]); b++) { |
| 256 | struct loader_dri3_buffer *buf = draw->buffers[b]; |
| 257 | |
| 258 | if (buf && buf->pixmap == ie->pixmap) { |
| 259 | buf->busy = 0; |
| 260 | if (draw->num_back <= b && b < LOADER_DRI3_MAX_BACK) { |
| 261 | dri3_free_render_buffer(draw, buf); |
| 262 | draw->buffers[b] = NULL; |
| 263 | } |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | free(ge); |
| 271 | } |
| 272 | |
| 273 | static bool |
| 274 | dri3_wait_for_event(struct loader_dri3_drawable *draw) |
| 275 | { |
| 276 | xcb_generic_event_t *ev; |
| 277 | xcb_present_generic_event_t *ge; |
| 278 | |
| 279 | xcb_flush(draw->conn); |
| 280 | ev = xcb_wait_for_special_event(draw->conn, draw->special_event); |
| 281 | if (!ev) |
| 282 | return false; |
| 283 | ge = (void *) ev; |
| 284 | dri3_handle_present_event(draw, ge); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | /** loader_dri3_wait_for_msc |
| 289 | * |
| 290 | * Get the X server to send an event when the target msc/divisor/remainder is |
| 291 | * reached. |
| 292 | */ |
| 293 | bool |
| 294 | loader_dri3_wait_for_msc(struct loader_dri3_drawable *draw, |
| 295 | int64_t target_msc, |
| 296 | int64_t divisor, int64_t remainder, |
| 297 | int64_t *ust, int64_t *msc, int64_t *sbc) |
| 298 | { |
| 299 | uint32_t msc_serial; |
| 300 | |
| 301 | msc_serial = ++draw->send_msc_serial; |
| 302 | xcb_present_notify_msc(draw->conn, |
| 303 | draw->drawable, |
| 304 | msc_serial, |
| 305 | target_msc, |
| 306 | divisor, |
| 307 | remainder); |
| 308 | |
| 309 | xcb_flush(draw->conn); |
| 310 | |
| 311 | /* Wait for the event */ |
| 312 | if (draw->special_event) { |
| 313 | while ((int32_t) (msc_serial - draw->recv_msc_serial) > 0) { |
| 314 | if (!dri3_wait_for_event(draw)) |
| 315 | return false; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | *ust = draw->notify_ust; |
| 320 | *msc = draw->notify_msc; |
| 321 | *sbc = draw->recv_sbc; |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | /** loader_dri3_wait_for_sbc |
| 327 | * |
| 328 | * Wait for the completed swap buffer count to reach the specified |
| 329 | * target. Presumably the application knows that this will be reached with |
| 330 | * outstanding complete events, or we're going to be here awhile. |
| 331 | */ |
| 332 | int |
| 333 | loader_dri3_wait_for_sbc(struct loader_dri3_drawable *draw, |
| 334 | int64_t target_sbc, int64_t *ust, |
| 335 | int64_t *msc, int64_t *sbc) |
| 336 | { |
| 337 | /* From the GLX_OML_sync_control spec: |
| 338 | * |
| 339 | * "If <target_sbc> = 0, the function will block until all previous |
| 340 | * swaps requested with glXSwapBuffersMscOML for that window have |
| 341 | * completed." |
| 342 | */ |
| 343 | if (!target_sbc) |
| 344 | target_sbc = draw->send_sbc; |
| 345 | |
| 346 | while (draw->recv_sbc < target_sbc) { |
| 347 | if (!dri3_wait_for_event(draw)) |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | *ust = draw->ust; |
| 352 | *msc = draw->msc; |
| 353 | *sbc = draw->recv_sbc; |
| 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | /** loader_dri3_find_back |
| 358 | * |
| 359 | * Find an idle back buffer. If there isn't one, then |
| 360 | * wait for a present idle notify event from the X server |
| 361 | */ |
| 362 | static int |
| 363 | dri3_find_back(struct loader_dri3_drawable *draw) |
| 364 | { |
| 365 | int b; |
| 366 | xcb_generic_event_t *ev; |
| 367 | xcb_present_generic_event_t *ge; |
| 368 | |
| 369 | for (;;) { |
| 370 | for (b = 0; b < draw->num_back; b++) { |
| 371 | int id = LOADER_DRI3_BACK_ID((b + draw->cur_back) % draw->num_back); |
| 372 | struct loader_dri3_buffer *buffer = draw->buffers[id]; |
| 373 | |
| 374 | if (!buffer || !buffer->busy) { |
| 375 | draw->cur_back = id; |
| 376 | return id; |
| 377 | } |
| 378 | } |
| 379 | xcb_flush(draw->conn); |
| 380 | ev = xcb_wait_for_special_event(draw->conn, draw->special_event); |
| 381 | if (!ev) |
| 382 | return -1; |
| 383 | ge = (void *) ev; |
| 384 | dri3_handle_present_event(draw, ge); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static xcb_gcontext_t |
| 389 | dri3_drawable_gc(struct loader_dri3_drawable *draw) |
| 390 | { |
| 391 | if (!draw->gc) { |
| 392 | uint32_t v = 0; |
| 393 | xcb_create_gc(draw->conn, |
| 394 | (draw->gc = xcb_generate_id(draw->conn)), |
| 395 | draw->drawable, |
| 396 | XCB_GC_GRAPHICS_EXPOSURES, |
| 397 | &v); |
| 398 | } |
| 399 | return draw->gc; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static struct loader_dri3_buffer * |
| 404 | dri3_back_buffer(struct loader_dri3_drawable *draw) |
| 405 | { |
| 406 | return draw->buffers[LOADER_DRI3_BACK_ID(draw->cur_back)]; |
| 407 | } |
| 408 | |
| 409 | static struct loader_dri3_buffer * |
| 410 | dri3_fake_front_buffer(struct loader_dri3_drawable *draw) |
| 411 | { |
| 412 | return draw->buffers[LOADER_DRI3_FRONT_ID]; |
| 413 | } |
| 414 | |
| 415 | static void |
| 416 | dri3_copy_area(xcb_connection_t *c, |
| 417 | xcb_drawable_t src_drawable, |
| 418 | xcb_drawable_t dst_drawable, |
| 419 | xcb_gcontext_t gc, |
| 420 | int16_t src_x, |
| 421 | int16_t src_y, |
| 422 | int16_t dst_x, |
| 423 | int16_t dst_y, |
| 424 | uint16_t width, |
| 425 | uint16_t height) |
| 426 | { |
| 427 | xcb_void_cookie_t cookie; |
| 428 | |
| 429 | cookie = xcb_copy_area_checked(c, |
| 430 | src_drawable, |
| 431 | dst_drawable, |
| 432 | gc, |
| 433 | src_x, |
| 434 | src_y, |
| 435 | dst_x, |
| 436 | dst_y, |
| 437 | width, |
| 438 | height); |
| 439 | xcb_discard_reply(c, cookie.sequence); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Asks the driver to flush any queued work necessary for serializing with the |
| 444 | * X command stream, and optionally the slightly more strict requirement of |
| 445 | * glFlush() equivalence (which would require flushing even if nothing had |
| 446 | * been drawn to a window system framebuffer, for example). |
| 447 | */ |
| 448 | void |
| 449 | loader_dri3_flush(struct loader_dri3_drawable *draw, |
| 450 | unsigned flags, |
| 451 | enum __DRI2throttleReason throttle_reason) |
| 452 | { |
| 453 | /* NEED TO CHECK WHETHER CONTEXT IS NULL */ |
| 454 | __DRIcontext *dri_context = draw->vtable->get_dri_context(draw); |
| 455 | |
| 456 | if (dri_context) { |
| 457 | draw->ext->flush->flush_with_flags(dri_context, draw->dri_drawable, |
| 458 | flags, throttle_reason); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | loader_dri3_copy_sub_buffer(struct loader_dri3_drawable *draw, |
| 464 | int x, int y, |
| 465 | int width, int height, |
| 466 | bool flush) |
| 467 | { |
| 468 | struct loader_dri3_buffer *back; |
| 469 | unsigned flags = __DRI2_FLUSH_DRAWABLE; |
| 470 | __DRIcontext *dri_context; |
| 471 | |
| 472 | dri_context = draw->vtable->get_dri_context(draw); |
| 473 | |
| 474 | /* Check we have the right attachments */ |
| 475 | if (!draw->have_back || draw->is_pixmap) |
| 476 | return; |
| 477 | |
| 478 | if (flush) |
| 479 | flags |= __DRI2_FLUSH_CONTEXT; |
| 480 | loader_dri3_flush(draw, flags, __DRI2_THROTTLE_SWAPBUFFER); |
| 481 | |
| 482 | back = dri3_back_buffer(draw); |
| 483 | y = draw->height - y - height; |
| 484 | |
| 485 | if (draw->is_different_gpu && draw->vtable->in_current_context(draw)) { |
| 486 | /* Update the linear buffer part of the back buffer |
| 487 | * for the dri3_copy_area operation |
| 488 | */ |
| 489 | draw->ext->image->blitImage(dri_context, |
| 490 | back->linear_buffer, |
| 491 | back->image, |
| 492 | 0, 0, back->width, |
| 493 | back->height, |
| 494 | 0, 0, back->width, |
| 495 | back->height, __BLIT_FLAG_FLUSH); |
| 496 | /* We use blitImage to update our fake front, |
| 497 | */ |
| 498 | if (draw->have_fake_front) |
| 499 | draw->ext->image->blitImage(dri_context, |
| 500 | dri3_fake_front_buffer(draw)->image, |
| 501 | back->image, |
| 502 | x, y, width, height, |
| 503 | x, y, width, height, __BLIT_FLAG_FLUSH); |
| 504 | } |
| 505 | |
| 506 | dri3_fence_reset(draw->conn, back); |
| 507 | dri3_copy_area(draw->conn, |
| 508 | dri3_back_buffer(draw)->pixmap, |
| 509 | draw->drawable, |
| 510 | dri3_drawable_gc(draw), |
| 511 | x, y, x, y, width, height); |
| 512 | dri3_fence_trigger(draw->conn, back); |
| 513 | /* Refresh the fake front (if present) after we just damaged the real |
| 514 | * front. |
| 515 | */ |
| 516 | if (draw->have_fake_front && !draw->is_different_gpu) { |
| 517 | dri3_fence_reset(draw->conn, dri3_fake_front_buffer(draw)); |
| 518 | dri3_copy_area(draw->conn, |
| 519 | dri3_back_buffer(draw)->pixmap, |
| 520 | dri3_fake_front_buffer(draw)->pixmap, |
| 521 | dri3_drawable_gc(draw), |
| 522 | x, y, x, y, width, height); |
| 523 | dri3_fence_trigger(draw->conn, dri3_fake_front_buffer(draw)); |
| 524 | dri3_fence_await(draw->conn, dri3_fake_front_buffer(draw)); |
| 525 | } |
| 526 | dri3_fence_await(draw->conn, back); |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | loader_dri3_copy_drawable(struct loader_dri3_drawable *draw, |
| 531 | xcb_drawable_t dest, |
| 532 | xcb_drawable_t src) |
| 533 | { |
| 534 | loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, 0); |
| 535 | |
| 536 | dri3_fence_reset(draw->conn, dri3_fake_front_buffer(draw)); |
| 537 | dri3_copy_area(draw->conn, |
| 538 | src, dest, |
| 539 | dri3_drawable_gc(draw), |
| 540 | 0, 0, 0, 0, draw->width, draw->height); |
| 541 | dri3_fence_trigger(draw->conn, dri3_fake_front_buffer(draw)); |
| 542 | dri3_fence_await(draw->conn, dri3_fake_front_buffer(draw)); |
| 543 | } |
| 544 | |
| 545 | void |
| 546 | loader_dri3_wait_x(struct loader_dri3_drawable *draw) |
| 547 | { |
| 548 | struct loader_dri3_buffer *front; |
| 549 | __DRIcontext *dri_context; |
| 550 | |
| 551 | if (draw == NULL || !draw->have_fake_front) |
| 552 | return; |
| 553 | |
| 554 | front = dri3_fake_front_buffer(draw); |
| 555 | dri_context = draw->vtable->get_dri_context(draw); |
| 556 | |
| 557 | loader_dri3_copy_drawable(draw, front->pixmap, draw->drawable); |
| 558 | |
| 559 | /* In the psc->is_different_gpu case, the linear buffer has been updated, |
| 560 | * but not yet the tiled buffer. |
| 561 | * Copy back to the tiled buffer we use for rendering. |
| 562 | * Note that we don't need flushing. |
| 563 | */ |
| 564 | if (draw->is_different_gpu && draw->vtable->in_current_context(draw)) |
| 565 | draw->ext->image->blitImage(dri_context, |
| 566 | front->image, |
| 567 | front->linear_buffer, |
| 568 | 0, 0, front->width, |
| 569 | front->height, |
| 570 | 0, 0, front->width, |
| 571 | front->height, 0); |
| 572 | } |
| 573 | |
| 574 | void |
| 575 | loader_dri3_wait_gl(struct loader_dri3_drawable *draw) |
| 576 | { |
| 577 | struct loader_dri3_buffer *front; |
| 578 | __DRIcontext *dri_context; |
| 579 | |
| 580 | if (draw == NULL || !draw->have_fake_front) |
| 581 | return; |
| 582 | |
| 583 | front = dri3_fake_front_buffer(draw); |
| 584 | dri_context = draw->vtable->get_dri_context(draw); |
| 585 | |
| 586 | /* In the psc->is_different_gpu case, we update the linear_buffer |
| 587 | * before updating the real front. |
| 588 | */ |
| 589 | if (draw->is_different_gpu && draw->vtable->in_current_context(draw)) |
| 590 | draw->ext->image->blitImage(dri_context, |
| 591 | front->linear_buffer, |
| 592 | front->image, |
| 593 | 0, 0, front->width, |
| 594 | front->height, |
| 595 | 0, 0, front->width, |
| 596 | front->height, __BLIT_FLAG_FLUSH); |
| 597 | loader_dri3_copy_drawable(draw, draw->drawable, front->pixmap); |
| 598 | } |
| 599 | |
| 600 | /** dri3_flush_present_events |
| 601 | * |
| 602 | * Process any present events that have been received from the X server |
| 603 | */ |
| 604 | static void |
| 605 | dri3_flush_present_events(struct loader_dri3_drawable *draw) |
| 606 | { |
| 607 | /* Check to see if any configuration changes have occurred |
| 608 | * since we were last invoked |
| 609 | */ |
| 610 | if (draw->special_event) { |
| 611 | xcb_generic_event_t *ev; |
| 612 | |
| 613 | while ((ev = xcb_poll_for_special_event(draw->conn, |
| 614 | draw->special_event)) != NULL) { |
| 615 | xcb_present_generic_event_t *ge = (void *) ev; |
| 616 | dri3_handle_present_event(draw, ge); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** loader_dri3_swap_buffers_msc |
| 622 | * |
| 623 | * Make the current back buffer visible using the present extension |
| 624 | */ |
| 625 | int64_t |
| 626 | loader_dri3_swap_buffers_msc(struct loader_dri3_drawable *draw, |
| 627 | int64_t target_msc, int64_t divisor, |
| 628 | int64_t remainder, unsigned flush_flags, |
| 629 | bool force_copy) |
| 630 | { |
| 631 | struct loader_dri3_buffer *back; |
| 632 | __DRIcontext *dri_context; |
| 633 | int64_t ret = 0; |
| 634 | uint32_t options = XCB_PRESENT_OPTION_NONE; |
| 635 | int swap_interval; |
| 636 | |
| 637 | dri_context = draw->vtable->get_dri_context(draw); |
| 638 | swap_interval = draw->vtable->get_swap_interval(draw); |
| 639 | |
| 640 | draw->vtable->flush_drawable(draw, flush_flags); |
| 641 | |
| 642 | back = draw->buffers[LOADER_DRI3_BACK_ID(draw->cur_back)]; |
| 643 | if (draw->is_different_gpu && back) { |
| 644 | /* Update the linear buffer before presenting the pixmap */ |
| 645 | draw->ext->image->blitImage(dri_context, |
| 646 | back->linear_buffer, |
| 647 | back->image, |
| 648 | 0, 0, back->width, |
| 649 | back->height, |
| 650 | 0, 0, back->width, |
| 651 | back->height, __BLIT_FLAG_FLUSH); |
| 652 | /* Update the fake front */ |
| 653 | if (draw->have_fake_front) |
| 654 | draw->ext->image->blitImage(dri_context, |
| 655 | draw->buffers[LOADER_DRI3_FRONT_ID]->image, |
| 656 | back->image, |
| 657 | 0, 0, draw->width, draw->height, |
| 658 | 0, 0, draw->width, draw->height, |
| 659 | __BLIT_FLAG_FLUSH); |
| 660 | } |
| 661 | |
| 662 | dri3_flush_present_events(draw); |
| 663 | |
| 664 | if (back && !draw->is_pixmap) { |
| 665 | dri3_fence_reset(draw->conn, back); |
| 666 | |
| 667 | /* Compute when we want the frame shown by taking the last known |
| 668 | * successful MSC and adding in a swap interval for each outstanding swap |
| 669 | * request. target_msc=divisor=remainder=0 means "Use glXSwapBuffers() |
| 670 | * semantic" |
| 671 | */ |
| 672 | ++draw->send_sbc; |
| 673 | if (target_msc == 0 && divisor == 0 && remainder == 0) |
| 674 | target_msc = draw->msc + swap_interval * |
| 675 | (draw->send_sbc - draw->recv_sbc); |
| 676 | else if (divisor == 0 && remainder > 0) { |
| 677 | /* From the GLX_OML_sync_control spec: |
| 678 | * "If <divisor> = 0, the swap will occur when MSC becomes |
| 679 | * greater than or equal to <target_msc>." |
| 680 | * |
| 681 | * Note that there's no mention of the remainder. The Present |
| 682 | * extension throws BadValue for remainder != 0 with divisor == 0, so |
| 683 | * just drop the passed in value. |
| 684 | */ |
| 685 | remainder = 0; |
| 686 | } |
| 687 | |
| 688 | /* From the GLX_EXT_swap_control spec |
| 689 | * and the EGL 1.4 spec (page 53): |
| 690 | * |
| 691 | * "If <interval> is set to a value of 0, buffer swaps are not |
| 692 | * synchronized to a video frame." |
| 693 | * |
| 694 | * Implementation note: It is possible to enable triple buffering |
| 695 | * behaviour by not using XCB_PRESENT_OPTION_ASYNC, but this should not be |
| 696 | * the default. |
| 697 | */ |
| 698 | if (swap_interval == 0) |
| 699 | options |= XCB_PRESENT_OPTION_ASYNC; |
| 700 | if (force_copy) |
| 701 | options |= XCB_PRESENT_OPTION_COPY; |
| 702 | |
| 703 | back->busy = 1; |
| 704 | back->last_swap = draw->send_sbc; |
| 705 | xcb_present_pixmap(draw->conn, |
| 706 | draw->drawable, |
| 707 | back->pixmap, |
| 708 | (uint32_t) draw->send_sbc, |
| 709 | 0, /* valid */ |
| 710 | 0, /* update */ |
| 711 | 0, /* x_off */ |
| 712 | 0, /* y_off */ |
| 713 | None, /* target_crtc */ |
| 714 | None, |
| 715 | back->sync_fence, |
| 716 | options, |
| 717 | target_msc, |
| 718 | divisor, |
| 719 | remainder, 0, NULL); |
| 720 | ret = (int64_t) draw->send_sbc; |
| 721 | |
| 722 | /* If there's a fake front, then copy the source back buffer |
| 723 | * to the fake front to keep it up to date. This needs |
| 724 | * to reset the fence and make future users block until |
| 725 | * the X server is done copying the bits |
| 726 | */ |
| 727 | if (draw->have_fake_front && !draw->is_different_gpu) { |
| 728 | dri3_fence_reset(draw->conn, draw->buffers[LOADER_DRI3_FRONT_ID]); |
| 729 | dri3_copy_area(draw->conn, |
| 730 | back->pixmap, |
| 731 | draw->buffers[LOADER_DRI3_FRONT_ID]->pixmap, |
| 732 | dri3_drawable_gc(draw), |
| 733 | 0, 0, 0, 0, |
| 734 | draw->width, draw->height); |
| 735 | dri3_fence_trigger(draw->conn, draw->buffers[LOADER_DRI3_FRONT_ID]); |
| 736 | } |
| 737 | xcb_flush(draw->conn); |
| 738 | if (draw->stamp) |
| 739 | ++(*draw->stamp); |
| 740 | } |
| 741 | |
| 742 | (draw->ext->flush->invalidate)(draw->dri_drawable); |
| 743 | |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | int |
| 748 | loader_dri3_query_buffer_age(struct loader_dri3_drawable *draw) |
| 749 | { |
| 750 | int back_id = LOADER_DRI3_BACK_ID(dri3_find_back(draw)); |
| 751 | |
| 752 | if (back_id < 0 || !draw->buffers[back_id]) |
| 753 | return 0; |
| 754 | |
| 755 | if (draw->buffers[back_id]->last_swap != 0) |
| 756 | return draw->send_sbc - draw->buffers[back_id]->last_swap + 1; |
| 757 | else |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /** loader_dri3_open |
| 762 | * |
| 763 | * Wrapper around xcb_dri3_open |
| 764 | */ |
| 765 | int |
| 766 | loader_dri3_open(xcb_connection_t *conn, |
| 767 | xcb_window_t root, |
| 768 | uint32_t provider) |
| 769 | { |
| 770 | xcb_dri3_open_cookie_t cookie; |
| 771 | xcb_dri3_open_reply_t *reply; |
| 772 | int fd; |
| 773 | |
| 774 | cookie = xcb_dri3_open(conn, |
| 775 | root, |
| 776 | provider); |
| 777 | |
| 778 | reply = xcb_dri3_open_reply(conn, cookie, NULL); |
| 779 | if (!reply) |
| 780 | return -1; |
| 781 | |
| 782 | if (reply->nfd != 1) { |
| 783 | free(reply); |
| 784 | return -1; |
| 785 | } |
| 786 | |
| 787 | fd = xcb_dri3_open_reply_fds(conn, reply)[0]; |
| 788 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
| 789 | |
| 790 | return fd; |
| 791 | } |
| 792 | |
| 793 | static uint32_t |
| 794 | dri3_cpp_for_format(uint32_t format) { |
| 795 | switch (format) { |
| 796 | case __DRI_IMAGE_FORMAT_R8: |
| 797 | return 1; |
| 798 | case __DRI_IMAGE_FORMAT_RGB565: |
| 799 | case __DRI_IMAGE_FORMAT_GR88: |
| 800 | return 2; |
| 801 | case __DRI_IMAGE_FORMAT_XRGB8888: |
| 802 | case __DRI_IMAGE_FORMAT_ARGB8888: |
| 803 | case __DRI_IMAGE_FORMAT_ABGR8888: |
| 804 | case __DRI_IMAGE_FORMAT_XBGR8888: |
| 805 | case __DRI_IMAGE_FORMAT_XRGB2101010: |
| 806 | case __DRI_IMAGE_FORMAT_ARGB2101010: |
| 807 | case __DRI_IMAGE_FORMAT_SARGB8: |
| 808 | return 4; |
| 809 | case __DRI_IMAGE_FORMAT_NONE: |
| 810 | default: |
| 811 | return 0; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /** loader_dri3_alloc_render_buffer |
| 816 | * |
| 817 | * Use the driver createImage function to construct a __DRIimage, then |
| 818 | * get a file descriptor for that and create an X pixmap from that |
| 819 | * |
| 820 | * Allocate an xshmfence for synchronization |
| 821 | */ |
| 822 | static struct loader_dri3_buffer * |
| 823 | dri3_alloc_render_buffer(struct loader_dri3_drawable *draw, unsigned int format, |
| 824 | int width, int height, int depth) |
| 825 | { |
| 826 | struct loader_dri3_buffer *buffer; |
| 827 | __DRIimage *pixmap_buffer; |
| 828 | xcb_pixmap_t pixmap; |
| 829 | xcb_sync_fence_t sync_fence; |
| 830 | struct xshmfence *shm_fence; |
| 831 | int buffer_fd, fence_fd; |
| 832 | int stride; |
| 833 | |
| 834 | /* Create an xshmfence object and |
| 835 | * prepare to send that to the X server |
| 836 | */ |
| 837 | |
| 838 | fence_fd = xshmfence_alloc_shm(); |
| 839 | if (fence_fd < 0) |
| 840 | return NULL; |
| 841 | |
| 842 | shm_fence = xshmfence_map_shm(fence_fd); |
| 843 | if (shm_fence == NULL) |
| 844 | goto no_shm_fence; |
| 845 | |
| 846 | /* Allocate the image from the driver |
| 847 | */ |
| 848 | buffer = calloc(1, sizeof *buffer); |
| 849 | if (!buffer) |
| 850 | goto no_buffer; |
| 851 | |
| 852 | buffer->cpp = dri3_cpp_for_format(format); |
| 853 | if (!buffer->cpp) |
| 854 | goto no_image; |
| 855 | |
| 856 | if (!draw->is_different_gpu) { |
| 857 | buffer->image = (draw->ext->image->createImage)(draw->dri_screen, |
| 858 | width, height, |
| 859 | format, |
| 860 | __DRI_IMAGE_USE_SHARE | |
Axel Davy | d943ac4 | 2015-10-21 12:28:00 +0200 | [diff] [blame^] | 861 | __DRI_IMAGE_USE_SCANOUT | |
| 862 | __DRI_IMAGE_USE_BACKBUFFER, |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 863 | buffer); |
| 864 | pixmap_buffer = buffer->image; |
| 865 | |
| 866 | if (!buffer->image) |
| 867 | goto no_image; |
| 868 | } else { |
| 869 | buffer->image = (draw->ext->image->createImage)(draw->dri_screen, |
| 870 | width, height, |
| 871 | format, |
| 872 | 0, |
| 873 | buffer); |
| 874 | |
| 875 | if (!buffer->image) |
| 876 | goto no_image; |
| 877 | |
| 878 | buffer->linear_buffer = |
| 879 | (draw->ext->image->createImage)(draw->dri_screen, |
| 880 | width, height, format, |
| 881 | __DRI_IMAGE_USE_SHARE | |
Axel Davy | d943ac4 | 2015-10-21 12:28:00 +0200 | [diff] [blame^] | 882 | __DRI_IMAGE_USE_LINEAR | |
| 883 | __DRI_IMAGE_USE_BACKBUFFER, |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 884 | buffer); |
| 885 | pixmap_buffer = buffer->linear_buffer; |
| 886 | |
| 887 | if (!buffer->linear_buffer) |
| 888 | goto no_linear_buffer; |
| 889 | } |
| 890 | |
| 891 | /* X wants the stride, so ask the image for it |
| 892 | */ |
| 893 | if (!(draw->ext->image->queryImage)(pixmap_buffer, __DRI_IMAGE_ATTRIB_STRIDE, |
| 894 | &stride)) |
| 895 | goto no_buffer_attrib; |
| 896 | |
| 897 | buffer->pitch = stride; |
| 898 | |
| 899 | if (!(draw->ext->image->queryImage)(pixmap_buffer, __DRI_IMAGE_ATTRIB_FD, |
| 900 | &buffer_fd)) |
| 901 | goto no_buffer_attrib; |
| 902 | |
| 903 | xcb_dri3_pixmap_from_buffer(draw->conn, |
| 904 | (pixmap = xcb_generate_id(draw->conn)), |
| 905 | draw->drawable, |
| 906 | buffer->size, |
| 907 | width, height, buffer->pitch, |
| 908 | depth, buffer->cpp * 8, |
| 909 | buffer_fd); |
| 910 | |
| 911 | xcb_dri3_fence_from_fd(draw->conn, |
| 912 | pixmap, |
| 913 | (sync_fence = xcb_generate_id(draw->conn)), |
| 914 | false, |
| 915 | fence_fd); |
| 916 | |
| 917 | buffer->pixmap = pixmap; |
| 918 | buffer->own_pixmap = true; |
| 919 | buffer->sync_fence = sync_fence; |
| 920 | buffer->shm_fence = shm_fence; |
| 921 | buffer->width = width; |
| 922 | buffer->height = height; |
| 923 | |
| 924 | /* Mark the buffer as idle |
| 925 | */ |
| 926 | dri3_fence_set(buffer); |
| 927 | |
| 928 | return buffer; |
| 929 | |
| 930 | no_buffer_attrib: |
| 931 | (draw->ext->image->destroyImage)(pixmap_buffer); |
| 932 | no_linear_buffer: |
| 933 | if (draw->is_different_gpu) |
| 934 | (draw->ext->image->destroyImage)(buffer->image); |
| 935 | no_image: |
| 936 | free(buffer); |
| 937 | no_buffer: |
| 938 | xshmfence_unmap_shm(shm_fence); |
| 939 | no_shm_fence: |
| 940 | close(fence_fd); |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | /** loader_dri3_update_drawable |
| 945 | * |
| 946 | * Called the first time we use the drawable and then |
| 947 | * after we receive present configure notify events to |
| 948 | * track the geometry of the drawable |
| 949 | */ |
| 950 | static int |
| 951 | dri3_update_drawable(__DRIdrawable *driDrawable, |
| 952 | struct loader_dri3_drawable *draw) |
| 953 | { |
| 954 | if (draw->first_init) { |
| 955 | xcb_get_geometry_cookie_t geom_cookie; |
| 956 | xcb_get_geometry_reply_t *geom_reply; |
| 957 | xcb_void_cookie_t cookie; |
| 958 | xcb_generic_error_t *error; |
| 959 | xcb_present_query_capabilities_cookie_t present_capabilities_cookie; |
| 960 | xcb_present_query_capabilities_reply_t *present_capabilities_reply; |
| 961 | |
| 962 | draw->first_init = false; |
| 963 | |
| 964 | /* Try to select for input on the window. |
| 965 | * |
| 966 | * If the drawable is a window, this will get our events |
| 967 | * delivered. |
| 968 | * |
| 969 | * Otherwise, we'll get a BadWindow error back from this request which |
| 970 | * will let us know that the drawable is a pixmap instead. |
| 971 | */ |
| 972 | |
| 973 | draw->eid = xcb_generate_id(draw->conn); |
| 974 | cookie = |
| 975 | xcb_present_select_input_checked(draw->conn, draw->eid, draw->drawable, |
| 976 | XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY | |
| 977 | XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY | |
| 978 | XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY); |
| 979 | |
| 980 | present_capabilities_cookie = |
| 981 | xcb_present_query_capabilities(draw->conn, draw->drawable); |
| 982 | |
| 983 | /* Create an XCB event queue to hold present events outside of the usual |
| 984 | * application event queue |
| 985 | */ |
| 986 | draw->special_event = xcb_register_for_special_xge(draw->conn, |
| 987 | &xcb_present_id, |
| 988 | draw->eid, |
| 989 | draw->stamp); |
| 990 | geom_cookie = xcb_get_geometry(draw->conn, draw->drawable); |
| 991 | |
| 992 | geom_reply = xcb_get_geometry_reply(draw->conn, geom_cookie, NULL); |
| 993 | |
| 994 | if (!geom_reply) |
| 995 | return false; |
| 996 | |
| 997 | draw->width = geom_reply->width; |
| 998 | draw->height = geom_reply->height; |
| 999 | draw->depth = geom_reply->depth; |
| 1000 | draw->vtable->set_drawable_size(draw, draw->width, draw->height); |
| 1001 | |
| 1002 | free(geom_reply); |
| 1003 | |
| 1004 | draw->is_pixmap = false; |
| 1005 | |
| 1006 | /* Check to see if our select input call failed. If it failed with a |
| 1007 | * BadWindow error, then assume the drawable is a pixmap. Destroy the |
| 1008 | * special event queue created above and mark the drawable as a pixmap |
| 1009 | */ |
| 1010 | |
| 1011 | error = xcb_request_check(draw->conn, cookie); |
| 1012 | |
| 1013 | present_capabilities_reply = |
| 1014 | xcb_present_query_capabilities_reply(draw->conn, |
| 1015 | present_capabilities_cookie, |
| 1016 | NULL); |
| 1017 | |
| 1018 | if (present_capabilities_reply) { |
| 1019 | draw->present_capabilities = present_capabilities_reply->capabilities; |
| 1020 | free(present_capabilities_reply); |
| 1021 | } else |
| 1022 | draw->present_capabilities = 0; |
| 1023 | |
| 1024 | if (error) { |
| 1025 | if (error->error_code != BadWindow) { |
| 1026 | free(error); |
| 1027 | return false; |
| 1028 | } |
| 1029 | draw->is_pixmap = true; |
| 1030 | xcb_unregister_for_special_event(draw->conn, draw->special_event); |
| 1031 | draw->special_event = NULL; |
| 1032 | } |
| 1033 | } |
| 1034 | dri3_flush_present_events(draw); |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while |
| 1039 | * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid |
| 1040 | * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and |
| 1041 | * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds |
| 1042 | */ |
| 1043 | static int |
| 1044 | image_format_to_fourcc(int format) |
| 1045 | { |
| 1046 | |
| 1047 | /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */ |
| 1048 | switch (format) { |
| 1049 | case __DRI_IMAGE_FORMAT_SARGB8: return __DRI_IMAGE_FOURCC_SARGB8888; |
| 1050 | case __DRI_IMAGE_FORMAT_RGB565: return __DRI_IMAGE_FOURCC_RGB565; |
| 1051 | case __DRI_IMAGE_FORMAT_XRGB8888: return __DRI_IMAGE_FOURCC_XRGB8888; |
| 1052 | case __DRI_IMAGE_FORMAT_ARGB8888: return __DRI_IMAGE_FOURCC_ARGB8888; |
| 1053 | case __DRI_IMAGE_FORMAT_ABGR8888: return __DRI_IMAGE_FOURCC_ABGR8888; |
| 1054 | case __DRI_IMAGE_FORMAT_XBGR8888: return __DRI_IMAGE_FOURCC_XBGR8888; |
| 1055 | } |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Boyan Ding | bd6131a | 2015-07-21 23:44:01 +0800 | [diff] [blame] | 1059 | __DRIimage * |
| 1060 | loader_dri3_create_image(xcb_connection_t *c, |
| 1061 | xcb_dri3_buffer_from_pixmap_reply_t *bp_reply, |
| 1062 | unsigned int format, |
| 1063 | __DRIscreen *dri_screen, |
| 1064 | const __DRIimageExtension *image, |
| 1065 | void *loaderPrivate) |
| 1066 | { |
| 1067 | int *fds; |
| 1068 | __DRIimage *image_planar, *ret; |
| 1069 | int stride, offset; |
| 1070 | |
| 1071 | /* Get an FD for the pixmap object |
| 1072 | */ |
| 1073 | fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply); |
| 1074 | |
| 1075 | stride = bp_reply->stride; |
| 1076 | offset = 0; |
| 1077 | |
| 1078 | /* createImageFromFds creates a wrapper __DRIimage structure which |
| 1079 | * can deal with multiple planes for things like Yuv images. So, once |
| 1080 | * we've gotten the planar wrapper, pull the single plane out of it and |
| 1081 | * discard the wrapper. |
| 1082 | */ |
| 1083 | image_planar = (image->createImageFromFds)(dri_screen, |
| 1084 | bp_reply->width, |
| 1085 | bp_reply->height, |
| 1086 | image_format_to_fourcc(format), |
| 1087 | fds, 1, |
| 1088 | &stride, &offset, loaderPrivate); |
| 1089 | close(fds[0]); |
| 1090 | if (!image_planar) |
| 1091 | return NULL; |
| 1092 | |
| 1093 | ret = (image->fromPlanar)(image_planar, 0, loaderPrivate); |
| 1094 | |
| 1095 | (image->destroyImage)(image_planar); |
| 1096 | |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1100 | /** dri3_get_pixmap_buffer |
| 1101 | * |
| 1102 | * Get the DRM object for a pixmap from the X server and |
| 1103 | * wrap that with a __DRIimage structure using createImageFromFds |
| 1104 | */ |
| 1105 | static struct loader_dri3_buffer * |
| 1106 | dri3_get_pixmap_buffer(__DRIdrawable *driDrawable, unsigned int format, |
| 1107 | enum loader_dri3_buffer_type buffer_type, |
| 1108 | struct loader_dri3_drawable *draw) |
| 1109 | { |
| 1110 | int buf_id = loader_dri3_pixmap_buf_id(buffer_type); |
| 1111 | struct loader_dri3_buffer *buffer = draw->buffers[buf_id]; |
| 1112 | xcb_drawable_t pixmap; |
| 1113 | xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie; |
| 1114 | xcb_dri3_buffer_from_pixmap_reply_t *bp_reply; |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1115 | xcb_sync_fence_t sync_fence; |
| 1116 | struct xshmfence *shm_fence; |
| 1117 | int fence_fd; |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1118 | |
| 1119 | if (buffer) |
| 1120 | return buffer; |
| 1121 | |
| 1122 | pixmap = draw->drawable; |
| 1123 | |
| 1124 | buffer = calloc(1, sizeof *buffer); |
| 1125 | if (!buffer) |
| 1126 | goto no_buffer; |
| 1127 | |
| 1128 | fence_fd = xshmfence_alloc_shm(); |
| 1129 | if (fence_fd < 0) |
| 1130 | goto no_fence; |
| 1131 | shm_fence = xshmfence_map_shm(fence_fd); |
| 1132 | if (shm_fence == NULL) { |
| 1133 | close (fence_fd); |
| 1134 | goto no_fence; |
| 1135 | } |
| 1136 | |
| 1137 | xcb_dri3_fence_from_fd(draw->conn, |
| 1138 | pixmap, |
| 1139 | (sync_fence = xcb_generate_id(draw->conn)), |
| 1140 | false, |
| 1141 | fence_fd); |
| 1142 | |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1143 | bp_cookie = xcb_dri3_buffer_from_pixmap(draw->conn, pixmap); |
Boyan Ding | bd6131a | 2015-07-21 23:44:01 +0800 | [diff] [blame] | 1144 | bp_reply = xcb_dri3_buffer_from_pixmap_reply(draw->conn, bp_cookie, NULL); |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1145 | if (!bp_reply) |
| 1146 | goto no_image; |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1147 | |
Boyan Ding | bd6131a | 2015-07-21 23:44:01 +0800 | [diff] [blame] | 1148 | buffer->image = loader_dri3_create_image(draw->conn, bp_reply, format, |
| 1149 | draw->dri_screen, draw->ext->image, |
| 1150 | buffer); |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1151 | if (!buffer->image) |
| 1152 | goto no_image; |
| 1153 | |
| 1154 | buffer->pixmap = pixmap; |
| 1155 | buffer->own_pixmap = false; |
| 1156 | buffer->width = bp_reply->width; |
| 1157 | buffer->height = bp_reply->height; |
| 1158 | buffer->buffer_type = buffer_type; |
| 1159 | buffer->shm_fence = shm_fence; |
| 1160 | buffer->sync_fence = sync_fence; |
| 1161 | |
| 1162 | draw->buffers[buf_id] = buffer; |
Boyan Ding | bd6131a | 2015-07-21 23:44:01 +0800 | [diff] [blame] | 1163 | |
| 1164 | free(bp_reply); |
| 1165 | |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1166 | return buffer; |
| 1167 | |
| 1168 | no_image: |
Boyan Ding | bd6131a | 2015-07-21 23:44:01 +0800 | [diff] [blame] | 1169 | free(bp_reply); |
Boyan Ding | 6bd9ba7 | 2015-07-21 23:43:54 +0800 | [diff] [blame] | 1170 | xcb_sync_destroy_fence(draw->conn, sync_fence); |
| 1171 | xshmfence_unmap_shm(shm_fence); |
| 1172 | no_fence: |
| 1173 | free(buffer); |
| 1174 | no_buffer: |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | |
| 1178 | /** dri3_get_buffer |
| 1179 | * |
| 1180 | * Find a front or back buffer, allocating new ones as necessary |
| 1181 | */ |
| 1182 | static struct loader_dri3_buffer * |
| 1183 | dri3_get_buffer(__DRIdrawable *driDrawable, |
| 1184 | unsigned int format, |
| 1185 | enum loader_dri3_buffer_type buffer_type, |
| 1186 | struct loader_dri3_drawable *draw) |
| 1187 | { |
| 1188 | struct loader_dri3_buffer *buffer; |
| 1189 | int buf_id; |
| 1190 | __DRIcontext *dri_context; |
| 1191 | |
| 1192 | dri_context = draw->vtable->get_dri_context(draw); |
| 1193 | |
| 1194 | if (buffer_type == loader_dri3_buffer_back) { |
| 1195 | buf_id = dri3_find_back(draw); |
| 1196 | |
| 1197 | if (buf_id < 0) |
| 1198 | return NULL; |
| 1199 | } else { |
| 1200 | buf_id = LOADER_DRI3_FRONT_ID; |
| 1201 | } |
| 1202 | |
| 1203 | buffer = draw->buffers[buf_id]; |
| 1204 | |
| 1205 | /* Allocate a new buffer if there isn't an old one, or if that |
| 1206 | * old one is the wrong size |
| 1207 | */ |
| 1208 | if (!buffer || buffer->width != draw->width || |
| 1209 | buffer->height != draw->height) { |
| 1210 | struct loader_dri3_buffer *new_buffer; |
| 1211 | |
| 1212 | /* Allocate the new buffers |
| 1213 | */ |
| 1214 | new_buffer = dri3_alloc_render_buffer(draw, |
| 1215 | format, |
| 1216 | draw->width, |
| 1217 | draw->height, |
| 1218 | draw->depth); |
| 1219 | if (!new_buffer) |
| 1220 | return NULL; |
| 1221 | |
| 1222 | /* When resizing, copy the contents of the old buffer, waiting for that |
| 1223 | * copy to complete using our fences before proceeding |
| 1224 | */ |
| 1225 | switch (buffer_type) { |
| 1226 | case loader_dri3_buffer_back: |
| 1227 | if (buffer) { |
| 1228 | if (!buffer->linear_buffer) { |
| 1229 | dri3_fence_reset(draw->conn, new_buffer); |
| 1230 | dri3_fence_await(draw->conn, buffer); |
| 1231 | dri3_copy_area(draw->conn, |
| 1232 | buffer->pixmap, |
| 1233 | new_buffer->pixmap, |
| 1234 | dri3_drawable_gc(draw), |
| 1235 | 0, 0, 0, 0, |
| 1236 | draw->width, draw->height); |
| 1237 | dri3_fence_trigger(draw->conn, new_buffer); |
| 1238 | } else if (draw->vtable->in_current_context(draw)) { |
| 1239 | draw->ext->image->blitImage(dri_context, |
| 1240 | new_buffer->image, |
| 1241 | buffer->image, |
| 1242 | 0, 0, draw->width, draw->height, |
| 1243 | 0, 0, draw->width, draw->height, 0); |
| 1244 | } |
| 1245 | dri3_free_render_buffer(draw, buffer); |
| 1246 | } |
| 1247 | break; |
| 1248 | case loader_dri3_buffer_front: |
| 1249 | dri3_fence_reset(draw->conn, new_buffer); |
| 1250 | dri3_copy_area(draw->conn, |
| 1251 | draw->drawable, |
| 1252 | new_buffer->pixmap, |
| 1253 | dri3_drawable_gc(draw), |
| 1254 | 0, 0, 0, 0, |
| 1255 | draw->width, draw->height); |
| 1256 | dri3_fence_trigger(draw->conn, new_buffer); |
| 1257 | |
| 1258 | if (new_buffer->linear_buffer && |
| 1259 | draw->vtable->in_current_context(draw)) { |
| 1260 | dri3_fence_await(draw->conn, new_buffer); |
| 1261 | draw->ext->image->blitImage(dri_context, |
| 1262 | new_buffer->image, |
| 1263 | new_buffer->linear_buffer, |
| 1264 | 0, 0, draw->width, draw->height, |
| 1265 | 0, 0, draw->width, draw->height, 0); |
| 1266 | } |
| 1267 | break; |
| 1268 | } |
| 1269 | buffer = new_buffer; |
| 1270 | buffer->buffer_type = buffer_type; |
| 1271 | draw->buffers[buf_id] = buffer; |
| 1272 | } |
| 1273 | dri3_fence_await(draw->conn, buffer); |
| 1274 | |
| 1275 | /* Return the requested buffer */ |
| 1276 | return buffer; |
| 1277 | } |
| 1278 | |
| 1279 | /** dri3_free_buffers |
| 1280 | * |
| 1281 | * Free the front bufffer or all of the back buffers. Used |
| 1282 | * when the application changes which buffers it needs |
| 1283 | */ |
| 1284 | static void |
| 1285 | dri3_free_buffers(__DRIdrawable *driDrawable, |
| 1286 | enum loader_dri3_buffer_type buffer_type, |
| 1287 | struct loader_dri3_drawable *draw) |
| 1288 | { |
| 1289 | struct loader_dri3_buffer *buffer; |
| 1290 | int first_id; |
| 1291 | int n_id; |
| 1292 | int buf_id; |
| 1293 | |
| 1294 | switch (buffer_type) { |
| 1295 | case loader_dri3_buffer_back: |
| 1296 | first_id = LOADER_DRI3_BACK_ID(0); |
| 1297 | n_id = LOADER_DRI3_MAX_BACK; |
| 1298 | break; |
| 1299 | case loader_dri3_buffer_front: |
| 1300 | first_id = LOADER_DRI3_FRONT_ID; |
| 1301 | n_id = 1; |
| 1302 | } |
| 1303 | |
| 1304 | for (buf_id = first_id; buf_id < first_id + n_id; buf_id++) { |
| 1305 | buffer = draw->buffers[buf_id]; |
| 1306 | if (buffer) { |
| 1307 | dri3_free_render_buffer(draw, buffer); |
| 1308 | draw->buffers[buf_id] = NULL; |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /** loader_dri3_get_buffers |
| 1314 | * |
| 1315 | * The published buffer allocation API. |
| 1316 | * Returns all of the necessary buffers, allocating |
| 1317 | * as needed. |
| 1318 | */ |
| 1319 | int |
| 1320 | loader_dri3_get_buffers(__DRIdrawable *driDrawable, |
| 1321 | unsigned int format, |
| 1322 | uint32_t *stamp, |
| 1323 | void *loaderPrivate, |
| 1324 | uint32_t buffer_mask, |
| 1325 | struct __DRIimageList *buffers) |
| 1326 | { |
| 1327 | struct loader_dri3_drawable *draw = loaderPrivate; |
| 1328 | struct loader_dri3_buffer *front, *back; |
| 1329 | |
| 1330 | buffers->image_mask = 0; |
| 1331 | buffers->front = NULL; |
| 1332 | buffers->back = NULL; |
| 1333 | |
| 1334 | front = NULL; |
| 1335 | back = NULL; |
| 1336 | |
| 1337 | if (!dri3_update_drawable(driDrawable, draw)) |
| 1338 | return false; |
| 1339 | |
| 1340 | /* pixmaps always have front buffers */ |
| 1341 | if (draw->is_pixmap) |
| 1342 | buffer_mask |= __DRI_IMAGE_BUFFER_FRONT; |
| 1343 | |
| 1344 | if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) { |
| 1345 | /* All pixmaps are owned by the server gpu. |
| 1346 | * When we use a different gpu, we can't use the pixmap |
| 1347 | * as buffer since it is potentially tiled a way |
| 1348 | * our device can't understand. In this case, use |
| 1349 | * a fake front buffer. Hopefully the pixmap |
| 1350 | * content will get synced with the fake front |
| 1351 | * buffer. |
| 1352 | */ |
| 1353 | if (draw->is_pixmap && !draw->is_different_gpu) |
| 1354 | front = dri3_get_pixmap_buffer(driDrawable, |
| 1355 | format, |
| 1356 | loader_dri3_buffer_front, |
| 1357 | draw); |
| 1358 | else |
| 1359 | front = dri3_get_buffer(driDrawable, |
| 1360 | format, |
| 1361 | loader_dri3_buffer_front, |
| 1362 | draw); |
| 1363 | |
| 1364 | if (!front) |
| 1365 | return false; |
| 1366 | } else { |
| 1367 | dri3_free_buffers(driDrawable, loader_dri3_buffer_front, draw); |
| 1368 | draw->have_fake_front = 0; |
| 1369 | } |
| 1370 | |
| 1371 | if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) { |
| 1372 | back = dri3_get_buffer(driDrawable, |
| 1373 | format, |
| 1374 | loader_dri3_buffer_back, |
| 1375 | draw); |
| 1376 | if (!back) |
| 1377 | return false; |
| 1378 | draw->have_back = 1; |
| 1379 | } else { |
| 1380 | dri3_free_buffers(driDrawable, loader_dri3_buffer_back, draw); |
| 1381 | draw->have_back = 0; |
| 1382 | } |
| 1383 | |
| 1384 | if (front) { |
| 1385 | buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT; |
| 1386 | buffers->front = front->image; |
| 1387 | draw->have_fake_front = draw->is_different_gpu || !draw->is_pixmap; |
| 1388 | } |
| 1389 | |
| 1390 | if (back) { |
| 1391 | buffers->image_mask |= __DRI_IMAGE_BUFFER_BACK; |
| 1392 | buffers->back = back->image; |
| 1393 | } |
| 1394 | |
| 1395 | draw->stamp = stamp; |
| 1396 | |
| 1397 | return true; |
| 1398 | } |