vmwgfx_saa.c revision 22f7e8e5
1/* 2 * Copyright 2011 VMWare, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Author: Thomas Hellstrom <thellstrom@vmware.com> 26 */ 27 28#include <xorg-server.h> 29#include <xorgVersion.h> 30#include <mi.h> 31#include <fb.h> 32#include <xf86drmMode.h> 33#include <xa_context.h> 34#include "vmwgfx_saa.h" 35#include "vmwgfx_drmi.h" 36#include "vmwgfx_saa_priv.h" 37 38/* 39 * Damage to be added as soon as we attach storage to the pixmap. 40 */ 41static Bool 42vmwgfx_pixmap_add_damage(PixmapPtr pixmap) 43{ 44 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 45 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 46 DrawablePtr draw = &pixmap->drawable; 47 BoxRec box; 48 49 if (spix->damage) 50 return TRUE; 51 52 if (!saa_add_damage(pixmap)) 53 return FALSE; 54 55 box.x1 = 0; 56 box.x2 = draw->width; 57 box.y1 = 0; 58 box.y2 = draw->height; 59 60 if (vpix->hw) { 61 REGION_RESET(draw->pScreen, &spix->dirty_hw, &box); 62 REGION_EMPTY(draw->pScreen, &spix->dirty_shadow); 63 } else { 64 REGION_RESET(draw->pScreen, &spix->dirty_shadow, &box); 65 REGION_EMPTY(draw->pScreen, &spix->dirty_hw); 66 } 67 68 return TRUE; 69} 70 71static void 72vmwgfx_pixmap_remove_damage(PixmapPtr pixmap) 73{ 74 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 75 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 76 77 if (!spix->damage || vpix->hw || vpix->gmr || vpix->malloc) 78 return; 79 80#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,14,99,2,0) 81 DamageUnregister(spix->damage); 82#else 83 DamageUnregister(&pixmap->drawable, spix->damage); 84#endif 85 86 DamageDestroy(spix->damage); 87 spix->damage = NULL; 88} 89 90static void 91vmwgfx_pixmap_remove_present(struct vmwgfx_saa_pixmap *vpix) 92{ 93 if (vpix->dirty_present) 94 REGION_DESTROY(pixmap->drawable.pScreen, vpix->dirty_present); 95 if (vpix->present_damage) 96 REGION_DESTROY(pixmap->drawable.pScreen, vpix->present_damage); 97 if (vpix->pending_update) 98 REGION_DESTROY(pixmap->drawable.pScreen, vpix->pending_update); 99 if (vpix->pending_present) 100 REGION_DESTROY(pixmap->drawable.pScreen, vpix->pending_present); 101 vpix->dirty_present = NULL; 102 vpix->present_damage = NULL; 103 vpix->pending_update = NULL; 104 vpix->pending_present = NULL; 105} 106 107static Bool 108vmwgfx_pixmap_add_present(PixmapPtr pixmap, Bool present_opt) 109{ 110 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 111 ScreenPtr pScreen = pixmap->drawable.pScreen; 112 (void) pScreen; 113 114 if (present_opt) { 115 vpix->dirty_present = REGION_CREATE(pScreen, NULL, 0); 116 if (!vpix->dirty_present) 117 return FALSE; 118 vpix->present_damage = REGION_CREATE(pScreen, NULL, 0); 119 if (!vpix->present_damage) 120 goto out_no_present_damage; 121 } 122 vpix->pending_update = REGION_CREATE(pScreen, NULL, 0); 123 if (!vpix->pending_update) 124 goto out_no_pending_update; 125 vpix->pending_present = REGION_CREATE(pScreen, NULL, 0); 126 if (!vpix->pending_present) 127 goto out_no_pending_present; 128 129 return TRUE; 130 out_no_pending_present: 131 REGION_DESTROY(pScreen, vpix->pending_update); 132 out_no_pending_update: 133 if (vpix->present_damage) 134 REGION_DESTROY(pScreen, vpix->present_damage); 135 out_no_present_damage: 136 if (vpix->dirty_present) 137 REGION_DESTROY(pScreen, vpix->dirty_present); 138 return FALSE; 139} 140 141static void 142vmwgfx_pixmap_free_storage(struct vmwgfx_saa_pixmap *vpix) 143{ 144 if (!(vpix->backing & VMWGFX_PIX_MALLOC) && vpix->malloc) { 145 free(vpix->malloc); 146 vpix->malloc = NULL; 147 } 148 if (!(vpix->backing & VMWGFX_PIX_SURFACE) && vpix->hw) { 149 xa_surface_destroy(vpix->hw); 150 vpix->hw = NULL; 151 } 152 if (!(vpix->backing & VMWGFX_PIX_GMR) && vpix->gmr) { 153 vmwgfx_dmabuf_destroy(vpix->gmr); 154 vpix->gmr = NULL; 155 } 156} 157 158static Bool 159vmwgfx_pixmap_create_gmr(struct vmwgfx_saa *vsaa, PixmapPtr pixmap) 160{ 161 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 162 size_t size; 163 struct vmwgfx_dmabuf *gmr; 164 void *addr; 165 166 if (vpix->gmr) 167 return TRUE; 168 169 size = pixmap->devKind * pixmap->drawable.height; 170 gmr = vmwgfx_dmabuf_alloc(vsaa->drm_fd, size); 171 if (!gmr) 172 return FALSE; 173 174 if (vpix->malloc) { 175 176 addr = vmwgfx_dmabuf_map(gmr); 177 if (!addr) 178 goto out_no_transfer; 179 memcpy(addr, vpix->malloc, size); 180 vmwgfx_dmabuf_unmap(gmr); 181 182 } else if (!vmwgfx_pixmap_add_damage(pixmap)) 183 goto out_no_transfer; 184 185 vpix->backing |= VMWGFX_PIX_GMR; 186 vpix->backing &= ~VMWGFX_PIX_MALLOC; 187 vpix->gmr = gmr; 188 189 vmwgfx_pixmap_free_storage(vpix); 190 191 return TRUE; 192 193 out_no_transfer: 194 vmwgfx_dmabuf_destroy(gmr); 195 return FALSE; 196} 197 198static Bool 199vmwgfx_pixmap_create_sw(struct vmwgfx_saa *vsaa, PixmapPtr pixmap) 200{ 201 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 202 203 if (!(vpix->backing & (VMWGFX_PIX_MALLOC | VMWGFX_PIX_GMR))) 204 return FALSE; 205 206 if (!vpix->malloc && (vpix->backing & VMWGFX_PIX_MALLOC)) { 207 vpix->malloc = malloc(pixmap->devKind * pixmap->drawable.height); 208 if (!vpix->malloc) 209 goto out_no_malloc; 210 if (!vmwgfx_pixmap_add_damage(pixmap)) 211 goto out_no_damage; 212 } else if (vpix->backing & VMWGFX_PIX_GMR) 213 return vmwgfx_pixmap_create_gmr(vsaa, pixmap); 214 215 return TRUE; 216 217 out_no_damage: 218 free(vpix->malloc); 219 vpix->malloc = NULL; 220 out_no_malloc: 221 return FALSE; 222} 223 224 225/** 226 * 227 * Makes sure all presented contents covered by @region are read 228 * back and are present in a valid GMR. 229 */ 230 231static Bool 232vmwgfx_pixmap_present_readback(struct vmwgfx_saa *vsaa, 233 PixmapPtr pixmap, 234 RegionPtr region) 235{ 236 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 237 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 238 RegionRec intersection; 239 240 if (!spix->damage || !REGION_NOTEMPTY(vsaa->pScreen, &spix->dirty_hw) || 241 !vpix->dirty_present) 242 return TRUE; 243 244 /* 245 * Intersect dirty region with region to be read back, if any. 246 */ 247 248 REGION_NULL(vsaa->pScreen, &intersection); 249 REGION_COPY(vsaa->pScreen, &intersection, &spix->dirty_hw); 250 REGION_INTERSECT(vsaa->pScreen, &intersection, &intersection, 251 vpix->dirty_present); 252 253 if (region) 254 REGION_INTERSECT(vsaa->pScreen, &intersection, &intersection, region); 255 256 if (!REGION_NOTEMPTY(vsaa->pScreen, &intersection)) 257 goto out; 258 259 /* 260 * Make really sure there is a GMR to read back to. 261 */ 262 263 if (!vmwgfx_pixmap_create_gmr(vsaa, pixmap)) 264 goto out_err; 265 266 if (vmwgfx_present_readback(vsaa->drm_fd, vpix->fb_id, 267 &intersection) != 0) 268 goto out_err; 269 270 REGION_SUBTRACT(vsaa->pScreen, &spix->dirty_hw, 271 &spix->dirty_hw, &intersection); 272 out: 273 REGION_UNINIT(vsaa->pScreen, &intersection); 274 return TRUE; 275 276 out_err: 277 REGION_UNINIT(vsaa->pScreen, &intersection); 278 return FALSE; 279} 280 281static Bool 282vmwgfx_saa_dma(struct vmwgfx_saa *vsaa, 283 PixmapPtr pixmap, 284 RegionPtr reg, 285 Bool to_hw, 286 int dx, 287 int dy, 288 struct xa_surface *srf) 289{ 290 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 291 292 if (!srf) 293 srf = vpix->hw; 294 295 if (!srf || (!vpix->gmr && !vpix->malloc)) 296 return TRUE; 297 298 if (vpix->gmr && vsaa->can_optimize_dma) { 299 uint32_t handle, dummy; 300 301 if (_xa_surface_handle(srf, &handle, &dummy) != 0) 302 goto out_err; 303 if (vmwgfx_dma(dx, dy, reg, vpix->gmr, pixmap->devKind, handle, 304 to_hw) != 0) 305 goto out_err; 306 } else { 307 uint8_t *data = (uint8_t *) vpix->malloc; 308 int ret; 309 310 if (vpix->gmr) { 311 data = (uint8_t *) vmwgfx_dmabuf_map(vpix->gmr); 312 if (!data) 313 goto out_err; 314 } 315 316 if (dx || dy) { 317 REGION_TRANSLATE(pScreen, reg, dx, dy); 318 data -= ((dx * pixmap->drawable.bitsPerPixel + 7)/8 + 319 dy * pixmap->devKind); 320 } 321 322 ret = xa_surface_dma(vsaa->xa_ctx, srf, data, pixmap->devKind, 323 (int) to_hw, 324 (struct xa_box *) REGION_RECTS(reg), 325 REGION_NUM_RECTS(reg)); 326 if (to_hw) 327 xa_context_flush(vsaa->xa_ctx); 328 if (vpix->gmr) 329 vmwgfx_dmabuf_unmap(vpix->gmr); 330 if (dx || dy) 331 REGION_TRANSLATE(pScreen, reg, -dx, -dy); 332 if (ret) 333 goto out_err; 334 } 335 return TRUE; 336 out_err: 337 LogMessage(X_ERROR, "DMA %s surface failed.\n", 338 to_hw ? "to" : "from"); 339 return FALSE; 340} 341 342 343static Bool 344vmwgfx_download_from_hw(struct saa_driver *driver, PixmapPtr pixmap, 345 RegionPtr readback) 346{ 347 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 348 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 349 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 350 351 RegionRec intersection; 352 353 if (!vmwgfx_pixmap_present_readback(vsaa, pixmap, readback)) 354 return FALSE; 355 356 if (!REGION_NOTEMPTY(vsaa->pScreen, &spix->dirty_hw)) 357 return TRUE; 358 359 if (!vpix->hw) 360 return TRUE; 361 362 REGION_NULL(vsaa->pScreen, &intersection); 363 REGION_INTERSECT(vsaa->pScreen, &intersection, readback, 364 &spix->dirty_hw); 365 readback = &intersection; 366 367 if (!vmwgfx_pixmap_create_sw(vsaa, pixmap)) 368 goto out_err; 369 370 if (!vmwgfx_saa_dma(vsaa, pixmap, readback, FALSE, 0, 0, NULL)) 371 goto out_err; 372 REGION_SUBTRACT(vsaa->pScreen, &spix->dirty_hw, &spix->dirty_hw, readback); 373 REGION_UNINIT(vsaa->pScreen, &intersection); 374 return TRUE; 375 out_err: 376 REGION_UNINIT(vsaa->pScreen, &intersection); 377 return FALSE; 378} 379 380 381static Bool 382vmwgfx_upload_to_hw(struct saa_driver *driver, PixmapPtr pixmap, 383 RegionPtr upload) 384{ 385 return vmwgfx_saa_dma(to_vmwgfx_saa(driver), pixmap, upload, TRUE, 386 0, 0, NULL); 387} 388 389static void 390vmwgfx_release_from_cpu(struct saa_driver *driver, PixmapPtr pixmap, saa_access_t access) 391{ 392 // LogMessage(X_INFO, "Release 0x%08lx access 0x%08x\n", 393 // (unsigned long) pixmap, (unsigned) access); 394} 395 396static void * 397vmwgfx_sync_for_cpu(struct saa_driver *driver, PixmapPtr pixmap, saa_access_t access) 398{ 399 /* 400 * Errors in this functions will turn up in subsequent map 401 * calls. 402 */ 403 404 (void) vmwgfx_pixmap_create_sw(to_vmwgfx_saa(driver), pixmap); 405 406 return NULL; 407} 408 409static void * 410vmwgfx_map(struct saa_driver *driver, PixmapPtr pixmap, saa_access_t access) 411{ 412 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 413 414 if (vpix->malloc) 415 return vpix->malloc; 416 else if (vpix->gmr) 417 return vmwgfx_dmabuf_map(vpix->gmr); 418 else 419 return NULL; 420} 421 422static void 423vmwgfx_unmap(struct saa_driver *driver, PixmapPtr pixmap, saa_access_t access) 424{ 425 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 426 427 if (vpix->gmr) 428 return vmwgfx_dmabuf_unmap(vpix->gmr); 429 430// LogMessage(X_INFO, "Unmap 0x%08lx access 0x%08x\n", 431 // (unsigned long) pixmap, (unsigned) access); 432 ; 433} 434 435static Bool 436vmwgfx_create_pixmap(struct saa_driver *driver, struct saa_pixmap *spix, 437 int w, int h, int depth, 438 unsigned int usage_hint, int bpp, int *new_pitch) 439{ 440 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 441 442 *new_pitch = ((w * bpp + FB_MASK) >> FB_SHIFT) * sizeof(FbBits); 443 444 WSBMINITLISTHEAD(&vpix->sync_x_head); 445 WSBMINITLISTHEAD(&vpix->scanout_list); 446 WSBMINITLISTHEAD(&vpix->pixmap_list); 447 448 return TRUE; 449} 450 451Bool 452vmwgfx_hw_kill(struct vmwgfx_saa *vsaa, 453 struct saa_pixmap *spix) 454{ 455 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 456 457 if (!vpix->hw) 458 return TRUE; 459 460 /* 461 * Read back any dirty regions from hardware. 462 */ 463 464 if (!vmwgfx_download_from_hw(&vsaa->driver, spix->pixmap, 465 &spix->dirty_hw)) 466 return FALSE; 467 468 xa_surface_destroy(vpix->hw); 469 vpix->hw = NULL; 470 471 /* 472 * Remove damage tracking if this is not a scanout pixmap. 473 */ 474 475 if (WSBMLISTEMPTY(&vpix->scanout_list)) 476 vmwgfx_pixmap_remove_damage(spix->pixmap); 477 478 return TRUE; 479} 480 481void 482vmwgfx_flush_dri2(ScreenPtr pScreen) 483{ 484 struct vmwgfx_saa *vsaa = 485 to_vmwgfx_saa(saa_get_driver(pScreen)); 486 struct _WsbmListHead *list, *next; 487 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); 488 489 if (!pScrn->vtSema) 490 return; 491 492 WSBMLISTFOREACHSAFE(list, next, &vsaa->sync_x_list) { 493 struct vmwgfx_saa_pixmap *vpix = 494 WSBMLISTENTRY(list, struct vmwgfx_saa_pixmap, sync_x_head); 495 struct saa_pixmap *spix = &vpix->base; 496 PixmapPtr pixmap = spix->pixmap; 497 498 if (vmwgfx_upload_to_hw(&vsaa->driver, pixmap, &spix->dirty_shadow)) { 499 REGION_EMPTY(vsaa->pScreen, &spix->dirty_shadow); 500 WSBMLISTDELINIT(list); 501 } 502 } 503} 504 505 506static void 507vmwgfx_destroy_pixmap(struct saa_driver *driver, PixmapPtr pixmap) 508{ 509 ScreenPtr pScreen = to_vmwgfx_saa(driver)->pScreen; 510 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 511 (void) pScreen; 512 513 vpix->backing = 0; 514 vmwgfx_pixmap_free_storage(vpix); 515 516 /* 517 * Any damage we've registered has already been removed by the server 518 * at this point. Any attempt to unregister / destroy it will result 519 * in a double free. 520 */ 521 522 vmwgfx_pixmap_remove_present(vpix); 523 WSBMLISTDELINIT(&vpix->pixmap_list); 524 WSBMLISTDELINIT(&vpix->sync_x_head); 525 526 if (vpix->hw_is_dri2_fronts) 527 LogMessage(X_ERROR, "Incorrect dri2 front count.\n"); 528} 529 530 531 532/** 533 * 534 * Makes sure we have a surface with valid contents. 535 */ 536 537static void 538vmwgfx_copy_stride(uint8_t *dst, uint8_t *src, unsigned int dst_pitch, 539 unsigned int src_pitch, unsigned int dst_height, 540 unsigned int src_height) 541{ 542 unsigned int i; 543 unsigned int height = (dst_height < src_height) ? dst_height : src_height; 544 unsigned int pitch = (dst_pitch < src_pitch) ? dst_pitch : src_pitch; 545 546 for(i=0; i<height; ++i) { 547 memcpy(dst, src, pitch); 548 dst += dst_pitch; 549 src += src_pitch; 550 } 551} 552 553 554static Bool 555vmwgfx_pix_resize(PixmapPtr pixmap, unsigned int old_pitch, 556 unsigned int old_height, unsigned int old_width) 557{ 558 ScreenPtr pScreen = pixmap->drawable.pScreen; 559 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(saa_get_driver(pScreen)); 560 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 561 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 562 DrawablePtr draw = &pixmap->drawable; 563 unsigned int size = pixmap->devKind * draw->height; 564 BoxRec b_box; 565 RegionRec b_reg; 566 567 /* 568 * Ignore copying errors. At worst they will show up as rendering 569 * artefacts. 570 */ 571 572 if (vpix->malloc) { 573 574 void *new_malloc = malloc(size); 575 if (!new_malloc) 576 return FALSE; 577 578 vmwgfx_copy_stride(new_malloc, vpix->malloc, pixmap->devKind, 579 old_pitch, draw->height, 580 old_height); 581 free(vpix->malloc); 582 vpix->malloc = new_malloc; 583 } 584 585 if (vpix->gmr) { 586 struct vmwgfx_dmabuf *gmr; 587 void *new_addr; 588 void *old_addr; 589 590 gmr = vmwgfx_dmabuf_alloc(vsaa->drm_fd, size); 591 if (!gmr) 592 return FALSE; 593 594 new_addr = vmwgfx_dmabuf_map(gmr); 595 old_addr = vmwgfx_dmabuf_map(vpix->gmr); 596 597 if (new_addr && old_addr) 598 vmwgfx_copy_stride(new_addr, old_addr, pixmap->devKind, 599 old_pitch, draw->height, 600 old_height); 601 else 602 LogMessage(X_ERROR, "Failed pixmap resize copy.\n"); 603 604 if (old_addr) 605 vmwgfx_dmabuf_unmap(vpix->gmr); 606 if (new_addr) 607 vmwgfx_dmabuf_unmap(gmr); 608 vmwgfx_dmabuf_destroy(vpix->gmr); 609 vpix->gmr = gmr; 610 } 611 612 if (vpix->hw) { 613 if (xa_surface_redefine(vpix->hw, draw->width, draw->height, 614 draw->depth, xa_type_argb, 615 xa_format_unknown, vpix->xa_flags, 1) != 0) 616 return FALSE; 617 } 618 619 b_box.x1 = 0; 620 b_box.x2 = draw->width; 621 b_box.y1 = 0; 622 b_box.y2 = draw->height; 623 624 REGION_INIT(pScreen, &b_reg, &b_box, 1); 625 REGION_INTERSECT(pScreen, &spix->dirty_shadow, &spix->dirty_shadow, 626 &b_reg); 627 REGION_INTERSECT(pScreen, &spix->dirty_hw, &spix->dirty_hw, &b_reg); 628 if (vpix->dirty_present) 629 REGION_INTERSECT(pScreen, vpix->dirty_present, vpix->dirty_present, 630 &b_reg); 631 if (vpix->pending_update) 632 REGION_INTERSECT(pScreen, vpix->pending_update, vpix->pending_update, 633 &b_reg); 634 if (vpix->pending_present) 635 REGION_INTERSECT(pScreen, vpix->pending_present, 636 vpix->pending_present, &b_reg); 637 if (vpix->present_damage) 638 REGION_INTERSECT(pScreen, vpix->present_damage, vpix->present_damage, 639 &b_reg); 640 641 REGION_UNINIT(pScreen, &b_reg); 642 643 return TRUE; 644} 645 646 647static Bool 648vmwgfx_modify_pixmap_header (PixmapPtr pixmap, int w, int h, int depth, 649 int bpp, int devkind, void *pixdata) 650{ 651 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 652 ScreenPtr pScreen = pixmap->drawable.pScreen; 653 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(saa_get_driver(pScreen)); 654 unsigned int old_height; 655 unsigned int old_width; 656 unsigned int old_pitch; 657 658 if (!vpix) { 659 LogMessage(X_ERROR, "Not an SAA pixmap.\n"); 660 return FALSE; 661 } 662 663 if (pixdata) { 664 vpix->backing = 0; 665 vmwgfx_pixmap_free_storage(vpix); 666 return FALSE; 667 } 668 669 if (depth <= 0) 670 depth = pixmap->drawable.depth; 671 672 if (bpp <= 0) 673 bpp = pixmap->drawable.bitsPerPixel; 674 675 if (w <= 0) 676 w = pixmap->drawable.width; 677 678 if (h <= 0) 679 h = pixmap->drawable.height; 680 681 if (w <= 0 || h <= 0 || depth <= 0) 682 return FALSE; 683 684 old_height = pixmap->drawable.height; 685 old_width = pixmap->drawable.width; 686 old_pitch = pixmap->devKind; 687 688 if (!miModifyPixmapHeader(pixmap, w, h, depth, 689 bpp, devkind, NULL)) 690 goto out_no_modify; 691 692 if (!vpix->backing) 693 vpix->backing = VMWGFX_PIX_MALLOC; 694 695 vmwgfx_pix_resize(pixmap, old_pitch, old_height, old_width); 696 vmwgfx_pixmap_free_storage(vpix); 697 WSBMLISTADDTAIL(&vpix->pixmap_list, &vsaa->pixmaps); 698 699 return TRUE; 700 701 out_no_modify: 702 return FALSE; 703} 704 705static Bool 706vmwgfx_present_prepare(struct vmwgfx_saa *vsaa, 707 struct vmwgfx_saa_pixmap *src_vpix, 708 struct vmwgfx_saa_pixmap *dst_vpix) 709{ 710 ScreenPtr pScreen = vsaa->pScreen; 711 unsigned int dummy; 712 713 (void) pScreen; 714 if (src_vpix == dst_vpix || !src_vpix->hw || 715 _xa_surface_handle(src_vpix->hw, &vsaa->src_handle, &dummy) != 0) 716 return FALSE; 717 718 REGION_NULL(pScreen, &vsaa->present_region); 719 vsaa->diff_valid = FALSE; 720 vsaa->dst_vpix = dst_vpix; 721 vsaa->present_flush(pScreen); 722 723 return TRUE; 724} 725 726/** 727 * Determine whether we should try present copies on this pixmap. 728 */ 729 730static Bool 731vmwgfx_is_present_hw(PixmapPtr pixmap) 732{ 733 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 734 return (vpix->dirty_present != NULL); 735} 736 737static void 738vmwgfx_check_hw_contents(struct vmwgfx_saa *vsaa, 739 struct vmwgfx_saa_pixmap *vpix, 740 RegionPtr region, 741 Bool *has_dirty_hw, 742 Bool *has_valid_hw) 743{ 744 RegionRec intersection; 745 746 747 if (!vpix->hw) { 748 *has_dirty_hw = FALSE; 749 *has_valid_hw = FALSE; 750 return; 751 } 752 753 if (!region) { 754 *has_dirty_hw = REGION_NOTEMPTY(vsaa->pScreen, 755 &vpix->base.dirty_hw); 756 *has_valid_hw = !REGION_NOTEMPTY(vsaa->pScreen, 757 &vpix->base.dirty_shadow); 758 return; 759 } 760 761 REGION_NULL(vsaa->pScreen, &intersection); 762 REGION_INTERSECT(vsaa->pScreen, &intersection, &vpix->base.dirty_hw, 763 region); 764 *has_dirty_hw = REGION_NOTEMPTY(vsaa->pScreen, &intersection); 765 REGION_INTERSECT(vsaa->pScreen, &intersection, &vpix->base.dirty_shadow, 766 region); 767 *has_valid_hw = !REGION_NOTEMPTY(vsaa->pScreen, &intersection); 768 REGION_UNINIT(vsaa->pScreen, &intersection); 769} 770 771/** 772 * vmwgfx_prefer_gmr: Prefer a dma buffer over malloced memory for software 773 * rendered storage 774 * 775 * @vsaa: Pointer to a struct vmwgfx_saa accelerator. 776 * @pixmap: Pointer to pixmap whose storage preference we want to alter. 777 * 778 * If possible, alter the storage or future storage of the software contents 779 * of this pixmap to be in a DMA buffer rather than in malloced memory. 780 * This function should be called when it's likely that frequent DMA operations 781 * will occur between a surface and the memory holding the software 782 * contents. 783 */ 784static void 785vmwgfx_prefer_gmr(struct vmwgfx_saa *vsaa, PixmapPtr pixmap) 786{ 787 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 788 789 if (vsaa->can_optimize_dma) { 790 if (vpix->malloc) { 791 (void) vmwgfx_pixmap_create_gmr(vsaa, pixmap); 792 } else if (vpix->backing & VMWGFX_PIX_MALLOC) { 793 vpix->backing |= VMWGFX_PIX_GMR; 794 vpix->backing &= ~VMWGFX_PIX_MALLOC; 795 } 796 } 797} 798 799Bool 800vmwgfx_create_hw(struct vmwgfx_saa *vsaa, 801 PixmapPtr pixmap) 802{ 803 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 804 struct xa_surface *hw; 805 uint32_t new_flags; 806 807 if (!vsaa->xat) 808 return FALSE; 809 810 if (vpix->hw) 811 return TRUE; 812 813 new_flags = (vpix->xa_flags & ~vpix->staging_remove_flags) | 814 vpix->staging_add_flags | XA_FLAG_SHARED; 815 816 hw = xa_surface_create(vsaa->xat, 817 pixmap->drawable.width, 818 pixmap->drawable.height, 819 0, 820 xa_type_other, 821 vpix->staging_format, 822 new_flags); 823 if (hw == NULL) 824 return FALSE; 825 826 vpix->xa_flags = new_flags; 827 828 if (!vmwgfx_pixmap_add_damage(pixmap)) 829 goto out_no_damage; 830 831 vpix->hw = hw; 832 vpix->backing |= VMWGFX_PIX_SURFACE; 833 vmwgfx_pixmap_free_storage(vpix); 834 835 /* 836 * If there is a HW surface, make sure that the shadow is 837 * (or will be) a GMR, provided we can do fast DMAs from / to it. 838 */ 839 vmwgfx_prefer_gmr(vsaa, pixmap); 840 841 return TRUE; 842 843out_no_damage: 844 xa_surface_destroy(hw); 845 return FALSE; 846} 847 848 849Bool 850vmwgfx_hw_validate(PixmapPtr pixmap, RegionPtr region) 851{ 852 struct vmwgfx_saa *vsaa = 853 to_vmwgfx_saa(saa_get_driver(pixmap->drawable.pScreen)); 854 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 855 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 856 RegionRec intersection; 857 858 if (!vmwgfx_pixmap_present_readback(vsaa, pixmap, region)) 859 return FALSE; 860 861 REGION_NULL(vsaa->pScreen, &intersection); 862 REGION_COPY(vsaa->pScreen, &intersection, &spix->dirty_shadow); 863 864 if (vpix->dirty_present) 865 REGION_UNION(vsaa->pScreen, &intersection, vpix->dirty_present, 866 &spix->dirty_shadow); 867 868 if (spix->damage && REGION_NOTEMPTY(vsaa->pScreen, &intersection)) { 869 RegionPtr upload = &intersection; 870 871 /* 872 * Check whether we need to upload from GMR. 873 */ 874 875 if (region) { 876 REGION_INTERSECT(vsaa->pScreen, &intersection, region, 877 &intersection); 878 upload = &intersection; 879 } 880 881 if (REGION_NOTEMPTY(vsaa->pScreen, upload)) { 882 Bool ret = vmwgfx_upload_to_hw(&vsaa->driver, pixmap, upload); 883 if (ret) { 884 REGION_SUBTRACT(vsaa->pScreen, &spix->dirty_shadow, 885 &spix->dirty_shadow, upload); 886 if (vpix->dirty_present) 887 REGION_SUBTRACT(vsaa->pScreen, vpix->dirty_present, 888 vpix->dirty_present, upload); 889 } else { 890 REGION_UNINIT(vsaa->pScreen, &intersection); 891 return FALSE; 892 } 893 } 894 } 895 REGION_UNINIT(vsaa->pScreen, &intersection); 896 return TRUE; 897} 898 899static Bool 900vmwgfx_copy_prepare(struct saa_driver *driver, 901 PixmapPtr src_pixmap, 902 PixmapPtr dst_pixmap, 903 int dx, 904 int dy, 905 int alu, 906 RegionPtr src_reg, 907 uint32_t plane_mask) 908{ 909 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 910 struct vmwgfx_saa_pixmap *src_vpix; 911 struct vmwgfx_saa_pixmap *dst_vpix; 912 Bool has_dirty_hw; 913 Bool has_valid_hw; 914 915 if (!vsaa->xat || !SAA_PM_IS_SOLID(&dst_pixmap->drawable, plane_mask) || 916 alu != GXcopy || !vsaa->is_master) 917 return FALSE; 918 919 src_vpix = vmwgfx_saa_pixmap(src_pixmap); 920 dst_vpix = vmwgfx_saa_pixmap(dst_pixmap); 921 922 vmwgfx_check_hw_contents(vsaa, src_vpix, src_reg, 923 &has_dirty_hw, &has_valid_hw); 924 925 if (vmwgfx_is_present_hw(dst_pixmap) && 926 src_vpix->backing & VMWGFX_PIX_SURFACE) { 927 928 if (!has_dirty_hw && !has_valid_hw) 929 return FALSE; 930 931 if (!vmwgfx_hw_accel_validate(src_pixmap, 0, 0, 0, src_reg)) 932 return FALSE; 933 if (vmwgfx_present_prepare(vsaa, src_vpix, dst_vpix)) { 934 vsaa->present_copy = TRUE; 935 return TRUE; 936 } 937 return FALSE; 938 } 939 940 vsaa->present_copy = FALSE; 941 if (src_vpix != dst_vpix) { 942 943 /* 944 * Use hardware acceleration either if source is partially only 945 * in hardware, or if source is entirely in hardware and destination 946 * has a hardware surface. 947 */ 948 949 if (!has_dirty_hw && !(has_valid_hw && (dst_vpix->hw != NULL))) 950 return FALSE; 951 952 /* 953 * Determine surface formats. 954 */ 955 956 if (src_vpix->base.src_format == 0) { 957 if (!vmwgfx_hw_accel_stage(src_pixmap, 0, XA_FLAG_RENDER_TARGET, 0)) 958 return FALSE; 959 } else { 960 if (PICT_FORMAT_TYPE(src_vpix->base.src_format) != PICT_TYPE_ARGB || 961 !vmwgfx_hw_composite_src_stage(src_pixmap, src_vpix->base.src_format)) 962 return FALSE; 963 } 964 965 if (dst_vpix->base.dst_format == 0) { 966 if (!vmwgfx_hw_accel_stage(dst_pixmap, 0, XA_FLAG_RENDER_TARGET, 0)) 967 return FALSE; 968 } else { 969 if (PICT_FORMAT_TYPE(dst_vpix->base.dst_format) != PICT_TYPE_ARGB || 970 !vmwgfx_hw_composite_dst_stage(dst_pixmap, dst_vpix->base.dst_format)) 971 return FALSE; 972 } 973 974 /* 975 * Create hardware surfaces. 976 */ 977 978 if (!vmwgfx_hw_commit(src_pixmap)) 979 return FALSE; 980 if (!vmwgfx_hw_commit(dst_pixmap)) 981 return FALSE; 982 983 /* 984 * Migrate data. 985 */ 986 987 if (!vmwgfx_hw_validate(src_pixmap, src_reg)) { 988 xa_copy_done(vsaa->xa_ctx); 989 xa_context_flush(vsaa->xa_ctx); 990 return FALSE; 991 } 992 993 /* 994 * Setup copy state. 995 */ 996 997 if (xa_copy_prepare(vsaa->xa_ctx, dst_vpix->hw, src_vpix->hw) != 998 XA_ERR_NONE) 999 return FALSE; 1000 1001 return TRUE; 1002 } 1003 1004 return FALSE; 1005} 1006 1007 1008static void 1009vmwgfx_present_done(struct vmwgfx_saa *vsaa) 1010{ 1011 ScreenPtr pScreen = vsaa->pScreen; 1012 struct vmwgfx_saa_pixmap *dst_vpix = vsaa->dst_vpix; 1013 1014 (void) pScreen; 1015 if (!vsaa->diff_valid) 1016 return; 1017 1018 (void) vmwgfx_present(vsaa->drm_fd, dst_vpix->fb_id, 1019 vsaa->xdiff, vsaa->ydiff, 1020 &vsaa->present_region, vsaa->src_handle); 1021 1022 REGION_TRANSLATE(pScreen, &vsaa->present_region, vsaa->xdiff, vsaa->ydiff); 1023 REGION_UNION(pScreen, dst_vpix->present_damage, dst_vpix->present_damage, 1024 &vsaa->present_region); 1025 vsaa->diff_valid = FALSE; 1026 REGION_UNINIT(pScreen, &vsaa->present_region); 1027} 1028 1029static void 1030vmwgfx_present_copy(struct vmwgfx_saa *vsaa, 1031 int src_x, 1032 int src_y, 1033 int dst_x, 1034 int dst_y, 1035 int w, 1036 int h) 1037{ 1038 int xdiff = dst_x - src_x; 1039 int ydiff = dst_y - src_y; 1040 BoxRec box; 1041 RegionRec reg; 1042 1043 if (vsaa->diff_valid && ((xdiff != vsaa->xdiff) || (ydiff != vsaa->ydiff))) 1044 (void) vmwgfx_present_done(vsaa); 1045 1046 if (!vsaa->diff_valid) { 1047 vsaa->xdiff = xdiff; 1048 vsaa->ydiff = ydiff; 1049 vsaa->diff_valid = TRUE; 1050 } 1051 1052 box.x1 = src_x; 1053 box.x2 = src_x + w; 1054 box.y1 = src_y; 1055 box.y2 = src_y + h; 1056 1057 REGION_INIT(pScreen, ®, &box, 1); 1058 REGION_UNION(pScreen, &vsaa->present_region, &vsaa->present_region, ®); 1059 REGION_UNINIT(pScreen, ®); 1060} 1061 1062static void 1063vmwgfx_copy(struct saa_driver *driver, 1064 int src_x, 1065 int src_y, 1066 int dst_x, 1067 int dst_y, 1068 int w, 1069 int h) 1070{ 1071 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1072 1073 if (vsaa->present_copy) { 1074 vmwgfx_present_copy(vsaa, src_x, src_y, dst_x, dst_y, w, h); 1075 return; 1076 } 1077 xa_copy(vsaa->xa_ctx, dst_x, dst_y, src_x, src_y, w, h); 1078} 1079 1080static void 1081vmwgfx_copy_done(struct saa_driver *driver) 1082{ 1083 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1084 1085 if (vsaa->present_copy) { 1086 vmwgfx_present_done(vsaa); 1087 return; 1088 } 1089 xa_copy_done(vsaa->xa_ctx); 1090 xa_context_flush(vsaa->xa_ctx); 1091} 1092 1093static Bool 1094vmwgfx_composite_prepare(struct saa_driver *driver, CARD8 op, 1095 PicturePtr src_pict, PicturePtr mask_pict, 1096 PicturePtr dst_pict, 1097 PixmapPtr src_pix, PixmapPtr mask_pix, 1098 PixmapPtr dst_pix, 1099 RegionPtr src_region, 1100 RegionPtr mask_region, 1101 RegionPtr dst_region) 1102{ 1103 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1104 struct vmwgfx_saa_pixmap *src_vpix; 1105 struct vmwgfx_saa_pixmap *dst_vpix; 1106 struct vmwgfx_saa_pixmap *mask_vpix; 1107 Bool tmp_valid_hw; 1108 Bool dirty_hw; 1109 Bool valid_hw; 1110 RegionRec empty; 1111 struct xa_composite *xa_comp; 1112 1113 if (!vsaa->is_master) 1114 return FALSE; 1115 1116 REGION_NULL(pScreen, &empty); 1117 1118 /* 1119 * First we define our migration policy. We accelerate only if there 1120 * are dirty hw regions to be read or if all source data is 1121 * available in hw, and the destination has a hardware surface. 1122 */ 1123 dst_vpix = vmwgfx_saa_pixmap(dst_pix); 1124 valid_hw = (dst_vpix->hw != NULL); 1125 if (saa_op_reads_destination(op)) { 1126 vmwgfx_check_hw_contents(vsaa, dst_vpix, dst_region, 1127 &dirty_hw, &tmp_valid_hw); 1128 valid_hw = (valid_hw && tmp_valid_hw); 1129 } else { 1130 dirty_hw = FALSE; 1131 dst_region = ∅ 1132 } 1133 1134 if (src_pix && !dirty_hw) { 1135 src_vpix = vmwgfx_saa_pixmap(src_pix); 1136 vmwgfx_check_hw_contents(vsaa, src_vpix, src_region, 1137 &dirty_hw, &tmp_valid_hw); 1138 valid_hw = (valid_hw && tmp_valid_hw); 1139 } 1140 1141 if (mask_pict && mask_pix && !dirty_hw) { 1142 mask_vpix = vmwgfx_saa_pixmap(mask_pix); 1143 vmwgfx_check_hw_contents(vsaa, mask_vpix, mask_region, 1144 &dirty_hw, &tmp_valid_hw); 1145 valid_hw = (valid_hw && tmp_valid_hw); 1146 } 1147 1148 /* 1149 * In rendercheck mode we try to accelerate all supported 1150 * composite operations. 1151 */ 1152 1153 if (!valid_hw && !dirty_hw && !vsaa->rendercheck) 1154 goto out_err; 1155 1156 /* 1157 * Then, setup most of the XA composite state (except hardware surfaces) 1158 * and check whether XA can accelerate. 1159 */ 1160 1161 xa_comp = vmwgfx_xa_setup_comp(vsaa->vcomp, op, 1162 src_pict, mask_pict, dst_pict); 1163 if (!xa_comp) 1164 goto out_err; 1165 1166 if (xa_composite_check_accelerated(xa_comp) != XA_ERR_NONE) 1167 goto out_err; 1168 1169 /* 1170 * Check that we can create the needed hardware surfaces. 1171 */ 1172 if (src_pix && !vmwgfx_hw_composite_src_stage(src_pix, src_pict->format)) 1173 goto out_err; 1174 if (mask_pict && mask_pix && 1175 !vmwgfx_hw_composite_src_stage(mask_pix, mask_pict->format)) 1176 goto out_err; 1177 if (!vmwgfx_hw_composite_dst_stage(dst_pix, dst_pict->format)) 1178 goto out_err; 1179 1180 /* 1181 * Seems OK. Commit the changes, creating hardware surfaces. 1182 */ 1183 if (src_pix && !vmwgfx_hw_commit(src_pix)) 1184 goto out_err; 1185 if (mask_pict && mask_pix && !vmwgfx_hw_commit(mask_pix)) 1186 goto out_err; 1187 if (!vmwgfx_hw_commit(dst_pix)) 1188 goto out_err; 1189 1190 /* 1191 * Update the XA state with our hardware surfaces and 1192 * surface formats 1193 */ 1194 if (!vmwgfx_xa_update_comp(xa_comp, src_pix, mask_pix, dst_pix)) 1195 goto out_err; 1196 1197 /* 1198 * Migrate data to surfaces. 1199 */ 1200 if (src_pix && src_region && !vmwgfx_hw_validate(src_pix, NULL)) 1201 goto out_err; 1202 if (mask_pict && mask_pix && mask_region && 1203 !vmwgfx_hw_validate(mask_pix, NULL)) 1204 goto out_err; 1205 if (dst_region && !vmwgfx_hw_validate(dst_pix, NULL)) 1206 goto out_err; 1207 1208 1209 /* 1210 * Bind the XA state. This must be done after data migration, since 1211 * migration may change the hardware surfaces. 1212 */ 1213 if (xa_composite_prepare(vsaa->xa_ctx, xa_comp)) 1214 goto out_err; 1215 1216 return TRUE; 1217 1218 out_err: 1219 return FALSE; 1220} 1221 1222static void 1223vmwgfx_composite(struct saa_driver *driver, 1224 int src_x, int src_y, int mask_x, int mask_y, 1225 int dst_x, int dst_y, 1226 int width, int height) 1227{ 1228 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1229 1230 xa_composite_rect(vsaa->xa_ctx, src_x, src_y, mask_x, mask_y, 1231 dst_x, dst_y, width, height); 1232} 1233 1234static void 1235vmwgfx_composite_done(struct saa_driver *driver) 1236{ 1237 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1238 1239 xa_composite_done(vsaa->xa_ctx); 1240 xa_context_flush(vsaa->xa_ctx); 1241} 1242 1243static void 1244vmwgfx_takedown(struct saa_driver *driver) 1245{ 1246 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1247 1248 if (vsaa->vcomp) 1249 vmwgfx_free_composite(vsaa->vcomp); 1250 free(vsaa); 1251} 1252 1253/* 1254 * This function call originates from the damage layer (outside SAA) 1255 * to indicate that an operation is complete, and that damage is being 1256 * processed. 1257 */ 1258static void 1259vmwgfx_operation_complete(struct saa_driver *driver, 1260 PixmapPtr pixmap) 1261{ 1262 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1263 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 1264 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 1265 ScrnInfoPtr pScrn = xf86ScreenToScrn(vsaa->pScreen); 1266 1267 /* 1268 * Make dri2 drawables up to date, or add them to the flush list 1269 * executed at glxWaitX(). Currently glxWaitX() is broken, so 1270 * we flush immediately, unless we're VT-switched away, in which 1271 * case a flush would deadlock in the kernel. 1272 * 1273 * For pixmaps for which vpix->hw_is_hosted is true, we can explicitly 1274 * inform the compositor when contents has changed, so for those pixmaps 1275 * we defer the upload until the compositor is informed, by putting 1276 * them on the sync_x_list. Note that hw_is_dri2_fronts take precedence. 1277 */ 1278 if (vpix->hw && (vpix->hw_is_dri2_fronts || vpix->hw_is_hosted)) { 1279 if (pScrn->vtSema && vpix->hw_is_dri2_fronts && 1280 vmwgfx_upload_to_hw(driver, pixmap, &spix->dirty_shadow)) { 1281 1282 REGION_EMPTY(vsaa->pScreen, &spix->dirty_shadow); 1283 return; 1284 } 1285 1286 if (WSBMLISTEMPTY(&vpix->sync_x_head)) 1287 WSBMLISTADDTAIL(&vpix->sync_x_head, &vsaa->sync_x_list); 1288 } 1289} 1290 1291/* 1292 * This function is called by SAA to indicate that SAA has 1293 * dirtied a region of a pixmap, either as hw (accelerated) or as 1294 * !hw (not accelerated). 1295 */ 1296static Bool 1297vmwgfx_dirty(struct saa_driver *driver, PixmapPtr pixmap, 1298 Bool hw, RegionPtr damage) 1299{ 1300 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(driver); 1301 struct saa_pixmap *spix = saa_get_saa_pixmap(pixmap); 1302 struct vmwgfx_saa_pixmap *vpix = to_vmwgfx_saa_pixmap(spix); 1303 1304 /* 1305 * Return if this is not a scanout pixmap. 1306 */ 1307 if (WSBMLISTEMPTY(&vpix->scanout_list)) 1308 return TRUE; 1309 1310#if 0 1311 /* 1312 * This code can be enabled to immediately upload scanout sw 1313 * contents to the hw surface. Otherwise this is done 1314 * just before we call the kms update function for the hw 1315 * surface. 1316 */ 1317 if (vsaa->only_hw_presents) { 1318 if (!hw && !vmwgfx_upload_to_hw(&vsaa->driver, pixmap, damage)) 1319 return FALSE; 1320 1321 REGION_SUBTRACT(&vsaa->pScreen, &spix->dirty_shadow, 1322 &spix->dirty_shadow, damage); 1323 hw = TRUE; 1324 } 1325#endif 1326 1327 /* 1328 * Is the new scanout damage hw or sw? 1329 */ 1330 if (hw) { 1331 /* 1332 * Dump pending present into present tracking region. 1333 */ 1334 if (vpix->dirty_present && 1335 REGION_NOTEMPTY(vsaa->pScreen, vpix->present_damage)) { 1336 REGION_UNION(vsaa->pScreen, vpix->dirty_present, 1337 vpix->dirty_present, damage); 1338 REGION_EMPTY(vsaa->pScreen, vpix->present_damage); 1339 } else { 1340 if (REGION_NOTEMPTY(vsaa->pScreen, vpix->pending_update)) { 1341 RegionRec reg; 1342 1343 REGION_NULL(vsaa->pScreen, ®); 1344 REGION_INTERSECT(vsaa->pScreen, ®, vpix->pending_update, 1345 damage); 1346 if (REGION_NOTEMPTY(vsaa->pScreen, ®)) 1347 vsaa->present_flush(vsaa->pScreen); 1348 REGION_UNINIT(pScreen, ®); 1349 } 1350 REGION_UNION(vsaa->pScreen, vpix->pending_present, 1351 vpix->pending_present, damage); 1352 if (vpix->dirty_present) 1353 REGION_SUBTRACT(vsaa->pScreen, vpix->dirty_present, 1354 vpix->dirty_present, damage); 1355 } 1356 } else { 1357 if (REGION_NOTEMPTY(vsaa->pScreen, vpix->pending_present)) { 1358 RegionRec reg; 1359 1360 REGION_NULL(vsaa->pScreen, ®); 1361 REGION_INTERSECT(vsaa->pScreen, ®, vpix->pending_present, 1362 damage); 1363 if (REGION_NOTEMPTY(vsaa->pScreen, ®)) 1364 vsaa->present_flush(vsaa->pScreen); 1365 REGION_UNINIT(pScreen, ®); 1366 } 1367 REGION_UNION(vsaa->pScreen, vpix->pending_update, 1368 vpix->pending_update, damage); 1369 if (vpix->dirty_present) 1370 REGION_SUBTRACT(vsaa->pScreen, vpix->dirty_present, 1371 vpix->dirty_present, damage); 1372 } 1373 1374 return TRUE; 1375} 1376 1377 1378static const struct saa_driver vmwgfx_saa_driver = { 1379 .saa_major = SAA_VERSION_MAJOR, 1380 .saa_minor = SAA_VERSION_MINOR, 1381 .pixmap_size = sizeof(struct vmwgfx_saa_pixmap), 1382 .damage = vmwgfx_dirty, 1383 .operation_complete = vmwgfx_operation_complete, 1384 .download_from_hw = vmwgfx_download_from_hw, 1385 .release_from_cpu = vmwgfx_release_from_cpu, 1386 .sync_for_cpu = vmwgfx_sync_for_cpu, 1387 .map = vmwgfx_map, 1388 .unmap = vmwgfx_unmap, 1389 .create_pixmap = vmwgfx_create_pixmap, 1390 .destroy_pixmap = vmwgfx_destroy_pixmap, 1391 .modify_pixmap_header = vmwgfx_modify_pixmap_header, 1392 .copy_prepare = vmwgfx_copy_prepare, 1393 .copy = vmwgfx_copy, 1394 .copy_done = vmwgfx_copy_done, 1395 .composite_prepare = vmwgfx_composite_prepare, 1396 .composite = vmwgfx_composite, 1397 .composite_done = vmwgfx_composite_done, 1398 .takedown = vmwgfx_takedown, 1399}; 1400 1401 1402Bool 1403vmwgfx_saa_init(ScreenPtr pScreen, int drm_fd, struct xa_tracker *xat, 1404 void (*present_flush)(ScreenPtr pScreen), 1405 Bool direct_presents, 1406 Bool only_hw_presents, 1407 Bool rendercheck) 1408{ 1409 struct vmwgfx_saa *vsaa; 1410 1411 vsaa = calloc(1, sizeof(*vsaa)); 1412 if (!vsaa) 1413 return FALSE; 1414 1415 if (xat == NULL) { 1416 direct_presents = FALSE; 1417 only_hw_presents = FALSE; 1418 } 1419 1420 vsaa->pScreen = pScreen; 1421 vsaa->xat = xat; 1422 if (xat) 1423 vsaa->xa_ctx = xa_context_default(xat); 1424 vsaa->drm_fd = drm_fd; 1425 vsaa->present_flush = present_flush; 1426 vsaa->can_optimize_dma = TRUE; 1427 vsaa->use_present_opt = direct_presents; 1428 vsaa->only_hw_presents = only_hw_presents; 1429 vsaa->rendercheck = rendercheck; 1430 vsaa->is_master = TRUE; 1431 vsaa->known_prime_format = FALSE; 1432 WSBMINITLISTHEAD(&vsaa->sync_x_list); 1433 WSBMINITLISTHEAD(&vsaa->pixmaps); 1434 1435 vsaa->driver = vmwgfx_saa_driver; 1436 vsaa->vcomp = vmwgfx_alloc_composite(); 1437 1438 if (!vsaa->vcomp) 1439 vsaa->driver.composite_prepare = NULL; 1440 1441 if (!saa_driver_init(pScreen, &vsaa->driver)) 1442 goto out_no_saa; 1443 1444 return TRUE; 1445 out_no_saa: 1446 free(vsaa); 1447 return FALSE; 1448} 1449 1450/* 1451 * ************************************************************************* 1452 * Scanout functions. 1453 * These do not strictly belong here, but we choose to hide the scanout 1454 * pixmap private data in the saa pixmaps. Might want to revisit this. 1455 */ 1456 1457/* 1458 * Make sure we flush / update this scanout on next update run. 1459 */ 1460 1461void 1462vmwgfx_scanout_refresh(PixmapPtr pixmap) 1463{ 1464 ScreenPtr pScreen = pixmap->drawable.pScreen; 1465 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 1466 BoxRec box; 1467 1468 (void) pScreen; 1469 box.x1 = 0; 1470 box.y1 = 0; 1471 box.x2 = pixmap->drawable.width; 1472 box.y2 = pixmap->drawable.height; 1473 1474 REGION_RESET(vsaa->pScreen, vpix->pending_present, &box); 1475 if (vpix->dirty_present) 1476 REGION_SUBTRACT(vsaa->pScreen, vpix->pending_present, 1477 vpix->pending_present, vpix->dirty_present); 1478 REGION_SUBTRACT(vsaa->pScreen, vpix->pending_present, 1479 vpix->pending_present, &vpix->base.dirty_shadow); 1480 REGION_COPY(vsaa->pScreen, vpix->pending_update, 1481 &vpix->base.dirty_shadow); 1482} 1483 1484/* 1485 * Take a "scanout reference" on a pixmap. If this is the first scanout 1486 * reference, allocate resources needed for scanout, like proper 1487 * damage tracking and kms fbs. 1488 */ 1489 1490uint32_t 1491vmwgfx_scanout_ref(struct vmwgfx_screen_entry *entry) 1492{ 1493 PixmapPtr pixmap = entry->pixmap; 1494 struct vmwgfx_saa *vsaa = 1495 to_vmwgfx_saa(saa_get_driver(pixmap->drawable.pScreen)); 1496 struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pixmap); 1497 1498 if (WSBMLISTEMPTY(&vpix->scanout_list)) { 1499 uint32_t handle, dummy; 1500 unsigned int depth; 1501 1502 if (vsaa->only_hw_presents) { 1503 /* 1504 * The KMS fb will be a HW surface. Create it, add damage 1505 * and get the handle. 1506 */ 1507 if (!vmwgfx_hw_accel_validate(pixmap, 0, XA_FLAG_SCANOUT, 0, NULL)) 1508 goto out_err; 1509 if (_xa_surface_handle(vpix->hw, &handle, &dummy) != 0) 1510 goto out_err; 1511 depth = xa_format_depth(xa_surface_format(vpix->hw)); 1512 1513 } else { 1514 /* 1515 * The KMS fb will be a Guest Memory Region. Create it, 1516 * add damage and get the handle. 1517 */ 1518 if (!vmwgfx_pixmap_create_gmr(vsaa, pixmap)) 1519 goto out_err; 1520 1521 handle = vpix->gmr->handle; 1522 depth = pixmap->drawable.depth; 1523 1524 } 1525 1526 if (!vmwgfx_pixmap_add_present(pixmap, vsaa->use_present_opt)) 1527 goto out_no_present; 1528 1529 if (drmModeAddFB(vsaa->drm_fd, 1530 pixmap->drawable.width, 1531 pixmap->drawable.height, 1532 depth, 1533 pixmap->drawable.bitsPerPixel, 1534 pixmap->devKind, 1535 handle, 1536 &vpix->fb_id) != 0) 1537 goto out_no_fb;; 1538 } 1539 pixmap->refcnt += 1; 1540 WSBMLISTADDTAIL(&entry->scanout_head, &vpix->scanout_list); 1541 return vpix->fb_id; 1542 1543 out_no_fb: 1544 vmwgfx_pixmap_remove_present(vpix); 1545 out_no_present: 1546 vmwgfx_pixmap_remove_damage(pixmap); 1547 out_err: 1548 vpix->fb_id = -1; 1549 return -1; 1550} 1551 1552/* 1553 * Free a "scanout reference" on a pixmap. If this was the last scanout 1554 * reference, free pixmap resources needed for scanout, like 1555 * damage tracking and kms fbs. 1556 */ 1557void 1558vmwgfx_scanout_unref(struct vmwgfx_screen_entry *entry) 1559{ 1560 struct vmwgfx_saa *vsaa; 1561 struct vmwgfx_saa_pixmap *vpix; 1562 PixmapPtr pixmap = entry->pixmap; 1563 1564 if (!pixmap) 1565 return; 1566 1567 vsaa = to_vmwgfx_saa(saa_get_driver(pixmap->drawable.pScreen)); 1568 vpix = vmwgfx_saa_pixmap(pixmap); 1569 WSBMLISTDELINIT(&entry->scanout_head); 1570 1571 if (WSBMLISTEMPTY(&vpix->scanout_list)) { 1572 REGION_EMPTY(vsaa->pScreen, vpix->pending_update); 1573 drmModeRmFB(vsaa->drm_fd, vpix->fb_id); 1574 vpix->fb_id = -1; 1575 vmwgfx_pixmap_present_readback(vsaa, pixmap, NULL); 1576 vmwgfx_pixmap_remove_present(vpix); 1577 vmwgfx_pixmap_remove_damage(pixmap); 1578 } 1579 1580 entry->pixmap = NULL; 1581 pixmap->drawable.pScreen->DestroyPixmap(pixmap); 1582} 1583 1584void 1585vmwgfx_saa_set_master(ScreenPtr pScreen) 1586{ 1587 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(saa_get_driver(pScreen)); 1588 1589 vsaa->is_master = TRUE; 1590 vmwgfx_flush_dri2(pScreen); 1591} 1592 1593void 1594vmwgfx_saa_drop_master(ScreenPtr pScreen) 1595{ 1596 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(saa_get_driver(pScreen)); 1597 struct _WsbmListHead *list; 1598 struct vmwgfx_saa_pixmap *vpix; 1599 struct saa_pixmap *spix; 1600 1601 WSBMLISTFOREACH(list, &vsaa->pixmaps) { 1602 vpix = WSBMLISTENTRY(list, struct vmwgfx_saa_pixmap, pixmap_list); 1603 spix = &vpix->base; 1604 1605 if (!vpix->hw) 1606 continue; 1607 1608 (void) vmwgfx_download_from_hw(&vsaa->driver, spix->pixmap, 1609 &spix->dirty_hw); 1610 REGION_EMPTY(draw->pScreen, &spix->dirty_hw); 1611 } 1612 1613 vsaa->is_master = FALSE; 1614} 1615 1616/* 1617 * ************************************************************************* 1618 * Helpers for hosted. 1619 */ 1620 1621#if (XA_TRACKER_VERSION_MAJOR >= 2) && defined(HAVE_LIBDRM_2_4_38) 1622 1623/** 1624 * vmwgfx_saa_copy_to_surface - Copy Drawable contents to an external surface. 1625 * 1626 * @pDraw: Pointer to source drawable. 1627 * @surface_fd: Prime file descriptor of external surface to copy to. 1628 * @dst_box: BoxRec describing the destination bounding box. 1629 * @region: Region of drawable to copy. Note: The code assumes that the 1630 * region is relative to the drawable origin, not the underlying pixmap 1631 * origin. 1632 * 1633 * Copies the contents (both software- and accelerated contents) to an 1634 * external surface. 1635 */ 1636Bool 1637vmwgfx_saa_copy_to_surface(DrawablePtr pDraw, uint32_t surface_fd, 1638 const BoxRec *dst_box, RegionPtr region) 1639{ 1640 ScreenPtr pScreen = pDraw->pScreen; 1641 struct vmwgfx_saa *vsaa = to_vmwgfx_saa(saa_get_driver(pScreen)); 1642 PixmapPtr src; 1643 struct saa_pixmap *spix; 1644 struct vmwgfx_saa_pixmap *vpix; 1645 const BoxRec *box; 1646 int n; 1647 int sx, sy, dx, dy; 1648 struct xa_surface *dst; 1649 uint32_t handle; 1650 Bool ret = TRUE; 1651 RegionRec intersection; 1652 RegionPtr copy_region = region; 1653 1654 if (vmwgfx_prime_fd_to_handle(vsaa->drm_fd, surface_fd, &handle) < 0) 1655 return FALSE; 1656 1657 dst = xa_surface_from_handle(vsaa->xat, pDraw->width, pDraw->height, 1658 pDraw->depth, xa_type_argb, 1659 xa_format_unknown, 1660 XA_FLAG_SHARED | XA_FLAG_RENDER_TARGET, 1661 handle, 1662 (pDraw->width * pDraw->bitsPerPixel + 7) / 8); 1663 1664 if (!dst) { 1665 ret = FALSE; 1666 goto out_no_surface; 1667 } 1668 1669 /* 1670 * Assume damage region is relative to the source window. 1671 */ 1672 src = saa_get_pixmap(pDraw, &sx, &sy); 1673 sx += pDraw->x; 1674 sy += pDraw->y; 1675 if (sx || sy) 1676 REGION_TRANSLATE(pScreen, region, sx, sy); 1677 1678 dx = dst_box->x1 - sx; 1679 dy = dst_box->y1 - sy; 1680 1681 spix = saa_get_saa_pixmap(src); 1682 vpix = to_vmwgfx_saa_pixmap(spix); 1683 1684 /* 1685 * Make sure software contents of the source pixmap is henceforth put 1686 * in a GMR to avoid the extra copy in the xa DMA. 1687 */ 1688 vmwgfx_prefer_gmr(vsaa, src); 1689 1690 /* 1691 * Determine the intersection between software contents and region to copy. 1692 */ 1693 1694 if (vsaa->known_prime_format) { 1695 REGION_NULL(pScreen, &intersection); 1696 if (!vpix->hw) 1697 REGION_COPY(pScreen, &intersection, region); 1698 else if (spix->damage && REGION_NOTEMPTY(pScreen, &spix->dirty_shadow)) 1699 REGION_INTERSECT(pScreen, &intersection, region, &spix->dirty_shadow); 1700 1701 /* 1702 * DMA software contents directly into the destination. Then subtract 1703 * the region we've DMA'd from the region to copy. 1704 */ 1705 if (REGION_NOTEMPTY(pScreen, &intersection)) { 1706 if (vmwgfx_saa_dma(vsaa, src, &intersection, TRUE, dx, dy, dst)) { 1707 REGION_SUBTRACT(pScreen, &intersection, region, &intersection); 1708 copy_region = &intersection; 1709 } 1710 } 1711 } 1712 1713 if (!REGION_NOTEMPTY(pScreen, copy_region)) 1714 goto out_no_copy; 1715 1716 /* 1717 * Copy Hardware contents to the destination 1718 */ 1719 box = REGION_RECTS(copy_region); 1720 n = REGION_NUM_RECTS(copy_region); 1721 1722 if (!vmwgfx_hw_accel_validate(src, 0, 0, 0, copy_region)) { 1723 ret = FALSE; 1724 goto out_no_copy; 1725 } 1726 1727 if (xa_copy_prepare(vsaa->xa_ctx, dst, vpix->hw) != XA_ERR_NONE) { 1728 ret = FALSE; 1729 goto out_no_copy; 1730 } 1731 1732 while(n--) { 1733 xa_copy(vsaa->xa_ctx, box->x1 + dx, box->y1 + dy, box->x1, box->y1, 1734 box->x2 - box->x1, box->y2 - box->y1); 1735 box++; 1736 } 1737 1738 xa_copy_done(vsaa->xa_ctx); 1739 xa_context_flush(vsaa->xa_ctx); 1740 1741 out_no_copy: 1742 if (vsaa->known_prime_format) 1743 REGION_UNINIT(pScreen, &intersection); 1744 if (sx || sy) 1745 REGION_TRANSLATE(pScreen, region, -sx, -sy); 1746 xa_surface_unref(dst); 1747 out_no_surface: 1748 vmwgfx_prime_release_handle(vsaa->drm_fd, handle); 1749 1750 return ret; 1751} 1752#endif 1753