xf86dri.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /**************************************************************************
  2. Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
  3. Copyright 2000 VA Linux Systems, Inc.
  4. All Rights Reserved.
  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. The above copyright notice and this permission notice (including the
  13. next paragraph) shall be included in all copies or substantial portions
  14. of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  18. IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
  19. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. **************************************************************************/
  23. /*
  24. * Authors:
  25. * Kevin E. Martin <martin@valinux.com>
  26. * Jens Owen <jens@tungstengraphics.com>
  27. * Rickard E. (Rik) Faith <faith@valinux.com>
  28. *
  29. */
  30. #ifdef HAVE_XORG_CONFIG_H
  31. #include <xorg-config.h>
  32. #endif
  33. #include <string.h>
  34. #include "xf86.h"
  35. #include <X11/X.h>
  36. #include <X11/Xproto.h>
  37. #include "misc.h"
  38. #include "dixstruct.h"
  39. #include "extnsionst.h"
  40. #include "extinit.h"
  41. #include "colormapst.h"
  42. #include "cursorstr.h"
  43. #include "scrnintstr.h"
  44. #include "servermd.h"
  45. #define _XF86DRI_SERVER_
  46. #include <X11/dri/xf86driproto.h>
  47. #include "swaprep.h"
  48. #include "xf86str.h"
  49. #include "dri.h"
  50. #include "sarea.h"
  51. #include "dristruct.h"
  52. #include "xf86drm.h"
  53. #include "protocol-versions.h"
  54. #include "xf86Extensions.h"
  55. static int DRIErrorBase;
  56. static void XF86DRIResetProc(ExtensionEntry *extEntry);
  57. static unsigned char DRIReqCode = 0;
  58. /*ARGSUSED*/
  59. static void
  60. XF86DRIResetProc(ExtensionEntry *extEntry)
  61. {
  62. DRIReset();
  63. }
  64. static int
  65. ProcXF86DRIQueryVersion(register ClientPtr client)
  66. {
  67. xXF86DRIQueryVersionReply rep = {
  68. .type = X_Reply,
  69. .sequenceNumber = client->sequence,
  70. .length = 0,
  71. .majorVersion = SERVER_XF86DRI_MAJOR_VERSION,
  72. .minorVersion = SERVER_XF86DRI_MINOR_VERSION,
  73. .patchVersion = SERVER_XF86DRI_PATCH_VERSION
  74. };
  75. REQUEST_SIZE_MATCH(xXF86DRIQueryVersionReq);
  76. if (client->swapped) {
  77. swaps(&rep.sequenceNumber);
  78. swapl(&rep.length);
  79. swaps(&rep.majorVersion);
  80. swaps(&rep.minorVersion);
  81. swapl(&rep.patchVersion);
  82. }
  83. WriteToClient(client, sizeof(xXF86DRIQueryVersionReply), &rep);
  84. return Success;
  85. }
  86. static int
  87. ProcXF86DRIQueryDirectRenderingCapable(register ClientPtr client)
  88. {
  89. xXF86DRIQueryDirectRenderingCapableReply rep;
  90. Bool isCapable;
  91. REQUEST(xXF86DRIQueryDirectRenderingCapableReq);
  92. REQUEST_SIZE_MATCH(xXF86DRIQueryDirectRenderingCapableReq);
  93. if (stuff->screen >= screenInfo.numScreens) {
  94. client->errorValue = stuff->screen;
  95. return BadValue;
  96. }
  97. if (!DRIQueryDirectRenderingCapable(screenInfo.screens[stuff->screen],
  98. &isCapable)) {
  99. return BadValue;
  100. }
  101. if (!client->local || client->swapped)
  102. isCapable = 0;
  103. rep = (xXF86DRIQueryDirectRenderingCapableReply) {
  104. .type = X_Reply,
  105. .sequenceNumber = client->sequence,
  106. .length = 0,
  107. .isCapable = isCapable
  108. };
  109. if (client->swapped) {
  110. swaps(&rep.sequenceNumber);
  111. swapl(&rep.length);
  112. }
  113. WriteToClient(client,
  114. sizeof(xXF86DRIQueryDirectRenderingCapableReply),
  115. &rep);
  116. return Success;
  117. }
  118. static int
  119. ProcXF86DRIOpenConnection(register ClientPtr client)
  120. {
  121. xXF86DRIOpenConnectionReply rep;
  122. drm_handle_t hSAREA;
  123. char *busIdString;
  124. CARD32 busIdStringLength = 0;
  125. REQUEST(xXF86DRIOpenConnectionReq);
  126. REQUEST_SIZE_MATCH(xXF86DRIOpenConnectionReq);
  127. if (stuff->screen >= screenInfo.numScreens) {
  128. client->errorValue = stuff->screen;
  129. return BadValue;
  130. }
  131. if (!DRIOpenConnection(screenInfo.screens[stuff->screen],
  132. &hSAREA, &busIdString)) {
  133. return BadValue;
  134. }
  135. if (busIdString)
  136. busIdStringLength = strlen(busIdString);
  137. rep = (xXF86DRIOpenConnectionReply) {
  138. .type = X_Reply,
  139. .sequenceNumber = client->sequence,
  140. .length = bytes_to_int32(SIZEOF(xXF86DRIOpenConnectionReply) -
  141. SIZEOF(xGenericReply) +
  142. pad_to_int32(busIdStringLength)),
  143. .busIdStringLength = busIdStringLength,
  144. .hSAREALow = (CARD32) (hSAREA & 0xffffffff),
  145. #if defined(LONG64) && !defined(__linux__)
  146. .hSAREAHigh = (CARD32) (hSAREA >> 32),
  147. #else
  148. .hSAREAHigh = 0
  149. #endif
  150. };
  151. WriteToClient(client, sizeof(xXF86DRIOpenConnectionReply), &rep);
  152. if (busIdStringLength)
  153. WriteToClient(client, busIdStringLength, busIdString);
  154. return Success;
  155. }
  156. static int
  157. ProcXF86DRIAuthConnection(register ClientPtr client)
  158. {
  159. xXF86DRIAuthConnectionReply rep = {
  160. .type = X_Reply,
  161. .sequenceNumber = client->sequence,
  162. .length = 0,
  163. .authenticated = 1
  164. };
  165. REQUEST(xXF86DRIAuthConnectionReq);
  166. REQUEST_SIZE_MATCH(xXF86DRIAuthConnectionReq);
  167. if (stuff->screen >= screenInfo.numScreens) {
  168. client->errorValue = stuff->screen;
  169. return BadValue;
  170. }
  171. if (!DRIAuthConnection(screenInfo.screens[stuff->screen], stuff->magic)) {
  172. ErrorF("Failed to authenticate %lu\n", (unsigned long) stuff->magic);
  173. rep.authenticated = 0;
  174. }
  175. WriteToClient(client, sizeof(xXF86DRIAuthConnectionReply), &rep);
  176. return Success;
  177. }
  178. static int
  179. ProcXF86DRICloseConnection(register ClientPtr client)
  180. {
  181. REQUEST(xXF86DRICloseConnectionReq);
  182. REQUEST_SIZE_MATCH(xXF86DRICloseConnectionReq);
  183. if (stuff->screen >= screenInfo.numScreens) {
  184. client->errorValue = stuff->screen;
  185. return BadValue;
  186. }
  187. DRICloseConnection(screenInfo.screens[stuff->screen]);
  188. return Success;
  189. }
  190. static int
  191. ProcXF86DRIGetClientDriverName(register ClientPtr client)
  192. {
  193. xXF86DRIGetClientDriverNameReply rep = {
  194. .type = X_Reply,
  195. .sequenceNumber = client->sequence,
  196. .clientDriverNameLength = 0
  197. };
  198. char *clientDriverName;
  199. REQUEST(xXF86DRIGetClientDriverNameReq);
  200. REQUEST_SIZE_MATCH(xXF86DRIGetClientDriverNameReq);
  201. if (stuff->screen >= screenInfo.numScreens) {
  202. client->errorValue = stuff->screen;
  203. return BadValue;
  204. }
  205. DRIGetClientDriverName(screenInfo.screens[stuff->screen],
  206. (int *) &rep.ddxDriverMajorVersion,
  207. (int *) &rep.ddxDriverMinorVersion,
  208. (int *) &rep.ddxDriverPatchVersion,
  209. &clientDriverName);
  210. if (clientDriverName)
  211. rep.clientDriverNameLength = strlen(clientDriverName);
  212. rep.length = bytes_to_int32(SIZEOF(xXF86DRIGetClientDriverNameReply) -
  213. SIZEOF(xGenericReply) +
  214. pad_to_int32(rep.clientDriverNameLength));
  215. WriteToClient(client, sizeof(xXF86DRIGetClientDriverNameReply), &rep);
  216. if (rep.clientDriverNameLength)
  217. WriteToClient(client, rep.clientDriverNameLength, clientDriverName);
  218. return Success;
  219. }
  220. static int
  221. ProcXF86DRICreateContext(register ClientPtr client)
  222. {
  223. xXF86DRICreateContextReply rep = {
  224. .type = X_Reply,
  225. .sequenceNumber = client->sequence,
  226. .length = 0
  227. };
  228. ScreenPtr pScreen;
  229. REQUEST(xXF86DRICreateContextReq);
  230. REQUEST_SIZE_MATCH(xXF86DRICreateContextReq);
  231. if (stuff->screen >= screenInfo.numScreens) {
  232. client->errorValue = stuff->screen;
  233. return BadValue;
  234. }
  235. pScreen = screenInfo.screens[stuff->screen];
  236. if (!DRICreateContext(pScreen,
  237. NULL,
  238. stuff->context, (drm_context_t *) &rep.hHWContext)) {
  239. return BadValue;
  240. }
  241. WriteToClient(client, sizeof(xXF86DRICreateContextReply), &rep);
  242. return Success;
  243. }
  244. static int
  245. ProcXF86DRIDestroyContext(register ClientPtr client)
  246. {
  247. REQUEST(xXF86DRIDestroyContextReq);
  248. REQUEST_SIZE_MATCH(xXF86DRIDestroyContextReq);
  249. if (stuff->screen >= screenInfo.numScreens) {
  250. client->errorValue = stuff->screen;
  251. return BadValue;
  252. }
  253. if (!DRIDestroyContext(screenInfo.screens[stuff->screen], stuff->context)) {
  254. return BadValue;
  255. }
  256. return Success;
  257. }
  258. static int
  259. ProcXF86DRICreateDrawable(ClientPtr client)
  260. {
  261. xXF86DRICreateDrawableReply rep = {
  262. .type = X_Reply,
  263. .sequenceNumber = client->sequence,
  264. .length = 0
  265. };
  266. DrawablePtr pDrawable;
  267. int rc;
  268. REQUEST(xXF86DRICreateDrawableReq);
  269. REQUEST_SIZE_MATCH(xXF86DRICreateDrawableReq);
  270. if (stuff->screen >= screenInfo.numScreens) {
  271. client->errorValue = stuff->screen;
  272. return BadValue;
  273. }
  274. rc = dixLookupDrawable(&pDrawable, stuff->drawable, client, 0,
  275. DixReadAccess);
  276. if (rc != Success)
  277. return rc;
  278. if (!DRICreateDrawable(screenInfo.screens[stuff->screen], client,
  279. pDrawable, (drm_drawable_t *) &rep.hHWDrawable)) {
  280. return BadValue;
  281. }
  282. WriteToClient(client, sizeof(xXF86DRICreateDrawableReply), &rep);
  283. return Success;
  284. }
  285. static int
  286. ProcXF86DRIDestroyDrawable(register ClientPtr client)
  287. {
  288. REQUEST(xXF86DRIDestroyDrawableReq);
  289. DrawablePtr pDrawable;
  290. int rc;
  291. REQUEST_SIZE_MATCH(xXF86DRIDestroyDrawableReq);
  292. if (stuff->screen >= screenInfo.numScreens) {
  293. client->errorValue = stuff->screen;
  294. return BadValue;
  295. }
  296. rc = dixLookupDrawable(&pDrawable, stuff->drawable, client, 0,
  297. DixReadAccess);
  298. if (rc != Success)
  299. return rc;
  300. if (!DRIDestroyDrawable(screenInfo.screens[stuff->screen], client,
  301. pDrawable)) {
  302. return BadValue;
  303. }
  304. return Success;
  305. }
  306. static int
  307. ProcXF86DRIGetDrawableInfo(register ClientPtr client)
  308. {
  309. xXF86DRIGetDrawableInfoReply rep = {
  310. .type = X_Reply,
  311. .sequenceNumber = client->sequence,
  312. .length = 0
  313. };
  314. DrawablePtr pDrawable;
  315. int X, Y, W, H;
  316. drm_clip_rect_t *pClipRects, *pClippedRects;
  317. drm_clip_rect_t *pBackClipRects;
  318. int backX, backY, rc;
  319. REQUEST(xXF86DRIGetDrawableInfoReq);
  320. REQUEST_SIZE_MATCH(xXF86DRIGetDrawableInfoReq);
  321. if (stuff->screen >= screenInfo.numScreens) {
  322. client->errorValue = stuff->screen;
  323. return BadValue;
  324. }
  325. rc = dixLookupDrawable(&pDrawable, stuff->drawable, client, 0,
  326. DixReadAccess);
  327. if (rc != Success)
  328. return rc;
  329. if (!DRIGetDrawableInfo(screenInfo.screens[stuff->screen],
  330. pDrawable,
  331. (unsigned int *) &rep.drawableTableIndex,
  332. (unsigned int *) &rep.drawableTableStamp,
  333. (int *) &X,
  334. (int *) &Y,
  335. (int *) &W,
  336. (int *) &H,
  337. (int *) &rep.numClipRects,
  338. &pClipRects,
  339. &backX,
  340. &backY,
  341. (int *) &rep.numBackClipRects, &pBackClipRects)) {
  342. return BadValue;
  343. }
  344. rep.drawableX = X;
  345. rep.drawableY = Y;
  346. rep.drawableWidth = W;
  347. rep.drawableHeight = H;
  348. rep.length = (SIZEOF(xXF86DRIGetDrawableInfoReply) - SIZEOF(xGenericReply));
  349. rep.backX = backX;
  350. rep.backY = backY;
  351. if (rep.numBackClipRects)
  352. rep.length += sizeof(drm_clip_rect_t) * rep.numBackClipRects;
  353. pClippedRects = pClipRects;
  354. if (rep.numClipRects) {
  355. /* Clip cliprects to screen dimensions (redirected windows) */
  356. pClippedRects = malloc(rep.numClipRects * sizeof(drm_clip_rect_t));
  357. if (pClippedRects) {
  358. ScreenPtr pScreen = screenInfo.screens[stuff->screen];
  359. int i, j;
  360. for (i = 0, j = 0; i < rep.numClipRects; i++) {
  361. pClippedRects[j].x1 = max(pClipRects[i].x1, 0);
  362. pClippedRects[j].y1 = max(pClipRects[i].y1, 0);
  363. pClippedRects[j].x2 = min(pClipRects[i].x2, pScreen->width);
  364. pClippedRects[j].y2 = min(pClipRects[i].y2, pScreen->height);
  365. if (pClippedRects[j].x1 < pClippedRects[j].x2 &&
  366. pClippedRects[j].y1 < pClippedRects[j].y2) {
  367. j++;
  368. }
  369. }
  370. rep.numClipRects = j;
  371. }
  372. else {
  373. rep.numClipRects = 0;
  374. }
  375. rep.length += sizeof(drm_clip_rect_t) * rep.numClipRects;
  376. }
  377. rep.length = bytes_to_int32(rep.length);
  378. WriteToClient(client, sizeof(xXF86DRIGetDrawableInfoReply), &rep);
  379. if (rep.numClipRects) {
  380. WriteToClient(client,
  381. sizeof(drm_clip_rect_t) * rep.numClipRects,
  382. pClippedRects);
  383. free(pClippedRects);
  384. }
  385. if (rep.numBackClipRects) {
  386. WriteToClient(client,
  387. sizeof(drm_clip_rect_t) * rep.numBackClipRects,
  388. pBackClipRects);
  389. }
  390. return Success;
  391. }
  392. static int
  393. ProcXF86DRIGetDeviceInfo(register ClientPtr client)
  394. {
  395. xXF86DRIGetDeviceInfoReply rep = {
  396. .type = X_Reply,
  397. .sequenceNumber = client->sequence,
  398. .length = 0
  399. };
  400. drm_handle_t hFrameBuffer;
  401. void *pDevPrivate;
  402. REQUEST(xXF86DRIGetDeviceInfoReq);
  403. REQUEST_SIZE_MATCH(xXF86DRIGetDeviceInfoReq);
  404. if (stuff->screen >= screenInfo.numScreens) {
  405. client->errorValue = stuff->screen;
  406. return BadValue;
  407. }
  408. if (!DRIGetDeviceInfo(screenInfo.screens[stuff->screen],
  409. &hFrameBuffer,
  410. (int *) &rep.framebufferOrigin,
  411. (int *) &rep.framebufferSize,
  412. (int *) &rep.framebufferStride,
  413. (int *) &rep.devPrivateSize, &pDevPrivate)) {
  414. return BadValue;
  415. }
  416. rep.hFrameBufferLow = (CARD32) (hFrameBuffer & 0xffffffff);
  417. #if defined(LONG64) && !defined(__linux__)
  418. rep.hFrameBufferHigh = (CARD32) (hFrameBuffer >> 32);
  419. #else
  420. rep.hFrameBufferHigh = 0;
  421. #endif
  422. if (rep.devPrivateSize) {
  423. rep.length = bytes_to_int32(SIZEOF(xXF86DRIGetDeviceInfoReply) -
  424. SIZEOF(xGenericReply) +
  425. pad_to_int32(rep.devPrivateSize));
  426. }
  427. WriteToClient(client, sizeof(xXF86DRIGetDeviceInfoReply), &rep);
  428. if (rep.length) {
  429. WriteToClient(client, rep.devPrivateSize, pDevPrivate);
  430. }
  431. return Success;
  432. }
  433. static int
  434. ProcXF86DRIDispatch(register ClientPtr client)
  435. {
  436. REQUEST(xReq);
  437. switch (stuff->data) {
  438. case X_XF86DRIQueryVersion:
  439. return ProcXF86DRIQueryVersion(client);
  440. case X_XF86DRIQueryDirectRenderingCapable:
  441. return ProcXF86DRIQueryDirectRenderingCapable(client);
  442. }
  443. if (!client->local)
  444. return DRIErrorBase + XF86DRIClientNotLocal;
  445. switch (stuff->data) {
  446. case X_XF86DRIOpenConnection:
  447. return ProcXF86DRIOpenConnection(client);
  448. case X_XF86DRICloseConnection:
  449. return ProcXF86DRICloseConnection(client);
  450. case X_XF86DRIGetClientDriverName:
  451. return ProcXF86DRIGetClientDriverName(client);
  452. case X_XF86DRICreateContext:
  453. return ProcXF86DRICreateContext(client);
  454. case X_XF86DRIDestroyContext:
  455. return ProcXF86DRIDestroyContext(client);
  456. case X_XF86DRICreateDrawable:
  457. return ProcXF86DRICreateDrawable(client);
  458. case X_XF86DRIDestroyDrawable:
  459. return ProcXF86DRIDestroyDrawable(client);
  460. case X_XF86DRIGetDrawableInfo:
  461. return ProcXF86DRIGetDrawableInfo(client);
  462. case X_XF86DRIGetDeviceInfo:
  463. return ProcXF86DRIGetDeviceInfo(client);
  464. case X_XF86DRIAuthConnection:
  465. return ProcXF86DRIAuthConnection(client);
  466. /* {Open,Close}FullScreen are deprecated now */
  467. default:
  468. return BadRequest;
  469. }
  470. }
  471. static int
  472. SProcXF86DRIQueryVersion(register ClientPtr client)
  473. {
  474. REQUEST(xXF86DRIQueryVersionReq);
  475. swaps(&stuff->length);
  476. return ProcXF86DRIQueryVersion(client);
  477. }
  478. static int
  479. SProcXF86DRIQueryDirectRenderingCapable(register ClientPtr client)
  480. {
  481. REQUEST(xXF86DRIQueryDirectRenderingCapableReq);
  482. swaps(&stuff->length);
  483. swapl(&stuff->screen);
  484. return ProcXF86DRIQueryDirectRenderingCapable(client);
  485. }
  486. static int
  487. SProcXF86DRIDispatch(register ClientPtr client)
  488. {
  489. REQUEST(xReq);
  490. /*
  491. * Only local clients are allowed DRI access, but remote clients still need
  492. * these requests to find out cleanly.
  493. */
  494. switch (stuff->data) {
  495. case X_XF86DRIQueryVersion:
  496. return SProcXF86DRIQueryVersion(client);
  497. case X_XF86DRIQueryDirectRenderingCapable:
  498. return SProcXF86DRIQueryDirectRenderingCapable(client);
  499. default:
  500. return DRIErrorBase + XF86DRIClientNotLocal;
  501. }
  502. }
  503. void
  504. XFree86DRIExtensionInit(void)
  505. {
  506. ExtensionEntry *extEntry;
  507. #ifdef XF86DRI_EVENTS
  508. EventType = CreateNewResourceType(XF86DRIFreeEvents, "DRIEvent");
  509. #endif
  510. if (DRIExtensionInit() &&
  511. #ifdef XF86DRI_EVENTS
  512. EventType && ScreenPrivateIndex != -1 &&
  513. #endif
  514. (extEntry = AddExtension(XF86DRINAME,
  515. XF86DRINumberEvents,
  516. XF86DRINumberErrors,
  517. ProcXF86DRIDispatch,
  518. SProcXF86DRIDispatch,
  519. XF86DRIResetProc, StandardMinorOpcode))) {
  520. DRIReqCode = (unsigned char) extEntry->base;
  521. DRIErrorBase = extEntry->errorBase;
  522. }
  523. }