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.

515 lines
17 KiB

  1. #include "ddrawpr.h"
  2. #include "..\..\..\d3d8\inc\d3d8ddi.h"
  3. #include "d3d8sddi.h"
  4. #include "ddithunk.h"
  5. HRESULT
  6. SwDDICreateSurface( PD3D8_CREATESURFACEDATA pCreateSurface, DDSURFACEDESC2* pSurfDesc)
  7. {
  8. LPDDRAWI_DIRECTDRAW_LCL pDrv =
  9. ((PDDDEVICEHANDLE)pCreateSurface->hDD)->pSwDD->lpLcl;
  10. DDHAL_CREATESURFACEDATA CreateSurfaceData;
  11. PD3D8_SWCALLBACKS pCallbacks =
  12. (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  13. DWORD dwRet = DDHAL_DRIVER_NOTHANDLED;
  14. DWORD i;
  15. if( pCallbacks->CreateSurface )
  16. {
  17. memset(&CreateSurfaceData, 0, sizeof(CreateSurfaceData));
  18. CreateSurfaceData.lpDD = pDrv->lpGbl;
  19. CreateSurfaceData.lpDDSurfaceDesc = (DDSURFACEDESC*)pSurfDesc;
  20. CreateSurfaceData.lplpSList = NULL;
  21. CreateSurfaceData.dwSCnt = pCreateSurface->dwSCnt;
  22. CreateSurfaceData.lplpSList = (LPDDRAWI_DDRAWSURFACE_LCL*)
  23. MemAlloc(sizeof(LPDDRAWI_DDRAWSURFACE_LCL) * CreateSurfaceData.dwSCnt);
  24. if (CreateSurfaceData.lplpSList == NULL)
  25. {
  26. return DDERR_OUTOFMEMORY;
  27. }
  28. for (i = 0; i < CreateSurfaceData.dwSCnt; i++)
  29. {
  30. CreateSurfaceData.lplpSList[i] =
  31. ((PDDSURFACE)pCreateSurface->pSList[i].hKernelHandle)->pTempHeavy->lpLcl;
  32. }
  33. dwRet = pCallbacks->CreateSurface( &CreateSurfaceData );
  34. // Now copy the fpVidMem and the pitch that the driver setup
  35. // back to the permanent structures
  36. for (i = 0; i < CreateSurfaceData.dwSCnt; i++)
  37. {
  38. pCreateSurface->pSList[i].pbPixels = (BYTE*)
  39. CreateSurfaceData.lplpSList[i]->lpGbl->fpVidMem;
  40. pCreateSurface->pSList[i].iPitch =
  41. CreateSurfaceData.lplpSList[i]->lpGbl->lPitch;
  42. }
  43. // Now clean everything up
  44. MemFree(CreateSurfaceData.lplpSList);
  45. if( dwRet == DDHAL_DRIVER_NOTHANDLED )
  46. {
  47. return DDERR_UNSUPPORTED;
  48. }
  49. return CreateSurfaceData.ddRVal;
  50. }
  51. else
  52. {
  53. return DDERR_UNSUPPORTED;
  54. }
  55. }
  56. void
  57. SwDDIAttachSurfaces( LPDDRAWI_DDRAWSURFACE_LCL psurf_from_lcl,
  58. LPDDRAWI_DDRAWSURFACE_LCL psurf_to_lcl )
  59. {
  60. LPATTACHLIST pal_from = NULL;
  61. LPATTACHLIST pal_to = NULL;
  62. /*
  63. * allocate attachment structures
  64. */
  65. pal_from = MemAlloc(sizeof(ATTACHLIST));
  66. pal_to = MemAlloc(sizeof(ATTACHLIST));
  67. if (pal_to == NULL || pal_from == NULL)
  68. {
  69. if( pal_from ) MemFree( pal_from );
  70. if( pal_to ) MemFree( pal_to );
  71. DPF_ERR("Failed memalloc, not attaching");
  72. return;
  73. }
  74. /*
  75. * connect the surfaces
  76. */
  77. pal_from->lpAttached = psurf_to_lcl;
  78. pal_from->dwFlags = DDAL_IMPLICIT;
  79. pal_from->lpLink = psurf_from_lcl->lpAttachList;
  80. psurf_from_lcl->lpAttachList = pal_from;
  81. psurf_from_lcl->dwFlags |= DDRAWISURF_ATTACHED;
  82. pal_to->lpAttached = psurf_from_lcl;
  83. pal_to->dwFlags = DDAL_IMPLICIT;
  84. pal_to->lpLink = psurf_to_lcl->lpAttachListFrom;
  85. psurf_to_lcl->lpAttachListFrom = pal_to;
  86. psurf_to_lcl->dwFlags |= DDRAWISURF_ATTACHED_FROM;
  87. }
  88. void
  89. SwDDICreateSurfaceEx(LPDDRAWI_DIRECTDRAW_LCL pDrv,
  90. LPDDRAWI_DDRAWSURFACE_LCL pLcl)
  91. {
  92. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  93. DDHAL_CREATESURFACEEXDATA CreateExData;
  94. if ((pLcl != NULL) &&
  95. (pCallbacks->CreateSurfaceEx != NULL))
  96. {
  97. CreateExData.dwFlags = 0;
  98. CreateExData.lpDDLcl = pDrv;
  99. CreateExData.lpDDSLcl = pLcl;
  100. pCallbacks->CreateSurfaceEx(&CreateExData);
  101. }
  102. }
  103. void BreakOutstandingLocks(PDDSURFACE pSurf, LPDDRAWI_DDRAWSURFACE_LCL pLcl)
  104. {
  105. D3D8_UNLOCKDATA UnlockData;
  106. while (pLcl->lpGbl->dwUsageCount > 0)
  107. {
  108. SwDDIUnlock(pSurf->pDevice, pSurf, &UnlockData, pLcl);
  109. }
  110. }
  111. DWORD
  112. SwDDIDestroySurface( HANDLE hDD, PDDSURFACE pSurf, LPDDRAWI_DDRAWSURFACE_LCL pLcl)
  113. {
  114. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDDEVICEHANDLE)hDD)->pSwDD->lpLcl;
  115. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  116. DDHAL_DESTROYSURFACEDATA DestroyData;
  117. DestroyData.lpDD = pDrv->lpGbl;
  118. DestroyData.lpDDSurface = pLcl;
  119. DestroyData.ddRVal = DD_OK;
  120. BreakOutstandingLocks(pSurf, pLcl);
  121. if (pCallbacks->DestroySurface != NULL)
  122. {
  123. pCallbacks->DestroySurface(&DestroyData);
  124. }
  125. return DestroyData.ddRVal;
  126. }
  127. DWORD WINAPI
  128. SwContextCreate(PD3D8_CONTEXTCREATEDATA pCreateContext)
  129. {
  130. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDDEVICEHANDLE)pCreateContext->hDD)->pSwDD->lpLcl;
  131. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  132. D3DHAL_CONTEXTCREATEDATA ContextData;
  133. DWORD dwRet = DDHAL_DRIVER_NOTHANDLED;
  134. if (pCallbacks->CreateContext != NULL)
  135. {
  136. ContextData.lpDDLcl = pDrv;
  137. DDASSERT(((PDDSURFACE)pCreateContext->hSurface)->dwFlags & DDSURFACE_HEAVYWEIGHT);
  138. ContextData.lpDDSLcl = ((PDDSURFACE)pCreateContext->hSurface)->Surface.pHeavy->lpLcl;
  139. if (pCreateContext->hDDSZ == NULL)
  140. {
  141. ContextData.lpDDSZLcl = NULL;
  142. }
  143. else
  144. {
  145. DDASSERT(((PDDSURFACE)pCreateContext->hDDSZ)->dwFlags & DDSURFACE_HEAVYWEIGHT);
  146. ContextData.lpDDSZLcl = ((PDDSURFACE)pCreateContext->hDDSZ)->Surface.pHeavy->lpLcl;
  147. }
  148. ContextData.dwPID = pCreateContext->dwPID;
  149. ContextData.dwhContext = pCreateContext->dwhContext;
  150. dwRet = pCallbacks->CreateContext(&ContextData);
  151. pCreateContext->dwhContext = ContextData.dwhContext;
  152. pCreateContext->ddrval = MapLegacyResult(ContextData.ddrval);
  153. }
  154. return dwRet;
  155. }
  156. DWORD WINAPI
  157. SwDrawPrimitives2(PD3D8_DRAWPRIMITIVES2DATA pdp2data)
  158. {
  159. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDSURFACE)pdp2data->hDDCommands)->pDevice->pSwDD->lpLcl;
  160. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  161. D3DHAL_DRAWPRIMITIVES2DATA DP2Data;
  162. HRESULT hr;
  163. DWORD dwRet;
  164. LPDDRAWI_DDRAWSURFACE_INT pHeavyCommand;
  165. LPDDRAWI_DDRAWSURFACE_INT pHeavyVertex = NULL;
  166. DDSURFACE* pSurfCommand;
  167. DDSURFACE* pSurfVertex = NULL;
  168. dwRet = DDHAL_DRIVER_NOTHANDLED;
  169. if (pCallbacks->DrawPrimitives2 != NULL)
  170. {
  171. memcpy(&DP2Data, pdp2data, sizeof(DP2Data));
  172. ENTER_DDRAW();
  173. pSurfCommand = pdp2data->hDDCommands;
  174. if (pSurfCommand->dwFlags & DDSURFACE_LIGHTWEIGHT)
  175. {
  176. pHeavyCommand = MapLightweightSurface(pSurfCommand);
  177. }
  178. else
  179. {
  180. pHeavyCommand = pSurfCommand->Surface.pHeavy;
  181. }
  182. if (pHeavyCommand != NULL)
  183. {
  184. DP2Data.ddrval = DD_OK;
  185. DP2Data.lpDDCommands = pHeavyCommand->lpLcl;
  186. if (!(DP2Data.dwFlags & D3DHALDP2_USERMEMVERTICES))
  187. {
  188. pSurfVertex = pdp2data->hDDCommands;
  189. if (pSurfVertex->dwFlags & DDSURFACE_LIGHTWEIGHT)
  190. {
  191. pHeavyVertex = MapLightweightSurface(pSurfVertex);
  192. }
  193. else
  194. {
  195. pHeavyVertex = pSurfVertex->Surface.pHeavy;
  196. }
  197. if (pHeavyVertex != NULL)
  198. {
  199. DP2Data.lpDDVertex = pHeavyVertex->lpLcl;
  200. }
  201. else
  202. {
  203. DP2Data.ddrval = DDERR_OUTOFMEMORY;
  204. }
  205. }
  206. if (DP2Data.ddrval == DD_OK)
  207. {
  208. dwRet = pCallbacks->DrawPrimitives2(&DP2Data);
  209. pdp2data->ddrval = DP2Data.ddrval;
  210. pdp2data->dwErrorOffset = DP2Data.dwErrorOffset;
  211. }
  212. }
  213. if ((pHeavyCommand != NULL) &&
  214. (pSurfCommand->dwFlags & DDSURFACE_LIGHTWEIGHT ))
  215. {
  216. UnmapLightweightSurface(pSurfCommand);
  217. }
  218. if ((pHeavyVertex != NULL) &&
  219. (pSurfVertex->dwFlags & DDSURFACE_LIGHTWEIGHT ))
  220. {
  221. UnmapLightweightSurface(pSurfVertex);
  222. }
  223. LEAVE_DDRAW();
  224. }
  225. return dwRet;
  226. }
  227. HRESULT
  228. SwDDILock( HANDLE hDD, PDDSURFACE pSurf, PD3D8_LOCKDATA pLockData, LPDDRAWI_DDRAWSURFACE_LCL pLcl )
  229. {
  230. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDDEVICEHANDLE)hDD)->pSwDD->lpLcl;
  231. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  232. DDHAL_LOCKDATA LockData;
  233. DWORD dwRet = DDHAL_DRIVER_HANDLED;
  234. LockData.lpDD = pDrv->lpGbl;
  235. LockData.lpDDSurface = pLcl;
  236. LockData.bHasRect = pLockData->bHasRect;
  237. LockData.rArea = pLockData->rArea;
  238. LockData.dwFlags = pLockData->dwFlags;
  239. LockData.ddRVal = DDERR_WASSTILLDRAWING;
  240. if (pLockData->bHasBox)
  241. {
  242. LockData.bHasRect = TRUE;
  243. LockData.rArea.left = pLockData->box.Left;
  244. LockData.rArea.right = pLockData->box.Right;
  245. LockData.rArea.top = pLockData->box.Top;
  246. LockData.rArea.bottom = pLockData->box.Bottom;
  247. LockData.rArea.left |= (pLockData->box.Front << 16);
  248. LockData.rArea.right |= (pLockData->box.Back << 16);
  249. }
  250. pLcl->lpGbl->dwUsageCount++;
  251. while (LockData.ddRVal == DDERR_WASSTILLDRAWING)
  252. {
  253. if (pCallbacks->Lock != NULL)
  254. {
  255. dwRet = pCallbacks->Lock(&LockData);
  256. }
  257. else
  258. {
  259. LockData.ddRVal = DD_OK;
  260. }
  261. }
  262. if (LockData.ddRVal != DD_OK)
  263. {
  264. pLcl->lpGbl->dwUsageCount--;
  265. }
  266. pLockData->lpSurfData = LockData.lpSurfData;
  267. return LockData.ddRVal;
  268. }
  269. HRESULT
  270. SwDDIUnlock( HANDLE hDD, PDDSURFACE pSurf, D3D8_UNLOCKDATA* pUnlockData, LPDDRAWI_DDRAWSURFACE_LCL pLcl )
  271. {
  272. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDDEVICEHANDLE)hDD)->pSwDD->lpLcl;
  273. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  274. DDHAL_UNLOCKDATA UnlockData;
  275. DWORD dwRet = DDHAL_DRIVER_HANDLED;;
  276. UnlockData.lpDD = pDrv->lpGbl;
  277. UnlockData.lpDDSurface = pLcl;
  278. UnlockData.ddRVal = DD_OK;
  279. pLcl->lpGbl->dwUsageCount--;
  280. if (pCallbacks->Unlock != NULL)
  281. {
  282. pCallbacks->Unlock(&UnlockData);
  283. }
  284. return UnlockData.ddRVal;
  285. }
  286. DWORD APIENTRY SwDdSetColorkey( PD3D8_SETCOLORKEYDATA pSetColorkey)
  287. {
  288. #if 0
  289. LPDDRAWI_DIRECTDRAW_LCL pDrv = ((PDDDEVICEHANDLE)pSetColorkey->hDD)->pSwDD->lpLcl;
  290. // LPDDRAWI_DDRAWSURFACE_LCL surf_lcl = ((PDDSURFACE)pSetColorkey->hSurface)->Surface.pLight->lpLcl;
  291. PD3D8_SWCALLBACKS pCallbacks = (PD3D8_SWCALLBACKS)pDrv->lpGbl->lpDDCBtmp;
  292. surf_lcl->ddckCKSrcBlt.dwColorSpaceLowValue = pSetColorkey->ColorValue;
  293. surf_lcl->dwFlags |= DDRAWISURF_HASCKEYSRCBLT;
  294. pSetColorkey->ddRVal = DD_OK;
  295. if (pCallbacks->SetColorkey != NULL)
  296. {
  297. DDHAL_SETCOLORKEYDATA data;
  298. data.lpDD = pDrv->lpGbl;
  299. data.lpDDSurface = surf_lcl;
  300. data.dwFlags = DDCKEY_SRCBLT;
  301. data.ckNew.dwColorSpaceLowValue = pSetColorkey->ColorValue;
  302. data.ddRVal = DD_OK;
  303. pCallbacks->SetColorkey(&data);
  304. pSetColorkey->ddRVal = data.ddRVal;
  305. }
  306. #endif
  307. return DDHAL_DRIVER_HANDLED;
  308. }
  309. LPDDRAWI_DIRECTDRAW_INT
  310. SwDDICreateDirectDraw( void)
  311. {
  312. LPDDRAWI_DIRECTDRAW_INT pInt;
  313. LPDDRAWI_DIRECTDRAW_LCL pLcl;
  314. LPDDRAWI_DIRECTDRAW_GBL pGbl;
  315. BYTE* pTemp;
  316. pInt = (LPDDRAWI_DIRECTDRAW_INT)MemAlloc(sizeof(DDRAWI_DIRECTDRAW_LCL) +
  317. sizeof(DDRAWI_DIRECTDRAW_GBL) +
  318. sizeof(DDRAWI_DIRECTDRAW_INT));
  319. if (pInt == NULL)
  320. {
  321. return NULL;
  322. }
  323. pTemp = (BYTE*)pInt;
  324. pTemp += sizeof(DDRAWI_DIRECTDRAW_INT);
  325. pLcl = (LPDDRAWI_DIRECTDRAW_LCL) pTemp;
  326. pInt->lpLcl = pLcl;
  327. pTemp += sizeof(DDRAWI_DIRECTDRAW_LCL);
  328. pGbl = (LPDDRAWI_DIRECTDRAW_GBL) pTemp;
  329. pLcl->lpGbl = pGbl;
  330. pLcl->dwLocalRefCnt = 1;
  331. pGbl->dwRefCnt = 1;
  332. return pInt;
  333. }
  334. void
  335. SwDDIMungeCaps( HINSTANCE hLibrary,
  336. HANDLE hDD,
  337. PD3D8_DRIVERCAPS pDriverCaps,
  338. PD3D8_CALLBACKS pCallbacks,
  339. LPDDSURFACEDESC pTextureFormats,
  340. UINT* pcTextureFormats,
  341. VOID* pInitFunction
  342. )
  343. {
  344. PD3D8GetSWInfo pfnGetSWInfo;
  345. D3DCAPS8 swCaps;
  346. D3D8_SWCALLBACKS swCallbacks;
  347. LPDDRAWI_DIRECTDRAW_LCL pLcl;
  348. LPDDRAWI_DIRECTDRAW_GBL pGbl;
  349. PDDDEVICEHANDLE pDevice = (PDDDEVICEHANDLE) hDD;
  350. DWORD i;
  351. DWORD NumTex = 0;
  352. DDSURFACEDESC* pTexList = NULL;
  353. // Get the info from the software driver
  354. memset (&swCaps, 0, sizeof(swCaps));
  355. memset (&swCallbacks, 0, sizeof(swCallbacks));
  356. if (hLibrary != NULL)
  357. {
  358. pfnGetSWInfo = (PD3D8GetSWInfo)GetProcAddress (hLibrary, D3D8HOOK_GETSWINFOPROCNAME);
  359. }
  360. else if ((hLibrary == NULL) && (pDevice->DeviceType == D3DDEVTYPE_REF))
  361. {
  362. HINSTANCE hLibraryD3D8 = NULL;
  363. // No hLibrary or an init function...it means time to fall
  364. // back on crippled ref.
  365. DPF(0,"Could not find d3dref8.dll, loading internal crippled ReferenceDevice, no rendering will take place\n");
  366. hLibraryD3D8 = LoadLibrary("d3d8.dll");
  367. if (hLibraryD3D8 != NULL)
  368. {
  369. pfnGetSWInfo = (PD3D8GetSWInfo)GetProcAddress (hLibraryD3D8, D3D8HOOK_GETSWINFOPROCNAME);
  370. FreeLibrary( hLibraryD3D8 );
  371. }
  372. else
  373. {
  374. DPF(0,"Could not find d3d8.dll to get the crippled reference device, a really bad problem indeed!!!\n");
  375. return;
  376. }
  377. }
  378. else
  379. {
  380. pfnGetSWInfo = (PD3D8GetSWInfo)pInitFunction;
  381. }
  382. if (pfnGetSWInfo != NULL )
  383. {
  384. (*pfnGetSWInfo)(&swCaps, &swCallbacks, &NumTex, &pTexList);
  385. }
  386. // Fill in out DDraw structure with the info that we have
  387. pLcl = pDevice->pSwDD->lpLcl;
  388. pGbl = pLcl->lpGbl;
  389. strcpy (pGbl->cDriverName, pDevice->szDeviceName);
  390. pGbl->vmiData.dwDisplayWidth = pDriverCaps->DisplayWidth;
  391. pGbl->vmiData.dwDisplayHeight = pDriverCaps->DisplayHeight;
  392. ConvertToOldFormat( &pGbl->vmiData.ddpfDisplay, pDriverCaps->DisplayFormatWithAlpha);
  393. // Overwite the hardware caps w/ the software caps
  394. memcpy (&pDriverCaps->D3DCaps, &swCaps, sizeof(swCaps));
  395. pDriverCaps->dwFlags |= DDIFLAG_D3DCAPS8;
  396. // Copy over our texture format list if required.
  397. *pcTextureFormats = NumTex;
  398. if (pTextureFormats && pTexList)
  399. {
  400. memcpy(
  401. pTextureFormats,
  402. pTexList,
  403. sizeof (*pTexList) * NumTex);
  404. }
  405. // Now change the callback table to point to the ones for the SW drivers
  406. if (swCallbacks.CreateContext == NULL)
  407. {
  408. pCallbacks->CreateContext = NULL;
  409. }
  410. else
  411. {
  412. pCallbacks->CreateContext = SwContextCreate;
  413. }
  414. pCallbacks->ContextDestroy = (PD3D8DDI_CONTEXTDESTROY) swCallbacks.ContextDestroy;
  415. pCallbacks->ContextDestroyAll = (PD3D8DDI_CONTEXTDESTROYALL) swCallbacks.ContextDestroyAll;
  416. pCallbacks->RenderState = (PD3D8DDI_RENDERSTATE) swCallbacks.RenderState;
  417. pCallbacks->RenderPrimitive = (PD3D8DDI_RENDERPRIMITIVE) swCallbacks.RenderPrimitive;
  418. if (swCallbacks.DrawPrimitives2 == NULL)
  419. {
  420. pCallbacks->DrawPrimitives2 = NULL;
  421. }
  422. else
  423. {
  424. pCallbacks->DrawPrimitives2 = SwDrawPrimitives2;
  425. }
  426. pCallbacks->GetDriverState = (PD3D8DDI_GETDRIVERSTATE) swCallbacks.GetDriverState;
  427. pCallbacks->ValidateTextureStageState = (PD3D8DDI_VALIDATETEXTURESTAGESTATE) swCallbacks.ValidateTextureStageState;
  428. pCallbacks->SceneCapture = (PD3D8DDI_SCENECAPTURE) swCallbacks.SceneCapture;
  429. pCallbacks->Clear2 = (PD3D8DDI_CLEAR2) swCallbacks.Clear2;
  430. pCallbacks->SetColorkey = (PD3D8DDI_SETCOLORKEY) SwDdSetColorkey;
  431. // Save the original software callbacks so we can call the software driver later
  432. if (pGbl->lpDDCBtmp == NULL)
  433. {
  434. pGbl->lpDDCBtmp = (LPDDHAL_CALLBACKS) MemAlloc(sizeof(D3D8_SWCALLBACKS));
  435. }
  436. if (pGbl->lpDDCBtmp != NULL)
  437. {
  438. memcpy(pGbl->lpDDCBtmp, &swCallbacks, sizeof(swCallbacks));
  439. }
  440. }