Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2008 Red Hat, Inc.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Soft-
  6.  * ware"), to deal in the Software without restriction, including without
  7.  * limitation the rights to use, copy, modify, merge, publish, distribute,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, provided that the above copyright
  10.  * notice(s) and this permission notice appear in all copies of the Soft-
  11.  * ware and that both the above copyright notice(s) and this permission
  12.  * notice appear in supporting documentation.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16.  * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
  17.  * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
  18.  * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
  19.  * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  20.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  21.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
  22.  * MANCE OF THIS SOFTWARE.
  23.  *
  24.  * Except as contained in this notice, the name of a copyright holder shall
  25.  * not be used in advertising or otherwise to promote the sale, use or
  26.  * other dealings in this Software without prior written authorization of
  27.  * the copyright holder.
  28.  *
  29.  * Authors:
  30.  *   Kristian Høgsberg (krh@redhat.com)
  31.  */
  32.  
  33.  
  34. #define NEED_REPLIES
  35. #include <X11/Xlibint.h>
  36. #include <X11/extensions/Xext.h>
  37. #include <X11/extensions/extutil.h>
  38. #include "xf86drm.h"
  39. #include "va_dri2.h"
  40. #include "va_dri2str.h"
  41. #include "va_dri2tokens.h"
  42.  
  43. #ifndef DRI2DriverDRI
  44. #define DRI2DriverDRI 0
  45. #endif
  46.  
  47. static int
  48. VA_DRI2Error(Display *dpy, xError *err, XExtCodes *codes, int *ret_code);
  49.  
  50. static VA_DRI2Buffer *
  51. VA_DRI2GetBuffers_internal(XExtDisplayInfo *info,
  52.                            Display *dpy, XID drawable,
  53.                            int *width, int *height,
  54.                            unsigned int *attachments,
  55.                            int count,
  56.                            int *outCount);
  57.  
  58. static char va_dri2ExtensionName[] = DRI2_NAME;
  59. static XExtensionInfo _va_dri2_info_data;
  60. static XExtensionInfo *va_dri2Info = &_va_dri2_info_data;
  61. static XEXT_GENERATE_CLOSE_DISPLAY (VA_DRI2CloseDisplay, va_dri2Info)
  62. static /* const */ XExtensionHooks va_dri2ExtensionHooks = {
  63.     NULL,                               /* create_gc */
  64.     NULL,                               /* copy_gc */
  65.     NULL,                               /* flush_gc */
  66.     NULL,                               /* free_gc */
  67.     NULL,                               /* create_font */
  68.     NULL,                               /* free_font */
  69.     VA_DRI2CloseDisplay,                /* close_display */
  70.     NULL,                               /* wire_to_event */
  71.     NULL,                               /* event_to_wire */
  72.     VA_DRI2Error,                       /* error */
  73.     NULL,                               /* error_string */
  74. };
  75.  
  76. static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay, va_dri2Info,
  77.                                    va_dri2ExtensionName,
  78.                                    &va_dri2ExtensionHooks,
  79.                                    0, NULL)
  80.  
  81. static CARD32 _va_resource_x_error_drawable = 0;
  82. static Bool   _va_resource_x_error_matched = False;
  83.  
  84. #define VA_EnterResourceError(drawable)                 \
  85.   do {                                                  \
  86.     _va_resource_x_error_drawable = (drawable);         \
  87.     _va_resource_x_error_matched = False;               \
  88.   } while (0)
  89.  
  90. #define VA_LeaveResourceError()                 \
  91.   do {                                          \
  92.     _va_resource_x_error_drawable = 0;          \
  93.   } while (0)
  94.  
  95. #define VA_ResourceErrorMatched()               \
  96.   (_va_resource_x_error_matched)
  97.  
  98. static int
  99. VA_DRI2Error(Display *dpy, xError *err, XExtCodes *codes, int *ret_code)
  100. {
  101.     if (_va_resource_x_error_drawable == err->resourceID) {
  102.       _va_resource_x_error_matched = True;
  103.       return True;
  104.     }
  105.  
  106.     return False;
  107. }
  108.  
  109. Bool VA_DRI2QueryExtension(Display *dpy, int *eventBase, int *errorBase)
  110. {
  111.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  112.  
  113.     if (XextHasExtension(info)) {
  114.         *eventBase = info->codes->first_event;
  115.         *errorBase = info->codes->first_error;
  116.         return True;
  117.     }
  118.  
  119.     return False;
  120. }
  121.  
  122. Bool VA_DRI2QueryVersion(Display *dpy, int *major, int *minor)
  123. {
  124.     XExtDisplayInfo *info = DRI2FindDisplay (dpy);
  125.     xDRI2QueryVersionReply rep;
  126.     xDRI2QueryVersionReq *req;
  127.  
  128.     XextCheckExtension (dpy, info, va_dri2ExtensionName, False);
  129.  
  130.     LockDisplay(dpy);
  131.     GetReq(DRI2QueryVersion, req);
  132.     req->reqType = info->codes->major_opcode;
  133.     req->dri2ReqType = X_DRI2QueryVersion;
  134.     req->majorVersion = DRI2_MAJOR;
  135.     req->minorVersion = DRI2_MINOR;
  136.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  137.         UnlockDisplay(dpy);
  138.         SyncHandle();
  139.         return False;
  140.     }
  141.     *major = rep.majorVersion;
  142.     *minor = rep.minorVersion;
  143.     UnlockDisplay(dpy);
  144.     SyncHandle();
  145.  
  146.     return True;
  147. }
  148.  
  149. Bool VA_DRI2Connect(Display *dpy, XID window,
  150.                  char **driverName, char **deviceName)
  151. {
  152.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  153.     xDRI2ConnectReply rep;
  154.     xDRI2ConnectReq *req;
  155.  
  156.     XextCheckExtension (dpy, info, va_dri2ExtensionName, False);
  157.  
  158.     LockDisplay(dpy);
  159.     GetReq(DRI2Connect, req);
  160.     req->reqType = info->codes->major_opcode;
  161.     req->dri2ReqType = X_DRI2Connect;
  162.     req->window = window;
  163.     req->driverType = DRI2DriverDRI;
  164.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  165.         UnlockDisplay(dpy);
  166.         SyncHandle();
  167.         return False;
  168.     }
  169.  
  170.     if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
  171.         UnlockDisplay(dpy);
  172.         SyncHandle();
  173.         return False;
  174.     }
  175.  
  176.     *driverName = Xmalloc(rep.driverNameLength + 1);
  177.     if (*driverName == NULL) {
  178.         _XEatData(dpy,
  179.                   ((rep.driverNameLength + 3) & ~3) +
  180.                   ((rep.deviceNameLength + 3) & ~3));
  181.         UnlockDisplay(dpy);
  182.         SyncHandle();
  183.         return False;
  184.     }
  185.     _XReadPad(dpy, *driverName, rep.driverNameLength);
  186.     (*driverName)[rep.driverNameLength] = '\0';
  187.  
  188.     *deviceName = Xmalloc(rep.deviceNameLength + 1);
  189.     if (*deviceName == NULL) {
  190.         Xfree(*driverName);
  191.         _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
  192.         UnlockDisplay(dpy);
  193.         SyncHandle();
  194.         return False;
  195.     }
  196.     _XReadPad(dpy, *deviceName, rep.deviceNameLength);
  197.     (*deviceName)[rep.deviceNameLength] = '\0';
  198.  
  199.     UnlockDisplay(dpy);
  200.     SyncHandle();
  201.  
  202.     return True;
  203. }
  204.  
  205. Bool VA_DRI2Authenticate(Display *dpy, XID window, drm_magic_t magic)
  206. {
  207.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  208.     xDRI2AuthenticateReq *req;
  209.     xDRI2AuthenticateReply rep;
  210.  
  211.     XextCheckExtension (dpy, info, va_dri2ExtensionName, False);
  212.  
  213.     LockDisplay(dpy);
  214.     GetReq(DRI2Authenticate, req);
  215.     req->reqType = info->codes->major_opcode;
  216.     req->dri2ReqType = X_DRI2Authenticate;
  217.     req->window = window;
  218.     req->magic = magic;
  219.  
  220.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  221.         UnlockDisplay(dpy);
  222.         SyncHandle();
  223.         return False;
  224.     }
  225.  
  226.     UnlockDisplay(dpy);
  227.     SyncHandle();
  228.  
  229.     return rep.authenticated;
  230. }
  231.  
  232. void VA_DRI2CreateDrawable(Display *dpy, XID drawable)
  233. {
  234.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  235.     xDRI2CreateDrawableReq *req;
  236.  
  237.     XextSimpleCheckExtension (dpy, info, va_dri2ExtensionName);
  238.  
  239.     LockDisplay(dpy);
  240.     GetReq(DRI2CreateDrawable, req);
  241.     req->reqType = info->codes->major_opcode;
  242.     req->dri2ReqType = X_DRI2CreateDrawable;
  243.     req->drawable = drawable;
  244.     UnlockDisplay(dpy);
  245.     SyncHandle();
  246. }
  247.  
  248. void VA_DRI2DestroyDrawable(Display *dpy, XID drawable)
  249. {
  250.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  251.     xDRI2DestroyDrawableReq *req;
  252.     unsigned int attachement = 0; // FRONT_LEFT
  253.     VA_DRI2Buffer *buffers;
  254.  
  255.     XextSimpleCheckExtension (dpy, info, va_dri2ExtensionName);
  256.  
  257.     XSync(dpy, False);
  258.  
  259.     LockDisplay(dpy);
  260.     /*
  261.      * We have no way of catching DRI2DestroyDrawable errors because
  262.      * this message doesn't have a defined answer. So we test whether
  263.      * the drawable is still alive by sending DRIGetBuffers first and
  264.      * checking whether we get an error.
  265.      */
  266.     VA_EnterResourceError(drawable);
  267.  
  268.     buffers = VA_DRI2GetBuffers_internal(info, dpy, drawable,
  269.                                          NULL, NULL,
  270.                                          &attachement, 1, NULL);
  271.     VA_LeaveResourceError();
  272.     if (buffers)
  273.       XFree(buffers);
  274.     if (VA_ResourceErrorMatched()) {
  275.       UnlockDisplay(dpy);
  276.       SyncHandle();
  277.       return;
  278.     }
  279.  
  280.     GetReq(DRI2DestroyDrawable, req);
  281.     req->reqType = info->codes->major_opcode;
  282.     req->dri2ReqType = X_DRI2DestroyDrawable;
  283.     req->drawable = drawable;
  284.     UnlockDisplay(dpy);
  285.     SyncHandle();
  286. }
  287.  
  288. VA_DRI2Buffer *VA_DRI2GetBuffers_internal(XExtDisplayInfo *info,
  289.                                           Display *dpy, XID drawable,
  290.                                           int *width, int *height,
  291.                                           unsigned int *attachments, int count,
  292.                                           int *outCount)
  293. {
  294.     xDRI2GetBuffersReply rep;
  295.     xDRI2GetBuffersReq *req;
  296.     VA_DRI2Buffer *buffers;
  297.     xDRI2Buffer repBuffer;
  298.     CARD32 *p;
  299.     int i;
  300.  
  301.     GetReqExtra(DRI2GetBuffers, count * 4, req);
  302.     req->reqType = info->codes->major_opcode;
  303.     req->dri2ReqType = X_DRI2GetBuffers;
  304.     req->drawable = drawable;
  305.     req->count = count;
  306.     p = (CARD32 *) &req[1];
  307.     for (i = 0; i < count; i++)
  308.         p[i] = attachments[i];
  309.  
  310.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  311.         return NULL;
  312.     }
  313.  
  314.     if (width)
  315.       *width = rep.width;
  316.     if (height)
  317.       *height = rep.height;
  318.     if (outCount)
  319.       *outCount = rep.count;
  320.  
  321.     buffers = Xmalloc(rep.count * sizeof buffers[0]);
  322.     if (buffers == NULL) {
  323.         _XEatData(dpy, rep.count * sizeof repBuffer);
  324.         return NULL;
  325.     }
  326.  
  327.     for (i = 0; i < rep.count; i++) {
  328.         _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
  329.         buffers[i].attachment = repBuffer.attachment;
  330.         buffers[i].name = repBuffer.name;
  331.         buffers[i].pitch = repBuffer.pitch;
  332.         buffers[i].cpp = repBuffer.cpp;
  333.         buffers[i].flags = repBuffer.flags;
  334.     }
  335.  
  336.     return buffers;
  337. }
  338.  
  339. VA_DRI2Buffer *VA_DRI2GetBuffers(Display *dpy, XID drawable,
  340.                            int *width, int *height,
  341.                            unsigned int *attachments, int count,
  342.                            int *outCount)
  343. {
  344.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  345.     VA_DRI2Buffer *buffers;
  346.  
  347.     XextCheckExtension (dpy, info, va_dri2ExtensionName, False);
  348.  
  349.     LockDisplay(dpy);
  350.  
  351.     buffers = VA_DRI2GetBuffers_internal(info, dpy, drawable, width, height,
  352.                                          attachments, count, outCount);
  353.  
  354.     UnlockDisplay(dpy);
  355.     SyncHandle();
  356.  
  357.     return buffers;
  358. }
  359.  
  360. void VA_DRI2CopyRegion(Display *dpy, XID drawable, XserverRegion region,
  361.                     CARD32 dest, CARD32 src)
  362. {
  363.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  364.     xDRI2CopyRegionReq *req;
  365.     xDRI2CopyRegionReply rep;
  366.  
  367.     XextSimpleCheckExtension (dpy, info, va_dri2ExtensionName);
  368.  
  369.     LockDisplay(dpy);
  370.     GetReq(DRI2CopyRegion, req);
  371.     req->reqType = info->codes->major_opcode;
  372.     req->dri2ReqType = X_DRI2CopyRegion;
  373.     req->drawable = drawable;
  374.     req->region = region;
  375.     req->dest = dest;
  376.     req->src = src;
  377.  
  378.     _XReply(dpy, (xReply *)&rep, 0, xFalse);
  379.  
  380.     UnlockDisplay(dpy);
  381.     SyncHandle();
  382. }
  383.  
  384. static void
  385. load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
  386.               CARD64 remainder)
  387. {
  388.     req->target_msc_hi = target >> 32;
  389.     req->target_msc_lo = target & 0xffffffff;
  390.     req->divisor_hi = divisor >> 32;
  391.     req->divisor_lo = divisor & 0xffffffff;
  392.     req->remainder_hi = remainder >> 32;
  393.     req->remainder_lo = remainder & 0xffffffff;
  394. }
  395.  
  396. static CARD64
  397. vals_to_card64(CARD32 lo, CARD32 hi)
  398. {
  399.     return (CARD64)hi << 32 | lo;
  400. }
  401.  
  402. void VA_DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
  403.                         CARD64 divisor, CARD64 remainder, CARD64 *count)
  404. {
  405.     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
  406.     xDRI2SwapBuffersReq *req;
  407.     xDRI2SwapBuffersReply rep;
  408.  
  409.     XextSimpleCheckExtension (dpy, info, va_dri2ExtensionName);
  410.  
  411.     LockDisplay(dpy);
  412.     GetReq(DRI2SwapBuffers, req);
  413.     req->reqType = info->codes->major_opcode;
  414.     req->dri2ReqType = X_DRI2SwapBuffers;
  415.     req->drawable = drawable;
  416.     load_swap_req(req, target_msc, divisor, remainder);
  417.  
  418.     _XReply(dpy, (xReply *)&rep, 0, xFalse);
  419.  
  420.     *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
  421.  
  422.     UnlockDisplay(dpy);
  423.     SyncHandle();
  424. }
  425.