ReactOS  0.4.14-dev-554-g2f8d847
ddraw_main.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS DirectX
4  * FILE: dll/directx/ddraw/Ddraw/ddraw_main.c
5  * PURPOSE: IDirectDraw7 Implementation
6  * PROGRAMMER: Magnus Olsen, Maarten Bosma
7  *
8  */
9 
10 /* TODO
11  * add warper functions for dx 1 - 6
12  * map the DirectDraw4_Vtable, DirectDraw2_Vtable, DirectDraw_Vtable
13  * table to right version of the functions
14  */
15 
16 
17 
18 #include "rosdraw.h"
19 
20 #include <string.h>
21 
24 {
26  DxHeapMemAlloc(newThis, sizeof(DDRAWI_DIRECTDRAW_INT));
27  if (newThis)
28  {
29  newThis->lpLcl = This->lpLcl;
30  newThis->lpLink = This;
31  }
32 
33  return newThis;
34 }
35 
38  REFIID id,
39  LPVOID *obj)
40 {
41  HRESULT retVal = DD_OK;
42 
44 
45  _SEH2_TRY
46  {
47  /* FIXME
48  the D3D object can be obtained from here
49  Direct3D7
50  */
51  if (IsEqualGUID(&IID_IDirectDraw7, id))
52  {
53  if (This->lpVtbl != &DirectDraw7_Vtable)
54  {
56  if (!This)
57  {
58  retVal = DDERR_OUTOFVIDEOMEMORY;
60  }
61  }
62 
63  This->lpVtbl = &DirectDraw7_Vtable;
64  *obj = This;
66  }
67  else if (IsEqualGUID(&IID_IDirectDraw4, id))
68  {
69  if (This->lpVtbl != &DirectDraw4_Vtable)
70  {
72  if (!This)
73  {
74  retVal = DDERR_OUTOFVIDEOMEMORY;
76  }
77  }
78 
79  This->lpVtbl = &DirectDraw4_Vtable;
80  *obj = This;
82  }
83 
84  else if (IsEqualGUID(&IID_IDirectDraw2, id))
85  {
86  if (This->lpVtbl != &DirectDraw2_Vtable)
87  {
89  if (!This)
90  {
91  retVal = DDERR_OUTOFVIDEOMEMORY;
93  }
94  }
95 
96  This->lpVtbl = &DirectDraw2_Vtable;
97  *obj = This;
99  }
100  else if (IsEqualGUID(&IID_IDirectDraw, id))
101  {
102  if (This->lpVtbl != &DirectDraw_Vtable)
103  {
105  if (!This)
106  {
107  retVal = DDERR_OUTOFVIDEOMEMORY;
108  _SEH2_LEAVE;
109  }
110  }
111 
112  This->lpVtbl = &DirectDraw_Vtable;
113  *obj = This;
115  }
116  else
117  {
118  *obj = NULL;
119  DX_STUB_str("E_NOINTERFACE");
120  retVal = E_NOINTERFACE;
121  }
122  }
124  {
125  }
126  _SEH2_END;
127 
128  return retVal;
129 }
130 
131 /*++
132 * @name DDraw->AddRef
133 * @implemented
134 *
135 * The function DDraw->AddRef manages all ref counters in the COM object DDraw->
136 
137 * @return
138 * Returns the local Ref counter value for the COM object
139 *
140 * @remarks.
141 * none
142 *
143 *--*/
146 {
147  ULONG retValue = 0;
148 
149  DX_WINDBG_trace();
150 
151  /* Lock the thread */
153 
154  _SEH2_TRY
155  {
156  /* Increment the internal ref counter */
157  This->dwIntRefCnt++;
158 
159  /* Increment the local internal ref counter */
160  This->lpLcl->dwLocalRefCnt++;
161 
162  if (This->lpLcl->lpGbl != NULL)
163  {
164  /* Increment the global internal ref counter */
165  This->lpLcl->lpGbl->dwRefCnt++;
166  }
167  }
169  {
170  }
171  _SEH2_END;
172 
173  _SEH2_TRY
174  {
175  retValue = This->dwIntRefCnt;
176  }
178  {
179  retValue = 0;
180  }
181  _SEH2_END;
182 
183  /* Release the thread lock */
185 
186  /* Return the local Ref counter */
187  return retValue;
188 }
189 
190 
191 
192 
195 {
196  ULONG Counter = 0;
197 
198  DX_WINDBG_trace();
199 
200  /* Lock the thread */
202 
203  _SEH2_TRY
204  {
205  if (This!=NULL)
206  {
207  This->lpLcl->dwLocalRefCnt--;
208  This->dwIntRefCnt--;
209 
210  if (This->lpLcl->lpGbl != NULL)
211  {
212  This->lpLcl->lpGbl->dwRefCnt--;
213  }
214 
215  if ( This->lpLcl->lpGbl->dwRefCnt == 0)
216  {
217  // set resolution back to the one in registry
218  /*if(This->cooperative_level & DDSCL_EXCLUSIVE)
219  {
220  ChangeDisplaySettings(NULL, 0);
221  }*/
222 
223  Cleanup(This);
224  }
225 
226  /* FIXME cleanup being not call why ?? */
227  Counter = This->dwIntRefCnt;
228  }
229  else
230  {
231  Counter = This->dwIntRefCnt;
232  }
233  }
235  {
236  }
237  _SEH2_END;
238 
239  /* Release the thread lock */
241 
242  return Counter;
243 }
244 
245 
248 {
250 }
251 
252 
253 /*++
254 * @name DDraw->Compact
255 * @implemented
256 *
257 * In exclusive mode the function DDraw->Compact returns DERR_NOEXCLUSIVEMODE, otherwise it returns DD_OK
258 *
259 * @return
260 * Returns only error code DD_OK or DERR_NOEXCLUSIVEMODE
261 *
262 * @remarks.
263 * Microsoft says Compact is not implemented in ddraw.dll, but it returns DDERR_NOEXCLUSIVEMODE or DD_OK
264 *
265 *--*/
268 {
269  HRESULT retVal = DD_OK;
270 
271  DX_WINDBG_trace();
272 
273  /* Lock the thread */
275 
276  _SEH2_TRY
277  {
278  /* Check if Exclusive mode has been activated */
279  if (This->lpLcl->lpGbl->lpExclusiveOwner != This->lpLcl)
280  {
281  retVal = DDERR_NOEXCLUSIVEMODE;
282  }
283  }
285  {
286  }
287  _SEH2_END;
288 
289  /* Release the thread lock */
291 
292  return retVal;
293 }
294 
297 {
298  DDSCAPS2 myddscaps;
299  HRESULT retValue = DD_OK;
300 
301  ZeroMemory(&myddscaps, sizeof(DDSCAPS2));
302 
303  _SEH2_TRY
304  {
305  myddscaps.dwCaps = ddscaps->dwCaps;
306  retValue = Main_DirectDraw_GetAvailableVidMem4(This, &myddscaps, dwTotal, dwFree);
307  }
309  {
310  retValue = DDERR_INVALIDPARAMS;
311  }
312  _SEH2_END;
313 
314  return retValue;
315 }
316 
319  LPDWORD dwTotal, LPDWORD dwFree)
320 {
321  HRESULT retVal = DD_OK;
323 
324  DX_WINDBG_trace();
325 
326  _SEH2_TRY
327  {
328  // There is no HEL implementation of this api
329  if (!(This->lpLcl->lpDDCB->HALDDMiscellaneous.dwFlags & DDHAL_MISCCB32_GETAVAILDRIVERMEMORY) ||
330  (This->lpLcl->lpGbl->dwFlags & DDRAWI_NOHARDWARE) )
331  {
332  retVal = DDERR_NODIRECTDRAWHW;
333  }
334  else
335  {
336  if ((!dwTotal && !dwFree) || !ddscaps)
337  {
338  retVal = DDERR_INVALIDPARAMS;
339  _SEH2_LEAVE;
340  }
341 
345  {
346  retVal = DDERR_INVALIDPARAMS;
347  _SEH2_LEAVE;
348  }
349 
350 
351  /* ddscaps->dwCaps2 & 0x01
352  this flag is outdate and are
353  set to 0 in ms dxsdk the name of
354  this flag is DDSCAPS2_HARDWAREDEINTERLACE
355  */
356 
357  if ( ddscaps->dwCaps2 & 0x01)
358  {
359  retVal = DDERR_INVALIDCAPS;
360  _SEH2_LEAVE;
361  }
362 
366  DDSCAPS3_DMAP))
367  {
368  retVal = DDERR_INVALIDCAPS;
369  _SEH2_LEAVE;
370  }
371 
372  if ( ddscaps->dwCaps4)
373  {
374  retVal = DDERR_INVALIDCAPS;
375  _SEH2_LEAVE;
376  }
377 
378  ZeroMemory(&memdata, sizeof(DDHAL_GETAVAILDRIVERMEMORYDATA));
379  memdata.lpDD = This->lpLcl->lpGbl;
380  memdata.ddRVal = DDERR_INVALIDPARAMS;
381 
382  memdata.ddsCapsEx.dwCaps2 = ddscaps->dwCaps2;
383  memdata.ddsCapsEx.dwCaps3 = ddscaps->dwCaps3;
384 
385  This->lpLcl->lpGbl->hDD = This->lpLcl->hDD;
386 
387  if (This->lpLcl->lpDDCB->HALDDMiscellaneous.GetAvailDriverMemory(&memdata) == DDHAL_DRIVER_NOTHANDLED)
388  {
389  retVal = DDERR_NODIRECTDRAWHW;
390 
391  if (dwTotal)
392  *dwTotal = 0;
393 
394  if (dwFree)
395  *dwFree = 0;
396  }
397  else
398  {
399  if (dwTotal)
400  *dwTotal = memdata.dwTotal;
401 
402  if (dwFree)
403  *dwFree = memdata.dwFree;
404 
405  retVal = memdata.ddRVal;
406  }
407  }
408  }
410  {
411  }
412  _SEH2_END;
413 
414  return retVal;
415 }
416 
419 {
420  HRESULT retVal = DD_OK;
421 
422  DX_WINDBG_trace();
423 
424 
425  // EnterCriticalSection(&ddcs);
426 
427  _SEH2_TRY
428  {
429  if(IsBadWritePtr(lpNumCodes,sizeof(LPDWORD)))
430  {
431  retVal = DDERR_INVALIDPARAMS;
432  }
433  else
434  {
435  if(!(IsBadWritePtr(lpNumCodes,sizeof(LPDWORD))))
436  {
437  DWORD size;
438 
439  if (*lpNumCodes > This->lpLcl->lpGbl->dwNumFourCC)
440  {
441  *lpNumCodes = This->lpLcl->lpGbl->dwNumFourCC;
442  }
443 
444  size = *lpNumCodes * sizeof(DWORD);
445 
446  if(!IsBadWritePtr(lpCodes, size ))
447  {
448  memcpy(lpCodes, This->lpLcl->lpGbl->lpdwFourCC, size );
449  }
450  else
451  {
452  *lpNumCodes = This->lpLcl->lpGbl->dwNumFourCC;
453  }
454  }
455  }
456  }
458  {
459  }
460  _SEH2_END;
461 
462  //LeaveCriticalSection(&ddcs);
463  return retVal;
464 }
465 
466 
467 /*
468  * We can obtain the version of the directdraw object by compare the
469  * vtl table pointer from iface we do not need pass which version
470  * we whant to use
471  *
472  * Main_DirectDraw_CreateSurface is dead at moment we do only support
473  * directdraw 7 at moment
474  */
475 
476 /* For DirectDraw 1 - 3 */
479  LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
480 {
482  DDSURFACEDESC2 dd_desc_v2;
483 
484  DX_WINDBG_trace();
485 
487  *ppSurf = NULL;
488 
489  _SEH2_TRY
490  {
491  if (pDDSD->dwSize == sizeof(DDSURFACEDESC))
492  {
493  CopyDDSurfDescToDDSurfDesc2(&dd_desc_v2, (LPDDSURFACEDESC)pDDSD);
495  &dd_desc_v2,
496  ppSurf,
497  pUnkOuter);
498  }
499  else
500  {
502  }
503  }
505  {
507  }
508  _SEH2_END;
510  return ret;
511 }
512 
513 
514 /* For DirectDraw 4 - 7 */
517  LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
518 {
519  HRESULT ret = DD_OK;
520  DX_WINDBG_trace();
521 
523  *ppSurf = NULL;
524 
525  _SEH2_TRY
526  {
527  ret = Internal_CreateSurface(This, pDDSD, ppSurf, pUnkOuter);
528  }
530  {
532  }
533  _SEH2_END;
534 
535  if(*ppSurf != NULL)
537 
539  return ret;
540 }
541 
542 /* 5 of 31 DirectDraw7_Vtable api are working similar to windows */
543 /* 8 of 31 DirectDraw7_Vtable api are under devloping / testing */
544 
546  LPPALETTEENTRY palent, LPDIRECTDRAWPALETTE* ppPalette, LPUNKNOWN pUnkOuter)
547 {
548  HRESULT ret = DD_OK;
549  DX_WINDBG_trace();
550 
552  *ppPalette = NULL;
553 
554  _SEH2_TRY
555  {
556  ret = Internal_CreatePalette(This, dwFlags, palent, ppPalette, pUnkOuter);
557  }
559  {
561  }
562  _SEH2_END;
563 
564  //Versions 7 and 4 are addref'ed
565  if((This->lpVtbl == &DirectDraw7_Vtable || This->lpVtbl == &DirectDraw4_Vtable) && *ppPalette != NULL)
567 
569  return ret;
570 }
571 
572 
573 
574 
575 
576 
577 
LPDDRAWI_DIRECTDRAW_INT internal_directdraw_int_alloc(LPDDRAWI_DIRECTDRAW_INT This)
Definition: ddraw_main.c:23
#define DX_WINDBG_trace()
Definition: rosdraw.h:262
CRITICAL_SECTION ddcs
Definition: main.c:14
#define DDHAL_MISCCB32_GETAVAILDRIVERMEMORY
Definition: ddrawi.h:688
#define DDERR_NODIRECTDRAWHW
Definition: ddraw.h:137
#define DDSCAPS3_MULTISAMPLE_QUALITY_MASK
Definition: ddraw.h:321
ULONG WINAPI Main_DirectDraw_Release(LPDDRAWI_DIRECTDRAW_INT This)
Definition: ddraw_main.c:194
HRESULT WINAPI Main_DirectDraw_CreateSurface4(LPDDRAWI_DIRECTDRAW_INT This, LPDDSURFACEDESC2 pDDSD, LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
Definition: ddraw_main.c:516
#define REFIID
Definition: guiddef.h:118
#define DDSCAPS3_AUTOGENMIPMAP
Definition: ddraw.h:326
#define E_NOINTERFACE
Definition: winerror.h:2364
HRESULT WINAPI Main_DirectDraw_QueryInterface(LPDDRAWI_DIRECTDRAW_INT This, REFIID id, LPVOID *obj)
Definition: ddraw_main.c:37
#define DDSCAPS3_DMAP
Definition: ddraw.h:327
BOOL NTAPI IsBadWritePtr(IN LPVOID lp, IN UINT_PTR ucb)
Definition: except.c:885
IDirectDrawVtbl DirectDraw_Vtable
Definition: ddraw_thunk.c:835
#define DDSCAPS_WRITEONLY
Definition: ddraw.h:266
DWORD dwCaps2
Definition: ddraw.h:733
#define DDSCAPS_PALETTE
Definition: ddraw.h:257
LPDDRAWI_DIRECTDRAW_LCL lpLcl
Definition: ddrawi.h:1148
DWORD dwCaps
Definition: ddraw.h:727
#define DDHAL_DRIVER_NOTHANDLED
Definition: ddrawi.h:320
DWORD dwCaps3
Definition: ddraw.h:1060
#define DX_STUB_str(x)
Definition: rosdraw.h:254
#define DDERR_OUTOFVIDEOMEMORY
Definition: ddraw.h:112
#define ZeroMemory
Definition: winbase.h:1642
DWORD dwCaps4
Definition: ddraw.h:737
#define DDSCAPS_OWNDC
Definition: ddraw.h:268
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
IDirectDraw2Vtbl DirectDraw2_Vtable
Definition: ddraw_thunk.c:862
#define DWORD
Definition: nt_native.h:44
_SEH2_TRY
Definition: create.c:4250
HRESULT Internal_CreateSurface(LPDDRAWI_DIRECTDRAW_INT pDDraw, LPDDSURFACEDESC2 pDDSD, LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
Definition: createsurface.c:18
IDirectDraw4Vtbl DirectDraw4_Vtable
Definition: ddraw_thunk.c:890
VOID WINAPI AcquireDDThreadLock()
Definition: main.c:412
#define DDSCAPS_FLIP
Definition: ddraw.h:253
#define DDSCAPS_VISIBLE
Definition: ddraw.h:265
#define DDSCAPS3_MULTISAMPLE_MASK
Definition: ddraw.h:320
#define DD_OK
Definition: ddraw.h:186
HRESULT WINAPI Main_DirectDraw_GetAvailableVidMem(LPDDRAWI_DIRECTDRAW_INT This, LPDDSCAPS ddscaps, LPDWORD dwTotal, LPDWORD dwFree)
Definition: ddraw_main.c:296
static LPUNKNOWN
Definition: ndr_ole.c:49
#define DDSCAPS_SYSTEMMEMORY
Definition: ddraw.h:261
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define DDRAWI_NOHARDWARE
Definition: ddrawi.h:1301
DWORD dwCaps
Definition: ddraw.h:732
smooth NULL
Definition: ftsmooth.c:416
#define DDSCAPS_FRONTBUFFER
Definition: ddraw.h:254
#define DDSCAPS3_RESERVED1
Definition: ddraw.h:323
#define DDERR_ALREADYINITIALIZED
Definition: ddraw.h:67
#define DDERR_GENERIC
Definition: ddraw.h:72
#define DDSCAPS_COMPLEX
Definition: ddraw.h:252
GLsizeiptr size
Definition: glext.h:5919
LONG HRESULT
Definition: typedefs.h:77
DWORD dwCaps2
Definition: ddraw.h:1059
#define DxHeapMemAlloc(p, m)
Definition: rosdraw.h:113
#define WINAPI
Definition: msvc.h:8
VOID WINAPI ReleaseDDThreadLock()
Definition: main.c:421
unsigned long DWORD
Definition: ntddk_ex.h:95
LPDDRAWI_DIRECTDRAW_GBL lpDD
Definition: ddrawi.h:1754
int ret
DWORD dwCaps3
Definition: ddraw.h:734
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define DDERR_NOEXCLUSIVEMODE
Definition: ddraw.h:93
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static const WCHAR Cleanup[]
Definition: register.c:80
HRESULT WINAPI Main_DirectDraw_CreateSurface(LPDDRAWI_DIRECTDRAW_INT This, LPDDSURFACEDESC pDDSD, LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
Definition: ddraw_main.c:478
LPDDRAWI_DIRECTDRAW_INT lpLink
Definition: ddrawi.h:1149
void CopyDDSurfDescToDDSurfDesc2(LPDDSURFACEDESC2 dst_pDesc, LPDDSURFACEDESC src_pDesc)
_SEH2_END
Definition: create.c:4424
#define DDSCAPS_BACKBUFFER
Definition: ddraw.h:251
HRESULT WINAPI Main_DirectDraw_Initialize(LPDDRAWI_DIRECTDRAW_INT This, LPGUID lpGUID)
Definition: ddraw_main.c:247
BOOL WINAPI IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
Definition: compobj.c:4112
static LARGE_INTEGER Counter
Definition: clock.c:43
uint32_t * LPDWORD
Definition: typedefs.h:57
unsigned int ULONG
Definition: retypes.h:1
#define DDSCAPS3_RESERVED2
Definition: ddraw.h:324
#define DDERR_INVALIDCAPS
Definition: ddraw.h:75
#define DDSCAPS3_LIGHTWEIGHTMIPMAP
Definition: ddraw.h:325
ULONG WINAPI Main_DirectDraw_AddRef(LPDDRAWI_DIRECTDRAW_INT This)
Definition: ddraw_main.c:145
HRESULT Internal_CreatePalette(LPDDRAWI_DIRECTDRAW_INT pDDraw, DWORD dwFlags, LPPALETTEENTRY palent, LPDIRECTDRAWPALETTE *ppPalette, LPUNKNOWN pUnkOuter)
Definition: createpalette.c:33
HRESULT WINAPI Main_DirectDraw_GetAvailableVidMem4(LPDDRAWI_DIRECTDRAW_INT This, LPDDSCAPS2 ddscaps, LPDWORD dwTotal, LPDWORD dwFree)
Definition: ddraw_main.c:318
HRESULT WINAPI Main_DirectDraw_CreatePalette(LPDDRAWI_DIRECTDRAW_INT This, DWORD dwFlags, LPPALETTEENTRY palent, LPDIRECTDRAWPALETTE *ppPalette, LPUNKNOWN pUnkOuter)
Definition: ddraw_main.c:545
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
HRESULT WINAPI Main_DirectDraw_Compact(LPDDRAWI_DIRECTDRAW_INT This)
Definition: ddraw_main.c:267
#define _SEH2_LEAVE
Definition: filesup.c:20
IDirectDraw7Vtbl DirectDraw7_Vtable
HRESULT WINAPI Main_DirectDraw_GetFourCCCodes(LPDDRAWI_DIRECTDRAW_INT This, LPDWORD lpNumCodes, LPDWORD lpCodes)
Definition: ddraw_main.c:418
#define DDERR_INVALIDPARAMS
Definition: ddraw.h:79
struct IDirectDrawPalette * LPDIRECTDRAWPALETTE
Definition: ddraw.h:720