Skip to content

Commit

Permalink
Fix some concurrency issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jul 28, 2019
1 parent be4b34b commit a6e1b79
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 6297
#define BUILD_NUMBER 6298
1 change: 1 addition & 0 deletions ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class m_IDirect3DDeviceX : public IUnknown
ddrawParent->SetD3DDevice(this);
}
}
void ClearDdraw() { ddrawParent = nullptr; }

// Release interface
void ReleaseInterface();
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirect3DX.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class m_IDirect3DX : public IUnknown

// Functions handling the ddraw parent interface
void SetDdrawParent(m_IDirectDrawX *ddraw) { ddrawParent = ddraw; }
void ClearDdraw() { ddrawParent = nullptr; }

// Resolution hack
void ResolutionHack();
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirectDrawPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class m_IDirectDrawPalette : public IDirectDrawPalette, public AddressLookupTabl

// Functions handling the ddraw parent interface
void SetDdrawParent(m_IDirectDrawX *ddraw) { ddrawParent = ddraw; }
void ClearDdraw() { ddrawParent = nullptr; }

// Release interface
void ReleaseInterface();
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirectDrawSurfaceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class m_IDirectDrawSurfaceX : public IUnknown

// Functions handling the ddraw parent interface
void SetDdrawParent(m_IDirectDrawX *ddraw) { ddrawParent = ddraw; }
void ClearDdraw() { ddrawParent = nullptr; }

// Direct3D9 interfaces
LPDIRECT3DTEXTURE9 *GetSurfaceTexture() { return &surfaceTexture; }
Expand Down
38 changes: 30 additions & 8 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,36 +1582,37 @@ void m_IDirectDrawX::ReleaseDdraw()
{
InterlockedDecrement(&ddrawRefCount);

SetCriticalSection();

// Release Direct3DDevice interfaces
if (D3DDeviceInterface)
{
while (D3DDeviceInterface->Release() != 0) {}
D3DDeviceInterface->ClearDdraw();
D3DDeviceInterface = nullptr;
}

// Release Direct3D interfaces
if (D3DInterface)
{
while (D3DInterface->Release() != 0) {}
D3DInterface->ClearDdraw();
D3DInterface = nullptr;
}

// Release surfaces
for (m_IDirectDrawSurfaceX *pSurface : SurfaceVector)
{
while (pSurface->CanSurfaceBeDeleted() && pSurface->Release() != 0) {}
pSurface->ReleaseD9Surface();
pSurface->ClearDdraw();
}
SurfaceVector.clear();

// Release palettes
for (m_IDirectDrawPalette *pPalette : PaletteVector)
{
while (pPalette->Release() != 0) {}
pPalette->ClearDdraw();
}
PaletteVector.clear();

// ToDo: Release ColorControl and GammaControl

// Release shared d3d9device
ReleaseD3d9Device();

Expand All @@ -1623,6 +1624,8 @@ void m_IDirectDrawX::ReleaseDdraw()
d3d9Object = nullptr;
}
}

ReleaseCriticalSection();
}

HWND m_IDirectDrawX::GetHwnd()
Expand Down Expand Up @@ -1880,7 +1883,7 @@ void m_IDirectDrawX::ReleaseD3d9Device()
// Add surface wrapper to vector
void m_IDirectDrawX::AddSurfaceToVector(m_IDirectDrawSurfaceX* lpSurfaceX)
{
if (!lpSurfaceX)
if (!lpSurfaceX || DoesSurfaceExist(lpSurfaceX))
{
return;
}
Expand Down Expand Up @@ -1961,7 +1964,7 @@ void m_IDirectDrawX::EvictManagedTextures()
// Add palette wrapper to vector
void m_IDirectDrawX::AddPaletteToVector(m_IDirectDrawPalette* lpPalette)
{
if (!lpPalette)
if (!lpPalette || DoesPaletteExist(lpPalette))
{
return;
}
Expand All @@ -1987,6 +1990,25 @@ void m_IDirectDrawX::RemovePaletteFromVector(m_IDirectDrawPalette* lpPalette)
}
}

// Check if palette wrapper exists
bool m_IDirectDrawX::DoesPaletteExist(m_IDirectDrawPalette* lpPalette)
{
if (!lpPalette)
{
return false;
}

auto it = std::find_if(PaletteVector.begin(), PaletteVector.end(),
[=](auto pSurface) -> bool { return pSurface == lpPalette; });

if (it == std::end(PaletteVector))
{
return false;
}

return true;
}

void m_IDirectDrawX::SetVidMemory(LPDWORD lpdwTotal, LPDWORD lpdwFree)
{
// Set available memory, some games have issues if this is set to high
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirectDrawX.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class m_IDirectDrawX : public IUnknown
// Palette vector functions
void AddPaletteToVector(m_IDirectDrawPalette* lpPalette);
void RemovePaletteFromVector(m_IDirectDrawPalette* lpPalette);
bool DoesPaletteExist(m_IDirectDrawPalette* lpPalette);

// Video memory size
static void SetVidMemory(LPDWORD lpdwTotal, LPDWORD lpdwFree);
Expand Down

0 comments on commit a6e1b79

Please sign in to comment.