DXGL r435 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r434‎ | r435 | r436 >
Date:01:05, 22 April 2014
Author:admin
Status:new
Tags:
Comment:
Allow multiple DirectDraw objects.
Add some thread safety for critical initialization sections.
Modified paths:
  • /ddraw/common.h (modified) (history)
  • /ddraw/ddraw.cpp (modified) (history)
  • /ddraw/ddraw.h (modified) (history)
  • /ddraw/dllmain.cpp (modified) (history)
  • /ddraw/glDirectDraw.cpp (modified) (history)
  • /ddraw/glDirectDraw.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)

Diff [purge]

Index: ddraw/common.h
@@ -104,6 +104,8 @@
105105 #define NextMultipleOfWord NextMultipleOf4
106106 #endif
107107
 108+extern CRITICAL_SECTION dll_cs;
 109+
108110 #include "trace.h"
109111
110112 #include "../cfgmgr/cfgmgr.h"
Index: ddraw/ddraw.cpp
@@ -29,7 +29,7 @@
3030 #include <tlhelp32.h>
3131
3232 DXGLCFG dxglcfg;
33 -bool gllock = false;
 33+DWORD gllock = 0;
3434 HMODULE sysddraw = NULL;
3535 HRESULT (WINAPI *sysddrawcreate)(GUID FAR *lpGUID, LPDIRECTDRAW FAR *lplpDD, IUnknown FAR *pUnkOuter) = NULL;
3636
@@ -36,9 +36,6 @@
3737 const GUID device_template =
3838 { 0x9ff8900, 0x8c4a, 0x4ba4, { 0xbf, 0x29, 0x56, 0x50, 0x4a, 0xf, 0x3b, 0xb3 } };
3939
40 -DWORD timer;
41 -int vsyncstatus;
42 -glDirectDraw7 *dxglinterface = NULL;
4340
4441 void InitGL(int width, int height, int bpp, bool fullscreen, unsigned int frequency, HWND hWnd, glDirectDraw7 *glDD7)
4542 {
@@ -159,6 +156,7 @@
160157 {
161158 TRACE_ENTER(3,24,lpGUID,14,lplpDD,14,pUnkOuter);
162159 if(!lplpDD) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
 160+ EnterCriticalSection(&dll_cs);
163161 HRESULT ret;
164162 if(gllock || IsCallerOpenGL(_ReturnAddress()))
165163 {
@@ -170,6 +168,7 @@
171169 sysddraw = LoadLibraryA(buffer);
172170 if(!sysddraw)
173171 {
 172+ LeaveCriticalSection(&dll_cs);
174173 TRACE_EXIT(23,DDERR_GENERIC);
175174 ERR(DDERR_GENERIC);
176175 }
@@ -179,20 +178,17 @@
180179 sysddrawcreate = (HRESULT(WINAPI *)(GUID FAR*,LPDIRECTDRAW FAR*, IUnknown FAR*))GetProcAddress(sysddraw,"DirectDrawCreate");
181180 if(!sysddrawcreate)
182181 {
183 - TRACE_EXIT(23,DDERR_GENERIC);
 182+ LeaveCriticalSection(&dll_cs);
 183+ TRACE_EXIT(23, DDERR_GENERIC);
184184 ERR(DDERR_GENERIC);
185185 }
186186 }
187187 ret = sysddrawcreate(lpGUID,lplpDD,pUnkOuter);
188188 TRACE_VAR("*lplpDD",14,*lplpDD);
189 - TRACE_EXIT(23,ret);
 189+ LeaveCriticalSection(&dll_cs);
 190+ TRACE_EXIT(23, ret);
190191 return ret;
191192 }
192 - if(dxglinterface)
193 - {
194 - TRACE_EXIT(23,DDERR_DIRECTDRAWALREADYCREATED);
195 - return DDERR_DIRECTDRAWALREADYCREATED;
196 - }
197193 GetCurrentConfig(&dxglcfg,false);
198194 glDirectDraw7 *myddraw7;
199195 glDirectDraw1 *myddraw;
@@ -202,7 +198,8 @@
203199 if(error != DD_OK)
204200 {
205201 delete myddraw7;
206 - TRACE_EXIT(23,error);
 202+ LeaveCriticalSection(&dll_cs);
 203+ TRACE_EXIT(23, error);
207204 return error;
208205 }
209206 myddraw7->QueryInterface(IID_IDirectDraw,(VOID**)&myddraw);
@@ -209,7 +206,8 @@
210207 myddraw7->Release();
211208 *lplpDD = (LPDIRECTDRAW)myddraw;
212209 TRACE_VAR("*lplpDD",14,*lplpDD);
213 - TRACE_EXIT(23,error);
 210+ LeaveCriticalSection(&dll_cs);
 211+ TRACE_EXIT(23, error);
214212 return error;
215213 }
216214
@@ -261,11 +259,6 @@
262260 {
263261 TRACE_ENTER(4,24,lpGUID,14,lplpDD,24,&iid,14,pUnkOuter);
264262 if(!lplpDD) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
265 - if(dxglinterface)
266 - {
267 - TRACE_EXIT(23,DDERR_DIRECTDRAWALREADYCREATED);
268 - return DDERR_DIRECTDRAWALREADYCREATED;
269 - }
270263 GetCurrentConfig(&dxglcfg,false);
271264 glDirectDraw7 *myddraw;
272265 HRESULT error;
Index: ddraw/ddraw.h
@@ -57,13 +57,10 @@
5858 DDRAW_API BOOL IsDXGLDDraw();
5959
6060 extern DXGLCFG dxglcfg;
61 -extern bool gllock;
 61+extern DWORD gllock;
6262 extern const GUID device_template;
63 -extern DWORD timer;
64 -extern int vsyncstatus;
6563 class glRenderer;
6664 class glDirectDraw7;
67 -extern glDirectDraw7 *dxglinterface;
6865 void InitGL(int width, int height, int bpp, bool fullscreen, unsigned int frequency, HWND hWnd, glDirectDraw7 *glDD7);
6966
7067 #endif //_DDRAW_PRIVATE_H
\ No newline at end of file
Index: ddraw/dllmain.cpp
@@ -18,6 +18,7 @@
1919 #include "common.h"
2020 #include "ddraw.h"
2121 ATOM WindowClass = NULL;
 22+CRITICAL_SECTION dll_cs;
2223 BOOL APIENTRY DllMain( HMODULE hModule,
2324 DWORD ul_reason_for_call,
2425 LPVOID lpReserved
@@ -26,6 +27,7 @@
2728 switch (ul_reason_for_call)
2829 {
2930 case DLL_PROCESS_ATTACH:
 31+ InitializeCriticalSection(&dll_cs);
3032 GetCurrentConfig(&dxglcfg, true);
3133 break;
3234 case DLL_THREAD_ATTACH:
Index: ddraw/glDirectDraw.cpp
@@ -572,7 +572,6 @@
573573 glDirectDraw7::glDirectDraw7(GUID FAR* lpGUID, IUnknown FAR* pUnkOuter)
574574 {
575575 TRACE_ENTER(3,14,this,24,lpGUID,14,pUnkOuter);
576 - dxglinterface = this;
577576 initialized = false;
578577 if(((ULONG_PTR)lpGUID > 2) && IsBadReadPointer(lpGUID))
579578 {
@@ -608,7 +607,6 @@
609608 glDirectDraw7::~glDirectDraw7()
610609 {
611610 TRACE_ENTER(1,14,this);
612 - dxglinterface = NULL;
613611 if(initialized)
614612 {
615613 if(glD3D7) glD3D7->Release();
Index: ddraw/glDirectDraw.h
@@ -107,6 +107,7 @@
108108 DEVMODE oldmode;
109109 bool initialized;
110110 glDirect3D7 *glD3D7;
 111+ DWORD timer;
111112 };
112113
113114 class glDirectDraw1 : public IDirectDraw
Index: ddraw/glRenderer.cpp
@@ -738,6 +738,7 @@
739739 */
740740 BOOL glRenderer::_InitGL(int width, int height, int bpp, int fullscreen, unsigned int frequency, HWND hWnd, glDirectDraw7 *glDD7)
741741 {
 742+ EnterCriticalSection(&dll_cs);
742743 ddInterface = glDD7;
743744 if(hRC)
744745 {
@@ -753,11 +754,13 @@
754755 pfd.iPixelType = PFD_TYPE_RGBA;
755756 pfd.cColorBits = bpp;
756757 pfd.iLayerType = PFD_MAIN_PLANE;
757 - gllock = true;
 758+ InterlockedIncrement(&gllock);
758759 hDC = GetDC(RenderWnd->GetHWnd());
759760 if(!hDC)
760761 {
761762 DEBUG("glRenderer::InitGL: Can not create hDC\n");
 763+ InterlockedDecrement(&gllock);
 764+ LeaveCriticalSection(&dll_cs);
762765 return FALSE;
763766 }
764767 pf = ChoosePixelFormat(hDC,&pfd);
@@ -764,6 +767,8 @@
765768 if(!pf)
766769 {
767770 DEBUG("glRenderer::InitGL: Can not get pixelformat\n");
 771+ InterlockedDecrement(&gllock);
 772+ LeaveCriticalSection(&dll_cs);
768773 return FALSE;
769774 }
770775 if(!SetPixelFormat(hDC,pf,&pfd))
@@ -772,7 +777,8 @@
773778 if(!hRC)
774779 {
775780 DEBUG("glRenderer::InitGL: Can not create GL context\n");
776 - gllock = false;
 781+ InterlockedDecrement(&gllock);
 782+ LeaveCriticalSection(&dll_cs);
777783 return FALSE;
778784 }
779785 if(!wglMakeCurrent(hDC,hRC))
@@ -782,10 +788,12 @@
783789 hRC = NULL;
784790 ReleaseDC(RenderWnd->GetHWnd(),hDC);
785791 hDC = NULL;
786 - gllock = false;
 792+ InterlockedDecrement(&gllock);
 793+ LeaveCriticalSection(&dll_cs);
787794 return FALSE;
788795 }
789 - gllock = false;
 796+ InterlockedDecrement(&gllock);
 797+ LeaveCriticalSection(&dll_cs);
790798 ext = new glExtensions();
791799 util = new glUtil(ext);
792800 _SetSwap(1);
@@ -1298,13 +1306,14 @@
12991307 {
13001308 if(newwnd != hWnd)
13011309 {
1302 - wglMakeCurrent(NULL,NULL);
 1310+ EnterCriticalSection(&dll_cs);
 1311+ wglMakeCurrent(NULL, NULL);
13031312 ReleaseDC(hWnd,hDC);
13041313 delete RenderWnd;
13051314 RenderWnd = new glRenderWindow(width,height,fullscreen,newwnd,ddInterface);
13061315 PIXELFORMATDESCRIPTOR pfd;
13071316 GLuint pf;
1308 - gllock = true;
 1317+ InterlockedIncrement(&gllock);
13091318 ZeroMemory(&pfd,sizeof(PIXELFORMATDESCRIPTOR));
13101319 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
13111320 pfd.nVersion = 1;
@@ -1322,7 +1331,8 @@
13231332 DEBUG("glRenderer::SetWnd: Can not set pixelformat\n");
13241333 if(!wglMakeCurrent(hDC,hRC))
13251334 DEBUG("glRenderer::SetWnd: Can not activate GL context\n");
1326 - gllock = false;
 1335+ InterlockedDecrement(&gllock);
 1336+ LeaveCriticalSection(&dll_cs);
13271337 _SetSwap(1);
13281338 SwapBuffers(hDC);
13291339 timer.Calibrate(height, frequency);

Follow-up revisions

RevisionCommit summaryAuthorDate
r450Fix XP compatibility, which was broken in r435.admin01:12, 8 June 2014