Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4066 lines
123 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #include <imfuncs.h>
  4. #include "genci.h"
  5. #include "genrgb.h"
  6. #include "devlock.h"
  7. #include "imports.h"
  8. //
  9. // CJ_ALIGNDWORD computes the minimum size (in bytes) of a DWORD array that
  10. // contains at least cj bytes.
  11. //
  12. #define CJ_ALIGNDWORD(cj) ( ((cj) + (sizeof(DWORD)-1)) & (-((signed)sizeof(DWORD))) )
  13. //
  14. // BITS_ALIGNDWORD computes the minimum size (in bits) of a DWORD array that
  15. // contains at least c bits.
  16. //
  17. // We assume that there will always be 8 bits in a byte and that sizeof()
  18. // always returns size in bytes. The rest is independent of the definition
  19. // of DWORD.
  20. //
  21. #define BITS_ALIGNDWORD(c) ( ((c) + ((sizeof(DWORD)*8)-1)) & (-((signed)(sizeof(DWORD)*8))) )
  22. // change to "static" after debugging
  23. #define STATIC
  24. #if DBG
  25. // not multithreaded safe, but only for testing
  26. #define RANDOMDISABLE \
  27. { \
  28. long saveRandom = glRandomMallocFail; \
  29. glRandomMallocFail = 0;
  30. #define RANDOMREENABLE \
  31. if (saveRandom) \
  32. glRandomMallocFail = saveRandom; \
  33. }
  34. #else
  35. #define RANDOMDISABLE
  36. #define RANDOMREENABLE
  37. #endif /* DBG */
  38. #define INITIAL_TIMESTAMP ((ULONG)-1)
  39. /*
  40. * Function Prototypes
  41. */
  42. BOOL APIENTRY ValidateLayerIndex(int iLayer, PIXELFORMATDESCRIPTOR *ppfd);
  43. /*
  44. * Private functions
  45. */
  46. void FASTCALL GetContextModes(__GLGENcontext *gengc);
  47. STATIC void FASTCALL ApplyViewport(__GLcontext *gc);
  48. GLboolean ResizeAncillaryBuffer(__GLGENbuffers *, __GLbuffer *, GLint, GLint);
  49. GLboolean ResizeHardwareBackBuffer(__GLGENbuffers *, __GLcolorBuffer *, GLint, GLint);
  50. GLboolean ResizeUnownedDepthBuffer(__GLGENbuffers *, __GLbuffer *, GLint, GLint);
  51. /******************************Public*Routine******************************\
  52. *
  53. * EmptyFillStrokeCache
  54. *
  55. * Cleans up any objects in the fill and stroke cache
  56. *
  57. * History:
  58. * Tue Aug 15 15:37:30 1995 -by- Drew Bliss [drewb]
  59. * Created
  60. *
  61. \**************************************************************************/
  62. void FASTCALL EmptyFillStrokeCache(__GLGENcontext *gengc)
  63. {
  64. if (gengc->hbrFill != NULL)
  65. {
  66. DeleteObject(gengc->hbrFill);
  67. gengc->hbrFill = NULL;
  68. gengc->crFill = COLORREF_UNUSED;
  69. gengc->hdcFill = NULL;
  70. }
  71. #if DBG
  72. else
  73. {
  74. ASSERTOPENGL(gengc->crFill == COLORREF_UNUSED,
  75. "crFill inconsistent\n");
  76. }
  77. #endif
  78. if (gengc->hpenStroke != NULL)
  79. {
  80. // Deselect the pen before deletion if necessary
  81. if (gengc->hdcStroke != NULL)
  82. {
  83. SelectObject(gengc->hdcStroke, GetStockObject(BLACK_PEN));
  84. gengc->hdcStroke = NULL;
  85. }
  86. DeleteObject(gengc->hpenStroke);
  87. gengc->hpenStroke = NULL;
  88. gengc->cStroke.r = -1.0f;
  89. gengc->fStrokeInvalid = TRUE;
  90. }
  91. #if DBG
  92. else
  93. {
  94. ASSERTOPENGL(gengc->cStroke.r < 0.0f &&
  95. gengc->fStrokeInvalid,
  96. "rStroke inconsistent\n");
  97. }
  98. #endif
  99. }
  100. /******************************Public*Routine******************************\
  101. * glsrvDeleteContext
  102. *
  103. * Deletes the generic context.
  104. *
  105. * Returns:
  106. * TRUE if successful, FALSE otherwise.
  107. *
  108. \**************************************************************************/
  109. BOOL APIENTRY glsrvDeleteContext(__GLcontext *gc)
  110. {
  111. __GLGENcontext *gengc;
  112. gengc = (__GLGENcontext *)gc;
  113. /* Free ancillary buffer related data. Note that these calls do
  114. ** *not* free software ancillary buffers, just any related data
  115. ** stored in them. The ancillary buffers are freed on window destruction
  116. */
  117. if (gc->modes.accumBits) {
  118. DBGLEVEL(LEVEL_ALLOC,
  119. "DestroyContext: Freeing accumulation buffer related data\n");
  120. __glFreeAccum64(gc, &gc->accumBuffer);
  121. }
  122. if (gc->modes.depthBits) {
  123. DBGLEVEL(LEVEL_ALLOC,
  124. "DestroyContext: Freeing depth buffer related data\n");
  125. __glFreeDepth32(gc, &gc->depthBuffer);
  126. }
  127. if (gc->modes.stencilBits) {
  128. DBGLEVEL(LEVEL_ALLOC,
  129. "DestroyContext: Freeing stencil buffer related data\n");
  130. __glFreeStencil8(gc, &gc->stencilBuffer);
  131. }
  132. /* Free Translate & Inverse Translate vectors */
  133. if ((gengc->pajTranslateVector != NULL) &&
  134. (gengc->pajTranslateVector != gengc->xlatPalette))
  135. GCFREE(gc, gengc->pajTranslateVector);
  136. if (gengc->pajInvTranslateVector != NULL)
  137. GCFREE(gc, gengc->pajInvTranslateVector);
  138. // Make sure that any cached GDI objects are freed
  139. // This is normally done in LoseCurrent but a context may be
  140. // left current and then cleaned up
  141. EmptyFillStrokeCache(gengc);
  142. /*
  143. /* Free the span dibs and storage.
  144. */
  145. #ifndef _CLIENTSIDE_
  146. if (gengc->StippleBitmap)
  147. EngDeleteSurface((HSURF)gengc->StippleBitmap);
  148. #endif
  149. wglDeleteScanlineBuffers(gengc);
  150. if (gengc->StippleBits)
  151. GCFREE(gc, gengc->StippleBits);
  152. // Free __GLGENbitmap front-buffer structure
  153. if (gc->frontBuffer.bitmap)
  154. GCFREE(gc, gc->frontBuffer.bitmap);
  155. #ifndef _CLIENTSIDE_
  156. /*
  157. * Free the buffers that may have been allocated by feedback
  158. * or selection
  159. */
  160. if ( NULL != gengc->RenderState.SrvSelectBuffer )
  161. {
  162. #ifdef NT
  163. // match the allocation function
  164. FREE(gengc->RenderState.SrvSelectBuffer);
  165. #else
  166. GCFREE(gc, gengc->RenderState.SrvSelectBuffer);
  167. #endif
  168. }
  169. if ( NULL != gengc->RenderState.SrvFeedbackBuffer)
  170. {
  171. #ifdef NT
  172. // match the allocation function
  173. FREE(gengc->RenderState.SrvFeedbackBuffer);
  174. #else
  175. GCFREE(gc, gengc->RenderState.SrvFeedbackBuffer);
  176. #endif
  177. }
  178. #endif //_CLIENTSIDE_
  179. #ifdef _CLIENTSIDE_
  180. /*
  181. * Cleanup logical palette copy if it exists.
  182. */
  183. if ( gengc->ppalBuf )
  184. FREE(gengc->ppalBuf);
  185. #endif
  186. /* Destroy acceleration-specific context information */
  187. __glGenDestroyAccelContext(gc);
  188. #ifdef _MCD_
  189. /* Free the MCD state structure and associated resources. */
  190. if (gengc->_pMcdState) {
  191. GenMcdDeleteContext(gengc->_pMcdState);
  192. }
  193. #endif
  194. /* Free any temporay buffers in abnormal process exit */
  195. GC_TEMP_BUFFER_EXIT_CLEANUP(gc);
  196. // Release references to DirectDraw surfaces
  197. if (gengc->gsurf.dwFlags & GLSURF_DIRECTDRAW)
  198. {
  199. GLWINDOWID gwid;
  200. GLGENwindow *pwnd;
  201. // Destroy window created for this context
  202. gwid.iType = GLWID_DDRAW;
  203. gwid.pdds = gengc->gsurf.dd.gddsFront.pdds;
  204. gwid.hdc = gengc->gsurf.hdc;
  205. gwid.hwnd = NULL;
  206. pwnd = pwndGetFromID(&gwid);
  207. ASSERTOPENGL(pwnd != NULL,
  208. "Destroying DDraw context without window\n");
  209. pwndCleanup(pwnd);
  210. gengc->gsurf.dd.gddsFront.pdds->lpVtbl->
  211. Release(gengc->gsurf.dd.gddsFront.pdds);
  212. if (gengc->gsurf.dd.gddsZ.pdds != NULL)
  213. {
  214. gengc->gsurf.dd.gddsZ.pdds->lpVtbl->
  215. Release(gengc->gsurf.dd.gddsZ.pdds);
  216. }
  217. }
  218. /* Destroy rest of software context (in soft code) */
  219. __glDestroyContext(gc);
  220. return TRUE;
  221. }
  222. /******************************Public*Routine******************************\
  223. * glsrvLoseCurrent
  224. *
  225. * Releases the current context (makes it not current).
  226. *
  227. \**************************************************************************/
  228. VOID APIENTRY glsrvLoseCurrent(__GLcontext *gc)
  229. {
  230. __GLGENcontext *gengc;
  231. gengc = (__GLGENcontext *)gc;
  232. DBGENTRY("LoseCurrent\n");
  233. ASSERTOPENGL(gc == GLTEB_SRVCONTEXT(), "LoseCurrent not current!");
  234. /*
  235. ** Release lock if still held.
  236. */
  237. if (gengc->fsLocks != 0)
  238. {
  239. glsrvReleaseLock(gengc);
  240. }
  241. /*
  242. ** Unscale derived state that depends upon the color scales. This
  243. ** is needed so that if this context is rebound to a memdc, it can
  244. ** then rescale all of those colors using the memdc color scales.
  245. */
  246. __glContextUnsetColorScales(gc);
  247. memset(&gengc->gwidCurrent, 0, sizeof(gengc->gwidCurrent));
  248. /*
  249. ** Clean up HDC-specific GDI objects
  250. */
  251. EmptyFillStrokeCache(gengc);
  252. /*
  253. ** Free up fake window for IC's
  254. */
  255. if ((gengc->dwCurrentFlags & GLSURF_METAFILE) && gengc->ipfdCurrent == 0)
  256. {
  257. GLGENwindow *pwnd;
  258. pwnd = gengc->pwndMakeCur;
  259. ASSERTOPENGL(pwnd != NULL,
  260. "IC with no pixel format but no fake window\n");
  261. if (pwnd->buffers != NULL)
  262. {
  263. __glGenFreeBuffers(pwnd->buffers);
  264. }
  265. DeleteCriticalSection(&pwnd->sem);
  266. FREE(pwnd);
  267. }
  268. gengc->pwndMakeCur = NULL;
  269. #ifdef _MCD_
  270. /*
  271. ** Disconnect MCD state.
  272. */
  273. gengc->pMcdState = (GENMCDSTATE *) NULL;
  274. #endif
  275. gc->constants.width = 0;
  276. gc->constants.height = 0;
  277. // Set paTeb to NULL for debugging.
  278. gc->paTeb = NULL;
  279. GLTEB_SET_SRVCONTEXT(0);
  280. }
  281. /******************************Public*Routine******************************\
  282. * glsrvSwapBuffers
  283. *
  284. * This uses the software implementation of double buffering. An engine
  285. * allocated bitmap is allocated for use as the back buffer. The SwapBuffer
  286. * routine copies the back buffer to the front buffer surface (which may
  287. * be another DIB, a device surface in DIB format, or a device managed
  288. * surface (with a device specific format).
  289. *
  290. * The SwapBuffer routine does not disturb the contents of the back buffer,
  291. * though the defined behavior for now is undefined.
  292. *
  293. * Note: the caller should be holding the per-window semaphore.
  294. *
  295. * History:
  296. * 19-Nov-1993 -by- Gilman Wong [gilmanw]
  297. * Wrote it.
  298. \**************************************************************************/
  299. BOOL APIENTRY glsrvSwapBuffers(HDC hdc, GLGENwindow *pwnd)
  300. {
  301. DBGENTRY("glsrvSwapBuffers\n");
  302. if ( pwnd->buffers ) {
  303. __GLGENbuffers *buffers;
  304. __GLGENbitmap *genBm;
  305. buffers = pwnd->buffers;
  306. if (buffers->pMcdSurf) {
  307. return GenMcdSwapBuffers(hdc, pwnd);
  308. }
  309. genBm = &buffers->backBitmap;
  310. // Make sure the backbuffer exists
  311. if (genBm->hbm) {
  312. if (!RECTLISTIsEmpty(&buffers->rl) && !buffers->fMax) {
  313. wglCopyBufRECTLIST(
  314. hdc,
  315. genBm->hdc,
  316. 0,
  317. 0,
  318. buffers->backBuffer.width,
  319. buffers->backBuffer.height,
  320. &buffers->rl
  321. );
  322. } else {
  323. buffers->fMax = FALSE;
  324. wglCopyBuf(
  325. hdc,
  326. genBm->hdc,
  327. 0,
  328. 0,
  329. buffers->backBuffer.width,
  330. buffers->backBuffer.height
  331. );
  332. }
  333. RECTLISTSetEmpty(&buffers->rl);
  334. }
  335. if( buffers->alphaBits
  336. && buffers->alphaBackBuffer
  337. && buffers->alphaFrontBuffer) {
  338. ASSERTOPENGL(buffers->alphaFrontBuffer->size ==
  339. buffers->alphaBackBuffer->size,
  340. "Destination alpha buffer size mismatch\n");
  341. // Destination alpha values are kept in separate buffers.
  342. // If this buffer set has destination alpha buffers,
  343. // copy the back alpha values into the front alpha buffer.
  344. RtlCopyMemory(buffers->alphaFrontBuffer->base,
  345. buffers->alphaBackBuffer->base,
  346. buffers->alphaBackBuffer->size);
  347. }
  348. return TRUE;
  349. }
  350. return FALSE;
  351. }
  352. /******************************Public*Routine******************************\
  353. * gdiCopyPixels
  354. *
  355. * Copy span [(x, y), (x + cx, y)) (inclusive-exclusive) to/from specified
  356. * color buffer cfb from/to the scanline buffer.
  357. *
  358. * If bIn is TRUE, the copy is from the scanline buffer to the buffer.
  359. * If bIn is FALSE, the copy is from the buffer to the scanline buffer.
  360. *
  361. \**************************************************************************/
  362. void gdiCopyPixels(__GLGENcontext *gengc, __GLcolorBuffer *cfb,
  363. GLint x, GLint y, GLint cx, BOOL bIn)
  364. {
  365. wglCopyBits(gengc, gengc->pwndLocked, gengc->ColorsBitmap, x, y, cx, bIn);
  366. }
  367. /******************************Public*Routine******************************\
  368. * dibCopyPixels
  369. *
  370. * Special case version of gdiCopyPixels that is used when cfb is a DIB,
  371. * either a real DIB or a device surface which has a DIB format.
  372. *
  373. * This function *must* be used in lieu of gdiCopyPixels if we are
  374. * directly accessing the screen as it is not safe to call GDI entry
  375. * points with the screen locked
  376. *
  377. * History:
  378. * 24-May-1995 -by- Gilman Wong [gilmanw]
  379. * Wrote it.
  380. \**************************************************************************/
  381. void dibCopyPixels(__GLGENcontext *gengc, __GLcolorBuffer *cfb,
  382. GLint x, GLint y, GLint cx, BOOL bIn)
  383. {
  384. VOID *pvDib;
  385. UINT cjPixel = gengc->gsurf.pfd.cColorBits >> 3;
  386. ULONG ulSpanVisibility;
  387. GLint cWalls;
  388. GLint *pWalls;
  389. // Don't handle VGA DIBs.
  390. //
  391. // We are not supposed to call GDI if directly accessing the screen. However,
  392. // we should be able to assume that 4bpp devices do not support
  393. // direct access making it OK for us to punt this call to the GDI version.
  394. // This is true for Win95 and, according to AndrewGo, will be
  395. // true for WinNT SUR.
  396. if (gengc->gsurf.pfd.cColorBits <= 4)
  397. {
  398. ASSERTOPENGL(
  399. !((cfb->buf.flags & DIB_FORMAT) &&
  400. !(cfb->buf.flags & MEMORY_DC)),
  401. "dibCopyPixels: unexpected 4bpp direct surface\n"
  402. );
  403. gdiCopyPixels(gengc, cfb, x, y, cx, bIn);
  404. return;
  405. }
  406. // Find out clipping info.
  407. if ((cfb->buf.flags & (NO_CLIP | DIB_FORMAT)) ==
  408. (NO_CLIP | DIB_FORMAT))
  409. {
  410. ulSpanVisibility = WGL_SPAN_ALL;
  411. }
  412. else
  413. {
  414. ulSpanVisibility = wglSpanVisible(x, y, cx, &cWalls, &pWalls);
  415. }
  416. // Completely clipped, nothing to do.
  417. if (ulSpanVisibility == WGL_SPAN_NONE)
  418. return;
  419. // Completely visible.
  420. //
  421. // Actually, if bIn == FALSE (i.e., copy from screen to scanline buffer)
  422. // we can cheat a little and ignore the clipping.
  423. else if ( (ulSpanVisibility == WGL_SPAN_ALL) || !bIn )
  424. {
  425. // Get pointer into DIB at position (x, y).
  426. pvDib = (VOID *) (((BYTE *) gengc->gc.front->buf.base) +
  427. gengc->gc.front->buf.outerWidth * y +
  428. cjPixel * x);
  429. // If bIn == TRUE, copy from scanline buffer to DIB.
  430. // Otherwise, copy from DIB to scanline buffer.
  431. if (bIn)
  432. RtlCopyMemory_UnalignedDst(pvDib, gengc->ColorsBits, cjPixel * cx);
  433. else
  434. RtlCopyMemory_UnalignedSrc(gengc->ColorsBits, pvDib, cjPixel * cx);
  435. }
  436. // Partially visible.
  437. else
  438. {
  439. GLint xEnd = x + cx; // end of the span
  440. UINT cjSpan; // size of the current portion of span to copy
  441. VOID *pvBits; // current copy position in the scanline buf
  442. BYTE *pjScan; // address of scan line in DIB
  443. ASSERTOPENGL( cWalls && pWalls, "dibCopyPixels(): bad wall array\n");
  444. // The walls are walked until either the end of the array is reached
  445. // or the walls exceed the end of the span. The do..while loop
  446. // construct was choosen because the first iteration will always
  447. // copy something and after the first iteration we are guaranteed
  448. // to be in the "cWalls is even" case. This makes the testing the
  449. // walls against the span end easier.
  450. //
  451. // If cWalls is even, clip the span to each pair of walls in pWalls.
  452. // If cWalls is odd, form the first pair with (x, pWalls[0]) and then
  453. // pair the remaining walls starting with pWalls[1].
  454. pjScan = (VOID *) (((BYTE *) gengc->gc.front->buf.base) +
  455. gengc->gc.front->buf.outerWidth * y);
  456. do
  457. {
  458. //!!!XXX -- Make faster by pulling the odd case out of the loop
  459. if (cWalls & 0x1)
  460. {
  461. pvDib = (VOID *) (pjScan + (cjPixel * x));
  462. pvBits = gengc->ColorsBits;
  463. if ( pWalls[0] <= xEnd )
  464. cjSpan = cjPixel * (pWalls[0] - x);
  465. else
  466. cjSpan = cjPixel * cx;
  467. pWalls++;
  468. cWalls--; // Now cWalls is even!
  469. }
  470. else
  471. {
  472. pvDib = (VOID *) (pjScan + (cjPixel * pWalls[0]));
  473. pvBits = (VOID *) (((BYTE *) gengc->ColorsBits) +
  474. cjPixel * (pWalls[0] - x));
  475. if ( pWalls[1] <= xEnd )
  476. cjSpan = cjPixel * (pWalls[1] - pWalls[0]);
  477. else
  478. cjSpan = cjPixel * (xEnd - pWalls[0]);
  479. pWalls += 2;
  480. cWalls -= 2;
  481. }
  482. // We are going to cheat and ignore clipping when copying from
  483. // the dib to the scanline buffer (i.e., we are going to handle
  484. // the !bIn case as if it were WGL_SPAN_ALL). Thus, we can assume
  485. // that bIn == TRUE if we get to here.
  486. //
  487. // If clipping is needed to read the DIB, its trivial to turn it
  488. // back on.
  489. //
  490. // RtlCopyMemory(bIn ? pvDib : pvBits,
  491. // bIn ? pvBits : pvDib,
  492. // cjSpan);
  493. //!!!dbug -- Possible COMPILER BUG (compiler should check for
  494. //!!!dbug alignment before doing the "rep movsd"). Keep around
  495. //!!!dbug as a test case.
  496. #if 1
  497. RtlCopyMemory_UnalignedDst(pvDib, pvBits, cjSpan);
  498. #else
  499. RtlCopyMemory(pvDib, pvBits, cjSpan);
  500. #endif
  501. } while ( cWalls && (pWalls[0] < xEnd) );
  502. }
  503. }
  504. /******************************Public*Routine******************************\
  505. * MaskFromBits
  506. *
  507. * Support routine for GetContextModes. Computes a color mask given that
  508. * colors bit count and shift position.
  509. *
  510. \**************************************************************************/
  511. #define MaskFromBits(shift, count) \
  512. ((0xffffffff >> (32-(count))) << (shift))
  513. /******************************Public*Routine******************************\
  514. * GetContextModes
  515. *
  516. * Convert the information from Gdi into OpenGL format after checking that
  517. * the formats are compatible and that the surface is compatible with the
  518. * format.
  519. *
  520. * Called during a glsrvMakeCurrent().
  521. *
  522. \**************************************************************************/
  523. void FASTCALL GetContextModes(__GLGENcontext *gengc)
  524. {
  525. PIXELFORMATDESCRIPTOR *pfmt;
  526. __GLcontextModes *Modes;
  527. DBGENTRY("GetContextModes\n");
  528. Modes = &((__GLcontext *)gengc)->modes;
  529. pfmt = &gengc->gsurf.pfd;
  530. if (pfmt->iPixelType == PFD_TYPE_RGBA)
  531. Modes->rgbMode = GL_TRUE;
  532. else
  533. Modes->rgbMode = GL_FALSE;
  534. Modes->colorIndexMode = !Modes->rgbMode;
  535. if (pfmt->dwFlags & PFD_DOUBLEBUFFER)
  536. Modes->doubleBufferMode = GL_TRUE;
  537. else
  538. Modes->doubleBufferMode = GL_FALSE;
  539. if (pfmt->dwFlags & PFD_STEREO)
  540. Modes->stereoMode = GL_TRUE;
  541. else
  542. Modes->stereoMode = GL_FALSE;
  543. Modes->accumBits = pfmt->cAccumBits;
  544. Modes->haveAccumBuffer = GL_FALSE;
  545. Modes->auxBits = NULL; // This is a pointer
  546. Modes->depthBits = pfmt->cDepthBits;
  547. Modes->haveDepthBuffer = GL_FALSE;
  548. Modes->stencilBits = pfmt->cStencilBits;
  549. Modes->haveStencilBuffer= GL_FALSE;
  550. if (pfmt->cColorBits > 8)
  551. Modes->indexBits = 8;
  552. else
  553. Modes->indexBits = pfmt->cColorBits;
  554. Modes->indexFractionBits= 0;
  555. // The Modes->{Red,Green,Blue}Bits are used in soft
  556. Modes->redBits = pfmt->cRedBits;
  557. Modes->greenBits = pfmt->cGreenBits;
  558. Modes->blueBits = pfmt->cBlueBits;
  559. Modes->alphaBits = pfmt->cAlphaBits;
  560. Modes->redMask = MaskFromBits(pfmt->cRedShift, pfmt->cRedBits);
  561. Modes->greenMask = MaskFromBits(pfmt->cGreenShift, pfmt->cGreenBits);
  562. Modes->blueMask = MaskFromBits(pfmt->cBlueShift, pfmt->cBlueBits);
  563. Modes->alphaMask = MaskFromBits(pfmt->cAlphaShift, pfmt->cAlphaBits);
  564. Modes->rgbMask = Modes->redMask | Modes->greenMask |
  565. Modes->blueMask;
  566. Modes->allMask = Modes->redMask | Modes->greenMask |
  567. Modes->blueMask | Modes->alphaMask;
  568. Modes->maxAuxBuffers = 0;
  569. Modes->isDirect = GL_FALSE;
  570. Modes->level = 0;
  571. #if DBG
  572. DBGBEGIN(LEVEL_INFO)
  573. DbgPrint("GL generic server get modes: rgbmode %d, cimode %d, index bits %d\n", Modes->rgbMode, Modes->colorIndexMode);
  574. DbgPrint(" redmask 0x%x, greenmask 0x%x, bluemask 0x%x\n", Modes->redMask, Modes->greenMask, Modes->blueMask);
  575. DbgPrint(" redbits %d, greenbits %d, bluebits %d\n", Modes->redBits, Modes->greenBits, Modes->blueBits);
  576. DbgPrint("GetContext Modes flags %X\n", gengc->gsurf.dwFlags);
  577. DBGEND
  578. #endif /* DBG */
  579. }
  580. /******************************Public*Routine******************************\
  581. * wglGetSurfacePalette
  582. *
  583. * Initialize the array of RGBQUADs to match the color table or palette
  584. * of the DC's surface.
  585. *
  586. * Note:
  587. * Should be called only for 8bpp or lesser surfaces.
  588. *
  589. * History:
  590. * 12-Jun-1995 -by- Gilman Wong [gilmanw]
  591. * Wrote it.
  592. \**************************************************************************/
  593. BOOL
  594. wglGetSurfacePalette( __GLGENcontext *gengc, RGBQUAD *prgbq,
  595. BOOL bTranslateDdb )
  596. {
  597. int nColors;
  598. BOOL bRet;
  599. BOOL bConvert;
  600. PALETTEENTRY ppe[256];
  601. int i;
  602. ASSERTOPENGL(gengc->gsurf.pfd.cColorBits <= 8,
  603. "wglGetSurfacePalette called for deep surface\n");
  604. ASSERTOPENGL((gengc->dwCurrentFlags & GLSURF_METAFILE) == 0,
  605. "wglGetSurfacePalette called for metafile\n");
  606. nColors = 1 << gengc->gsurf.pfd.cColorBits;
  607. if (gengc->dwCurrentFlags & GLSURF_DIRECTDRAW)
  608. {
  609. LPDIRECTDRAWPALETTE pddp;
  610. HRESULT hr;
  611. // Retrieve DirectDraw palette from surface
  612. if (gengc->gsurf.dd.gddsFront.pdds->lpVtbl->
  613. GetPalette(gengc->gsurf.dd.gddsFront.pdds, &pddp) != DD_OK ||
  614. pddp == NULL)
  615. {
  616. return FALSE;
  617. }
  618. hr = pddp->lpVtbl->GetEntries(pddp, 0, 0, nColors, ppe);
  619. pddp->lpVtbl->Release(pddp);
  620. bRet = hr == DD_OK;
  621. bConvert = TRUE;
  622. }
  623. else if (GLSURF_IS_DIRECTDC(gengc->dwCurrentFlags))
  624. {
  625. // Direct DC, so get the RGB values from the system palette.
  626. bRet = wglGetSystemPaletteEntries(gengc->gwidCurrent.hdc,
  627. 0, nColors, ppe);
  628. bConvert = TRUE;
  629. }
  630. else if (gengc->dwCurrentFlags & GLSURF_DIRECT_ACCESS)
  631. {
  632. // DIB section, so copy the color table.
  633. bRet = GetDIBColorTable(gengc->gwidCurrent.hdc, 0, nColors, prgbq);
  634. bConvert = FALSE;
  635. }
  636. else
  637. {
  638. // DDB surface, so use the logical palette.
  639. bRet = GetPaletteEntries(GetCurrentObject(gengc->gwidCurrent.hdc,
  640. OBJ_PAL),
  641. 0, nColors, ppe);
  642. // For certain DDB surfaces we need the palette to be permuted
  643. // by the forward translation vector before use.
  644. if (bRet && bTranslateDdb)
  645. {
  646. BYTE *pjTrans;
  647. bConvert = FALSE;
  648. // Convert to RGBQUAD with forward translation permutation.
  649. pjTrans = gengc->pajTranslateVector;
  650. for (i = 0; i < nColors; i++)
  651. {
  652. prgbq[pjTrans[i]].rgbRed = ppe[i].peRed;
  653. prgbq[pjTrans[i]].rgbGreen = ppe[i].peGreen;
  654. prgbq[pjTrans[i]].rgbBlue = ppe[i].peBlue;
  655. prgbq[pjTrans[i]].rgbReserved = 0;
  656. }
  657. }
  658. else
  659. {
  660. bConvert = TRUE;
  661. }
  662. }
  663. if (bRet && bConvert)
  664. {
  665. // Convert to RGBQUAD.
  666. for (i = 0; i < nColors; i++)
  667. {
  668. prgbq[i].rgbRed = ppe[i].peRed;
  669. prgbq[i].rgbGreen = ppe[i].peGreen;
  670. prgbq[i].rgbBlue = ppe[i].peBlue;
  671. prgbq[i].rgbReserved = 0;
  672. }
  673. }
  674. return bRet;
  675. }
  676. /******************************Public*Routine******************************\
  677. * SyncDibColorTables
  678. *
  679. * Setup the color table in each DIB associated with the specified
  680. * GLGENcontext to match the system palette.
  681. *
  682. * Called only for <= 8bpp surfaces.
  683. *
  684. * History:
  685. * 24-Oct-1994 -by- Gilman Wong [gilmanw]
  686. * Wrote it.
  687. \**************************************************************************/
  688. void
  689. SyncDibColorTables(__GLGENcontext *gengc)
  690. {
  691. __GLGENbuffers *buffers = gengc->pwndLocked->buffers;
  692. ASSERTOPENGL(gengc->gsurf.pfd.cColorBits <= 8,
  693. "SyncDibColorTables(): bad surface type");
  694. if (gengc->ColorsBitmap || buffers->backBitmap.hbm)
  695. {
  696. RGBQUAD rgbq[256];
  697. if (wglGetSurfacePalette(gengc, rgbq, TRUE))
  698. {
  699. int nColors;
  700. // If color table was obtained, setup the DIBs.
  701. nColors = 1 << gengc->gsurf.pfd.cColorBits;
  702. // Scan-line DIB.
  703. if (gengc->ColorsBitmap)
  704. SetDIBColorTable(gengc->ColorsMemDC, 0, nColors, rgbq);
  705. // Back buffer
  706. if (buffers->backBitmap.hbm)
  707. SetDIBColorTable(buffers->backBitmap.hdc, 0, nColors, rgbq);
  708. }
  709. else
  710. {
  711. WARNING("SyncDibColorTables: Unable to get surface palette\n");
  712. }
  713. }
  714. }
  715. static BYTE vubSystemToRGB8[20] = {
  716. 0x00,
  717. 0x04,
  718. 0x20,
  719. 0x24,
  720. 0x80,
  721. 0x84,
  722. 0xa0,
  723. 0xf6,
  724. 0xf6,
  725. 0xf5,
  726. 0xff,
  727. 0xad,
  728. 0xa4,
  729. 0x07,
  730. 0x38,
  731. 0x3f,
  732. 0xc0,
  733. 0xc7,
  734. 0xf8,
  735. 0xff
  736. };
  737. // ComputeInverseTranslationVector
  738. // Computes the inverse translation vector for 4-bit and 8-bit.
  739. //
  740. // Synopsis:
  741. // void ComputeInverseTranslation(
  742. // __GLGENcontext *gengc specifies the generic RC
  743. // int cColorEntries specifies the number of color entries
  744. // BYTE iPixeltype specifies the pixel format type
  745. //
  746. // Assumtions:
  747. // The inverse translation vector has been allocated and initialized with
  748. // zeros.
  749. //
  750. // History:
  751. // 23-NOV-93 Eddie Robinson [v-eddier] Wrote it.
  752. //
  753. void FASTCALL ComputeInverseTranslationVector(__GLGENcontext *gengc,
  754. int cColorEntries,
  755. int iPixelType)
  756. {
  757. BYTE *pXlate, *pInvXlate;
  758. int i, j;
  759. pInvXlate = gengc->pajInvTranslateVector;
  760. pXlate = gengc->pajTranslateVector;
  761. for (i = 0; i < cColorEntries; i++)
  762. {
  763. if (pXlate[i] == i) { // Look for trivial mapping first
  764. pInvXlate[i] = (BYTE)i;
  765. }
  766. else
  767. {
  768. for (j = 0; j < cColorEntries; j++)
  769. {
  770. if (pXlate[j] == i) // Look for exact match
  771. {
  772. pInvXlate[i] = (BYTE)j;
  773. goto match_found;
  774. }
  775. }
  776. //
  777. // If we reach here, there is no exact match, so we should find the
  778. // closest fit. These indices should match the system colors
  779. // for 8-bit devices.
  780. //
  781. // Note that these pixel values cannot be generated by OpenGL
  782. // drawing with the current foreground translation vector.
  783. //
  784. if (cColorEntries == 256)
  785. {
  786. if (i <= 9)
  787. {
  788. if (iPixelType == PFD_TYPE_RGBA)
  789. pInvXlate[i] = vubSystemToRGB8[i];
  790. else
  791. pInvXlate[i] = (BYTE)i;
  792. }
  793. else if (i >= 246)
  794. {
  795. if (iPixelType == PFD_TYPE_RGBA)
  796. pInvXlate[i] = vubSystemToRGB8[i-236];
  797. else
  798. pInvXlate[i] = i-236;
  799. }
  800. }
  801. }
  802. match_found:;
  803. }
  804. }
  805. // er: similar to function in so_textu.c, but rounds up the result
  806. /*
  807. ** Return the log based 2 of a number
  808. **
  809. ** logTab1 returns (int)ceil(log2(index))
  810. ** logTab2 returns (int)log2(index)+1
  811. */
  812. static GLubyte logTab1[256] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
  813. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  814. 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  815. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  816. 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  817. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  818. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  819. 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  820. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  821. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  822. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  823. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  824. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  825. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  826. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  827. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
  828. };
  829. static GLubyte logTab2[256] = { 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  830. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  831. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  832. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  833. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  834. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  835. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  836. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  837. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  838. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  839. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  840. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  841. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  842. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  843. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  844. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
  845. };
  846. static GLint FASTCALL Log2RoundUp(GLint i)
  847. {
  848. if (i & 0xffff0000) {
  849. if (i & 0xff000000) {
  850. if (i & 0x00ffffff)
  851. return(logTab2[i >> 24] + 24);
  852. else
  853. return(logTab1[i >> 24] + 24);
  854. } else {
  855. if (i & 0x0000ffff)
  856. return(logTab2[i >> 16] + 16);
  857. else
  858. return(logTab1[i >> 16] + 16);
  859. }
  860. } else {
  861. if (i & 0xff00) {
  862. if (i & 0x00ff)
  863. return (logTab2[i >> 8] + 8);
  864. else
  865. return (logTab1[i >> 8] + 8);
  866. } else {
  867. return (logTab1[i]);
  868. }
  869. }
  870. }
  871. // default translation vector for 4-bit RGB
  872. static GLubyte vujRGBtoVGA[16] = {
  873. 0x0, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
  874. 0x0, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf
  875. };
  876. // SetColorTranslationVector
  877. // Sets up the translation vector, which may take 2 forms:
  878. // - In all 8,4-bit surfaces, get the translation vector with
  879. // wglCopyTranslateVector(), with 2**numBits byte entries.
  880. // - For 16,24,32 ColorIndex, get the mapping from index to RGB
  881. // value with wglGetPalette(). All entries in the table are unsigned
  882. // longs, with the first entry being the number of entries. This table
  883. // always has (2**n) <= 4096 entries, because gl assumes n bits are
  884. // used for color index.
  885. //
  886. // Synopsis:
  887. // void SetColorTranslationVector
  888. // __GLGENcontext *gengc generic RC
  889. // int cColorEntries number of color entries
  890. // int cColorBits number of color bits
  891. // int iPixelType specifies RGB or ColorIndex
  892. //
  893. // History:
  894. // Feb. 02 Eddie Robinson [v-eddier] Added support for 4-bit and 8-bit
  895. // Jan. 29 Marc Fortier [v-marcf] Wrote it.
  896. void
  897. SetColorTranslationVector(__GLGENcontext *gengc, int cColorEntries,
  898. int cColorBits, int iPixelType)
  899. {
  900. int numEntries, numBits;
  901. __GLcontextModes *Modes;
  902. BYTE ajBGRtoRGB[256];
  903. BYTE ajTmp[256];
  904. Modes = &((__GLcontext *)gengc)->modes;
  905. // Handle formats with a hardware palette (i.e., 4bpp and 8bpp).
  906. if ( cColorBits <= 8 )
  907. {
  908. int i;
  909. BYTE *pXlate;
  910. // Compute logical to system palette forward translation vector.
  911. if (!wglCopyTranslateVector(gengc, gengc->pajTranslateVector,
  912. cColorEntries))
  913. {
  914. // if foreground translation vector doesn't exist, build one
  915. pXlate = gengc->pajTranslateVector;
  916. if (cColorBits == 4)
  917. {
  918. // for RGB, map 1-1-1 to VGA colors. For CI, just map 1 to 1
  919. if (iPixelType == PFD_TYPE_COLORINDEX)
  920. {
  921. for (i = 0; i < 16; i++)
  922. pXlate[i] = (BYTE)i;
  923. }
  924. else
  925. {
  926. for (i = 0; i < 16; i++)
  927. pXlate[i] = vujRGBtoVGA[i];
  928. }
  929. }
  930. else
  931. {
  932. // for RGB, map 1 to 1. For CI display, 1 - 20 to system colors
  933. // for CI DIB, just map 1 to 1
  934. if ((iPixelType == PFD_TYPE_COLORINDEX) &&
  935. GLSURF_IS_DIRECTDC(gengc->dwCurrentFlags))
  936. {
  937. for (i = 0; i < 10; i++)
  938. pXlate[i] = (BYTE)i;
  939. for (i = 10; i < 20; i++)
  940. pXlate[i] = i + 236;
  941. }
  942. else
  943. {
  944. for (i = 0; i < cColorEntries; i++)
  945. pXlate[i] = (BYTE)i;
  946. }
  947. }
  948. }
  949. // Some MCD pixelformats specify a 233BGR (i.e., 2-bits blue in least
  950. // significant bits, etc.) bit ordering. Unfortunately, this is the
  951. // slow path for simulations. For those formats, we force the ordering
  952. // to RGB internally and reorder the pajTranslateVector to convert it
  953. // back to BGR before writing to the surface.
  954. if (gengc->flags & GENGC_MCD_BGR_INTO_RGB)
  955. {
  956. pXlate = gengc->pajTranslateVector;
  957. // Compute a 233BGR to 332RGB translation vector.
  958. for (i = 0; i < 256; i++)
  959. {
  960. ajBGRtoRGB[i] = (((i & 0x03) << 6) | // blue
  961. ((i & 0x1c) << 1) | // green
  962. ((i & 0xe0) >> 5)) // red
  963. & 0xff;
  964. }
  965. // Remap the tranlation vector to 332RGB.
  966. RtlCopyMemory(ajTmp, pXlate, 256);
  967. for (i = 0; i < 256; i++)
  968. {
  969. pXlate[ajBGRtoRGB[i]] = ajTmp[i];
  970. }
  971. }
  972. //!!!XXX -- I think the code below to fixup 4bpp is no longer needed.
  973. //!!!XXX There is now special case code in wglCopyTranslateVector
  974. #if 0
  975. // wglCopyTranslateVector = TRUE, and 4-bit: need some fixing up
  976. // For now, zero out upper nibble of returned xlate vector
  977. else if( cColorBits == 4 ) {
  978. pXlate = gengc->pajTranslateVector;
  979. for( i = 0; i < 16; i ++ )
  980. pXlate[i] &= 0xf;
  981. }
  982. #endif
  983. ComputeInverseTranslationVector( gengc, cColorEntries, iPixelType );
  984. #ifdef _CLIENTSIDE_
  985. SyncDibColorTables( gengc );
  986. #endif
  987. }
  988. // Handle formats w/o a hardware format (i.e., 16bpp, 24bpp, 32bpp).
  989. else
  990. {
  991. if( cColorEntries <= 256 ) {
  992. numEntries = 256;
  993. numBits = 8;
  994. }
  995. else
  996. {
  997. numBits = Log2RoundUp( cColorEntries );
  998. numEntries = 1 << numBits;
  999. }
  1000. // We will always allocate 4096 entries for CI mode with > 8 bits
  1001. // of color. This enables us to use a constant (0xfff) mask for
  1002. // color-index clamping.
  1003. ASSERTOPENGL(numEntries <= MAXPALENTRIES,
  1004. "Maximum color-index size exceeded");
  1005. if( (numBits == Modes->indexBits) &&
  1006. (gengc->pajTranslateVector != NULL) )
  1007. {
  1008. // New palette same size as previous
  1009. ULONG *pTrans;
  1010. int i;
  1011. // zero out some entries
  1012. pTrans = (ULONG *)gengc->pajTranslateVector + cColorEntries + 1;
  1013. for( i = cColorEntries + 1; i < MAXPALENTRIES; i ++ )
  1014. *pTrans++ = 0;
  1015. }
  1016. else
  1017. {
  1018. __GLcontext *gc = (__GLcontext *) gengc;
  1019. __GLcolorBuffer *cfb;
  1020. // New palette has different size
  1021. if( gengc->pajTranslateVector != NULL &&
  1022. (gengc->pajTranslateVector != gengc->xlatPalette) )
  1023. GCFREE(gc, gengc->pajTranslateVector );
  1024. gengc->pajTranslateVector =
  1025. GCALLOCZ(gc, (MAXPALENTRIES+1)*sizeof(ULONG));
  1026. // Change indexBits
  1027. Modes->indexBits = numBits;
  1028. // For depths greater than 8 bits, cfb->redMax must change if the
  1029. // number of entries in the palette changes.
  1030. // Also, change the writemask so that if the palette grows, the
  1031. // new planes will be enabled by default.
  1032. if (cfb = gc->front)
  1033. {
  1034. GLint oldRedMax;
  1035. oldRedMax = cfb->redMax;
  1036. cfb->redMax = (1 << gc->modes.indexBits) - 1;
  1037. gc->state.raster.writeMask |= ~oldRedMax;
  1038. gc->state.raster.writeMask &= cfb->redMax;
  1039. }
  1040. if (cfb = gc->back)
  1041. {
  1042. GLint oldRedMax;
  1043. oldRedMax = cfb->redMax;
  1044. cfb->redMax = (1 << gc->modes.indexBits) - 1;
  1045. gc->state.raster.writeMask |= ~oldRedMax;
  1046. gc->state.raster.writeMask &= cfb->redMax;
  1047. }
  1048. // store procs may need to be picked based on the change in
  1049. // palette size
  1050. __GL_DELAY_VALIDATE(gc);
  1051. #ifdef _MCD_
  1052. MCD_STATE_DIRTY(gc, FBUFCTRL);
  1053. #endif
  1054. }
  1055. // Compute index-to-color table from current palette information
  1056. wglComputeIndexedColors( gengc,
  1057. (unsigned long *) gengc->pajTranslateVector,
  1058. MAXPALENTRIES );
  1059. }
  1060. }
  1061. // HandlePaletteChanges
  1062. // Check if palette has changed, update translation vectors
  1063. // XXX add support for malloc failures at attention time
  1064. // Synopsis:
  1065. // void HandlePaletteChanges(
  1066. // __GLGENcontext *gengc specifies the generic RC
  1067. //
  1068. // Assumtions:
  1069. // x wglPaletteChanged() will always return 0 when no palette is set
  1070. // by the client. This has proved to be not always true.
  1071. //
  1072. // History:
  1073. // Feb. 25 Fixed by rightful owner
  1074. // Feb. ?? Mutilated by others
  1075. // Jan. 29 Marc Fortier [v-marcf] Wrote it.
  1076. void HandlePaletteChanges( __GLGENcontext *gengc, GLGENwindow *pwnd )
  1077. {
  1078. ULONG Timestamp;
  1079. GLuint paletteSize;
  1080. PIXELFORMATDESCRIPTOR *pfmt;
  1081. // No palettes for IC's
  1082. if (gengc->dwCurrentFlags & GLSURF_METAFILE)
  1083. {
  1084. return;
  1085. }
  1086. Timestamp = wglPaletteChanged(gengc, pwnd);
  1087. if (Timestamp != gengc->PaletteTimestamp)
  1088. {
  1089. pfmt = &gengc->gsurf.pfd;
  1090. if (pfmt->iPixelType == PFD_TYPE_COLORINDEX)
  1091. {
  1092. if (pfmt->cColorBits <= 8)
  1093. {
  1094. paletteSize = 1 << pfmt->cColorBits;
  1095. }
  1096. else
  1097. {
  1098. paletteSize = min(wglPaletteSize(gengc), MAXPALENTRIES);
  1099. }
  1100. }
  1101. else
  1102. {
  1103. #ifndef _CLIENTSIDE_
  1104. /* Only update RGB at makecurrent time */
  1105. if( (gengc->PaletteTimestamp == INITIAL_TIMESTAMP) &&
  1106. (pfmt->cColorBits <= 8) )
  1107. #else
  1108. if (pfmt->cColorBits <= 8)
  1109. #endif
  1110. {
  1111. paletteSize = 1 << pfmt->cColorBits;
  1112. }
  1113. else
  1114. {
  1115. paletteSize = 0;
  1116. }
  1117. }
  1118. if (paletteSize)
  1119. {
  1120. SetColorTranslationVector( gengc, paletteSize,
  1121. pfmt->cColorBits, pfmt->iPixelType );
  1122. }
  1123. EmptyFillStrokeCache(gengc);
  1124. gengc->PaletteTimestamp = Timestamp;
  1125. }
  1126. }
  1127. #ifdef _CLIENTSIDE_
  1128. /******************************Public*Routine******************************\
  1129. * wglFillBitfields
  1130. *
  1131. * Return the Red, Green, and Blue color masks based on the DC surface
  1132. * format. The masks are returned in the pdwColorFields array in the
  1133. * order: red mask, green mask, blue mask.
  1134. *
  1135. * Note:
  1136. * Should be called only for 16bpp or greater surfaces.
  1137. *
  1138. * History:
  1139. * 12-Jun-1995 -by- Gilman Wong [gilmanw]
  1140. * Wrote it.
  1141. \**************************************************************************/
  1142. void
  1143. wglFillBitfields(PIXELFORMATDESCRIPTOR *ppfd, DWORD *pdwColorFields)
  1144. {
  1145. *pdwColorFields++ = MaskFromBits(ppfd->cRedShift, ppfd->cRedBits );
  1146. *pdwColorFields++ = MaskFromBits(ppfd->cGreenShift, ppfd->cGreenBits);
  1147. *pdwColorFields++ = MaskFromBits(ppfd->cBlueShift, ppfd->cBlueBits );
  1148. }
  1149. /******************************Public*Routine******************************\
  1150. * wglCreateBitmap
  1151. *
  1152. * Create a DIB section and color table that matches the specified format.
  1153. *
  1154. * Returns:
  1155. * A valid bitmap handle if sucessful, NULL otherwise.
  1156. *
  1157. * History:
  1158. * 20-Sep-1994 -by- Gilman Wong [gilmanw]
  1159. * Wrote it.
  1160. \**************************************************************************/
  1161. HBITMAP
  1162. wglCreateBitmap( __GLGENcontext *gengc, SIZEL sizl, PVOID *ppvBits )
  1163. {
  1164. BITMAPINFO *pbmi;
  1165. HBITMAP hbmRet = (HBITMAP) NULL;
  1166. size_t cjbmi;
  1167. DWORD dwCompression;
  1168. DWORD cjImage = 0;
  1169. WORD wBitCount;
  1170. int cColors = 0;
  1171. *ppvBits = (PVOID) NULL;
  1172. // Figure out what kind of DIB needs to be created based on the
  1173. // DC format.
  1174. switch ( gengc->gsurf.pfd.cColorBits )
  1175. {
  1176. case 4:
  1177. cjbmi = sizeof(BITMAPINFO) + 16*sizeof(RGBQUAD);
  1178. dwCompression = BI_RGB;
  1179. wBitCount = 4;
  1180. cColors = 16;
  1181. break;
  1182. case 8:
  1183. cjbmi = sizeof(BITMAPINFO) + 256*sizeof(RGBQUAD);
  1184. dwCompression = BI_RGB;
  1185. wBitCount = 8;
  1186. cColors = 256;
  1187. break;
  1188. case 16:
  1189. cjbmi = sizeof(BITMAPINFO) + 3*sizeof(RGBQUAD);
  1190. dwCompression = BI_BITFIELDS;
  1191. cjImage = sizl.cx * sizl.cy * 2;
  1192. wBitCount = 16;
  1193. break;
  1194. case 24:
  1195. cjbmi = sizeof(BITMAPINFO);
  1196. dwCompression = BI_RGB;
  1197. wBitCount = 24;
  1198. break;
  1199. case 32:
  1200. cjbmi = sizeof(BITMAPINFO) + 3*sizeof(RGBQUAD);
  1201. dwCompression = BI_BITFIELDS;
  1202. cjImage = sizl.cx * sizl.cy * 4;
  1203. wBitCount = 32;
  1204. break;
  1205. default:
  1206. WARNING1("wglCreateBitmap: unknown format 0x%lx\n",
  1207. gengc->gsurf.pfd.cColorBits);
  1208. return (HBITMAP) NULL;
  1209. }
  1210. // Allocate the BITMAPINFO structure and color table.
  1211. pbmi = ALLOC(cjbmi);
  1212. if (pbmi)
  1213. {
  1214. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1215. pbmi->bmiHeader.biWidth = sizl.cx;
  1216. pbmi->bmiHeader.biHeight = sizl.cy;
  1217. pbmi->bmiHeader.biPlanes = 1;
  1218. pbmi->bmiHeader.biBitCount = wBitCount;
  1219. pbmi->bmiHeader.biCompression = dwCompression;
  1220. pbmi->bmiHeader.biSizeImage = cjImage;
  1221. pbmi->bmiHeader.biXPelsPerMeter = 0;
  1222. pbmi->bmiHeader.biYPelsPerMeter = 0;
  1223. pbmi->bmiHeader.biClrUsed = 0;
  1224. pbmi->bmiHeader.biClrImportant = 0;
  1225. // Initialize DIB color table.
  1226. switch (gengc->gsurf.pfd.cColorBits)
  1227. {
  1228. case 4:
  1229. case 8:
  1230. if (!wglGetSurfacePalette(gengc, &pbmi->bmiColors[0], FALSE))
  1231. {
  1232. return NULL;
  1233. }
  1234. break;
  1235. case 16:
  1236. case 32:
  1237. wglFillBitfields(&gengc->gsurf.pfd, (DWORD *) &pbmi->bmiColors[0]);
  1238. break;
  1239. case 24:
  1240. // Color table is assumed to be BGR for 24BPP DIBs. Nothing to do.
  1241. break;
  1242. }
  1243. // Create DIB section.
  1244. hbmRet = CreateDIBSection(gengc->gwidCurrent.hdc, pbmi, DIB_RGB_COLORS,
  1245. ppvBits, NULL, 0);
  1246. #if DBG
  1247. if ( hbmRet == (HBITMAP) NULL )
  1248. WARNING("wglCreateBitmap(): DIB section creation failed\n");
  1249. #endif
  1250. FREE(pbmi);
  1251. }
  1252. else
  1253. {
  1254. WARNING("wglCreateBitmap(): memory allocation error\n");
  1255. }
  1256. return hbmRet;
  1257. }
  1258. #endif
  1259. /******************************Public*Routine******************************\
  1260. * wglCreateScanlineBuffers
  1261. *
  1262. * Allocate the scanline buffers. The scanline buffers are used by the
  1263. * generic implementation to write data to the target (display or bitmap)
  1264. * a span at a time when the target surface is not directly accessible.
  1265. *
  1266. * Returns:
  1267. * TRUE if successful, FALSE otherwise.
  1268. *
  1269. * History:
  1270. * 17-Apr-1996 -by- Gilman Wong [gilmanw]
  1271. * Taken from CreateGDIObjects and made into function.
  1272. \**************************************************************************/
  1273. BOOL FASTCALL wglCreateScanlineBuffers(__GLGENcontext *gengc)
  1274. {
  1275. BOOL bRet = FALSE;
  1276. PIXELFORMATDESCRIPTOR *pfmt;
  1277. UINT cBits;
  1278. UINT cBytes;
  1279. SIZEL size;
  1280. int cColorEntries;
  1281. __GLcontext *gc;
  1282. gc = (__GLcontext *)gengc;
  1283. pfmt = &gengc->gsurf.pfd;
  1284. //
  1285. // Bitmap must have DWORD sized scanlines.
  1286. //
  1287. cBits = BITS_ALIGNDWORD(__GL_MAX_WINDOW_WIDTH * pfmt->cColorBits);
  1288. cBytes = cBits / 8;
  1289. //
  1290. // Create color scanline DIB buffer.
  1291. //
  1292. size.cx = cBits / pfmt->cColorBits;
  1293. size.cy = 1;
  1294. gengc->ColorsMemDC = CreateCompatibleDC(gengc->gwidCurrent.hdc);
  1295. gengc->ColorsBitmap = wglCreateBitmap(gengc, size,
  1296. &gengc->ColorsBits);
  1297. if ( (NULL == gengc->ColorsMemDC) ||
  1298. (NULL == gengc->ColorsBitmap) ||
  1299. (NULL == gengc->ColorsBits) ||
  1300. !SelectObject(gengc->ColorsMemDC, gengc->ColorsBitmap) )
  1301. {
  1302. #if DBG
  1303. if (!gengc->ColorsMemDC)
  1304. WARNING("wglCreateScanlineBuffers: dc creation failed, ColorsMemDC\n");
  1305. if (!gengc->ColorsBitmap)
  1306. WARNING("wglCreateScanlineBuffers: bitmap creation failed, ColorsBitmap\n");
  1307. if (!gengc->ColorsBits)
  1308. WARNING("wglCreateScanlineBuffers: bitmap creation failed, ColorsBits\n");
  1309. #endif
  1310. goto wglCreateScanlineBuffers_exit;
  1311. }
  1312. //
  1313. // Screen to DIB BitBlt performance on Win95 is very poor.
  1314. // By doing the BitBlt via an intermediate DDB, we can avoid
  1315. // a lot of unnecessary overhead. So create an intermediate
  1316. // scanline DDB to match ColorsBitmap.
  1317. //
  1318. if ((gengc->dwCurrentFlags & GLSURF_DIRECTDRAW) == 0)
  1319. {
  1320. gengc->ColorsDdbDc = CreateCompatibleDC(gengc->gwidCurrent.hdc);
  1321. gengc->ColorsDdb = CreateCompatibleBitmap(gengc->gwidCurrent.hdc,
  1322. size.cx, size.cy);
  1323. //!!!Viper fix -- Diamond Viper (Weitek 9000) fails
  1324. //!!! CreateCompatibleBitmap for some (currently unknown)
  1325. //!!! reason
  1326. if ( !gengc->ColorsDdb )
  1327. {
  1328. WARNING("wglCreateScanlineBuffers: "
  1329. "CreateCompatibleBitmap failed\n");
  1330. if (gengc->ColorsDdbDc)
  1331. DeleteDC(gengc->ColorsDdbDc);
  1332. gengc->ColorsDdbDc = (HDC) NULL;
  1333. }
  1334. else
  1335. {
  1336. if ( (NULL == gengc->ColorsDdbDc) ||
  1337. !SelectObject(gengc->ColorsDdbDc, gengc->ColorsDdb) )
  1338. {
  1339. #if DBG
  1340. if (!gengc->ColorsDdbDc)
  1341. WARNING("wglCreateScanlineBuffers: "
  1342. "dc creation failed, ColorsDdbDc\n");
  1343. if (!gengc->ColorsDdb)
  1344. WARNING("wglCreateScanlineBuffers: "
  1345. "bitmap creation failed, ColorsDdb\n");
  1346. #endif
  1347. goto wglCreateScanlineBuffers_exit;
  1348. }
  1349. }
  1350. }
  1351. //
  1352. // Success.
  1353. //
  1354. bRet = TRUE;
  1355. wglCreateScanlineBuffers_exit:
  1356. if (!bRet)
  1357. {
  1358. //
  1359. // Error case. Delete whatever may have been allocated.
  1360. //
  1361. wglDeleteScanlineBuffers(gengc);
  1362. }
  1363. return bRet;
  1364. }
  1365. /******************************Public*Routine******************************\
  1366. * wglDeleteScanlineBuffers
  1367. *
  1368. * Delete the scanline buffers. The scanline buffers are used by the
  1369. * generic implementation to write data to the target (display or bitmap)
  1370. * a span at a time when the target surface is not directly accessible.
  1371. *
  1372. * History:
  1373. * 17-Apr-1996 -by- Gilman Wong [gilmanw]
  1374. * Taken from CreateGDIObjects and made into function.
  1375. \**************************************************************************/
  1376. VOID FASTCALL wglDeleteScanlineBuffers(__GLGENcontext *gengc)
  1377. {
  1378. __GLcontext *gc = (__GLcontext *)gengc;
  1379. //
  1380. // Delete color scanline DIB buffer.
  1381. //
  1382. if (gengc->ColorsMemDC)
  1383. {
  1384. DeleteDC(gengc->ColorsMemDC);
  1385. gengc->ColorsMemDC = NULL;
  1386. }
  1387. if (gengc->ColorsBitmap)
  1388. {
  1389. if (!DeleteObject(gengc->ColorsBitmap))
  1390. ASSERTOPENGL(FALSE, "wglDeleteScanlineBuffers: DeleteObject failed");
  1391. gengc->ColorsBitmap = NULL;
  1392. gengc->ColorsBits = NULL; // deleted for us when DIB section dies
  1393. }
  1394. //
  1395. // Delete intermediate color scanline DDB buffer.
  1396. //
  1397. if (gengc->ColorsDdbDc)
  1398. {
  1399. if (!DeleteDC(gengc->ColorsDdbDc))
  1400. {
  1401. ASSERTOPENGL(FALSE, "wglDeleteScanlineBuffers: DDB DeleteDC failed");
  1402. }
  1403. gengc->ColorsDdbDc = NULL;
  1404. }
  1405. if (gengc->ColorsDdb)
  1406. {
  1407. if (!DeleteObject(gengc->ColorsDdb))
  1408. {
  1409. ASSERTOPENGL(FALSE, "wglDeleteScanlineBuffers: DDB DeleteObject failed");
  1410. }
  1411. gengc->ColorsDdb = NULL;
  1412. }
  1413. }
  1414. /******************************Public*Routine******************************\
  1415. * wglInitializeColorBuffers
  1416. *
  1417. * Initialize the color buffer (front and/or back) information.
  1418. *
  1419. * History:
  1420. * 17-Apr-1996 -by- Gilman Wong [gilmanw]
  1421. * Taken out of glsrvCreateContext and made into function.
  1422. \**************************************************************************/
  1423. VOID FASTCALL wglInitializeColorBuffers(__GLGENcontext *gengc)
  1424. {
  1425. __GLcontext *gc = &gengc->gc;
  1426. gc->front = &gc->frontBuffer;
  1427. if ( gc->modes.doubleBufferMode)
  1428. {
  1429. gc->back = &gc->backBuffer;
  1430. if (gc->modes.colorIndexMode)
  1431. {
  1432. __glGenInitCI(gc, gc->front, GL_FRONT);
  1433. __glGenInitCI(gc, gc->back, GL_BACK);
  1434. }
  1435. else
  1436. {
  1437. __glGenInitRGB(gc, gc->front, GL_FRONT);
  1438. __glGenInitRGB(gc, gc->back, GL_BACK);
  1439. }
  1440. }
  1441. else
  1442. {
  1443. if (gc->modes.colorIndexMode)
  1444. {
  1445. __glGenInitCI(gc, gc->front, GL_FRONT);
  1446. }
  1447. else
  1448. {
  1449. __glGenInitRGB(gc, gc->front, GL_FRONT);
  1450. }
  1451. }
  1452. }
  1453. /******************************Public*Routine******************************\
  1454. * wglInitializeDepthBuffer
  1455. *
  1456. * Initialize the depth buffer information.
  1457. *
  1458. * History:
  1459. * 17-Apr-1996 -by- Gilman Wong [gilmanw]
  1460. * Taken out of glsrvCreateContext and made into function.
  1461. \**************************************************************************/
  1462. VOID FASTCALL wglInitializeDepthBuffer(__GLGENcontext *gengc)
  1463. {
  1464. __GLcontext *gc = &gengc->gc;
  1465. if (gc->modes.depthBits)
  1466. {
  1467. if (gengc->_pMcdState) {
  1468. // This is not the final initialization of the MCD depth buffer.
  1469. // This is being done now so that the validate proc can be done
  1470. // in glsrvCreateContext. The real initialization will occur
  1471. // during glsrvMakeCurrent.
  1472. GenMcdInitDepth(gc, &gc->depthBuffer);
  1473. gc->depthBuffer.scale = gengc->_pMcdState->McdRcInfo.depthBufferMax;
  1474. } else if (gc->modes.depthBits == 16) {
  1475. DBGINFO("CALLING: __glInitDepth16\n");
  1476. __glInitDepth16(gc, &gc->depthBuffer);
  1477. gc->depthBuffer.scale = 0x7fff;
  1478. } else {
  1479. DBGINFO("CALLING: __glInitDepth32\n");
  1480. __glInitDepth32(gc, &gc->depthBuffer);
  1481. gc->depthBuffer.scale = 0x7fffffff;
  1482. }
  1483. /*
  1484. * Note: scale factor does not use the high bit (this avoids
  1485. * floating point exceptions).
  1486. */
  1487. // XXX (mf) I changed 16 bit depth buffer to use high bit, since
  1488. // there is no possibility of overflow on conversion to float. For
  1489. // 32-bit, (float) 0xffffffff overflows to 0. I was able to avoid
  1490. // overflow in this case by using a scale factor of 0xffffff7f, but
  1491. // this is a weird number, and 31 bits is enough resolution anyways.
  1492. // !! Note asserts in px_fast.c that have hardcoded depth scales.
  1493. }
  1494. #ifdef _MCD_
  1495. else
  1496. {
  1497. // This is not the final initialization of the MCD depth buffer.
  1498. // This is being done now so that the validate proc can be done
  1499. // in glsrvCreateContext. The real initialization will occur
  1500. // during glsrvMakeCurrent.
  1501. GenMcdInitDepth(gc, &gc->depthBuffer);
  1502. gc->depthBuffer.scale = 0x7fffffff;
  1503. }
  1504. #endif
  1505. }
  1506. /******************************Public*Routine******************************\
  1507. * wglInitializePixelCopyFuncs
  1508. *
  1509. * Set the appropriate CopyPixels and PixelVisible functions in the context.
  1510. *
  1511. * History:
  1512. * 18-Apr-1996 -by- Gilman Wong [gilmanw]
  1513. * Taken out of glsrvCreateContext and made into function.
  1514. \**************************************************************************/
  1515. VOID FASTCALL wglInitializePixelCopyFuncs(__GLGENcontext *gengc)
  1516. {
  1517. __GLcontext *gc = &gengc->gc;
  1518. if ( gc->front->buf.flags & DIB_FORMAT )
  1519. gengc->pfnCopyPixels = dibCopyPixels;
  1520. else {
  1521. if (gengc->pMcdState) {
  1522. gengc->ColorsBits = gengc->pMcdState->pMcdSurf->McdColorBuf.pv;
  1523. gengc->pfnCopyPixels = GenMcdCopyPixels;
  1524. }
  1525. else
  1526. gengc->pfnCopyPixels = gdiCopyPixels;
  1527. }
  1528. gengc->pfnPixelVisible = wglPixelVisible;
  1529. }
  1530. /******************************Public*Routine******************************\
  1531. * CreateGDIObjects
  1532. *
  1533. * Create various buffers, and GDI objects that we will always need.
  1534. *
  1535. * Called from glsrvCreateContext().
  1536. *
  1537. * Returns:
  1538. * TRUE if sucessful, FALSE if error.
  1539. *
  1540. \**************************************************************************/
  1541. BOOL FASTCALL CreateGDIObjects(__GLGENcontext *gengc)
  1542. {
  1543. PIXELFORMATDESCRIPTOR *pfmt;
  1544. UINT cBytes;
  1545. SIZEL size;
  1546. int cColorEntries;
  1547. __GLcontext *gc;
  1548. gc = (__GLcontext *)gengc;
  1549. pfmt = &gengc->gsurf.pfd;
  1550. //
  1551. // Palette translation vectors.
  1552. //
  1553. // If not a true color surface, need space for the foreground xlation
  1554. //
  1555. if (pfmt->cColorBits <= 8)
  1556. {
  1557. cColorEntries = 1 << pfmt->cColorBits;
  1558. ASSERTOPENGL(NULL == gengc->pajTranslateVector, "have a xlate vector");
  1559. //
  1560. // Just set the translation vector to the cache space in the gc:
  1561. //
  1562. gengc->pajTranslateVector = gengc->xlatPalette;
  1563. //
  1564. // Allocate the inverse translation vector.
  1565. //
  1566. ASSERTOPENGL(NULL == gengc->pajInvTranslateVector, "have an inv xlate vector");
  1567. gengc->pajInvTranslateVector = GCALLOCZ(gc, cColorEntries);
  1568. if (NULL == gengc->pajInvTranslateVector)
  1569. {
  1570. WARNING("CreateGDIObjects: memory allocation failed, pajInvTrans\n");
  1571. goto ERROR_EXIT;
  1572. }
  1573. }
  1574. //
  1575. // Scanline buffers.
  1576. //
  1577. // Always create engine bitmaps to provide a generic means of performing
  1578. // pixel transfers using the ColorsBits and StippleBits buffers.
  1579. //
  1580. if (NULL == gengc->ColorsBits)
  1581. {
  1582. //
  1583. // Color scanline buffer
  1584. //
  1585. #ifdef _MCD_
  1586. //
  1587. // If MCD the ColorBits will be set when the MCD surface is
  1588. // created, so nothing to do.
  1589. //
  1590. // Otherwise, create the generic scanline buffer.
  1591. //
  1592. //
  1593. if (!gengc->_pMcdState)
  1594. #endif
  1595. {
  1596. //
  1597. // Generic case.
  1598. //
  1599. if (!wglCreateScanlineBuffers(gengc))
  1600. {
  1601. WARNING("CreateGDIObjects: wglCreateScanlineBuffers failed\n");
  1602. goto ERROR_EXIT;
  1603. }
  1604. }
  1605. //
  1606. // Stipple scanline buffer.
  1607. //
  1608. // Bitmap must have DWORD sized scanlines. Note that stipple only
  1609. // requires a 1 bit per pel bitmap.
  1610. ASSERTOPENGL(NULL == gengc->StippleBits, "StippleBits not null");
  1611. size.cx = BITS_ALIGNDWORD(__GL_MAX_WINDOW_WIDTH);
  1612. cBytes = size.cx / 8;
  1613. gengc->StippleBits = GCALLOCZ(gc, cBytes);
  1614. if (NULL == gengc->StippleBits)
  1615. {
  1616. WARNING("CreateGDIObjects: memory allocation failed, StippleBits\n");
  1617. goto ERROR_EXIT;
  1618. }
  1619. ASSERTOPENGL(NULL == gengc->StippleBitmap, "StippleBitmap not null");
  1620. #ifndef _CLIENTSIDE_
  1621. //!!!XXX -- why are we even bothering to make the stipple an engine bitmap?
  1622. //!!!XXX It is never used as such (at least not yet).
  1623. gengc->StippleBitmap = EngCreateBitmap(
  1624. size,
  1625. cBytes,
  1626. BMF_1BPP,
  1627. 0,
  1628. gengc->StippleBits);
  1629. if (NULL == gengc->StippleBitmap)
  1630. {
  1631. WARNING("CreateGDIObjects: memory allocation failed, StippleBitmap\n");
  1632. goto ERROR_EXIT;
  1633. }
  1634. #else
  1635. gengc->StippleBitmap = (HBITMAP) NULL;
  1636. #endif
  1637. }
  1638. return TRUE;
  1639. ERROR_EXIT:
  1640. //
  1641. // Error cleanup --
  1642. // Destroy everything we might have created, return false so makecurrent fails.
  1643. //
  1644. if (gengc->pajTranslateVector &&
  1645. (gengc->pajTranslateVector != gengc->xlatPalette))
  1646. {
  1647. GCFREE(gc,gengc->pajTranslateVector);
  1648. gengc->pajTranslateVector = NULL;
  1649. }
  1650. if (gengc->pajInvTranslateVector)
  1651. {
  1652. GCFREE(gc,gengc->pajInvTranslateVector);
  1653. gengc->pajInvTranslateVector = NULL;
  1654. }
  1655. wglDeleteScanlineBuffers(gengc);
  1656. if (gengc->StippleBits)
  1657. {
  1658. GCFREE(gc,gengc->StippleBits);
  1659. gengc->StippleBits = NULL;
  1660. }
  1661. #ifndef _CLIENTSIDE_
  1662. if (gengc->StippleBitmap)
  1663. {
  1664. if (!EngDeleteSurface((HSURF)gengc->StippleBitmap))
  1665. ASSERTOPENGL(FALSE, "EngDeleteSurface failed");
  1666. gengc->StippleBitmap = NULL;
  1667. }
  1668. #endif
  1669. return FALSE;
  1670. }
  1671. /******************************Public*Routine******************************\
  1672. * ApplyViewport
  1673. *
  1674. * Recompute viewport state and clipbox. May also be called via the
  1675. * applyViewport function pointer in the gc's proc table.
  1676. *
  1677. \**************************************************************************/
  1678. // This routine can be called because of a user vieport command, or because
  1679. // of a change in the size of the window
  1680. static void FASTCALL ApplyViewport(__GLcontext *gc)
  1681. {
  1682. GLint xlow, ylow, xhigh, yhigh;
  1683. GLint llx, lly, urx, ury;
  1684. GLboolean lastReasonable;
  1685. GLGENwindow *pwnd;
  1686. GLint clipLeft, clipRight, clipTop, clipBottom;
  1687. __GLGENcontext *gengc = (__GLGENcontext *) gc;
  1688. DBGENTRY("ApplyViewport\n");
  1689. ASSERTOPENGL(gengc->pwndLocked != NULL,
  1690. "ApplyViewport called without lock\n");
  1691. pwnd = gengc->pwndLocked;
  1692. if (pwnd)
  1693. {
  1694. gengc->visibleWidth = pwnd->rclBounds.right - pwnd->rclBounds.left;
  1695. gengc->visibleHeight = pwnd->rclBounds.bottom - pwnd->rclBounds.top;
  1696. }
  1697. else
  1698. {
  1699. gengc->visibleWidth = 0;
  1700. gengc->visibleHeight = 0;
  1701. }
  1702. // Sanity check the info from window.
  1703. ASSERTOPENGL(
  1704. gengc->visibleWidth <= __GL_MAX_WINDOW_WIDTH && gengc->visibleHeight <= __GL_MAX_WINDOW_HEIGHT,
  1705. "ApplyViewport(): bad visible rect size\n"
  1706. );
  1707. /* If this viewport is fully contained in the window, we note this fact,
  1708. ** and this can save us on scissoring tests.
  1709. */
  1710. if (gc->state.enables.general & __GL_SCISSOR_TEST_ENABLE)
  1711. {
  1712. xlow = gc->state.scissor.scissorX;
  1713. xhigh = xlow + gc->state.scissor.scissorWidth;
  1714. ylow = gc->state.scissor.scissorY;
  1715. yhigh = ylow + gc->state.scissor.scissorHeight;
  1716. }
  1717. else
  1718. {
  1719. xlow = 0;
  1720. ylow = 0;
  1721. xhigh = gc->constants.width;
  1722. yhigh = gc->constants.height;
  1723. }
  1724. /*
  1725. ** convert visible region to GL coords and intersect with scissor
  1726. */
  1727. if (pwnd)
  1728. {
  1729. clipLeft = pwnd->rclBounds.left - pwnd->rclClient.left;
  1730. clipRight = pwnd->rclBounds.right - pwnd->rclClient.left;
  1731. clipTop = gc->constants.height -
  1732. (pwnd->rclBounds.top - pwnd->rclClient.top);
  1733. clipBottom = gc->constants.height -
  1734. (pwnd->rclBounds.bottom - pwnd->rclClient.top);
  1735. }
  1736. else
  1737. {
  1738. clipLeft = 0;
  1739. clipRight = 0;
  1740. clipTop = 0;
  1741. clipBottom = 0;
  1742. }
  1743. if (xlow < clipLeft) xlow = clipLeft;
  1744. if (xhigh > clipRight) xhigh = clipRight;
  1745. if (ylow < clipBottom) ylow = clipBottom;
  1746. if (yhigh > clipTop) yhigh = clipTop;
  1747. // ComputeClipBox
  1748. {
  1749. if (xlow >= xhigh || ylow >= yhigh)
  1750. {
  1751. gc->transform.clipX0 = gc->constants.viewportXAdjust;
  1752. gc->transform.clipX1 = gc->constants.viewportXAdjust;
  1753. gc->transform.clipY0 = gc->constants.viewportYAdjust;
  1754. gc->transform.clipY1 = gc->constants.viewportYAdjust;
  1755. }
  1756. else
  1757. {
  1758. gc->transform.clipX0 = xlow + gc->constants.viewportXAdjust;
  1759. gc->transform.clipX1 = xhigh + gc->constants.viewportXAdjust;
  1760. if (gc->constants.yInverted) {
  1761. gc->transform.clipY0 = (gc->constants.height - yhigh) +
  1762. gc->constants.viewportYAdjust;
  1763. gc->transform.clipY1 = (gc->constants.height - ylow) +
  1764. gc->constants.viewportYAdjust;
  1765. } else {
  1766. gc->transform.clipY0 = ylow + gc->constants.viewportYAdjust;
  1767. gc->transform.clipY1 = yhigh + gc->constants.viewportYAdjust;
  1768. }
  1769. }
  1770. }
  1771. llx = (GLint)gc->state.viewport.x;
  1772. lly = (GLint)gc->state.viewport.y;
  1773. urx = llx + (GLint)gc->state.viewport.width;
  1774. ury = lly + (GLint)gc->state.viewport.height;
  1775. #ifdef NT
  1776. gc->transform.miny = (gc->constants.height - ury) +
  1777. gc->constants.viewportYAdjust;
  1778. gc->transform.maxy = gc->transform.miny + (GLint)gc->state.viewport.height;
  1779. gc->transform.fminy = (__GLfloat)gc->transform.miny;
  1780. gc->transform.fmaxy = (__GLfloat)gc->transform.maxy;
  1781. // The viewport xScale, xCenter, yScale and yCenter values are computed in
  1782. // first MakeCurrent and subsequent glViewport calls. When the window is
  1783. // resized (i.e. gc->constatns.height changes), however, we need to recompute
  1784. // yCenter if yInverted is TRUE.
  1785. if (gc->constants.yInverted)
  1786. {
  1787. __GLfloat hh, h2;
  1788. h2 = gc->state.viewport.height * __glHalf;
  1789. hh = h2 - gc->constants.viewportEpsilon;
  1790. gc->state.viewport.yCenter =
  1791. gc->constants.height - (gc->state.viewport.y + h2) +
  1792. gc->constants.fviewportYAdjust;
  1793. #if 0
  1794. DbgPrint("AV ys %.3lf, yc %.3lf (%.3lf)\n",
  1795. -hh, gc->state.viewport.yCenter,
  1796. gc->constants.height - (gc->state.viewport.y + h2));
  1797. #endif
  1798. }
  1799. #else
  1800. ww = gc->state.viewport.width * __glHalf;
  1801. hh = gc->state.viewport.height * __glHalf;
  1802. gc->state.viewport.xScale = ww;
  1803. gc->state.viewport.xCenter = gc->state.viewport.x + ww +
  1804. gc->constants.fviewportXAdjust;
  1805. if (gc->constants.yInverted) {
  1806. gc->state.viewport.yScale = -hh;
  1807. gc->state.viewport.yCenter =
  1808. (gc->constants.height - gc->constants.viewportEpsilon) -
  1809. (gc->state.viewport.y + hh) +
  1810. gc->constants.fviewportYAdjust;
  1811. } else {
  1812. gc->state.viewport.yScale = hh;
  1813. gc->state.viewport.yCenter = gc->state.viewport.y + hh +
  1814. gc->constants.fviewportYAdjust;
  1815. }
  1816. #endif
  1817. // Remember the current reasonableViewport. If it changes, we may
  1818. // need to change the pick procs.
  1819. lastReasonable = gc->transform.reasonableViewport;
  1820. // Is viewport entirely within the visible bounding rectangle (which
  1821. // includes scissoring if it is turned on)? reasonableViewport is
  1822. // TRUE if so, FALSE otherwise.
  1823. // The viewport must also have a non-zero size to be reasonable
  1824. if (llx >= xlow && lly >= ylow && urx <= xhigh && ury <= yhigh &&
  1825. urx-llx >= 1 && ury-lly >= 1)
  1826. {
  1827. gc->transform.reasonableViewport = GL_TRUE;
  1828. } else {
  1829. gc->transform.reasonableViewport = GL_FALSE;
  1830. }
  1831. #if 0
  1832. DbgPrint("%3X:Clipbox %4d,%4d - %4d,%4d, reasonable %d, g %p, w %p\n",
  1833. GetCurrentThreadId(),
  1834. gc->transform.clipX0, gc->transform.clipY0,
  1835. gc->transform.clipX1, gc->transform.clipY1,
  1836. gc->transform.reasonableViewport,
  1837. gc, ((__GLGENcontext *)gc)->pwndLocked);
  1838. #endif
  1839. #ifdef NT
  1840. // To be safe than sorry. The new poly array does not break up Begin/End pair.
  1841. if (lastReasonable != gc->transform.reasonableViewport)
  1842. __GL_DELAY_VALIDATE(gc);
  1843. #ifdef _MCD_
  1844. MCD_STATE_DIRTY(gc, VIEWPORT);
  1845. #endif
  1846. #else
  1847. // Old code use to use __GL_DELAY_VALIDATE() macro, this would
  1848. // blow up if resize/position changed and a flush occured between
  1849. // a glBegin/End pair. Only need to pick span, line, & triangle procs
  1850. // since that is safe
  1851. if (lastReasonable != gc->transform.reasonableViewport) {
  1852. (*gc->procs.pickSpanProcs)(gc);
  1853. (*gc->procs.pickTriangleProcs)(gc);
  1854. (*gc->procs.pickLineProcs)(gc);
  1855. }
  1856. #endif
  1857. }
  1858. /******************************Public*Routine******************************\
  1859. * __glGenFreeBuffers
  1860. *
  1861. * Free the __GLGENbuffers structure and its associated ancillary and
  1862. * back buffers.
  1863. *
  1864. \**************************************************************************/
  1865. void FASTCALL __glGenFreeBuffers(__GLGENbuffers *buffers)
  1866. {
  1867. if (buffers == NULL)
  1868. {
  1869. return;
  1870. }
  1871. #if DBG
  1872. DBGBEGIN(LEVEL_INFO)
  1873. DbgPrint("glGenFreeBuffers 0x%x, 0x%x, 0x%x, 0x%x\n",
  1874. buffers->accumBuffer.base,
  1875. buffers->stencilBuffer.base,
  1876. buffers->depthBuffer.base,
  1877. buffers);
  1878. DBGEND;
  1879. #endif
  1880. //
  1881. // Free ancillary buffers
  1882. //
  1883. if (buffers->accumBuffer.base) {
  1884. DBGLEVEL(LEVEL_ALLOC, "__glGenFreeBuffers: Freeing accumulation buffer\n");
  1885. FREE(buffers->accumBuffer.base);
  1886. }
  1887. if (buffers->stencilBuffer.base) {
  1888. DBGLEVEL(LEVEL_ALLOC, "__glGenFreeBuffers: Freeing stencil buffer\n");
  1889. FREE(buffers->stencilBuffer.base);
  1890. }
  1891. //
  1892. // Free alpha buffers
  1893. //
  1894. if (buffers->alphaBuffer0.base) {
  1895. DBGLEVEL(LEVEL_ALLOC, "__glGenFreeBuffers: Freeing alpha buffer 0\n");
  1896. FREE(buffers->alphaBuffer0.base);
  1897. }
  1898. if (buffers->alphaBuffer1.base) {
  1899. DBGLEVEL(LEVEL_ALLOC, "__glGenFreeBuffers: Freeing alpha buffer 1\n");
  1900. FREE(buffers->alphaBuffer1.base);
  1901. }
  1902. //
  1903. // If its not an MCD managed depth buffer, free the depth
  1904. // buffer.
  1905. //
  1906. if (buffers->resizeDepth != ResizeUnownedDepthBuffer)
  1907. {
  1908. if (buffers->depthBuffer.base)
  1909. {
  1910. DBGLEVEL(LEVEL_ALLOC,
  1911. "__glGenFreeBuffers: Freeing depth buffer\n");
  1912. FREE(buffers->depthBuffer.base);
  1913. }
  1914. }
  1915. //
  1916. // Free back buffer if we allocated one
  1917. //
  1918. if (buffers->backBitmap.pvBits) {
  1919. // Note: the DIB section deletion will delete
  1920. // buffers->backBitmap.pvBits for us
  1921. if (!DeleteDC(buffers->backBitmap.hdc))
  1922. WARNING("__glGenFreeBuffers: DeleteDC failed\n");
  1923. DeleteObject(buffers->backBitmap.hbm);
  1924. }
  1925. #ifdef _MCD_
  1926. //
  1927. // Free MCD surface.
  1928. //
  1929. if (buffers->pMcdSurf) {
  1930. GenMcdDeleteSurface(buffers->pMcdSurf);
  1931. }
  1932. #endif
  1933. //
  1934. // free up swap hint region
  1935. //
  1936. {
  1937. PYLIST pylist;
  1938. PXLIST pxlist;
  1939. RECTLISTSetEmpty(&buffers->rl);
  1940. //
  1941. // Free up the free lists
  1942. //
  1943. pylist = buffers->pylist;
  1944. while (pylist) {
  1945. PYLIST pylistKill = pylist;
  1946. pylist = pylist->pnext;
  1947. FREE(pylistKill);
  1948. }
  1949. buffers->pylist = NULL;
  1950. pxlist = buffers->pxlist;
  1951. while (pxlist) {
  1952. PXLIST pxlistKill = pxlist;
  1953. pxlist = pxlist->pnext;
  1954. FREE(pxlistKill);
  1955. }
  1956. buffers->pxlist = NULL;
  1957. }
  1958. //
  1959. // Free the private structure
  1960. //
  1961. FREE(buffers);
  1962. }
  1963. /******************************Public*Routine******************************\
  1964. * __glGenAllocAndInitPrivateBufferStruct
  1965. *
  1966. * Allocates and initializes a __GLGENbuffers structure and saves it as
  1967. * the drawable private data.
  1968. *
  1969. * The __GLGENbuffers structure contains the shared ancillary and back
  1970. * buffers, as well as the cache of clip rectangles enumerated from the
  1971. * CLIPOBJ.
  1972. *
  1973. * The __GLGENbuffers structure and its data is freed by calling
  1974. * __glGenFreeBuffers.
  1975. *
  1976. * Returns:
  1977. * NULL if error.
  1978. *
  1979. \**************************************************************************/
  1980. static __GLGENbuffers *
  1981. __glGenAllocAndInitPrivateBufferStruct(__GLcontext *gc)
  1982. {
  1983. __GLGENbuffers *buffers;
  1984. __GLGENcontext *gengc = (__GLGENcontext *)gc;
  1985. PIXELFORMATDESCRIPTOR *ppfd = &gengc->gsurf.pfd;
  1986. /* No private structure, no ancillary buffers */
  1987. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: No private struct existed\n");
  1988. buffers = (__GLGENbuffers *)ALLOCZ(sizeof(__GLGENbuffers));
  1989. if (NULL == buffers)
  1990. return(NULL);
  1991. buffers->resize = ResizeAncillaryBuffer;
  1992. buffers->resizeDepth = ResizeAncillaryBuffer;
  1993. buffers->accumBuffer.elementSize = gc->accumBuffer.buf.elementSize;
  1994. buffers->depthBuffer.elementSize = gc->depthBuffer.buf.elementSize;
  1995. buffers->stencilBuffer.elementSize = gc->stencilBuffer.buf.elementSize;
  1996. buffers->stencilBits = ppfd->cStencilBits;
  1997. buffers->depthBits = ppfd->cDepthBits;
  1998. buffers->accumBits = ppfd->cAccumBits;
  1999. buffers->colorBits = ppfd->cColorBits;
  2000. buffers->alphaBits = ppfd->cAlphaBits;
  2001. if (gc->modes.accumBits) {
  2002. gc->accumBuffer.buf.base = 0;
  2003. gc->accumBuffer.buf.size = 0;
  2004. gc->accumBuffer.buf.outerWidth = 0;
  2005. }
  2006. buffers->alphaFrontBuffer = buffers->alphaBackBuffer = NULL;
  2007. // These base values must *always* be set to 0, regardless of alphaBits,
  2008. // since base will be free'd if non-zero on buffer deletion
  2009. buffers->alphaBuffer0.base = 0;
  2010. buffers->alphaBuffer1.base = 0;
  2011. if (gc->modes.alphaBits) {
  2012. buffers->alphaBuffer0.size = 0;
  2013. buffers->alphaBuffer0.outerWidth = 0;
  2014. buffers->alphaFrontBuffer = &buffers->alphaBuffer0;
  2015. if (gc->modes.doubleBufferMode) {
  2016. buffers->alphaBuffer1.size = 0;
  2017. buffers->alphaBuffer1.outerWidth = 0;
  2018. buffers->alphaBackBuffer = &buffers->alphaBuffer1;
  2019. }
  2020. }
  2021. if (gc->modes.depthBits) {
  2022. gc->depthBuffer.buf.base = 0;
  2023. gc->depthBuffer.buf.size = 0;
  2024. gc->depthBuffer.buf.outerWidth = 0;
  2025. }
  2026. if (gc->modes.stencilBits) {
  2027. gc->stencilBuffer.buf.base = 0;
  2028. gc->stencilBuffer.buf.size = 0;
  2029. gc->stencilBuffer.buf.outerWidth = 0;
  2030. }
  2031. // If double-buffered, initialize the fake window for the back buffer
  2032. if (gc->modes.doubleBufferMode)
  2033. {
  2034. buffers->backBitmap.pwnd = &buffers->backBitmap.wnd;
  2035. buffers->backBitmap.wnd.clipComplexity = DC_TRIVIAL;
  2036. buffers->backBitmap.wnd.rclBounds.left = 0;
  2037. buffers->backBitmap.wnd.rclBounds.top = 0;
  2038. buffers->backBitmap.wnd.rclBounds.right = 0;
  2039. buffers->backBitmap.wnd.rclBounds.bottom = 0;
  2040. buffers->backBitmap.wnd.rclClient =
  2041. buffers->backBitmap.wnd.rclBounds;
  2042. }
  2043. #ifdef _MCD_
  2044. if (gengc->_pMcdState &&
  2045. !(gengc->flags & GLGEN_MCD_CONVERTED_TO_GENERIC)) {
  2046. if (bInitMcdSurface(gengc, gengc->pwndLocked, buffers)) {
  2047. if (gengc->pMcdState->pDepthSpan) {
  2048. gc->depthBuffer.buf.base = gengc->pMcdState->pDepthSpan;
  2049. buffers->depthBuffer.base = gengc->pMcdState->pDepthSpan;
  2050. buffers->resizeDepth = ResizeUnownedDepthBuffer;
  2051. }
  2052. } else {
  2053. WARNING("__glGenAllocAndInitPrivateBufferStruct: bInitMcdSurface failed\n");
  2054. FREE(buffers);
  2055. return NULL;
  2056. }
  2057. }
  2058. else
  2059. #endif
  2060. if (gengc->dwCurrentFlags & GLSURF_DIRECTDRAW)
  2061. {
  2062. // DDraw surfaces provide their own depth buffers
  2063. buffers->resizeDepth = ResizeUnownedDepthBuffer;
  2064. }
  2065. buffers->clip.WndUniq = -1;
  2066. //
  2067. // init swap hint region
  2068. //
  2069. buffers->pxlist = NULL;
  2070. buffers->pylist = NULL;
  2071. buffers->rl.buffers = buffers;
  2072. buffers->rl.pylist = NULL;
  2073. buffers->fMax = FALSE;
  2074. return buffers;
  2075. }
  2076. /******************************Public*Routine******************************\
  2077. * __glGenCheckBufferStruct
  2078. *
  2079. * Check if context and buffer struct are compatible.
  2080. *
  2081. * To satisfy this requirement, the attributes of the shared buffers
  2082. * (back, depth, stencil, and accum) must match. Otherwise, the context
  2083. * cannot be used with the given set of buffers.
  2084. *
  2085. * Returns:
  2086. * TRUE if compatible, FALSE otherwise.
  2087. *
  2088. * History:
  2089. * 17-Jul-1996 -by- Gilman Wong [gilmanw]
  2090. * Wrote it.
  2091. \**************************************************************************/
  2092. GLboolean __glGenCheckBufferStruct(__GLcontext *gc, __GLGENbuffers *buffers)
  2093. {
  2094. BOOL bRet = FALSE;
  2095. __GLGENcontext *gengc = (__GLGENcontext *)gc;
  2096. PIXELFORMATDESCRIPTOR *ppfd = &gengc->gsurf.pfd;
  2097. if ((buffers->stencilBits == ppfd->cStencilBits) &&
  2098. (buffers->depthBits == ppfd->cDepthBits ) &&
  2099. (buffers->accumBits == ppfd->cAccumBits ) &&
  2100. (buffers->colorBits == ppfd->cColorBits ) &&
  2101. (buffers->alphaBits == ppfd->cAlphaBits ))
  2102. {
  2103. bRet = TRUE;
  2104. }
  2105. return (GLboolean)bRet;
  2106. }
  2107. /******************************Public*Routine******************************\
  2108. * glsrvMakeCurrent
  2109. *
  2110. * Make generic context current to this thread with specified DC.
  2111. *
  2112. * Returns:
  2113. * TRUE if sucessful.
  2114. *
  2115. \**************************************************************************/
  2116. // Upper level code should make sure that this context is not current to
  2117. // any other thread, that we "lose" the old context first
  2118. // Called with DEVLOCK held, free to modify window
  2119. //
  2120. // FALSE will be returned if we cannot create the objects we need
  2121. // rcobj.cxx will set the error code to show we are out of memory
  2122. BOOL APIENTRY glsrvMakeCurrent(GLWINDOWID *pgwid, __GLcontext *gc,
  2123. GLGENwindow *pwnd)
  2124. {
  2125. __GLGENcontext *gengc;
  2126. __GLGENbuffers *buffers;
  2127. GLint width, height;
  2128. BOOL bFreePwnd = FALSE;
  2129. BOOL bUninitSem = FALSE;
  2130. DBGENTRY("Generic MakeCurrent\n");
  2131. // Common initialization
  2132. gengc = (__GLGENcontext *)gc;
  2133. ASSERTOPENGL(GLTEB_SRVCONTEXT() == 0, "current context in makecurrent!");
  2134. ASSERTOPENGL(gengc->pwndMakeCur == NULL &&
  2135. gengc->pwndLocked == NULL,
  2136. "Non-current context has window pointers\n");
  2137. gengc->gwidCurrent = *pgwid;
  2138. if (pwnd == NULL)
  2139. {
  2140. ASSERTOPENGL((gengc->gsurf.dwFlags & GLSURF_METAFILE),
  2141. "Non-metafile surface without a window\n");
  2142. // Drawing on an IC, create a fake window with no visible area
  2143. pwnd = (GLGENwindow *)ALLOC(sizeof(GLGENwindow));
  2144. if (pwnd == NULL)
  2145. {
  2146. WARNING("glsrvMakeCurrent: memory allocation failure "
  2147. "(IC, window)\n");
  2148. goto ERROR_EXIT;
  2149. }
  2150. bFreePwnd = TRUE;
  2151. RtlZeroMemory(pwnd, sizeof(GLGENwindow));
  2152. pwnd->clipComplexity = DC_TRIVIAL;
  2153. // This window data is private so technically there'll never
  2154. // be another thread accessing it so this critsec is unnecessary.
  2155. // However, having it eliminates special cases where
  2156. // window locks are taken or checked for ownership.
  2157. __try
  2158. {
  2159. InitializeCriticalSection(&pwnd->sem);
  2160. }
  2161. __except(EXCEPTION_EXECUTE_HANDLER)
  2162. {
  2163. goto ERROR_EXIT;
  2164. }
  2165. bUninitSem = TRUE;
  2166. // Set this so CreateGDIObjects doesn't attempt to create
  2167. // zero-size objects
  2168. gengc->ColorsBits = (void *)1;
  2169. gengc->dwCurrentFlags = gengc->gsurf.dwFlags;
  2170. }
  2171. else if (pgwid->iType == GLWID_DDRAW)
  2172. {
  2173. gengc->dwCurrentFlags = gengc->gsurf.dwFlags;
  2174. }
  2175. else
  2176. {
  2177. GLSURF gsurf;
  2178. if (!InitDeviceSurface(pgwid->hdc, pwnd->ipfd, gengc->gsurf.iLayer,
  2179. wglObjectType(pgwid->hdc), FALSE, &gsurf))
  2180. {
  2181. goto ERROR_EXIT;
  2182. }
  2183. gengc->dwCurrentFlags = gsurf.dwFlags;
  2184. }
  2185. if (gengc->dwCurrentFlags & GLSURF_DIRECTDRAW)
  2186. {
  2187. gengc->pgddsFront = &gengc->gsurf.dd.gddsFront;
  2188. }
  2189. else if (GLDIRECTSCREEN && GLSURF_IS_SCREENDC(gengc->dwCurrentFlags))
  2190. {
  2191. gengc->pgddsFront = &GLSCREENINFO->gdds;
  2192. }
  2193. else
  2194. {
  2195. gengc->pgddsFront = NULL;
  2196. }
  2197. // We need this field to tell whether we're using a fake window
  2198. // or a real one.
  2199. gengc->ipfdCurrent = pwnd->ipfd;
  2200. gengc->pwndMakeCur = pwnd;
  2201. ENTER_WINCRIT_GC(pwnd, gengc);
  2202. width = pwnd->rclClient.right - pwnd->rclClient.left;
  2203. height = pwnd->rclClient.bottom - pwnd->rclClient.top;
  2204. gengc->errorcode = 0;
  2205. // Sanity check the info from window.
  2206. ASSERTOPENGL(
  2207. width <= __GL_MAX_WINDOW_WIDTH && height <= __GL_MAX_WINDOW_HEIGHT,
  2208. "glsrvMakeCurrrent(): bad window client size\n"
  2209. );
  2210. // Make our context current in the TEB.
  2211. // If failures after this point, make sure to reset TEB entry
  2212. // Set up this thread's paTeb pointer.
  2213. gc->paTeb = GLTEB_CLTPOLYARRAY();
  2214. GLTEB_SET_SRVCONTEXT(gc);
  2215. buffers = pwnd->buffers;
  2216. /* We inherit any drawable state */
  2217. if (buffers)
  2218. {
  2219. gc->constants.width = buffers->width;
  2220. gc->constants.height = buffers->height;
  2221. if (!__glGenCheckBufferStruct(gc, buffers))
  2222. {
  2223. WARNING("glsrvMakeCurrent: __glGenCheckBufferStruct failed\n");
  2224. goto ERROR_EXIT;
  2225. }
  2226. #ifdef _MCD_
  2227. if (GLSURF_IS_DIRECTDC(gengc->dwCurrentFlags))
  2228. {
  2229. if (!(gengc->flags & GLGEN_MCD_CONVERTED_TO_GENERIC) &&
  2230. !(buffers->flags & GLGENBUF_MCD_LOST))
  2231. {
  2232. gengc->pMcdState = gengc->_pMcdState;
  2233. // Reset MCD scaling values since we're now using
  2234. // MCD hardware acceleration:
  2235. GenMcdSetScaling(gengc);
  2236. if (gengc->pMcdState)
  2237. {
  2238. gengc->pMcdState->pMcdSurf = buffers->pMcdSurf;
  2239. if (buffers->pMcdSurf)
  2240. {
  2241. gengc->pMcdState->pDepthSpan = buffers->pMcdSurf->pDepthSpan;
  2242. }
  2243. else
  2244. {
  2245. WARNING("glsrvMakeCurrent: MCD context, generic surface\n");
  2246. goto ERROR_EXIT;
  2247. }
  2248. }
  2249. else
  2250. {
  2251. // Generic context. If the surface is an MCD surface, we
  2252. // cannot continue. The context is generic but the pixelfmt
  2253. // is MCD, so what must have happened is that we attempted
  2254. // to create an MCD context, but failed, so we reverted
  2255. // to generic.
  2256. if (buffers->pMcdSurf)
  2257. {
  2258. WARNING("glsrvMakeCurrent: generic context, MCD surface\n");
  2259. goto ERROR_EXIT;
  2260. }
  2261. }
  2262. }
  2263. else
  2264. {
  2265. gengc->pMcdState = (GENMCDSTATE *)NULL;
  2266. // Reset MCD scaling values since we've fallen back to
  2267. // software:
  2268. GenMcdSetScaling(gengc);
  2269. // If MCD context (or former context), either surface or context
  2270. // needs conversion.
  2271. //
  2272. // The only other way to get here is if this is a generic context
  2273. // an a converted surface, which is perfectly OK and requires no
  2274. // further conversion.
  2275. //!!!SP1 -- should be able to skip this section if no conversion
  2276. //!!!SP1 needed, but we miss out on the forced repick, which
  2277. //!!!SP! could be risky for NT4.0
  2278. //if (gengc->_pMcdState &&
  2279. // (!(gengc->flags & GLGEN_MCD_CONVERTED_TO_GENERIC) ||
  2280. // !(buffers->flags & GLGENBUF_MCD_LOST)))
  2281. if (gengc->_pMcdState)
  2282. {
  2283. BOOL bConverted;
  2284. // Do conversion. We must have color scales set to do the
  2285. // conversion, but we must restore color scales afterwards.
  2286. __glContextSetColorScales(gc);
  2287. bConverted = GenMcdConvertContext(gengc, buffers);
  2288. __glContextUnsetColorScales(gc);
  2289. // Fail makecurrent if conversion failed.
  2290. if (!bConverted)
  2291. {
  2292. WARNING("glsrvMakeCurrent: GenMcdConvertContext failed\n");
  2293. goto ERROR_EXIT;
  2294. }
  2295. }
  2296. }
  2297. }
  2298. else
  2299. gengc->pMcdState = (GENMCDSTATE *)NULL;
  2300. #endif
  2301. if (buffers->accumBuffer.base && gc->modes.accumBits)
  2302. {
  2303. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: Accumulation buffer existed\n");
  2304. gc->accumBuffer.buf.base = buffers->accumBuffer.base;
  2305. gc->accumBuffer.buf.size = buffers->accumBuffer.size;
  2306. gc->accumBuffer.buf.outerWidth = buffers->width;
  2307. gc->modes.haveAccumBuffer = GL_TRUE;
  2308. }
  2309. else
  2310. {
  2311. /* No Accum buffer at this point in time */
  2312. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: Accum buffer doesn't exist\n");
  2313. gc->accumBuffer.buf.base = 0;
  2314. gc->accumBuffer.buf.size = 0;
  2315. gc->accumBuffer.buf.outerWidth = 0;
  2316. }
  2317. if (buffers->depthBuffer.base && gc->modes.depthBits)
  2318. {
  2319. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: Depth buffer existed\n");
  2320. gc->depthBuffer.buf.base = buffers->depthBuffer.base;
  2321. gc->depthBuffer.buf.size = buffers->depthBuffer.size;
  2322. gc->depthBuffer.buf.outerWidth = buffers->width;
  2323. gc->modes.haveDepthBuffer = GL_TRUE;
  2324. }
  2325. else
  2326. {
  2327. /* No Depth buffer at this point in time */
  2328. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: Depth buffer doesn't exist\n");
  2329. gc->depthBuffer.buf.base = 0;
  2330. gc->depthBuffer.buf.size = 0;
  2331. gc->depthBuffer.buf.outerWidth = 0;
  2332. }
  2333. if (buffers->stencilBuffer.base && gc->modes.stencilBits)
  2334. {
  2335. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent: Stencil buffer existed\n");
  2336. gc->stencilBuffer.buf.base = buffers->stencilBuffer.base;
  2337. gc->stencilBuffer.buf.size = buffers->stencilBuffer.size;
  2338. gc->stencilBuffer.buf.outerWidth = buffers->width;
  2339. gc->modes.haveStencilBuffer = GL_TRUE;
  2340. }
  2341. else
  2342. {
  2343. /* No Stencil buffer at this point in time */
  2344. DBGLEVEL(LEVEL_ALLOC, "glsrvMakeCurrent:Stencil buffer doesn't exist\n");
  2345. gc->stencilBuffer.buf.base = 0;
  2346. gc->stencilBuffer.buf.size = 0;
  2347. gc->stencilBuffer.buf.outerWidth = 0;
  2348. }
  2349. }
  2350. else
  2351. {
  2352. gc->modes.haveStencilBuffer = GL_FALSE;
  2353. gc->modes.haveDepthBuffer = GL_FALSE;
  2354. gc->modes.haveAccumBuffer = GL_FALSE;
  2355. }
  2356. /*
  2357. ** Allocate and initialize ancillary buffers structures if none were
  2358. ** inherited. This will happen if an RC has previously been current
  2359. ** and is made current to a new window.
  2360. */
  2361. if (!buffers)
  2362. {
  2363. buffers = __glGenAllocAndInitPrivateBufferStruct(gc);
  2364. if (NULL == buffers)
  2365. {
  2366. WARNING("glsrvMakeCurrent: __glGenAllocAndInitPrivateBufferStruct failed\n");
  2367. goto ERROR_EXIT;
  2368. }
  2369. pwnd->buffers = buffers;
  2370. }
  2371. // Setup pointer to generic back buffer
  2372. if ( gc->modes.doubleBufferMode)
  2373. {
  2374. gc->backBuffer.bitmap = &buffers->backBitmap;
  2375. UpdateSharedBuffer(&gc->backBuffer.buf, &buffers->backBuffer);
  2376. }
  2377. // Setup alpha buffer pointers
  2378. if ( buffers->alphaBits )
  2379. {
  2380. UpdateSharedBuffer( &gc->frontBuffer.alphaBuf.buf,
  2381. buffers->alphaFrontBuffer );
  2382. buffers->alphaFrontBuffer->elementSize =
  2383. gc->frontBuffer.alphaBuf.buf.elementSize;
  2384. if ( gc->modes.doubleBufferMode)
  2385. {
  2386. UpdateSharedBuffer( &gc->backBuffer.alphaBuf.buf,
  2387. buffers->alphaBackBuffer );
  2388. buffers->alphaBackBuffer->elementSize =
  2389. gc->backBuffer.alphaBuf.buf.elementSize;
  2390. }
  2391. }
  2392. if (gc->gcSig != GC_SIGNATURE)
  2393. {
  2394. __GL_DELAY_VALIDATE_MASK(gc, __GL_DIRTY_ALL);
  2395. #ifdef _MCD_
  2396. MCD_STATE_DIRTY(gc, ALL);
  2397. #endif
  2398. // After initializing all of the individual buffer structures, make
  2399. // sure we copy the element size back into the shared buffer.
  2400. // This is a little clumsy,
  2401. buffers->accumBuffer.elementSize = gc->accumBuffer.buf.elementSize;
  2402. buffers->depthBuffer.elementSize = gc->depthBuffer.buf.elementSize;
  2403. buffers->stencilBuffer.elementSize = gc->stencilBuffer.buf.elementSize;
  2404. // We always need to initialize the MCD-related scaling values:
  2405. GenMcdSetScaling(gengc);
  2406. /*
  2407. ** Need some stuff to exist before doing viewport stuff.
  2408. */
  2409. (*gc->procs.validate)(gc);
  2410. /*
  2411. ** The first time a context is made current the spec requires that
  2412. ** the viewport be initialized. The code below does it.
  2413. ** The ApplyViewport() routine will be called inside Viewport()
  2414. */
  2415. __glim_Viewport(0, 0, width, height);
  2416. __glim_Scissor(0, 0, width, height);
  2417. /*
  2418. ** Now that viewport is set, need to revalidate (initializes all
  2419. ** the proc pointers).
  2420. */
  2421. (*gc->procs.validate)(gc);
  2422. gc->gcSig = GC_SIGNATURE;
  2423. }
  2424. else /* Not the first makecurrent for this RC */
  2425. {
  2426. /* This will check the window size, and recompute relevant state */
  2427. ApplyViewport(gc);
  2428. }
  2429. #ifdef _MCD_
  2430. if (gengc->pMcdState)
  2431. {
  2432. // Now that we are assured that the mcd state is fully initialized,
  2433. // configure the depth buffer.
  2434. GenMcdInitDepth(gc, &gc->depthBuffer);
  2435. if (gc->modes.depthBits)
  2436. {
  2437. gc->depthBuffer.scale =
  2438. gengc->pMcdState->McdRcInfo.depthBufferMax;
  2439. }
  2440. else
  2441. {
  2442. gc->depthBuffer.scale = 0x7fffffff;
  2443. }
  2444. // Bind MCD context to window.
  2445. if (!GenMcdMakeCurrent(gengc, pwnd))
  2446. {
  2447. goto ERROR_EXIT;
  2448. }
  2449. gengc->pMcdState->mcdFlags |=
  2450. (MCD_STATE_FORCEPICK | MCD_STATE_FORCERESIZE);
  2451. __GL_DELAY_VALIDATE_MASK(gc, __GL_DIRTY_ALL);
  2452. MCD_STATE_DIRTY(gc, ALL);
  2453. }
  2454. #endif
  2455. // Common initialization
  2456. // Select correct pixel-copy function
  2457. wglInitializePixelCopyFuncs(gengc);
  2458. // Set front-buffer HDC, window to current HDC, window
  2459. gc->front->bitmap->hdc = pgwid->hdc;
  2460. ASSERT_WINCRIT(pwnd);
  2461. gc->front->bitmap->pwnd = pwnd;
  2462. // Make sure our GDI object cache is empty
  2463. // It should always be empty at MakeCurrent time since
  2464. // the objects in the cache are HDC-specific and so
  2465. // they cannot be cached between MakeCurrents since the
  2466. // HDC could change
  2467. //
  2468. // This should be done before HandlePaletteChanges since the
  2469. // cache is used there
  2470. ASSERTOPENGL(gengc->crFill == COLORREF_UNUSED &&
  2471. gengc->hbrFill == NULL &&
  2472. gengc->hdcFill == NULL,
  2473. "Fill cache inconsistent at MakeCurrent\n");
  2474. ASSERTOPENGL(gengc->cStroke.r < 0.0f &&
  2475. gengc->hpenStroke == NULL &&
  2476. gengc->hdcStroke == NULL &&
  2477. gengc->fStrokeInvalid,
  2478. "Stroke cache inconsistent at MakeCurrent\n");
  2479. // Get current xlation
  2480. gengc->PaletteTimestamp = INITIAL_TIMESTAMP;
  2481. HandlePaletteChanges(gengc, pwnd);
  2482. // Force attention code to check if resize is needed
  2483. gengc->WndUniq = -1;
  2484. gengc->WndSizeUniq = -1;
  2485. // Check for allocation failures during MakeCurrent
  2486. if (gengc->errorcode)
  2487. {
  2488. WARNING1("glsrvMakeCurrent: errorcode 0x%lx\n", gengc->errorcode);
  2489. goto ERROR_EXIT;
  2490. }
  2491. /*
  2492. ** Default value for rasterPos needs to be yInverted. The
  2493. ** defaults are filled in during SoftResetContext
  2494. ** we do the adjusting here.
  2495. */
  2496. if (gc->constants.yInverted) {
  2497. gc->state.current.rasterPos.window.y = height +
  2498. gc->constants.fviewportYAdjust - gc->constants.viewportEpsilon;
  2499. }
  2500. /*
  2501. ** Scale all state that depends upon the color scales.
  2502. */
  2503. __glContextSetColorScales(gc);
  2504. LEAVE_WINCRIT_GC(pwnd, gengc);
  2505. return TRUE;
  2506. ERROR_EXIT:
  2507. memset(&gengc->gwidCurrent, 0, sizeof(gengc->gwidCurrent));
  2508. // Set paTeb to NULL for debugging.
  2509. gc->paTeb = NULL;
  2510. GLTEB_SET_SRVCONTEXT(0);
  2511. // Remove window pointers.
  2512. if (gengc->pwndLocked != NULL)
  2513. {
  2514. LEAVE_WINCRIT_GC(pwnd, gengc);
  2515. }
  2516. if (bFreePwnd)
  2517. {
  2518. if (bUninitSem)
  2519. {
  2520. DeleteCriticalSection(&pwnd->sem);
  2521. }
  2522. FREE(pwnd);
  2523. }
  2524. gengc->pwndMakeCur = NULL;
  2525. return FALSE;
  2526. }
  2527. /******************************Public*Routine******************************\
  2528. * AddSwapHintRectWIN()
  2529. *
  2530. * 17-Feb-1995 mikeke Created
  2531. \**************************************************************************/
  2532. void APIPRIVATE __glim_AddSwapHintRectWIN(
  2533. GLint xs,
  2534. GLint ys,
  2535. GLint xe,
  2536. GLint ye)
  2537. {
  2538. __GLGENbuffers *buffers;
  2539. __GL_SETUP();
  2540. buffers = ((__GLGENcontext *)gc)->pwndLocked->buffers;
  2541. if (xs < 0) xs = 0;
  2542. if (xe > buffers->backBuffer.width) xe = buffers->backBuffer.width;
  2543. if (ys < 0) ys = 0;
  2544. if (ye > buffers->backBuffer.height) ye = buffers->backBuffer.height;
  2545. if (xs < xe && ys < ye) {
  2546. if (gc->constants.yInverted) {
  2547. RECTLISTAddRect(&buffers->rl,
  2548. xs, buffers->backBuffer.height - ye,
  2549. xe, buffers->backBuffer.height - ys);
  2550. } else {
  2551. RECTLISTAddRect(&buffers->rl, xs, ys, xe, ye);
  2552. }
  2553. }
  2554. }
  2555. /******************************Public*Routine******************************\
  2556. * wglFixupPixelFormat
  2557. *
  2558. * Fixes up certain MCD pixel format cases
  2559. *
  2560. * History:
  2561. * 21-Apr-1996 -by- Gilman Wong [gilmanw]
  2562. * Wrote it.
  2563. \**************************************************************************/
  2564. VOID FASTCALL wglFixupPixelFormat(__GLGENcontext *gengc,
  2565. PIXELFORMATDESCRIPTOR *ppfd)
  2566. {
  2567. // Some MCD pixelformats specify a 233BGR (i.e., 2-bits blue in least
  2568. // significant bits, etc.) bit ordering. Unfortunately, this is the
  2569. // slow path for simulations. For those formats, we force the ordering
  2570. // to RGB internally and reorder the pajTranslateVector to convert it
  2571. // back to BGR before writing to the surface.
  2572. if (((ppfd->dwFlags & (PFD_NEED_SYSTEM_PALETTE | PFD_GENERIC_ACCELERATED))
  2573. == (PFD_NEED_SYSTEM_PALETTE | PFD_GENERIC_ACCELERATED)) &&
  2574. (ppfd->cRedBits == 3) && (ppfd->cRedShift == 5) &&
  2575. (ppfd->cGreenBits == 3) && (ppfd->cGreenShift == 2) &&
  2576. (ppfd->cBlueBits == 2) && (ppfd->cBlueShift == 0))
  2577. {
  2578. ppfd->cRedShift = 0;
  2579. ppfd->cGreenShift = 3;
  2580. ppfd->cBlueShift = 6;
  2581. gengc->flags |= GENGC_MCD_BGR_INTO_RGB;
  2582. }
  2583. else
  2584. {
  2585. gengc->flags &= ~GENGC_MCD_BGR_INTO_RGB;
  2586. }
  2587. }
  2588. /******************************Public*Routine******************************\
  2589. * glsrvCreateContext
  2590. *
  2591. * Create a generic context.
  2592. *
  2593. * Returns:
  2594. * NULL for failure.
  2595. *
  2596. \**************************************************************************/
  2597. // hdc is the dc used to create the context, hrc is how the server
  2598. // identifies the GL context, the GLcontext pointer that is return is how
  2599. // the generic code identifies the context. The server will pass that pointer
  2600. // in all calls.
  2601. PVOID APIENTRY glsrvCreateContext(GLWINDOWID *pgwid, GLSURF *pgsurf)
  2602. {
  2603. __GLGENcontext *gengc;
  2604. __GLcontext *gc;
  2605. RANDOMDISABLE;
  2606. DBGENTRY("__glsrvCreateContext\n");
  2607. // Initialize the temporary memory allocation table
  2608. if (!InitTempAlloc())
  2609. {
  2610. return NULL;
  2611. }
  2612. gengc = ALLOCZ(sizeof(*gengc));
  2613. if (gengc == NULL)
  2614. {
  2615. WARNING("bad alloc\n");
  2616. return NULL;
  2617. }
  2618. gengc->hrc = NULL;
  2619. gc = (__GLcontext *)gengc;
  2620. // Initialize cached objects to nothing
  2621. gengc->crFill = COLORREF_UNUSED;
  2622. gengc->hbrFill = NULL;
  2623. gengc->hdcFill = NULL;
  2624. gengc->cStroke.r = -1.0f;
  2625. gengc->fStrokeInvalid = TRUE;
  2626. gengc->hpenStroke = NULL;
  2627. gengc->hdcStroke = NULL;
  2628. gc->gcSig = 0;
  2629. /*
  2630. * Add a bunch of constants to the context
  2631. */
  2632. gc->constants.maxViewportWidth = __GL_MAX_WINDOW_WIDTH;
  2633. gc->constants.maxViewportHeight = __GL_MAX_WINDOW_HEIGHT;
  2634. gc->constants.viewportXAdjust = __GL_VERTEX_X_BIAS+
  2635. __GL_VERTEX_X_FIX;
  2636. gc->constants.viewportYAdjust = __GL_VERTEX_Y_BIAS+
  2637. __GL_VERTEX_Y_FIX;
  2638. gc->constants.subpixelBits = __GL_WGL_SUBPIXEL_BITS;
  2639. gc->constants.numberOfLights = __GL_WGL_NUMBER_OF_LIGHTS;
  2640. gc->constants.numberOfClipPlanes = __GL_WGL_NUMBER_OF_CLIP_PLANES;
  2641. gc->constants.numberOfTextures = __GL_WGL_NUMBER_OF_TEXTURES;
  2642. gc->constants.numberOfTextureEnvs = __GL_WGL_NUMBER_OF_TEXTURE_ENVS;
  2643. gc->constants.maxTextureSize = __GL_WGL_MAX_MIPMAP_LEVEL;/*XXX*/
  2644. gc->constants.maxMipMapLevel = __GL_WGL_MAX_MIPMAP_LEVEL;
  2645. gc->constants.maxListNesting = __GL_WGL_MAX_LIST_NESTING;
  2646. gc->constants.maxEvalOrder = __GL_WGL_MAX_EVAL_ORDER;
  2647. gc->constants.maxPixelMapTable = __GL_WGL_MAX_PIXEL_MAP_TABLE;
  2648. gc->constants.maxAttribStackDepth = __GL_WGL_MAX_ATTRIB_STACK_DEPTH;
  2649. gc->constants.maxClientAttribStackDepth = __GL_WGL_MAX_CLIENT_ATTRIB_STACK_DEPTH;
  2650. gc->constants.maxNameStackDepth = __GL_WGL_MAX_NAME_STACK_DEPTH;
  2651. gc->constants.pointSizeMinimum =
  2652. (__GLfloat)__GL_WGL_POINT_SIZE_MINIMUM;
  2653. gc->constants.pointSizeMaximum =
  2654. (__GLfloat)__GL_WGL_POINT_SIZE_MAXIMUM;
  2655. gc->constants.pointSizeGranularity =
  2656. (__GLfloat)__GL_WGL_POINT_SIZE_GRANULARITY;
  2657. gc->constants.lineWidthMinimum =
  2658. (__GLfloat)__GL_WGL_LINE_WIDTH_MINIMUM;
  2659. gc->constants.lineWidthMaximum =
  2660. (__GLfloat)__GL_WGL_LINE_WIDTH_MAXIMUM;
  2661. gc->constants.lineWidthGranularity =
  2662. (__GLfloat)__GL_WGL_LINE_WIDTH_GRANULARITY;
  2663. #ifndef NT
  2664. gc->dlist.optimizer = __glDlistOptimizer;
  2665. gc->dlist.checkOp = __glNopGCListOp;
  2666. gc->dlist.listExec = __gl_GenericDlOps;
  2667. gc->dlist.baseListExec = __glListExecTable;
  2668. #endif
  2669. gc->dlist.initState = __glNopGC;
  2670. __glEarlyInitContext( gc );
  2671. if (gengc->errorcode)
  2672. {
  2673. WARNING1("Context error is %d\n", gengc->errorcode);
  2674. glsrvDeleteContext(gc);
  2675. return NULL;
  2676. }
  2677. RANDOMREENABLE;
  2678. // Many routines depend on a current surface so set it temporarily
  2679. gengc->gwidCurrent = *pgwid;
  2680. gengc->dwCurrentFlags = pgsurf->dwFlags;
  2681. // Get copy of current pixelformat
  2682. if (pgsurf->iLayer == 0 &&
  2683. (pgsurf->pfd.dwFlags &
  2684. (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) ==
  2685. (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED))
  2686. {
  2687. wglFixupPixelFormat(gengc, &pgsurf->pfd);
  2688. }
  2689. gengc->gsurf = *pgsurf;
  2690. #ifdef _MCD_
  2691. // Is the pixelformat compatible with the generic code, or is it some
  2692. // weird h/w (MCD) format generic cannot handle?
  2693. if (GenMcdGenericCompatibleFormat(gengc))
  2694. gengc->flags |= GENGC_GENERIC_COMPATIBLE_FORMAT;
  2695. #endif
  2696. // Extract information from pixel format to set up modes
  2697. GetContextModes(gengc);
  2698. ASSERTOPENGL(GLSURF_IS_MEMDC(gengc->dwCurrentFlags) ?
  2699. !gc->modes.doubleBufferMode : 1, "Double buffered memdc!");
  2700. // XXX! Reset buffer dimensions to force Bitmap resize call
  2701. // We should eventually handle the Bitmap as we do ancilliary buffers
  2702. gc->constants.width = 0;
  2703. gc->constants.height = 0;
  2704. __GL_DELAY_VALIDATE_MASK(gc, __GL_DIRTY_ALL);
  2705. #ifdef _MCD_
  2706. MCD_STATE_DIRTY(gc, ALL);
  2707. #endif
  2708. gc->constants.yInverted = GL_TRUE;
  2709. gc->constants.ySign = -1;
  2710. // Allocate GDI objects that we will need
  2711. if (!CreateGDIObjects(gengc))
  2712. {
  2713. goto ERROR_EXIT;
  2714. }
  2715. // Allocate __GLGENbitmap front-buffer structure
  2716. if (!(gc->frontBuffer.bitmap = GCALLOCZ(gc, sizeof(__GLGENbitmap))))
  2717. {
  2718. goto ERROR_EXIT;
  2719. }
  2720. // Create MCD rendering context, if MCD is available.
  2721. if (gengc->gsurf.dwFlags & GLSURF_VIDEO_MEMORY)
  2722. {
  2723. GLGENwindow *pwnd;
  2724. BOOL bMcdContext;
  2725. // Validate layer index
  2726. if (pgsurf->iLayer &&
  2727. !ValidateLayerIndex(pgsurf->iLayer, &pgsurf->pfd))
  2728. {
  2729. WARNING("glsrvCreateContext: bad iLayer\n");
  2730. goto ERROR_EXIT;
  2731. }
  2732. pwnd = pwndGetFromID(pgwid);
  2733. if (pwnd == NULL)
  2734. {
  2735. goto ERROR_EXIT;
  2736. }
  2737. // If this fails, _pMcdState is NULL and we fall back on
  2738. // the software-only implementation.
  2739. //
  2740. // Unless we were trying to create a layer context. Generic
  2741. // does not support layers, so fail if we cannot create an
  2742. // MCD context.
  2743. bMcdContext = bInitMcdContext(gengc, pwnd);
  2744. if (!(gengc->flags & GENGC_GENERIC_COMPATIBLE_FORMAT) && !bMcdContext)
  2745. {
  2746. goto ERROR_EXIT;
  2747. }
  2748. pwndRelease(pwnd);
  2749. }
  2750. /*
  2751. * Initialize front/back color buffer(s)
  2752. */
  2753. wglInitializeColorBuffers(gengc);
  2754. /*
  2755. * Initialize any other ancillary buffers
  2756. */
  2757. // Init accum buffer.
  2758. if (gc->modes.accumBits)
  2759. {
  2760. switch (gc->modes.accumBits)
  2761. {
  2762. case 16:
  2763. // We will now internally use a 32-bit accum for accumBits=16
  2764. case 32:
  2765. __glInitAccum32(gc, &gc->accumBuffer);
  2766. break;
  2767. case 64:
  2768. default:
  2769. __glInitAccum64(gc, &gc->accumBuffer);
  2770. break;
  2771. }
  2772. }
  2773. // Initialize depth buffer.
  2774. wglInitializeDepthBuffer(gengc);
  2775. // Init stencil buffer.
  2776. if (gc->modes.stencilBits)
  2777. {
  2778. __glInitStencil8( gc, &gc->stencilBuffer);
  2779. }
  2780. // Look at REX code for procs to make CPU specific
  2781. gc->procs.bitmap = __glDrawBitmap;
  2782. gc->procs.clipPolygon = __glClipPolygon;
  2783. gc->procs.validate = __glGenericValidate;
  2784. gc->procs.pickAllProcs = __glGenericPickAllProcs;
  2785. gc->procs.pickBlendProcs = __glGenericPickBlendProcs;
  2786. gc->procs.pickFogProcs = __glGenericPickFogProcs;
  2787. gc->procs.pickParameterClipProcs = __glGenericPickParameterClipProcs;
  2788. gc->procs.pickStoreProcs = __glGenPickStoreProcs;
  2789. gc->procs.pickTextureProcs = __glGenericPickTextureProcs;
  2790. gc->procs.copyImage = __glGenericPickCopyImage;
  2791. gc->procs.pixel.spanReadCI = __glSpanReadCI;
  2792. gc->procs.pixel.spanReadCI2 = __glSpanReadCI2;
  2793. gc->procs.pixel.spanReadRGBA = __glSpanReadRGBA;
  2794. gc->procs.pixel.spanReadRGBA2 = __glSpanReadRGBA2;
  2795. gc->procs.pixel.spanReadDepth = __glSpanReadDepth;
  2796. gc->procs.pixel.spanReadDepth2 = __glSpanReadDepth2;
  2797. gc->procs.pixel.spanReadStencil = __glSpanReadStencil;
  2798. gc->procs.pixel.spanReadStencil2 = __glSpanReadStencil2;
  2799. gc->procs.pixel.spanRenderCI = __glSpanRenderCI;
  2800. gc->procs.pixel.spanRenderCI2 = __glSpanRenderCI2;
  2801. gc->procs.pixel.spanRenderRGBA = __glSpanRenderRGBA;
  2802. gc->procs.pixel.spanRenderRGBA2 = __glSpanRenderRGBA2;
  2803. gc->procs.pixel.spanRenderDepth = __glSpanRenderDepth;
  2804. gc->procs.pixel.spanRenderDepth2 = __glSpanRenderDepth2;
  2805. gc->procs.pixel.spanRenderStencil = __glSpanRenderStencil;
  2806. gc->procs.pixel.spanRenderStencil2 = __glSpanRenderStencil2;
  2807. gc->procs.applyViewport = ApplyViewport;
  2808. gc->procs.pickBufferProcs = __glGenericPickBufferProcs;
  2809. gc->procs.pickColorMaterialProcs = __glGenericPickColorMaterialProcs;
  2810. gc->procs.pickPixelProcs = __glGenericPickPixelProcs;
  2811. gc->procs.pickClipProcs = __glGenericPickClipProcs;
  2812. gc->procs.pickLineProcs = __fastGenPickLineProcs;
  2813. gc->procs.pickSpanProcs = __fastGenPickSpanProcs;
  2814. gc->procs.pickTriangleProcs = __fastGenPickTriangleProcs;
  2815. gc->procs.pickRenderBitmapProcs = __glGenericPickRenderBitmapProcs;
  2816. gc->procs.pickPointProcs = __glGenericPickPointProcs;
  2817. gc->procs.pickVertexProcs = __glGenericPickVertexProcs;
  2818. gc->procs.pickDepthProcs = __glGenericPickDepthProcs;
  2819. gc->procs.convertPolygonStipple = __glConvertStipple;
  2820. /* Now reset the context to its default state */
  2821. RANDOMDISABLE;
  2822. __glSoftResetContext(gc);
  2823. // Check for allocation failures during SoftResetContext
  2824. if (gengc->errorcode)
  2825. {
  2826. goto ERROR_EXIT;
  2827. }
  2828. /* Create acceleration-specific context information */
  2829. if (!__glGenCreateAccelContext(gc))
  2830. {
  2831. goto ERROR_EXIT;
  2832. }
  2833. /*
  2834. ** Now that we have a context, we can initialize
  2835. ** all the proc pointers.
  2836. */
  2837. (*gc->procs.validate)(gc);
  2838. /*
  2839. ** NOTE: now that context is initialized reset to use the global
  2840. ** table.
  2841. */
  2842. RANDOMREENABLE;
  2843. // We won't be fully initialized until the first MakeCurrent
  2844. // so set the signature to uninitialized
  2845. gc->gcSig = 0;
  2846. memset(&gengc->gwidCurrent, 0, sizeof(gengc->gwidCurrent));
  2847. gengc->dwCurrentFlags = 0;
  2848. /*
  2849. * End stuff that may belong in the hardware context
  2850. */
  2851. return (PVOID)gc;
  2852. ERROR_EXIT:
  2853. memset(&gengc->gwidCurrent, 0, sizeof(gengc->gwidCurrent));
  2854. gengc->dwCurrentFlags = 0;
  2855. glsrvDeleteContext(gc);
  2856. return NULL;
  2857. }
  2858. /******************************Public*Routine******************************\
  2859. * UpdateSharedBuffer
  2860. *
  2861. * Make the context buffer state consistent with the shared buffer state.
  2862. * This is called separately for each of the shared buffers.
  2863. *
  2864. \**************************************************************************/
  2865. void UpdateSharedBuffer(__GLbuffer *to, __GLbuffer *from)
  2866. {
  2867. to->width = from->width;
  2868. to->height = from->height;
  2869. to->base = from->base;
  2870. to->outerWidth = from->outerWidth;
  2871. }
  2872. /******************************Public*Routine******************************\
  2873. * ResizeUnownedDepthBuffer
  2874. *
  2875. * Resizes a general-purpose hardware depth buffer. Just updates structure.
  2876. *
  2877. * Returns:
  2878. * TRUE always.
  2879. *
  2880. \**************************************************************************/
  2881. GLboolean ResizeUnownedDepthBuffer(__GLGENbuffers *buffers,
  2882. __GLbuffer *fb, GLint w, GLint h)
  2883. {
  2884. fb->width = w;
  2885. fb->height = h;
  2886. return TRUE;
  2887. }
  2888. /******************************Public*Routine******************************\
  2889. * ResizeHardwareBackBuffer
  2890. *
  2891. * Resizes a general-purpose hardware color buffer. Just updates structure.
  2892. *
  2893. * Returns:
  2894. * TRUE always.
  2895. *
  2896. \**************************************************************************/
  2897. GLboolean ResizeHardwareBackBuffer(__GLGENbuffers *buffers,
  2898. __GLcolorBuffer *cfb, GLint w, GLint h)
  2899. {
  2900. __GLGENbitmap *genBm = cfb->bitmap;
  2901. __GLGENcontext *gengc = (__GLGENcontext *) cfb->buf.gc;
  2902. // Fake up some of the __GLGENbitmap information. The window is required
  2903. // for clipping of the hardware back buffer. The hdc is required to
  2904. // retrieve drawing data from GDI.
  2905. ASSERT_WINCRIT(gengc->pwndLocked);
  2906. genBm->pwnd = gengc->pwndLocked;
  2907. genBm->hdc = gengc->gwidCurrent.hdc;
  2908. buffers->backBuffer.width = w;
  2909. buffers->backBuffer.height = h;
  2910. UpdateSharedBuffer(&cfb->buf, &buffers->backBuffer);
  2911. return TRUE;
  2912. }
  2913. /******************************Public*Routine******************************\
  2914. * ResizeAncillaryBuffer
  2915. *
  2916. * Resizes the indicated shared buffer via a realloc (to preserve as much of
  2917. * the existing data as possible).
  2918. *
  2919. * This is currently used for each of ancillary shared buffers except for
  2920. * the back buffer.
  2921. *
  2922. * Returns:
  2923. * TRUE if successful, FALSE if error.
  2924. *
  2925. \**************************************************************************/
  2926. GLboolean ResizeAncillaryBuffer(__GLGENbuffers *buffers, __GLbuffer *fb,
  2927. GLint w, GLint h)
  2928. {
  2929. size_t newSize = (size_t) (w * h * fb->elementSize);
  2930. __GLbuffer oldbuf, *ofb;
  2931. GLboolean result;
  2932. GLint i, imax, rowsize;
  2933. void *to, *from;
  2934. ofb = &oldbuf;
  2935. oldbuf = *fb;
  2936. if (newSize > 0)
  2937. {
  2938. fb->base = ALLOC(newSize);
  2939. }
  2940. else
  2941. {
  2942. // Buffer has no size. If we tried to allocate zero the debug alloc
  2943. // would complain, so skip directly to the underlying allocator
  2944. fb->base = HeapAlloc(GetProcessHeap(), 0, 0);
  2945. }
  2946. ASSERTOPENGL((ULONG_PTR)fb->base % 4 == 0, "base not aligned");
  2947. fb->size = newSize;
  2948. fb->width = w;
  2949. fb->height = h;
  2950. fb->outerWidth = w; // element size
  2951. if (fb->base) {
  2952. result = GL_TRUE;
  2953. if (ofb->base) {
  2954. if (ofb->width > fb->width)
  2955. rowsize = fb->width * fb->elementSize;
  2956. else
  2957. rowsize = ofb->width * fb->elementSize;
  2958. if (ofb->height > fb->height)
  2959. imax = fb->height;
  2960. else
  2961. imax = ofb->height;
  2962. from = ofb->base;
  2963. to = fb->base;
  2964. for (i = 0; i < imax; i++) {
  2965. __GL_MEMCOPY(to, from, rowsize);
  2966. (ULONG_PTR)from += (ofb->width * ofb->elementSize);
  2967. (ULONG_PTR)to += (fb->width * fb->elementSize);
  2968. }
  2969. }
  2970. } else {
  2971. result = GL_FALSE;
  2972. }
  2973. if (ofb->base)
  2974. {
  2975. FREE(ofb->base);
  2976. }
  2977. return result;
  2978. }
  2979. /******************************Private*Routine******************************\
  2980. * ResizeBitmapBuffer
  2981. *
  2982. * Used to resize the backbuffer that is implemented as a bitmap. Cannot
  2983. * use same code as ResizeAncillaryBuffer() because each scanline must be
  2984. * dword aligned. We also have to create engine objects for the bitmap.
  2985. *
  2986. * This code handles the case of a bitmap that has never been initialized.
  2987. *
  2988. * History:
  2989. * 18-Nov-1993 -by- Gilman Wong [gilmanw]
  2990. * Wrote it.
  2991. \**************************************************************************/
  2992. void
  2993. ResizeBitmapBuffer(__GLGENbuffers *buffers, __GLcolorBuffer *cfb,
  2994. GLint w, GLint h)
  2995. {
  2996. __GLGENcontext *gengc = (__GLGENcontext *) cfb->buf.gc;
  2997. __GLcontext *gc = cfb->buf.gc;
  2998. __GLGENbitmap *genBm;
  2999. UINT cBytes; // size of the bitmap in bytes
  3000. LONG cBytesPerScan; // size of a scanline (DWORD aligned)
  3001. SIZEL size; // dimensions of the bitmap
  3002. PIXELFORMATDESCRIPTOR *pfmt = &gengc->gsurf.pfd;
  3003. GLint cBitsPerScan;
  3004. #ifndef _CLIENTSIDE_
  3005. void *newbits;
  3006. #endif
  3007. DBGENTRY("Entering ResizeBitmapBuffer\n");
  3008. genBm = cfb->bitmap;
  3009. ASSERTOPENGL(
  3010. &gc->backBuffer == cfb,
  3011. "ResizeBitmapBuffer(): not back buffer!\n"
  3012. );
  3013. ASSERTOPENGL(
  3014. genBm == &buffers->backBitmap,
  3015. "ResizeBitmapBuffer(): bad __GLGENbitmap * in cfb\n"
  3016. );
  3017. // Compute the size of the bitmap.
  3018. // The engine bitmap must have scanlines that are DWORD aligned.
  3019. cBitsPerScan = BITS_ALIGNDWORD(w * pfmt->cColorBits);
  3020. cBytesPerScan = cBitsPerScan / 8;
  3021. cBytes = h * cBytesPerScan;
  3022. // Setup size structure with dimensions of the bitmap.
  3023. size.cx = cBitsPerScan / pfmt->cColorBits;
  3024. size.cy = h;
  3025. #ifndef _CLIENTSIDE_
  3026. // Malloc new buffer
  3027. if ( (!cBytes) ||
  3028. (NULL == (newbits = GCALLOC(gc, cBytes))) )
  3029. {
  3030. gengc->errorcode = GLGEN_OUT_OF_MEMORY;
  3031. goto ERROR_EXIT_ResizeBitmapBuffer;
  3032. }
  3033. // If old buffer existed:
  3034. if ( genBm->pvBits )
  3035. {
  3036. GLint i, imax, rowsize;
  3037. void *to, *from;
  3038. // Transfer old contents to new buffer
  3039. rowsize = min(-cfb->buf.outerWidth, cBytesPerScan);
  3040. imax = min(cfb->buf.height, h);
  3041. from = genBm->pvBits;
  3042. to = newbits;
  3043. for (i = 0; i < imax; i++)
  3044. {
  3045. __GL_MEMCOPY(to, from, rowsize);
  3046. (GLint) from -= cfb->buf.outerWidth;
  3047. (GLint) to += cBytesPerScan;
  3048. }
  3049. // Free old bitmap and delete old surface
  3050. EngDeleteSurface((HSURF) genBm->hbm);
  3051. GCFREE(gc, genBm->pvBits);
  3052. }
  3053. genBm->pvBits = newbits;
  3054. // Create new surface
  3055. if ( (genBm->hbm = EngCreateBitmap(size,
  3056. cBytesPerScan,
  3057. gengc->iFormatDC,
  3058. 0,
  3059. genBm->pvBits))
  3060. == (HBITMAP) 0 )
  3061. {
  3062. gengc->errorcode = GLGEN_GRE_FAILURE;
  3063. GCFREE(gc, genBm->pvBits);
  3064. genBm->pvBits = (PVOID) NULL;
  3065. goto ERROR_EXIT_ResizeBitmapBuffer;
  3066. }
  3067. #else
  3068. // Zero sized bitmap. The error case will set the dimensions to
  3069. // zero, thereby preventing drawing operations.
  3070. if ( !cBytes )
  3071. goto ERROR_EXIT_ResizeBitmapBuffer;
  3072. // Delete old back buffer.
  3073. if ( genBm->hbm )
  3074. {
  3075. if (!DeleteDC(genBm->hdc))
  3076. WARNING("ResizeBitmapBuffer: DeleteDC failed\n");
  3077. genBm->hdc = (HDC) NULL;
  3078. if (!DeleteObject(genBm->hbm))
  3079. WARNING("ResizeBitmapBuffer: DeleteBitmap failed");
  3080. genBm->hbm = (HBITMAP) NULL;
  3081. genBm->pvBits = (PVOID) NULL; // DIBsect deletion freed pvBits
  3082. }
  3083. if ( (genBm->hdc = CreateCompatibleDC(gengc->gwidCurrent.hdc)) == (HDC) 0 )
  3084. {
  3085. gengc->errorcode = GLGEN_GRE_FAILURE;
  3086. genBm->pvBits = (PVOID) NULL;
  3087. goto ERROR_EXIT_ResizeBitmapBuffer;
  3088. }
  3089. // Create new surface
  3090. if ( (genBm->hbm = wglCreateBitmap(gengc, size, &genBm->pvBits))
  3091. == (HBITMAP) 0 )
  3092. {
  3093. gengc->errorcode = GLGEN_GRE_FAILURE;
  3094. genBm->pvBits = (PVOID) NULL; // DIBsect deletion freed pvBits
  3095. DeleteDC(genBm->hdc);
  3096. genBm->hdc = (HDC) NULL;
  3097. goto ERROR_EXIT_ResizeBitmapBuffer;
  3098. }
  3099. if ( !SelectObject(genBm->hdc, genBm->hbm) )
  3100. {
  3101. gengc->errorcode = GLGEN_GRE_FAILURE;
  3102. DeleteDC(genBm->hdc);
  3103. genBm->hdc = (HDC) NULL;
  3104. DeleteObject(genBm->hbm);
  3105. genBm->hbm = (HBITMAP) NULL;
  3106. genBm->pvBits = (PVOID) NULL; // DIBsect deletion freed pvBits
  3107. goto ERROR_EXIT_ResizeBitmapBuffer;
  3108. }
  3109. #endif
  3110. // Update buffer data structure
  3111. // Setup the buffer to point to the DIB. A DIB is "upside down"
  3112. // from our perspective, so we will set buf.base to point to the
  3113. // last scan of the buffer and set buf.outerWidth to be negative
  3114. // (causing us to move "up" through the DIB with increasing y).
  3115. buffers->backBuffer.outerWidth = -(cBytesPerScan);
  3116. buffers->backBuffer.base =
  3117. (PVOID) (((BYTE *)genBm->pvBits) + (cBytesPerScan * (h - 1)));
  3118. buffers->backBuffer.xOrigin = 0;
  3119. buffers->backBuffer.yOrigin = 0;
  3120. buffers->backBuffer.width = w;
  3121. buffers->backBuffer.height = h;
  3122. buffers->backBuffer.size = cBytes;
  3123. UpdateSharedBuffer(&cfb->buf, &buffers->backBuffer);
  3124. // Update the dummy window for the back buffer
  3125. ASSERTOPENGL(genBm->wnd.clipComplexity == DC_TRIVIAL,
  3126. "Back buffer complexity non-trivial\n");
  3127. genBm->wnd.rclBounds.right = w;
  3128. genBm->wnd.rclBounds.bottom = h;
  3129. genBm->wnd.rclClient = genBm->wnd.rclBounds;
  3130. return;
  3131. ERROR_EXIT_ResizeBitmapBuffer:
  3132. // If we get to here, memory allocation or bitmap creation failed.
  3133. #if DBG
  3134. switch (gengc->errorcode)
  3135. {
  3136. case 0:
  3137. break;
  3138. case GLGEN_GRE_FAILURE:
  3139. WARNING("ResizeBitmapBuffer(): object creation failed\n");
  3140. break;
  3141. case GLGEN_OUT_OF_MEMORY:
  3142. if ( w && h )
  3143. WARNING("ResizeBitmapBuffer(): mem alloc failed\n");
  3144. break;
  3145. default:
  3146. WARNING1("ResizeBitmapBuffer(): errorcode = 0x%lx\n", gengc->errorcode);
  3147. break;
  3148. }
  3149. #endif
  3150. // If we've blown away the bitmap, we need to set the back buffer info
  3151. // to a consistent state.
  3152. if (!genBm->pvBits)
  3153. {
  3154. buffers->backBuffer.width = 0;
  3155. buffers->backBuffer.height = 0;
  3156. buffers->backBuffer.base = (PVOID) NULL;
  3157. }
  3158. cfb->buf.width = 0; // error state: empty buffer
  3159. cfb->buf.height = 0;
  3160. cfb->buf.outerWidth = 0;
  3161. }
  3162. /* Lazy allocation of ancillary buffers */
  3163. void FASTCALL LazyAllocateDepth(__GLcontext *gc)
  3164. {
  3165. GLint w = gc->constants.width;
  3166. GLint h = gc->constants.height;
  3167. __GLGENcontext *gengc = (__GLGENcontext *)gc;
  3168. __GLGENbuffers *buffers;
  3169. GLint depthIndex = gc->state.depth.testFunc;
  3170. ASSERTOPENGL(gc->modes.depthBits, "LazyAllocateDepth: zero depthBits\n");
  3171. buffers = gengc->pwndLocked->buffers;
  3172. buffers->createdDepthBuffer = GL_TRUE;
  3173. // If we're using the DDI, we've already allocated depth buffers
  3174. // on the device, so at this point we may simply assume that
  3175. // our depth buffer is available.
  3176. #ifdef _MCD_
  3177. // If we're using MCD, we allocated the depth buffer when we created
  3178. // the MCD context.
  3179. if ((gengc->pMcdState) && (gengc->pMcdState->pDepthSpan))
  3180. {
  3181. gc->modes.haveDepthBuffer = GL_TRUE;
  3182. return;
  3183. }
  3184. #endif
  3185. // Depth buffer should never be touched because
  3186. // no output should be generated
  3187. if (gengc->dwCurrentFlags & GLSURF_METAFILE)
  3188. {
  3189. gc->modes.haveDepthBuffer = GL_TRUE;
  3190. return;
  3191. }
  3192. if (buffers->depthBuffer.base) {
  3193. /* buffer already allocated by another RC */
  3194. UpdateSharedBuffer(&gc->depthBuffer.buf, &buffers->depthBuffer);
  3195. } else {
  3196. DBGLEVEL(LEVEL_ALLOC, "Depth buffer must be allocated\n");
  3197. (*buffers->resize)(buffers, &buffers->depthBuffer, w, h);
  3198. UpdateSharedBuffer(&gc->depthBuffer.buf, &buffers->depthBuffer);
  3199. }
  3200. if (gc->depthBuffer.buf.base) {
  3201. gc->modes.haveDepthBuffer = GL_TRUE;
  3202. } else {
  3203. gc->modes.haveDepthBuffer = GL_FALSE;
  3204. __glSetError(GL_OUT_OF_MEMORY);
  3205. }
  3206. __GL_DELAY_VALIDATE_MASK(gc, __GL_DIRTY_DEPTH);
  3207. // Note similar code in so_pick.c
  3208. // Don't need to handle (depthBits == 0) case because LazyAllocateDepth
  3209. // is not called unless depthBits is non-zero.
  3210. depthIndex -= GL_NEVER;
  3211. if( gc->state.depth.writeEnable == GL_FALSE ) {
  3212. depthIndex += 8;
  3213. }
  3214. if( gc->depthBuffer.buf.elementSize == 2 )
  3215. depthIndex += 16;
  3216. (*gc->depthBuffer.pick)(gc, &gc->depthBuffer, depthIndex);
  3217. }
  3218. void FASTCALL LazyAllocateStencil(__GLcontext *gc)
  3219. {
  3220. GLint w = gc->constants.width;
  3221. GLint h = gc->constants.height;
  3222. __GLGENbuffers *buffers;
  3223. __GLGENcontext *gengc = (__GLGENcontext *)gc;
  3224. ASSERTOPENGL(gc->modes.stencilBits, "LazyAllocateStencil: zero stencilBits\n");
  3225. buffers = gengc->pwndLocked->buffers;
  3226. buffers->createdStencilBuffer = GL_TRUE;
  3227. // Depth buffer should never be touched because
  3228. // no output should be generated
  3229. if (gengc->dwCurrentFlags & GLSURF_METAFILE)
  3230. {
  3231. gc->modes.haveStencilBuffer = GL_TRUE;
  3232. return;
  3233. }
  3234. if (buffers->stencilBuffer.base) {
  3235. /* buffer already allocated by another RC */
  3236. UpdateSharedBuffer(&gc->stencilBuffer.buf, &buffers->stencilBuffer);
  3237. } else {
  3238. DBGLEVEL(LEVEL_ALLOC, "stencil buffer must be allocated\n");
  3239. (*buffers->resize)(buffers, &buffers->stencilBuffer, w, h);
  3240. UpdateSharedBuffer(&gc->stencilBuffer.buf, &buffers->stencilBuffer);
  3241. }
  3242. if (gc->stencilBuffer.buf.base) {
  3243. gc->modes.haveStencilBuffer = GL_TRUE;
  3244. } else {
  3245. gc->modes.haveStencilBuffer = GL_FALSE;
  3246. __glSetError(GL_OUT_OF_MEMORY);
  3247. }
  3248. __GL_DELAY_VALIDATE(gc);
  3249. gc->validateMask |= (__GL_VALIDATE_STENCIL_FUNC | __GL_VALIDATE_STENCIL_OP);
  3250. (*gc->stencilBuffer.pick)(gc, &gc->stencilBuffer);
  3251. }
  3252. void FASTCALL LazyAllocateAccum(__GLcontext *gc)
  3253. {
  3254. GLint w = gc->constants.width;
  3255. GLint h = gc->constants.height;
  3256. __GLGENbuffers *buffers;
  3257. __GLGENcontext *gengc = (__GLGENcontext *)gc;
  3258. ASSERTOPENGL(gc->modes.accumBits, "LazyAllocateAccum: zero accumBits\n");
  3259. buffers = gengc->pwndLocked->buffers;
  3260. buffers->createdAccumBuffer = GL_TRUE;
  3261. // Depth buffer should never be touched because
  3262. // no output should be generated
  3263. if (gengc->dwCurrentFlags & GLSURF_METAFILE)
  3264. {
  3265. gc->modes.haveAccumBuffer = GL_TRUE;
  3266. return;
  3267. }
  3268. if (buffers->accumBuffer.base) {
  3269. /* buffer already allocated by another RC */
  3270. UpdateSharedBuffer(&gc->accumBuffer.buf, &buffers->accumBuffer);
  3271. } else {
  3272. DBGLEVEL(LEVEL_ALLOC, "Accum buffer must be allocated\n");
  3273. (*buffers->resize)(buffers, &buffers->accumBuffer, w, h);
  3274. UpdateSharedBuffer(&gc->accumBuffer.buf, &buffers->accumBuffer);
  3275. }
  3276. if (gc->accumBuffer.buf.base) {
  3277. gc->modes.haveAccumBuffer = GL_TRUE;
  3278. } else {
  3279. gc->modes.haveAccumBuffer = GL_FALSE;
  3280. __glSetError(GL_OUT_OF_MEMORY);
  3281. }
  3282. __GL_DELAY_VALIDATE(gc);
  3283. (*gc->accumBuffer.pick)(gc, &gc->accumBuffer);
  3284. }
  3285. /******************************Public*Routine******************************\
  3286. * glGenInitCommon
  3287. *
  3288. * Called from __glGenInitRGB and __glGenInitCI to handle the shared
  3289. * initialization chores.
  3290. *
  3291. \**************************************************************************/
  3292. void FASTCALL glGenInitCommon(__GLGENcontext *gengc, __GLcolorBuffer *cfb, GLenum type)
  3293. {
  3294. __GLbuffer *bp;
  3295. bp = &cfb->buf;
  3296. // If front buffer, we need to setup the buffer if we think its DIB format.
  3297. if (type == GL_FRONT)
  3298. {
  3299. #ifdef _MCD_
  3300. if (gengc->_pMcdState)
  3301. {
  3302. // Assume that MCD surface is not accessible. Accessibility
  3303. // must be determined on a per-batch basis by calling
  3304. // GenMcdUpdateBufferInfo.
  3305. bp->flags &= ~(DIB_FORMAT | MEMORY_DC | NO_CLIP);
  3306. }
  3307. #endif
  3308. {
  3309. if (gengc->dwCurrentFlags & GLSURF_DIRECT_ACCESS)
  3310. {
  3311. // These fields will be updated at attention time
  3312. bp->base = NULL;
  3313. bp->outerWidth = 0;
  3314. cfb->buf.flags = DIB_FORMAT;
  3315. }
  3316. if (GLSURF_IS_MEMDC(gengc->dwCurrentFlags))
  3317. {
  3318. bp->flags = bp->flags | (MEMORY_DC | NO_CLIP);
  3319. }
  3320. else if (gengc->gsurf.dwFlags & GLSURF_DIRECTDRAW)
  3321. {
  3322. LPDIRECTDRAWCLIPPER pddc;
  3323. HRESULT hr;
  3324. hr = gengc->gsurf.dd.gddsFront.pdds->lpVtbl->
  3325. GetClipper(gengc->gsurf.dd.gddsFront.pdds, &pddc);
  3326. if (hr == DDERR_NOCLIPPERATTACHED)
  3327. {
  3328. bp->flags = bp->flags | NO_CLIP;
  3329. }
  3330. }
  3331. }
  3332. }
  3333. // If back buffer, we assume its a DIB, or a hardware backbuffer.
  3334. // In the case of a DIB, the bitmap memory will be allocated via
  3335. // ResizeBitmapBuffer().
  3336. else
  3337. {
  3338. #ifdef _MCD_
  3339. if (gengc->_pMcdState)
  3340. {
  3341. // Assume that MCD surface is not accessible. Accessibility
  3342. // must be determined on a per-batch basis by calling
  3343. // GenMcdUpdateBufferInfo.
  3344. cfb->resize = ResizeHardwareBackBuffer;
  3345. bp->flags &= ~(DIB_FORMAT | MEMORY_DC | NO_CLIP);
  3346. }
  3347. else
  3348. #endif
  3349. {
  3350. cfb->resize = ResizeBitmapBuffer;
  3351. bp->flags = DIB_FORMAT | MEMORY_DC | NO_CLIP;
  3352. }
  3353. }
  3354. }
  3355. /******************************Public*Routine******************************\
  3356. * glsrvCleanupWindow
  3357. *
  3358. * Called from wglCleanupWindow to remove the pwnd reference from the
  3359. * context.
  3360. *
  3361. * History:
  3362. * 05-Jul-1994 -by- Gilman Wong [gilmanw]
  3363. * Wrote it.
  3364. \**************************************************************************/
  3365. VOID APIENTRY glsrvCleanupWindow(__GLcontext *gc, GLGENwindow *pwnd)
  3366. {
  3367. __GLGENcontext *gengc = (__GLGENcontext *) gc;
  3368. // The window in gengc should be consistent with the one in the rc object.
  3369. // wglCleanupWindow should have already checked to see if the pwnd in the
  3370. // rc is one we need to remove, so we can just assert here.
  3371. ASSERTOPENGL(gengc->pwndMakeCur == pwnd,
  3372. "glsrvCleanupWindow(): bad pwnd\n");
  3373. gengc->pwndLocked = NULL;
  3374. gengc->pwndMakeCur = NULL;
  3375. }
  3376. /*
  3377. ** Fetch the data for a query in its internal type, then convert it to the
  3378. ** type that the user asked for.
  3379. **
  3380. ** This only handles the NT generic driver specific values (so far just the
  3381. ** GL_ACCUM_*_BITS values). All others fall back to the soft code function,
  3382. ** __glDoGet().
  3383. */
  3384. // These types are stolen from ..\soft\so_get.c. To minimize changes to
  3385. // the soft code, we will pull them into here rather than moving them to
  3386. // a header file and changing so_get.c to use the header file.
  3387. #define __GL_FLOAT 0 /* __GLfloat */
  3388. #define __GL_FLOAT32 1 /* api 32 bit float */
  3389. #define __GL_FLOAT64 2 /* api 64 bit float */
  3390. #define __GL_INT32 3 /* api 32 bit int */
  3391. #define __GL_BOOLEAN 4 /* api 8 bit boolean */
  3392. #define __GL_COLOR 5 /* unscaled color in __GLfloat */
  3393. #define __GL_SCOLOR 6 /* scaled color in __GLfloat */
  3394. extern void __glDoGet(GLenum, void *, GLint, const char *);
  3395. extern void __glConvertResult(__GLcontext *, GLint, const void *, GLint,
  3396. void *, GLint);
  3397. void FASTCALL __glGenDoGet(GLenum sq, void *result, GLint type, const char *procName)
  3398. {
  3399. GLint iVal;
  3400. __GLGENcontext *gengc;
  3401. __GL_SETUP_NOT_IN_BEGIN();
  3402. gengc = (__GLGENcontext *) gc;
  3403. switch (sq) {
  3404. case GL_ACCUM_RED_BITS:
  3405. iVal = gengc->gsurf.pfd.cAccumRedBits;
  3406. break;
  3407. case GL_ACCUM_GREEN_BITS:
  3408. iVal = gengc->gsurf.pfd.cAccumGreenBits;
  3409. break;
  3410. case GL_ACCUM_BLUE_BITS:
  3411. iVal = gengc->gsurf.pfd.cAccumBlueBits;
  3412. break;
  3413. case GL_ACCUM_ALPHA_BITS:
  3414. iVal = gengc->gsurf.pfd.cAccumAlphaBits;
  3415. break;
  3416. default:
  3417. __glDoGet(sq, result, type, procName);
  3418. return;
  3419. }
  3420. __glConvertResult(gc, __GL_INT32, &iVal, type, result, 1);
  3421. }
  3422. /******************************Public*Routine******************************\
  3423. *
  3424. * glsrvCopyContext
  3425. *
  3426. * Copies state from one context to another
  3427. *
  3428. * History:
  3429. * Mon Jun 05 16:53:42 1995 -by- Drew Bliss [drewb]
  3430. * Created
  3431. *
  3432. \**************************************************************************/
  3433. BOOL APIENTRY glsrvCopyContext(__GLcontext *gcSource, __GLcontext *gcDest,
  3434. GLuint mask)
  3435. {
  3436. return (BOOL)__glCopyContext(gcDest, gcSource, mask);
  3437. }