glxext.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. 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 "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included in all copies or substantial portions 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 MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. #ifdef HAVE_DIX_CONFIG_H
  31. #include <dix-config.h>
  32. #endif
  33. #include <string.h>
  34. #include "glxserver.h"
  35. #include <windowstr.h>
  36. #include <propertyst.h>
  37. #include <registry.h>
  38. #include "privates.h"
  39. #include <os.h>
  40. #include "extinit.h"
  41. #include "glx_extinit.h"
  42. #include "unpack.h"
  43. #include "glxutil.h"
  44. #include "glxext.h"
  45. #include "indirect_table.h"
  46. #include "indirect_util.h"
  47. /*
  48. ** X resources.
  49. */
  50. RESTYPE __glXContextRes;
  51. RESTYPE __glXDrawableRes;
  52. /*
  53. ** Reply for most singles.
  54. */
  55. xGLXSingleReply __glXReply;
  56. static DevPrivateKeyRec glxClientPrivateKeyRec;
  57. #define glxClientPrivateKey (&glxClientPrivateKeyRec)
  58. /*
  59. ** Forward declarations.
  60. */
  61. static int __glXDispatch(ClientPtr);
  62. /*
  63. ** Called when the extension is reset.
  64. */
  65. static void
  66. ResetExtension(ExtensionEntry * extEntry)
  67. {
  68. lastGLContext = NULL;
  69. }
  70. /*
  71. ** Reset state used to keep track of large (multi-request) commands.
  72. */
  73. void
  74. __glXResetLargeCommandStatus(__GLXclientState * cl)
  75. {
  76. cl->largeCmdBytesSoFar = 0;
  77. cl->largeCmdBytesTotal = 0;
  78. cl->largeCmdRequestsSoFar = 0;
  79. cl->largeCmdRequestsTotal = 0;
  80. }
  81. /*
  82. * This procedure is called when the client who created the context goes away
  83. * OR when glXDestroyContext is called. In either case, all we do is flag that
  84. * the ID is no longer valid, and (maybe) free the context.
  85. */
  86. static int
  87. ContextGone(__GLXcontext * cx, XID id)
  88. {
  89. cx->idExists = GL_FALSE;
  90. if (!cx->currentClient) {
  91. __glXFreeContext(cx);
  92. }
  93. return True;
  94. }
  95. static __GLXcontext *glxPendingDestroyContexts;
  96. static __GLXcontext *glxAllContexts;
  97. static int glxServerLeaveCount;
  98. static int glxBlockClients;
  99. /*
  100. ** Destroy routine that gets called when a drawable is freed. A drawable
  101. ** contains the ancillary buffers needed for rendering.
  102. */
  103. static Bool
  104. DrawableGone(__GLXdrawable * glxPriv, XID xid)
  105. {
  106. __GLXcontext *c, *next;
  107. if (glxPriv->type == GLX_DRAWABLE_WINDOW) {
  108. /* If this was created by glXCreateWindow, free the matching resource */
  109. if (glxPriv->drawId != glxPriv->pDraw->id) {
  110. if (xid == glxPriv->drawId)
  111. FreeResourceByType(glxPriv->pDraw->id, __glXDrawableRes, TRUE);
  112. else
  113. FreeResourceByType(glxPriv->drawId, __glXDrawableRes, TRUE);
  114. }
  115. /* otherwise this window was implicitly created by MakeCurrent */
  116. }
  117. for (c = glxAllContexts; c; c = next) {
  118. next = c->next;
  119. if (c->currentClient &&
  120. (c->drawPriv == glxPriv || c->readPriv == glxPriv)) {
  121. /* just force a re-bind the next time through */
  122. (*c->loseCurrent) (c);
  123. lastGLContext = NULL;
  124. }
  125. if (c->drawPriv == glxPriv)
  126. c->drawPriv = NULL;
  127. if (c->readPriv == glxPriv)
  128. c->readPriv = NULL;
  129. }
  130. /* drop our reference to any backing pixmap */
  131. if (glxPriv->type == GLX_DRAWABLE_PIXMAP)
  132. glxPriv->pDraw->pScreen->DestroyPixmap((PixmapPtr) glxPriv->pDraw);
  133. glxPriv->destroy(glxPriv);
  134. return True;
  135. }
  136. Bool
  137. __glXAddContext(__GLXcontext * cx)
  138. {
  139. /* Register this context as a resource.
  140. */
  141. if (!AddResource(cx->id, __glXContextRes, (void *)cx)) {
  142. return False;
  143. }
  144. cx->next = glxAllContexts;
  145. glxAllContexts = cx;
  146. return True;
  147. }
  148. static void
  149. __glXRemoveFromContextList(__GLXcontext * cx)
  150. {
  151. __GLXcontext *c, *prev;
  152. if (cx == glxAllContexts)
  153. glxAllContexts = cx->next;
  154. else {
  155. prev = glxAllContexts;
  156. for (c = glxAllContexts; c; c = c->next) {
  157. if (c == cx)
  158. prev->next = c->next;
  159. prev = c;
  160. }
  161. }
  162. }
  163. /*
  164. ** Free a context.
  165. */
  166. GLboolean
  167. __glXFreeContext(__GLXcontext * cx)
  168. {
  169. if (cx->idExists || cx->currentClient)
  170. return GL_FALSE;
  171. __glXRemoveFromContextList(cx);
  172. free(cx->feedbackBuf);
  173. free(cx->selectBuf);
  174. if (cx == lastGLContext) {
  175. lastGLContext = NULL;
  176. }
  177. /* We can get here through both regular dispatching from
  178. * __glXDispatch() or as a callback from the resource manager. In
  179. * the latter case we need to lift the DRI lock manually. */
  180. if (!glxBlockClients) {
  181. __glXleaveServer(GL_FALSE);
  182. cx->destroy(cx);
  183. __glXenterServer(GL_FALSE);
  184. }
  185. else {
  186. cx->next = glxPendingDestroyContexts;
  187. glxPendingDestroyContexts = cx;
  188. }
  189. return GL_TRUE;
  190. }
  191. /************************************************************************/
  192. /*
  193. ** These routines can be used to check whether a particular GL command
  194. ** has caused an error. Specifically, we use them to check whether a
  195. ** given query has caused an error, in which case a zero-length data
  196. ** reply is sent to the client.
  197. */
  198. static GLboolean errorOccured = GL_FALSE;
  199. /*
  200. ** The GL was will call this routine if an error occurs.
  201. */
  202. void
  203. __glXErrorCallBack(GLenum code)
  204. {
  205. errorOccured = GL_TRUE;
  206. }
  207. /*
  208. ** Clear the error flag before calling the GL command.
  209. */
  210. void
  211. __glXClearErrorOccured(void)
  212. {
  213. errorOccured = GL_FALSE;
  214. }
  215. /*
  216. ** Check if the GL command caused an error.
  217. */
  218. GLboolean
  219. __glXErrorOccured(void)
  220. {
  221. return errorOccured;
  222. }
  223. static int __glXErrorBase;
  224. int __glXEventBase;
  225. int
  226. __glXError(int error)
  227. {
  228. return __glXErrorBase + error;
  229. }
  230. __GLXclientState *
  231. glxGetClient(ClientPtr pClient)
  232. {
  233. return dixLookupPrivate(&pClient->devPrivates, glxClientPrivateKey);
  234. }
  235. static void
  236. glxClientCallback(CallbackListPtr *list, void *closure, void *data)
  237. {
  238. NewClientInfoRec *clientinfo = (NewClientInfoRec *) data;
  239. ClientPtr pClient = clientinfo->client;
  240. __GLXclientState *cl = glxGetClient(pClient);
  241. __GLXcontext *c, *next;
  242. switch (pClient->clientState) {
  243. case ClientStateRunning:
  244. cl->client = pClient;
  245. break;
  246. case ClientStateGone:
  247. /* detach from all current contexts */
  248. for (c = glxAllContexts; c; c = next) {
  249. next = c->next;
  250. if (c->currentClient == pClient) {
  251. c->loseCurrent(c);
  252. lastGLContext = NULL;
  253. c->currentClient = NULL;
  254. __glXFreeContext(c);
  255. }
  256. }
  257. free(cl->returnBuf);
  258. free(cl->largeCmdBuf);
  259. free(cl->GLClientextensions);
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. /************************************************************************/
  266. static __GLXprovider *__glXProviderStack;
  267. void
  268. GlxPushProvider(__GLXprovider * provider)
  269. {
  270. provider->next = __glXProviderStack;
  271. __glXProviderStack = provider;
  272. }
  273. static Bool
  274. checkScreenVisuals(void)
  275. {
  276. int i, j;
  277. for (i = 0; i < screenInfo.numScreens; i++) {
  278. ScreenPtr screen = screenInfo.screens[i];
  279. for (j = 0; j < screen->numVisuals; j++) {
  280. if (screen->visuals[j].class == TrueColor ||
  281. screen->visuals[j].class == DirectColor)
  282. return True;
  283. }
  284. }
  285. return False;
  286. }
  287. /*
  288. ** Initialize the GLX extension.
  289. */
  290. void
  291. GlxExtensionInit(void)
  292. {
  293. ExtensionEntry *extEntry;
  294. ScreenPtr pScreen;
  295. int i;
  296. __GLXprovider *p, **stack;
  297. Bool glx_provided = False;
  298. if (serverGeneration == 1) {
  299. for (stack = &__glXProviderStack; *stack; stack = &(*stack)->next)
  300. ;
  301. *stack = &__glXDRISWRastProvider;
  302. }
  303. /* Mesa requires at least one True/DirectColor visual */
  304. if (!checkScreenVisuals())
  305. return;
  306. __glXContextRes = CreateNewResourceType((DeleteType) ContextGone,
  307. "GLXContext");
  308. __glXDrawableRes = CreateNewResourceType((DeleteType) DrawableGone,
  309. "GLXDrawable");
  310. if (!__glXContextRes || !__glXDrawableRes)
  311. return;
  312. if (!dixRegisterPrivateKey
  313. (&glxClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(__GLXclientState)))
  314. return;
  315. if (!AddCallback(&ClientStateCallback, glxClientCallback, 0))
  316. return;
  317. for (i = 0; i < screenInfo.numScreens; i++) {
  318. pScreen = screenInfo.screens[i];
  319. for (p = __glXProviderStack; p != NULL; p = p->next) {
  320. __GLXscreen *glxScreen;
  321. glxScreen = p->screenProbe(pScreen);
  322. if (glxScreen != NULL) {
  323. if (glxScreen->GLXminor < glxMinorVersion)
  324. glxMinorVersion = glxScreen->GLXminor;
  325. LogMessage(X_INFO,
  326. "GLX: Initialized %s GL provider for screen %d\n",
  327. p->name, i);
  328. break;
  329. }
  330. }
  331. if (!p)
  332. LogMessage(X_INFO,
  333. "GLX: no usable GL providers found for screen %d\n", i);
  334. else
  335. glx_provided = True;
  336. }
  337. /* don't register extension if GL is not provided on any screen */
  338. if (!glx_provided)
  339. return;
  340. /*
  341. ** Add extension to server extensions.
  342. */
  343. extEntry = AddExtension(GLX_EXTENSION_NAME, __GLX_NUMBER_EVENTS,
  344. __GLX_NUMBER_ERRORS, __glXDispatch,
  345. __glXDispatch, ResetExtension, StandardMinorOpcode);
  346. if (!extEntry) {
  347. FatalError("__glXExtensionInit: AddExtensions failed\n");
  348. return;
  349. }
  350. if (!AddExtensionAlias(GLX_EXTENSION_ALIAS, extEntry)) {
  351. ErrorF("__glXExtensionInit: AddExtensionAlias failed\n");
  352. return;
  353. }
  354. __glXErrorBase = extEntry->errorBase;
  355. __glXEventBase = extEntry->eventBase;
  356. #if PRESENT
  357. __glXregisterPresentCompleteNotify();
  358. #endif
  359. }
  360. /************************************************************************/
  361. /*
  362. ** Make a context the current one for the GL (in this implementation, there
  363. ** is only one instance of the GL, and we use it to serve all GL clients by
  364. ** switching it between different contexts). While we are at it, look up
  365. ** a context by its tag and return its (__GLXcontext *).
  366. */
  367. __GLXcontext *
  368. __glXForceCurrent(__GLXclientState * cl, GLXContextTag tag, int *error)
  369. {
  370. __GLXcontext *cx;
  371. /*
  372. ** See if the context tag is legal; it is managed by the extension,
  373. ** so if it's invalid, we have an implementation error.
  374. */
  375. cx = __glXLookupContextByTag(cl, tag);
  376. if (!cx) {
  377. cl->client->errorValue = tag;
  378. *error = __glXError(GLXBadContextTag);
  379. return 0;
  380. }
  381. if (!cx->isDirect) {
  382. if (cx->drawPriv == NULL) {
  383. /*
  384. ** The drawable has vanished. It must be a window, because only
  385. ** windows can be destroyed from under us; GLX pixmaps are
  386. ** refcounted and don't go away until no one is using them.
  387. */
  388. *error = __glXError(GLXBadCurrentWindow);
  389. return 0;
  390. }
  391. }
  392. if (cx->wait && (*cx->wait) (cx, cl, error))
  393. return NULL;
  394. if (cx == lastGLContext) {
  395. /* No need to re-bind */
  396. return cx;
  397. }
  398. /* Make this context the current one for the GL. */
  399. if (!cx->isDirect) {
  400. lastGLContext = cx;
  401. if (!(*cx->makeCurrent) (cx)) {
  402. /* Bind failed, and set the error code. Bummer */
  403. lastGLContext = NULL;
  404. cl->client->errorValue = cx->id;
  405. *error = __glXError(GLXBadContextState);
  406. return 0;
  407. }
  408. }
  409. return cx;
  410. }
  411. /************************************************************************/
  412. void
  413. glxSuspendClients(void)
  414. {
  415. int i;
  416. for (i = 1; i < currentMaxClients; i++) {
  417. if (clients[i] && glxGetClient(clients[i])->inUse)
  418. IgnoreClient(clients[i]);
  419. }
  420. glxBlockClients = TRUE;
  421. }
  422. void
  423. glxResumeClients(void)
  424. {
  425. __GLXcontext *cx, *next;
  426. int i;
  427. glxBlockClients = FALSE;
  428. for (i = 1; i < currentMaxClients; i++) {
  429. if (clients[i] && glxGetClient(clients[i])->inUse)
  430. AttendClient(clients[i]);
  431. }
  432. __glXleaveServer(GL_FALSE);
  433. for (cx = glxPendingDestroyContexts; cx != NULL; cx = next) {
  434. next = cx->next;
  435. cx->destroy(cx);
  436. }
  437. glxPendingDestroyContexts = NULL;
  438. __glXenterServer(GL_FALSE);
  439. }
  440. static void
  441. __glXnopEnterServer(GLboolean rendering)
  442. {
  443. }
  444. static void
  445. __glXnopLeaveServer(GLboolean rendering)
  446. {
  447. }
  448. static void (*__glXenterServerFunc) (GLboolean) = __glXnopEnterServer;
  449. static void (*__glXleaveServerFunc) (GLboolean) = __glXnopLeaveServer;
  450. void
  451. __glXsetEnterLeaveServerFuncs(void (*enter) (GLboolean),
  452. void (*leave) (GLboolean))
  453. {
  454. __glXenterServerFunc = enter;
  455. __glXleaveServerFunc = leave;
  456. }
  457. void
  458. __glXenterServer(GLboolean rendering)
  459. {
  460. glxServerLeaveCount--;
  461. if (glxServerLeaveCount == 0)
  462. (*__glXenterServerFunc) (rendering);
  463. }
  464. void
  465. __glXleaveServer(GLboolean rendering)
  466. {
  467. if (glxServerLeaveCount == 0)
  468. (*__glXleaveServerFunc) (rendering);
  469. glxServerLeaveCount++;
  470. }
  471. static glx_gpa_proc _get_proc_address;
  472. void
  473. __glXsetGetProcAddress(glx_gpa_proc get_proc_address)
  474. {
  475. _get_proc_address = get_proc_address;
  476. }
  477. void *__glGetProcAddress(const char *proc)
  478. {
  479. void *ret = (void *) _get_proc_address(proc);
  480. return ret ? ret : (void *) NoopDDA;
  481. }
  482. /*
  483. ** Top level dispatcher; all commands are executed from here down.
  484. */
  485. static int
  486. __glXDispatch(ClientPtr client)
  487. {
  488. REQUEST(xGLXSingleReq);
  489. CARD8 opcode;
  490. __GLXdispatchSingleProcPtr proc;
  491. __GLXclientState *cl;
  492. int retval;
  493. opcode = stuff->glxCode;
  494. cl = glxGetClient(client);
  495. /* Mark it in use so we suspend it on VT switch. */
  496. cl->inUse = TRUE;
  497. /*
  498. ** If we're expecting a glXRenderLarge request, this better be one.
  499. */
  500. if ((cl->largeCmdRequestsSoFar != 0) && (opcode != X_GLXRenderLarge)) {
  501. client->errorValue = stuff->glxCode;
  502. return __glXError(GLXBadLargeRequest);
  503. }
  504. /* If we're currently blocking GLX clients, just put this guy to
  505. * sleep, reset the request and return. */
  506. if (glxBlockClients) {
  507. ResetCurrentRequest(client);
  508. client->sequence--;
  509. IgnoreClient(client);
  510. return Success;
  511. }
  512. /*
  513. ** Use the opcode to index into the procedure table.
  514. */
  515. proc = __glXGetProtocolDecodeFunction(&Single_dispatch_info, opcode,
  516. client->swapped);
  517. if (proc != NULL) {
  518. GLboolean rendering = opcode <= X_GLXRenderLarge;
  519. __glXleaveServer(rendering);
  520. retval = (*proc) (cl, (GLbyte *) stuff);
  521. __glXenterServer(rendering);
  522. }
  523. else {
  524. retval = BadRequest;
  525. }
  526. return retval;
  527. }