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.

282 lines
7.8 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: d3diunk.c
  6. * Content: Direct3D IUnknown
  7. *@@BEGIN_MSINTERNAL
  8. *
  9. * $Id$
  10. *
  11. * History:
  12. * Date By Reason
  13. * ==== == ======
  14. * 07/12/95 stevela Merged Colin's changes.
  15. * 27/08/96 stevela Ifdefed out the Close of gHEvent. We're using
  16. * DirectDraw's critical section.
  17. *@@END_MSINTERNAL
  18. *
  19. ***************************************************************************/
  20. #include "pch.cpp"
  21. #pragma hdrstop
  22. /*
  23. * If we are built with aggregation enabled then we actually need two
  24. * different Direct3D QueryInterface, AddRef and Releases. One which
  25. * does the right thing on the Direct3D object and one which simply
  26. * punts to the owning interface.
  27. */
  28. /*
  29. * CDirect3DUnk::QueryInterface
  30. */
  31. #undef DPF_MODNAME
  32. #define DPF_MODNAME "Direct3D::QueryInterface"
  33. HRESULT D3DAPI CDirect3DUnk::QueryInterface(REFIID riid, LPVOID* ppvObj)
  34. {
  35. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  36. // Release in the destructor
  37. /*
  38. * validate parms
  39. */
  40. TRY
  41. {
  42. if( !VALID_OUTPTR( ppvObj ) )
  43. {
  44. D3D_ERR( "Invalid obj ptr" );
  45. return DDERR_INVALIDPARAMS;
  46. }
  47. }
  48. EXCEPT( EXCEPTION_EXECUTE_HANDLER )
  49. {
  50. D3D_ERR( "Exception encountered validating parameters" );
  51. return DDERR_INVALIDPARAMS;
  52. }
  53. *ppvObj = NULL;
  54. D3D_INFO(3, "Direct3D IUnknown QueryInterface");
  55. if(IsEqualIID(riid, IID_IUnknown))
  56. {
  57. /*
  58. * Asking for IUnknown and we are IUnknown so just bump the
  59. * reference count and return this interface.
  60. * NOTE: Must AddRef through the interface being returned.
  61. */
  62. pD3DI->AddRef();
  63. // explicit ::CDirect3D disambiguation required since there are multiple IUnknown DIRECT3DI inherits from
  64. *ppvObj = static_cast<LPVOID>(static_cast<LPUNKNOWN>(static_cast<CDirect3D*>(pD3DI)));
  65. }
  66. else if (IsEqualIID(riid, IID_IDirect3D))
  67. {
  68. /*
  69. * Asking for legacy interface - add ref and return old interface
  70. */
  71. pD3DI->AddRef();
  72. // No disambiguation required. Only one IDirect3D base for DIRECT3DI
  73. *ppvObj = static_cast<LPVOID>(static_cast<LPDIRECT3D>(pD3DI));
  74. }
  75. else if (IsEqualIID(riid, IID_IDirect3D2))
  76. {
  77. /*
  78. * Asking for (second) legacy interface - add ref and return old interface
  79. */
  80. pD3DI->AddRef();
  81. // No disambiguation required. Only one IDirect3D2 base for DIRECT3DI
  82. *ppvObj = static_cast<LPVOID>(static_cast<LPDIRECT3D2>(pD3DI));
  83. }
  84. else if (IsEqualIID(riid, IID_IDirect3D3))
  85. {
  86. pD3DI->AddRef();
  87. // No disambiguation required. Only one IDirect3D3 base for DIRECT3DI
  88. *ppvObj = static_cast<LPVOID>(static_cast<LPDIRECT3D3>(pD3DI));
  89. }
  90. else
  91. {
  92. /*
  93. * Don't understand this interface. Fail.
  94. * NOTE: Used to return DDERR_GENERIC. Now return
  95. * E_NOINTERFACE.
  96. */
  97. return (E_NOINTERFACE);
  98. }
  99. return (D3D_OK);
  100. } /* CDirect3DUnk::QueryInterface */
  101. /*
  102. * CDirect3DUnk::AddRef
  103. */
  104. #undef DPF_MODNAME
  105. #define DPF_MODNAME "CDirect3DUnk::AddRef"
  106. ULONG D3DAPI CDirect3DUnk::AddRef()
  107. {
  108. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  109. // Release in the destructor
  110. refCnt++;
  111. D3D_INFO(3, "Direct3D IUnknown AddRef: Reference count = %d", refCnt);
  112. return (refCnt);
  113. } /* CDirect3DUnk::AddRef */
  114. /*
  115. * CDirect3DUnk::Release
  116. */
  117. #undef DPF_MODNAME
  118. #define DPF_MODNAME "CDirect3DUnk::Release"
  119. ULONG D3DAPI CDirect3DUnk::Release()
  120. {
  121. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  122. // Release in the destructor
  123. /*
  124. * decrement the ref count. if we hit 0, free the object
  125. */
  126. refCnt--;
  127. D3D_INFO(3, "Direct3D IUnknown Release: Reference count = %d", refCnt);
  128. if( refCnt == 0 )
  129. {
  130. delete pD3DI; // Delete Parent object
  131. return 0;
  132. }
  133. return refCnt;
  134. } /* D3DIUnknown_Release */
  135. DIRECT3DI::~DIRECT3DI()
  136. {
  137. D3D_INFO(3, "Release Direct3D Object");
  138. /*
  139. * free up all viewports created by this object
  140. */
  141. while (LIST_FIRST(&this->viewports)) {
  142. LPDIRECT3DVIEWPORTI lpViewI = LIST_FIRST(&this->viewports);
  143. lpViewI->Release();
  144. }
  145. /*
  146. * free up all lights created by this object
  147. */
  148. while (LIST_FIRST(&this->lights)) {
  149. LPDIRECT3DLIGHTI lpLightI = LIST_FIRST(&this->lights);
  150. lpLightI->Release();
  151. }
  152. /*
  153. * free up all devices created by this object
  154. */
  155. while (LIST_FIRST(&this->devices)) {
  156. LPDIRECT3DDEVICEI lpDevI = LIST_FIRST(&this->devices);
  157. lpDevI->Release();
  158. }
  159. /*
  160. * free up all materials left around
  161. */
  162. while (LIST_FIRST(&this->materials)) {
  163. LPDIRECT3DMATERIALI lpMatI = LIST_FIRST(&this->materials);
  164. lpMatI->Release();
  165. }
  166. delete lpTextureManager;
  167. /*
  168. * free up all allocated Buckets
  169. */
  170. #if DBG
  171. /* this->lpFreeList must have all the buckets that are allocated */
  172. if (this->lpFreeList || this->lpBufferList)
  173. {
  174. int i,j;
  175. LPD3DBUCKET temp;
  176. for (i=0,temp=this->lpFreeList;temp;i++) temp=temp->next;
  177. for (j=0,temp=this->lpBufferList;temp;j++) temp=temp->next;
  178. D3D_INFO(4,"D3D Release: recovered %d buckets in lpFreeList in %d buffers",i,j);
  179. DDASSERT(j*(D3DBUCKETBUFFERSIZE-1)==i);
  180. }
  181. #endif //DBG
  182. while (this->lpBufferList)
  183. {
  184. LPD3DBUCKET temp=this->lpBufferList;
  185. this->lpBufferList=temp->next;
  186. D3DFree(temp->lpBuffer);
  187. D3D_INFO(4,"D3D Release:lpBufferList %d bytes freed",D3DBUCKETBUFFERSIZE*sizeof(D3DBUCKET));
  188. }
  189. this->lpFreeList=NULL;
  190. }
  191. /*
  192. * DIRECT3DI::QueryInterface
  193. */
  194. #undef DPF_MODNAME
  195. #define DPF_MODNAME "DIRECT3DI::QueryInterface"
  196. HRESULT D3DAPI DIRECT3DI::QueryInterface(REFIID riid, LPVOID* ppvObj)
  197. {
  198. HRESULT ret;
  199. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  200. // Release in the destructor
  201. /*
  202. * validate parms
  203. */
  204. TRY
  205. {
  206. if( !VALID_OUTPTR( ppvObj ) )
  207. {
  208. D3D_ERR( "Invalid obj ptr" );
  209. return DDERR_INVALIDPARAMS;
  210. }
  211. }
  212. EXCEPT( EXCEPTION_EXECUTE_HANDLER )
  213. {
  214. D3D_ERR( "Exception encountered validating parameters" );
  215. return DDERR_INVALIDPARAMS;
  216. }
  217. *ppvObj = NULL;
  218. ret = this->lpOwningIUnknown->QueryInterface(riid, ppvObj);
  219. return ret;
  220. }
  221. /*
  222. * DIRECT3DI::AddRef
  223. */
  224. #undef DPF_MODNAME
  225. #define DPF_MODNAME "DIRECT3DI::AddRef"
  226. ULONG D3DAPI DIRECT3DI::AddRef()
  227. {
  228. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  229. // Release in the destructor
  230. /*
  231. * Punt to the owning interface.
  232. */
  233. return this->lpOwningIUnknown->AddRef();
  234. }
  235. /*
  236. * DIRECT3DI::Release
  237. */
  238. #undef DPF_MODNAME
  239. #define DPF_MODNAME "DIRECT3DI::Release"
  240. ULONG D3DAPI DIRECT3DI::Release()
  241. {
  242. CLockD3D lockObject(DPF_MODNAME, REMIND("")); // Takes D3D lock.
  243. // Release in the destructor
  244. /*
  245. * Punt to the owning interface.
  246. */
  247. return this->lpOwningIUnknown->Release();
  248. }