1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
|
/*
* Copyright (c) 2009-2017, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//!
//! \file media_libva_putsurface_linux.cpp
//! \brief libva(and its extension) putsurface linux implementaion
//!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h> //open
#include <sys/stat.h> //fstat
#include <unistd.h> //read, lseek
#include <dlfcn.h> //dlopen,dlsym,dlclose
#include <time.h> //get_clocktime
#include <errno.h> //errno
#include <assert.h> //assert
#include <sys/mman.h>
#include <dlfcn.h>
#include <sys/ioctl.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "media_libva_putsurface_linux.h"
#include "media_libva_util.h"
#include "media_libva_common.h"
#include "media_libva_vp.h"
extern MOS_FORMAT VpGetFormatFromMediaFormat(DDI_MEDIA_FORMAT mf);
extern VPHAL_CSPACE DdiVp_GetColorSpaceFromMediaFormat(DDI_MEDIA_FORMAT mf);
extern MOS_TILE_TYPE VpGetTileTypeFromMediaTileType(uint32_t mediaTileType);
/* Closes and disposed any allocated data */
void dso_close(struct dso_handle *h)
{
if (!h){
return;
}
if (h->handle) {
if (h->handle != RTLD_DEFAULT)
dlclose(h->handle);
h->handle = nullptr;
}
free(h);
}
/* Opens the named shared library */
struct dso_handle * dso_open(const char *path)
{
struct dso_handle *h = nullptr;
h = (dso_handle *)calloc(1, sizeof(*h));
if (!h){
return nullptr;
}
if (path) {
h->handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL);
if (!h->handle)
goto error;
}
else{
h->handle = RTLD_DEFAULT;
}
return h;
error:
dso_close(h);
return nullptr;
}
/* Load function name from one dynamic lib */
static bool get_symbol(struct dso_handle *h, void *func_vptr, const char *name)
{
DDI_CHK_NULL(h, "nullptr h", false);
DDI_CHK_NULL(func_vptr, "nullptr func_vptr", false);
dso_generic_func func;
dso_generic_func * const func_ptr = (dso_generic_func*) func_vptr;
const char *error = nullptr;
dlerror();
func = (dso_generic_func)dlsym(h->handle, name);
error = dlerror();
if (error) {
fprintf(stderr, "error: failed to resolve %s(): %s\n", name, error);
return false;
}
*func_ptr = func;
return true;
}
//!
//! \brief Loads function name from vtable
//!
//! \param [in] h
//! Dso handle
//! \param [in] vtable
//! VA api table
//! \param [in] vtable_length
//! Length of VA api table
//! \param [in] symbols
//! Dso symbol
//!
//! \return bool
//! true if call success, else false
//!
bool
dso_get_symbols(
struct dso_handle *h,
void *vtable,
uint32_t vtable_length,
const struct dso_symbol *symbols
)
{
DDI_CHK_NULL(h, "nullptr h", false);
const struct dso_symbol *s = nullptr;
if (nullptr == symbols)
{
return VA_STATUS_ERROR_INVALID_PARAMETER;
}
for (s = symbols; s->name != nullptr; s++) {
if (s->offset + sizeof(dso_generic_func) > vtable_length)
return false;
if (!get_symbol(h, ((char *)vtable) + s->offset, s->name))
return false;
}
return true;
}
bool output_dri_init(VADriverContextP ctx)
{
DDI_CHK_NULL(ctx, "nullptr ctx", false);
PDDI_MEDIA_CONTEXT mediaDrvCtx = nullptr;
mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
DDI_CHK_NULL(mediaDrvCtx, "nullptr ctx", false);
struct dso_handle *dso_handle = nullptr;
struct dri_vtable *dri_vtable = nullptr;
mediaDrvCtx->dri_output = nullptr;
static const struct dso_symbol symbols[] = {
{ "va_dri_get_drawable",
offsetof(struct dri_vtable, get_drawable) },
{ "va_dri_get_rendering_buffer",
offsetof(struct dri_vtable, get_rendering_buffer) },
{ "va_dri_swap_buffer",
offsetof(struct dri_vtable, swap_buffer) },
{ nullptr, }
};
mediaDrvCtx->dri_output = (va_dri_output*) calloc(1, sizeof(struct va_dri_output));
if (!mediaDrvCtx->dri_output){
goto error;
}
mediaDrvCtx->dri_output->handle = dso_open(LIBVA_X11_NAME);
if (!mediaDrvCtx->dri_output->handle){
free(mediaDrvCtx->dri_output);
mediaDrvCtx->dri_output = nullptr;
goto error;
}
dso_handle = mediaDrvCtx->dri_output->handle;
dri_vtable = &mediaDrvCtx->dri_output->vtable;
if (!dso_get_symbols(dso_handle, dri_vtable, sizeof(*dri_vtable), symbols)){
dso_close(mediaDrvCtx->dri_output->handle);
free(mediaDrvCtx->dri_output);
mediaDrvCtx->dri_output = nullptr;
goto error;
}
return true;
error:
return false;
}
void
inline Rect_init(
RECT *rect,
int16_t destx,
int16_t desty,
uint16_t destw,
uint16_t desth
)
{
if (nullptr == rect)
{
return;
}
rect->left = destx;
rect->top = desty;
rect->right = destx + destw;
rect->bottom = desty + desth;
}
VAStatus DdiCodec_PutSurfaceLinuxVphalExt(
VADriverContextP ctx,
VASurfaceID surface,
void *draw, /* Drawable of window system */
int16_t srcx,
int16_t srcy,
uint16_t srcw,
uint16_t srch,
int16_t destx,
int16_t desty,
uint16_t destw,
uint16_t desth,
VARectangle *cliprects, /* client supplied clip list */
uint32_t number_cliprects, /* number of clip rects in the clip list */
uint32_t flags /* de-interlacing flags */
)
{
GC gc;
int32_t depth;
Visual* visual;
XImage* ximg;
int32_t surf_width;
int32_t surf_height;
PDDI_MEDIA_CONTEXT mediaDrvCtx;
PDDI_MEDIA_SURFACE dstSurfBuffObj;
TypeXCreateGC pfn_XCreateGC = nullptr;
TypeXFreeGC pfn_XFreeGC = nullptr;
TypeXCreateImage pfn_XCreateImage = nullptr;
TypeXDestroyImage pfn_XDestroyImage = nullptr;
TypeXPutImage pfn_XPutImage = nullptr;
if (nullptr == draw)
{
return VA_STATUS_ERROR_UNKNOWN;
}
visual = nullptr;
ximg = nullptr;
mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
dstSurfBuffObj = DdiMedia_GetSurfaceFromVASurfaceID(mediaDrvCtx, surface);
if (nullptr == dstSurfBuffObj)
{
return VA_STATUS_ERROR_UNKNOWN;
}
if (nullptr == mediaDrvCtx->X11FuncTable ||
nullptr == mediaDrvCtx->X11FuncTable->pfnXCreateGC ||
nullptr == mediaDrvCtx->X11FuncTable->pfnXFreeGC ||
nullptr == mediaDrvCtx->X11FuncTable->pfnXCreateImage ||
nullptr == mediaDrvCtx->X11FuncTable->pfnXDestroyImage ||
nullptr == mediaDrvCtx->X11FuncTable->pfnXPutImage)
{
return VA_STATUS_ERROR_UNKNOWN;
}
pfn_XCreateGC = (TypeXCreateGC)(mediaDrvCtx->X11FuncTable->pfnXCreateGC);
pfn_XFreeGC = (TypeXFreeGC)(mediaDrvCtx->X11FuncTable->pfnXFreeGC);
pfn_XCreateImage = (TypeXCreateImage)(mediaDrvCtx->X11FuncTable->pfnXCreateImage);
pfn_XDestroyImage = (TypeXDestroyImage)(mediaDrvCtx->X11FuncTable->pfnXDestroyImage);
pfn_XPutImage = (TypeXPutImage)(mediaDrvCtx->X11FuncTable->pfnXPutImage);
surf_width = dstSurfBuffObj->iWidth;
surf_height = dstSurfBuffObj->iHeight;
visual = DefaultVisual(ctx->native_dpy, ctx->x11_screen);
gc = (*pfn_XCreateGC)((Display*)ctx->native_dpy, (Drawable)draw, 0, nullptr);
depth = DefaultDepth(ctx->native_dpy, ctx->x11_screen);
if (TrueColor != visual->c_class)
{
DDI_ASSERTMESSAGE("Default visual of X display must be TrueColor.");
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
return VA_STATUS_ERROR_UNKNOWN;
}
ximg = (*pfn_XCreateImage)((Display*)ctx->native_dpy, visual, depth, ZPixmap, 0, nullptr,surf_width, surf_height, 32, 0 );
if (nullptr == ximg)
{
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
if (ximg->bits_per_pixel != 32)
{
DDI_ASSERTMESSAGE("Display uses %d bits/pixel this not supported.",ximg->bits_per_pixel);
(*pfn_XDestroyImage)(ximg);
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
return VA_STATUS_ERROR_UNKNOWN;
}
ximg->data = (char *)DdiMediaUtil_LockSurface(dstSurfBuffObj, (MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY));
if (nullptr == ximg->data)
{
DdiMediaUtil_UnlockSurface(dstSurfBuffObj);
(*pfn_XDestroyImage)(ximg);
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
(*pfn_XPutImage)((Display*)ctx->native_dpy, (Drawable)draw, gc, ximg, 0, 0, destx, desty, surf_width, surf_height);
DdiMediaUtil_UnlockSurface(dstSurfBuffObj);
ximg->data = nullptr;
if (nullptr != ximg)
{
(*pfn_XDestroyImage)(ximg);
}
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
return VA_STATUS_SUCCESS;
}
VAStatus DdiCodec_PutSurfaceLinuxHW(
VADriverContextP ctx,
VASurfaceID surface,
void* draw, /* Drawable of window system */
int16_t srcx,
int16_t srcy,
uint16_t srcw,
uint16_t srch,
int16_t destx,
int16_t desty,
uint16_t destw,
uint16_t desth,
VARectangle *cliprects, /* client supplied clip list */
uint32_t number_cliprects, /* number of clip rects in the clip list */
uint32_t flags /* de-interlacing flags */
)
{
VphalState *vpHal = nullptr;
int32_t ovRenderIndex = 0;
VPHAL_SURFACE Surf;
VPHAL_SURFACE target;
VPHAL_RENDER_PARAMS renderParams;
VPHAL_COLORFILL_PARAMS colorFill;
MOS_STATUS eStatus = MOS_STATUS_INVALID_PARAMETER;
RECT srcRect = { 0, 0, 0, 0 };
RECT dstRect = { 0, 0, 0, 0 };
PDDI_MEDIA_CONTEXT mediaCtx;
PDDI_MEDIA_SURFACE bufferObject;
uint32_t width,height,pitch;
uint32_t drawable_tiling_mode;
uint32_t drawable_swizzle_mode;
MOS_ALLOC_GFXRES_PARAMS allocParams;
MOS_TILE_TYPE tileType;
uint32_t ctxType;
PDDI_VP_CONTEXT vpCtx;
struct dri_drawable* dri_drawable = nullptr;
union dri_buffer* buffer = nullptr;
GMM_RESCREATE_PARAMS gmmParams;
mediaCtx = DdiMedia_GetMediaContext(ctx);
DDI_CHK_NULL(mediaCtx, "Null mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->dri_output, "Null mediaDrvCtx->dri_output", VA_STATUS_ERROR_INVALID_PARAMETER);
DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "Null mediaDrvCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_PARAMETER);
DDI_CHK_NULL(mediaCtx->pGmmClientContext, "Null mediaCtx->pGmmClientContext", VA_STATUS_ERROR_INVALID_PARAMETER);
DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surfaceId", VA_STATUS_ERROR_INVALID_SURFACE);
struct dri_vtable * const dri_vtable = &mediaCtx->dri_output->vtable;
DDI_CHK_NULL(dri_vtable, "Null dri_vtable", VA_STATUS_ERROR_INVALID_PARAMETER);
dri_drawable = dri_vtable->get_drawable(ctx, (Drawable)draw);
assert(dri_drawable);
buffer = dri_vtable->get_rendering_buffer(ctx, dri_drawable);
assert(buffer);
bufferObject = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
DDI_CHK_NULL(bufferObject, "Null bufferObject", VA_STATUS_ERROR_INVALID_SURFACE);
DdiMediaUtil_MediaPrintFps();
pitch = bufferObject->iPitch;
vpCtx = nullptr;
if (nullptr != mediaCtx->pVpCtxHeap->pHeapBase)
{
vpCtx = (PDDI_VP_CONTEXT)DdiMedia_GetContextFromContextID(ctx, (VAContextID)(0 + DDI_MEDIA_VACONTEXTID_OFFSET_VP), &ctxType);
DDI_CHK_NULL(vpCtx, "Null vpCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
vpHal = vpCtx->pVpHal;
DDI_CHK_NULL(vpHal, "Null vpHal", VA_STATUS_ERROR_INVALID_PARAMETER);
}
else
{
return VA_STATUS_ERROR_INVALID_CONTEXT;
}
// Zero memory
MOS_ZeroMemory(&Surf, sizeof(Surf));
MOS_ZeroMemory(&target, sizeof(target));
MOS_ZeroMemory(&renderParams, sizeof(renderParams));
MOS_ZeroMemory(&gmmParams, sizeof(gmmParams));
renderParams.Component = COMPONENT_LibVA;
//Init source rectangle
Rect_init(&srcRect, srcx, srcy, srcw, srch);
Rect_init(&dstRect, destx, desty, destw, desth);
// Source Surface Information
Surf.Format = VpGetFormatFromMediaFormat(bufferObject->format); // Surface format
Surf.SurfType = SURF_IN_PRIMARY; // Surface type (context)
Surf.SampleType = SAMPLE_PROGRESSIVE;
Surf.ScalingMode = VPHAL_SCALING_AVS;
Surf.OsResource.Format = VpGetFormatFromMediaFormat(bufferObject->format);
Surf.OsResource.iWidth = bufferObject->iWidth;
Surf.OsResource.iHeight = bufferObject->iHeight;
Surf.OsResource.iPitch = bufferObject->iPitch;
Surf.OsResource.iCount = 0;
Surf.OsResource.TileType = VpGetTileTypeFromMediaTileType(bufferObject->TileType);
Surf.OsResource.bMapped = bufferObject->bMapped;
Surf.OsResource.bo = bufferObject->bo;
Surf.OsResource.pGmmResInfo = bufferObject->pGmmResourceInfo;
Surf.dwWidth = bufferObject->iWidth;
Surf.dwHeight = bufferObject->iHeight;
Surf.dwPitch = bufferObject->iPitch;
Surf.TileType = VpGetTileTypeFromMediaTileType(bufferObject->TileType);
Surf.ColorSpace = DdiVp_GetColorSpaceFromMediaFormat(bufferObject->format);
Surf.ExtendedGamut = false;
Surf.rcSrc = srcRect;
Surf.rcDst = dstRect;
MOS_LINUX_BO* drawable_bo = mos_bo_gem_create_from_name(mediaCtx->pDrmBufMgr, "rendering buffer", buffer->dri2.name);
if (nullptr == drawable_bo)
{
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
if (!mos_bo_get_tiling(drawable_bo, &drawable_tiling_mode, &drawable_swizzle_mode))
{
switch (drawable_tiling_mode)
{
case I915_TILING_Y:
tileType = MOS_TILE_Y;
break;
case I915_TILING_X:
tileType = MOS_TILE_X;
gmmParams.Flags.Info.TiledX = true;
break;
case I915_TILING_NONE:
tileType = MOS_TILE_LINEAR;
gmmParams.Flags.Info.Linear = true;
break;
default:
drawable_tiling_mode = I915_TILING_NONE;
tileType = MOS_TILE_LINEAR;
gmmParams.Flags.Info.Linear = true;
break;
target.OsResource.TileType = (MOS_TILE_TYPE)drawable_tiling_mode;
}
}
else
{
target.OsResource.TileType = (MOS_TILE_TYPE)I915_TILING_NONE;
tileType = MOS_TILE_LINEAR;
gmmParams.Flags.Info.Linear = true;
}
gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrLocalMemory);
target.Format = Format_A8R8G8B8;
target.SurfType = SURF_OUT_RENDERTARGET;
//init target retangle
Rect_init(&srcRect, dri_drawable->x, dri_drawable->y, dri_drawable->width, dri_drawable->height);
Rect_init(&dstRect, dri_drawable->x, dri_drawable->y, dri_drawable->width, dri_drawable->height);
// Create GmmResourceInfo
gmmParams.Flags.Gpu.Video = true;
gmmParams.BaseWidth = dri_drawable->width;
gmmParams.BaseHeight = dri_drawable->height;
gmmParams.ArraySize = 1;
gmmParams.Type = RESOURCE_2D;
gmmParams.Format = GMM_FORMAT_R8G8B8A8_UNORM_TYPE;
//gmmParams.Format = GMM_FORMAT_B8G8R8A8_UNORM_TYPE;
target.OsResource.pGmmResInfo = mediaCtx->pGmmClientContext->CreateResInfoObject(&gmmParams);
if (nullptr == target.OsResource.pGmmResInfo)
{
mos_bo_unreference(drawable_bo);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
target.OsResource.iWidth = dri_drawable->width;
target.OsResource.iHeight = dri_drawable->height;
target.OsResource.iPitch = buffer->dri2.pitch;
target.OsResource.Format = Format_A8R8G8B8;
target.OsResource.iCount = 0;
target.OsResource.bo = drawable_bo;
target.OsResource.pData = (uint8_t *)drawable_bo->virt;
target.OsResource.TileType = tileType;
target.TileType = tileType;
target.dwWidth = dri_drawable->width;
target.dwHeight = dri_drawable->height;
target.dwPitch = target.OsResource.iPitch;
target.ColorSpace = CSpace_sRGB;
target.ExtendedGamut = false;
target.rcSrc = srcRect;
target.rcDst = dstRect;
renderParams.uSrcCount = 1;
renderParams.uDstCount = 1;
renderParams.pSrc[0] = &Surf;
renderParams.pTarget[0] = ⌖
renderParams.pColorFillParams = &colorFill;
renderParams.pColorFillParams->Color = 0xFF000000;
renderParams.pColorFillParams->bYCbCr = false;
renderParams.pColorFillParams->CSpace = CSpace_sRGB;
DdiMediaUtil_LockMutex(&mediaCtx->PutSurfaceRenderMutex);
eStatus = vpHal->Render(&renderParams);
if (MOS_FAILED(eStatus))
{
DdiMediaUtil_UnLockMutex(&mediaCtx->PutSurfaceRenderMutex);
mos_bo_unreference(drawable_bo);
return VA_STATUS_ERROR_OPERATION_FAILED;
}
DdiMediaUtil_UnLockMutex(&mediaCtx->PutSurfaceRenderMutex);
mos_bo_unreference(drawable_bo);
target.OsResource.bo = nullptr;
DdiMediaUtil_LockMutex(&mediaCtx->PutSurfaceSwapBufferMutex);
dri_vtable->swap_buffer(ctx, dri_drawable);
DdiMediaUtil_UnLockMutex(&mediaCtx->PutSurfaceSwapBufferMutex);
mediaCtx->pGmmClientContext->DestroyResInfoObject(target.OsResource.pGmmResInfo);
target.OsResource.pGmmResInfo = nullptr;
return VA_STATUS_SUCCESS;
}
#ifndef ANDROID
// move from media_libva_putsurface_linux.c
static unsigned long DdiMedia_mask2shift(unsigned long mask)
{
unsigned long shift = 0;
while((mask & 0x1) == 0)
{
mask = mask >> 1;
shift++;
}
return shift;
}
static void DdiMedia_yuv2pixel(uint32_t *pixel, int32_t y, int32_t u, int32_t v,
unsigned long rshift, unsigned long rmask,
unsigned long gshift, unsigned long gmask,
unsigned long bshift, unsigned long bmask)
{
DDI_CHK_NULL(pixel, "nullptr pixel", );
/* Warning, magic values ahead */
int32_t r = y + ((351 * (v-128)) >> 8);
int32_t g = y - (((179 * (v-128)) + (86 * (u-128))) >> 8);
int32_t b = y + ((444 * (u-128)) >> 8);
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
*pixel = (uint32_t)(((r << rshift) & rmask) | ((g << gshift) & gmask) |((b << bshift) & bmask));
}
#define YUV_444P_TO_ARGB() \
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * ((mediaSurface->iHeight));\
srcV = srcU + pitch * ((mediaSurface->iHeight));\
\
for(y = srcy; y < (srcy + height); y += 1) \
{\
for(x = srcx; x < (srcx + width); x += 1) \
{\
y1 = *(srcY + x); \
u1 = *(srcU + x);\
v1 = *(srcV + x);\
\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
\
}\
srcY += pitch;\
srcU += pitch;\
srcV += pitch;\
}
#define YUV_422H_TO_ARGB()\
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * mediaSurface->iHeight;\
srcV = srcU + pitch * mediaSurface->iHeight;\
\
for(y = srcy; y < (srcy + height); y += 1)\
{\
for(x = srcx; x < (srcx + width); x += 2)\
{\
y1 = *(srcY + x);\
y2 = *(srcY + x + 1);\
u1 = *(srcU + x / 2);\
v1 = *(srcV + x / 2);\
\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
}\
srcY += pitch;\
srcU += pitch;\
srcV += pitch;\
}
#define YUV_422V_TO_ARGB() \
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * mediaSurface->iHeight;\
srcV = srcU + pitch * mediaSurface->iHeight / 2;\
\
for(y = srcy; y < (srcy + width); y += 1)\
{\
for(x = srcx; x < (srcx + height); x += 2)\
{\
y1 = *(srcY + x * pitch);\
y2 = *(srcY + (x + 1) * pitch);\
u1 = *(srcU + (x / 2) * pitch);\
v1 = *(srcV + (x / 2) * pitch);\
\
pixel = (uint32_t *)(ximg->data + (x * ximg->bytes_per_line) + (y * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (x* ximg->bytes_per_line) + ((y + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
\
}\
\
srcY += 1;\
srcU += 1;\
srcV += 1;\
}
#define YUV_IMC3_TO_ARGB() \
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * mediaSurface->iHeight;\
srcV = srcU + pitch * mediaSurface->iHeight / 2;\
\
for(y = srcy; y < (srcy + height); y += 2) \
{\
for(x = srcx; x < (srcx + width); x += 2) \
{\
y1 = *(srcY + x);\
y2 = *(srcY + x + 1);\
y3 = *(srcY + x + pitch);\
y4 = *(srcY + x + pitch + 1);\
\
u1 = *(srcU + x / 2);\
v1 = *(srcV + x / 2);\
\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y3, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y4, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask); \
}\
srcY += pitch * 2;\
srcU += pitch;\
srcV += pitch;\
}
#define YUV_411P_TO_ARGB() \
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * mediaSurface->iHeight;\
srcV = srcU + pitch * mediaSurface->iHeight;\
\
for(y = srcy; y < (srcy + height); y += 1)\
{\
for(x = srcx; x < (srcx + width); x += 4)\
{\
y1 = *(srcY + x);\
y2 = *(srcY + x + 1);\
y3 = *(srcY + x + 2);\
y4 = *(srcY + x + 3);\
\
u1 = *(srcU + x / 4);\
v1 = *(srcV + x / 4);\
\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y ) * ximg->bytes_per_line) + ((x+2) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y3, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y) * ximg->bytes_per_line) + ((x + 3) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y4, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
}\
srcY += pitch;\
srcU += pitch;\
srcV += pitch;\
}
#define YUV_400P_TO_ARGB()\
srcY = umdContextY + pitch * srcy;\
srcU = srcY;\
srcV = srcY;\
\
for(y = srcy; y < (srcy + height); y += 2)\
{\
for(x = srcx; x < (srcx + width); x += 2)\
{\
y1 = *(srcY + x);\
y2 = *(srcY + x + 1);\
y3 = *(srcY + x + pitch);\
y4 = *(srcY + x + pitch + 1);\
\
u1 = 128;\
v1 = 128;\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y3, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y4, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
}\
srcY += pitch * 2;\
srcU += pitch;\
srcV += pitch;\
}
#define YUV_NV12_TO_ARGB()\
srcY = umdContextY + pitch * srcy;\
srcU = srcY + pitch * mediaSurface->iHeight;\
srcV = srcU + 1;\
\
for(y = srcy; y < (srcy + height); y += 2)\
{\
for(x = srcx; x < (srcx + width); x += 2)\
{\
y1 = *(srcY + x);\
y2 = *(srcY + x + 1);\
y3 = *(srcY + x + pitch);\
y4 = *(srcY + x + pitch + 1);\
\
u1 = *(srcU + x);\
v1 = *(srcU + x +1);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y1, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + (y * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y2, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + (x * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y3, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
pixel = (uint32_t *)(ximg->data + ((y + 1) * ximg->bytes_per_line) + ((x + 1) * (ximg->bits_per_pixel >> 3)));\
DdiMedia_yuv2pixel(pixel, y4, u1, v1, rshift, rmask, gshift, gmask, bshift, bmask);\
}\
srcY += pitch * 2;\
srcU += pitch;\
}
VAStatus DdiMedia_PutSurfaceLinuxSW(
VADriverContextP ctx,
VASurfaceID surface,
void* draw, /* Drawable of window system */
int16_t srcx,
int16_t srcy,
uint16_t srcw,
uint16_t srch,
int16_t destx,
int16_t desty,
uint16_t destw,
uint16_t desth,
VARectangle *cliprects, /* client supplied clip list */
uint32_t number_cliprects, /* number of clip rects in the clip list */
uint32_t flags /* de-interlacing flags */
)
{
PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable, "nullptr X11FuncTable", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable->pfnXCreateGC, "nullptr pfnXCreateGC", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable->pfnXFreeGC, "nullptr pfnXFreeGC", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable->pfnXCreateImage, "nullptr pfnXCreateImage", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable->pfnXDestroyImage, "nullptr pfnXDestroyImage", VA_STATUS_ERROR_INVALID_CONTEXT);
DDI_CHK_NULL(mediaCtx->X11FuncTable->pfnXPutImage, "nullptr pfnXPutImage", VA_STATUS_ERROR_INVALID_CONTEXT);
TypeXCreateGC pfn_XCreateGC = (TypeXCreateGC)(mediaCtx->X11FuncTable->pfnXCreateGC);
TypeXFreeGC pfn_XFreeGC = (TypeXFreeGC)(mediaCtx->X11FuncTable->pfnXFreeGC);
TypeXCreateImage pfn_XCreateImage = (TypeXCreateImage)(mediaCtx->X11FuncTable->pfnXCreateImage);
TypeXDestroyImage pfn_XDestroyImage = (TypeXDestroyImage)(mediaCtx->X11FuncTable->pfnXDestroyImage);
TypeXPutImage pfn_XPutImage = (TypeXPutImage)(mediaCtx->X11FuncTable->pfnXPutImage);
DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
uint16_t width = 0;
if (srcw <= destw)
width = srcw;
else
width = destw;
uint16_t height = 0;
if (srch <= desth)
height = srch;
else
height = desth;
int32_t pitch = mediaSurface->iPitch;
uint32_t adjustU = 1;
uint32_t adjustD = 1;
switch(mediaSurface->format)
{
case Media_Format_422H:
case Media_Format_444P:
case Media_Format_411P:
adjustU = 3;
adjustD = 1;
break;
case Media_Format_400P:
adjustU = 1;
adjustD = 1;
break;
case Media_Format_422V:
case Media_Format_IMC3:
adjustU = 2;
adjustD = 1;
break;
case Media_Format_NV12:
adjustU = 3;
adjustD = 2;
break;
default:
DDI_ASSERTMESSAGE("Color Format is not supported: %d",mediaSurface->format);
return VA_STATUS_ERROR_INVALID_VALUE;
}
uint32_t surfaceSize = pitch * mediaSurface->iHeight * adjustU / adjustD;
uint8_t *dispTempBuffer = (uint8_t *)malloc(surfaceSize);
if (dispTempBuffer == nullptr)
{
DdiMediaUtil_UnlockSurface(mediaSurface);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
uint8_t *umdContextY = dispTempBuffer;
uint8_t *ptr = (uint8_t*)DdiMediaUtil_LockSurface(mediaSurface, (MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY));
MOS_STATUS eStatus = MOS_SecureMemcpy(umdContextY, surfaceSize, ptr, surfaceSize);
DDI_CHK_CONDITION((eStatus != MOS_STATUS_SUCCESS), "DDI:Failed to copy surface buffer data!", VA_STATUS_ERROR_OPERATION_FAILED);
Visual *visual = DefaultVisual(ctx->native_dpy, ctx->x11_screen);
GC gc = (*pfn_XCreateGC)((Display*)ctx->native_dpy, (Drawable)draw, 0, nullptr);
if (TrueColor != visual->c_class)
{
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
MOS_FreeMemory(dispTempBuffer);
return VA_STATUS_ERROR_UNKNOWN;
}
unsigned long rmask = visual->red_mask;
unsigned long gmask = visual->green_mask;
unsigned long bmask = visual->blue_mask;
unsigned long rshift = DdiMedia_mask2shift(rmask);
unsigned long gshift = DdiMedia_mask2shift(gmask);
unsigned long bshift = DdiMedia_mask2shift(bmask);
int32_t depth = DefaultDepth(ctx->native_dpy, ctx->x11_screen);
XImage *ximg = (*pfn_XCreateImage)((Display*)ctx->native_dpy, visual, depth, ZPixmap, 0, nullptr,width, height, 32, 0 );
if (ximg == nullptr)
{
MOS_FreeMemory(dispTempBuffer);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
if (ximg->bits_per_pixel != 32)
{
(*pfn_XDestroyImage)(ximg);
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
MOS_FreeMemory(dispTempBuffer);
return VA_STATUS_ERROR_UNKNOWN;
}
ximg->data = (char *) malloc(ximg->bytes_per_line * MOS_ALIGN_CEIL(height, 2)); // If height is odd, need to add it by one for we process two lines per iteration
if (nullptr == ximg->data)
{
(*pfn_XDestroyImage)(ximg);
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
MOS_FreeMemory(dispTempBuffer);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
int32_t x = 0;
int32_t y = 0;
uint8_t *srcY = nullptr;
uint8_t *srcU = nullptr;
uint8_t *srcV = nullptr;
uint32_t *pixel = nullptr;
int32_t y1 = 0, y2 = 0, y3 = 0, y4 = 0, u1 = 0, v1 = 0;
switch(mediaSurface->format)
{
case Media_Format_444P:
YUV_444P_TO_ARGB();
break;
case Media_Format_422H:
YUV_422H_TO_ARGB();
break;
case Media_Format_422V:
YUV_422V_TO_ARGB();
break;
case Media_Format_IMC3:
YUV_IMC3_TO_ARGB();
break;
case Media_Format_411P:
YUV_411P_TO_ARGB();
break;
case Media_Format_400P:
YUV_400P_TO_ARGB();
break;
case Media_Format_NV12:
YUV_NV12_TO_ARGB();
break;
default:
DDI_ASSERTMESSAGE("Color Format is not supported: %d", mediaSurface->format);
}
DdiMediaUtil_UnlockSurface(mediaSurface);
(*pfn_XPutImage)((Display*)ctx->native_dpy,(Drawable)draw, gc, ximg, 0, 0, destx, desty, destw, desth);
if (ximg != nullptr)
{
(*pfn_XDestroyImage)(ximg);
}
(*pfn_XFreeGC)((Display*)ctx->native_dpy, gc);
MOS_FreeMemory(dispTempBuffer);
return VA_STATUS_SUCCESS;
}
VAStatus DdiMedia_PutSurfaceDummy(
VADriverContextP ctx,
VASurfaceID surface,
void *draw, /* Drawable of window system */
int16_t srcx,
int16_t srcy,
uint16_t srcw,
uint16_t srch,
int16_t destx,
int16_t desty,
uint16_t destw,
uint16_t desth,
VARectangle *cliprects, /* client supplied clip list */
uint32_t number_cliprects, /* number of clip rects in the clip list */
uint32_t flags /* de-interlacing flags */
)
{
return VA_STATUS_ERROR_UNIMPLEMENTED;
}
#endif
|