Leaked source code of windows server 2003
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.

244 lines
6.7 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: halbuf.c
  6. * Content: Direct3D HAL buffer management
  7. *@@BEGIN_MSINTERNAL
  8. *
  9. * $Id: halbuf.c,v 1.1 1995/11/21 15:12:30 sjl Exp $
  10. *
  11. * History:
  12. * Date By Reason
  13. * ==== == ======
  14. * 06/11/95 stevela Initial rev.
  15. * 07/11/95 stevela stuff.
  16. * 17/02/96 colinmc Fixed build problem.
  17. *@@END_MSINTERNAL
  18. *
  19. ***************************************************************************/
  20. #include "pch.cpp"
  21. #pragma hdrstop
  22. HRESULT D3DHAL_AllocateBuffer(LPDIRECT3DDEVICEI lpDevI,
  23. LPD3DI_BUFFERHANDLE lphBuf,
  24. LPD3DEXECUTEBUFFERDESC lpDebDesc,
  25. LPDIRECTDRAWSURFACE* lplpBuf)
  26. {
  27. DDSURFACEDESC ddsd;
  28. LPD3DHAL_EXDATA hexData;
  29. DWORD dwSize = lpDebDesc->dwBufferSize;
  30. HRESULT ddrval;
  31. D3D_INFO(6, "AllocateBuffer, dwhContext = %d, lpDebDesc = %08lx, size = %d",
  32. lpDevI->dwhContext, lpDebDesc, lpDebDesc->dwBufferSize);
  33. if ((!lpDebDesc->dwFlags) & D3DDEB_BUFSIZE)
  34. {
  35. return (DDERR_INVALIDPARAMS);
  36. }
  37. D3DMalloc((void**)&hexData, sizeof(D3DHAL_EXDATA));
  38. if (!hexData) {
  39. D3D_ERR("Failed to create buffer internal data");
  40. return (D3DERR_EXECUTE_CREATE_FAILED);
  41. }
  42. memset(hexData, 0, sizeof(D3DHAL_EXDATA));
  43. hexData->debDesc = *lpDebDesc;
  44. /*
  45. * Create the buffer through DirectDraw
  46. */
  47. memset(&ddsd, 0, sizeof(DDSURFACEDESC));
  48. ddsd.dwSize = sizeof(DDSURFACEDESC);
  49. ddsd.dwFlags = DDSD_WIDTH | DDSD_CAPS;
  50. ddsd.dwWidth = hexData->debDesc.dwBufferSize;
  51. ddsd.ddsCaps.dwCaps = DDSCAPS_EXECUTEBUFFER;
  52. // System memory exebufs only for now
  53. // if (hexData->debDesc.dwCaps & D3DDEBCAPS_VIDEOMEMORY) {
  54. // ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
  55. // } else {
  56. ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
  57. // }
  58. ddrval = lpDevI->lpDD->CreateSurface(&ddsd, &hexData->lpDDS, NULL);
  59. if (ddrval != DD_OK) {
  60. D3D_ERR("failed in AllocateBuffer");
  61. D3DFree(hexData);
  62. return (D3DERR_EXECUTE_CREATE_FAILED);
  63. }
  64. LIST_INSERT_ROOT(&lpDevI->bufferHandles, hexData, link);
  65. *lphBuf = (ULONG_PTR) hexData;
  66. *lplpBuf = hexData->lpDDS;
  67. memset(lpDebDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
  68. lpDebDesc->dwSize = sizeof(D3DEXECUTEBUFFERDESC);
  69. lpDebDesc->dwBufferSize = dwSize;
  70. if (hexData->debDesc.dwFlags & D3DDEB_CAPS)
  71. {
  72. lpDebDesc->dwCaps = hexData->debDesc.dwCaps;
  73. lpDebDesc->dwCaps &= ~DDSCAPS_VIDEOMEMORY;
  74. lpDebDesc->dwCaps |= DDSCAPS_SYSTEMMEMORY;
  75. }
  76. else
  77. lpDebDesc->dwCaps = DDSCAPS_SYSTEMMEMORY;
  78. lpDebDesc->dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
  79. D3D_INFO(9, "AllocateBuffer, succeeded.");
  80. return (D3D_OK);
  81. }
  82. HRESULT D3DHAL_DeallocateBuffer(LPDIRECT3DDEVICEI lpDevI, D3DI_BUFFERHANDLE hBuf)
  83. {
  84. LPD3DHAL_EXDATA hexData;
  85. HRESULT ddrval;
  86. D3D_INFO(6, "DeallocateBuffer, dwhContext = %d, hBuf = %08lx",
  87. lpDevI->dwhContext, hBuf);
  88. hexData = (LPD3DHAL_EXDATA) hBuf;
  89. TRY {
  90. ddrval = hexData->lpDDS->Release();
  91. if (ddrval != DD_OK) {
  92. D3D_ERR("did not free the memory");
  93. return (ddrval);
  94. }
  95. } EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
  96. D3D_INFO(6, "Execute buffer surface was already freed by DirectDraw");
  97. }
  98. LIST_DELETE(hexData, link);
  99. D3DFree(hexData);
  100. return (D3D_OK);
  101. }
  102. HRESULT D3DHAL_DeallocateBuffers(LPDIRECT3DDEVICEI lpDevI)
  103. {
  104. while (LIST_FIRST(&lpDevI->bufferHandles)) {
  105. D3DHAL_DeallocateBuffer(lpDevI,
  106. (D3DI_BUFFERHANDLE)
  107. LIST_FIRST(&lpDevI->bufferHandles));
  108. }
  109. return (D3D_OK);
  110. }
  111. HRESULT D3DHAL_LockBuffer(LPDIRECT3DDEVICEI lpDevI, D3DI_BUFFERHANDLE hBuf,
  112. LPD3DEXECUTEBUFFERDESC lpUserDebDesc,
  113. LPDIRECTDRAWSURFACE* lplpBuf)
  114. {
  115. LPD3DHAL_EXDATA hexData;
  116. DDSURFACEDESC ddsd;
  117. HRESULT ddrval;
  118. D3D_INFO(6, "LockBuffer, dwhContext = %d, hBuf = %08lx",
  119. lpDevI->dwhContext, hBuf);
  120. hexData = (LPD3DHAL_EXDATA) hBuf;
  121. #ifdef USE_INTERNAL_LOCK
  122. do {
  123. LPDDRAWI_DDRAWSURFACE_INT lpInt;
  124. lpInt = (LPDDRAWI_DDRAWSURFACE_INT) hexData->lpDDS;
  125. ddrval = DDInternalLock(lpInt->lpLcl, &ddsd.lpSurface);
  126. } while (ddrval == DDERR_WASSTILLDRAWING);
  127. #else
  128. memset(&ddsd, 0, sizeof(DDSURFACEDESC));
  129. ddsd.dwSize = sizeof(DDSURFACEDESC);
  130. do {
  131. ddrval = hexData->lpDDS->Lock(NULL, &ddsd,
  132. DDLOCK_WAIT, NULL);
  133. } while (ddrval == DDERR_WASSTILLDRAWING);
  134. #endif
  135. if (ddrval != DD_OK) {
  136. D3D_ERR("failed in LockBuffer");
  137. return (D3DERR_EXECUTE_LOCK_FAILED);
  138. }
  139. *lplpBuf = hexData->lpDDS;
  140. memset(lpUserDebDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
  141. lpUserDebDesc->dwSize = sizeof(D3DEXECUTEBUFFERDESC);
  142. lpUserDebDesc->dwBufferSize = hexData->debDesc.dwBufferSize;
  143. lpUserDebDesc->lpData = ddsd.lpSurface;
  144. lpUserDebDesc->dwFlags = D3DDEB_LPDATA;
  145. return (D3D_OK);
  146. }
  147. HRESULT D3DHAL_UnlockBuffer(LPDIRECT3DDEVICEI lpDevI, D3DI_BUFFERHANDLE hBuf)
  148. {
  149. LPD3DHAL_EXDATA hexData;
  150. HRESULT ddrval;
  151. D3D_INFO(6, "UnlockBuffer, dwhContext = %d, hBuf = %08lx",
  152. lpDevI->dwhContext, hBuf);
  153. hexData = (LPD3DHAL_EXDATA) hBuf;
  154. #ifdef USE_INTERNAL_LOCK
  155. {
  156. LPDDRAWI_DDRAWSURFACE_INT lpInt;
  157. lpInt = (LPDDRAWI_DDRAWSURFACE_INT) hexData->lpDDS;
  158. ddrval = DDInternalUnlock(lpInt->lpLcl);
  159. }
  160. #else
  161. ddrval = hexData->lpDDS->Unlock(NULL);
  162. #endif
  163. if (ddrval != DD_OK) {
  164. D3D_ERR("didn't handle handle UnlockBuffer");
  165. return (ddrval);
  166. }
  167. return (D3D_OK);
  168. }
  169. #ifndef USE_SURFACE_LOCK
  170. HRESULT D3DHAL_LockDibEngine(LPDIRECT3DDEVICEI lpDevI)
  171. {
  172. #ifndef WIN95
  173. return D3D_OK;
  174. #else
  175. HRESULT ret;
  176. LPDDRAWI_DIRECTDRAW_GBL pdrv = lpDevI->lpDDGbl;
  177. LPWORD pdflags;
  178. BOOL isbusy;
  179. pdflags = pdrv->lpwPDeviceFlags;
  180. isbusy = 0;
  181. _asm
  182. {
  183. mov eax, pdflags
  184. bts word ptr [eax], BUSY_BIT
  185. adc isbusy,0
  186. }
  187. if (isbusy) {
  188. D3D_WARN(2, "LOCK_DIBENGINE, dibengine is busy");
  189. ret = DDERR_SURFACEBUSY;
  190. } else
  191. ret = DD_OK;
  192. return ret;
  193. #endif
  194. }
  195. void D3DHAL_UnlockDibEngine(LPDIRECT3DDEVICEI lpDevI)
  196. {
  197. #ifndef WIN95
  198. return;
  199. #else
  200. LPDDRAWI_DIRECTDRAW_GBL pdrv = lpDevI->lpDDGbl;
  201. *pdrv->lpwPDeviceFlags &= ~BUSY;
  202. #endif
  203. }
  204. #endif