Skip to content

Commit 3b89b67

Browse files
authored
Hotfix for pull request #2831 (PR #2844)
1 parent c10ca92 commit 3b89b67

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Client/cefweb/CWebView.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ CWebView::~CWebView()
4141
g_pCore->GetWebCore()->SetFocusedWebView(nullptr);
4242
}
4343

44+
// Make sure we don't dead lock the CEF render thread
45+
ResumeCefThread();
46+
4447
// Ensure that CefRefPtr::~CefRefPtr doesn't try to release it twice (it has already been released in CWebView::OnBeforeClose)
45-
m_pWebView = nullptr;
48+
m_pWebView = nullptr;
4649

4750
OutputDebugLine("CWebView::~CWebView");
4851
}
@@ -77,6 +80,9 @@ void CWebView::CloseBrowser()
7780
// CefBrowserHost::CloseBrowser calls the destructor after the browser has been destroyed
7881
m_bBeingDestroyed = true;
7982

83+
// Make sure we don't dead lock the CEF render thread
84+
ResumeCefThread();
85+
8086
if (m_pWebView)
8187
m_pWebView->GetHost()->CloseBrowser(true);
8288
}
@@ -200,7 +206,7 @@ void CWebView::UpdateTexture()
200206

201207
auto pSurface = m_pWebBrowserRenderItem->m_pD3DRenderTargetSurface;
202208
if (m_bBeingDestroyed || !pSurface)
203-
return;
209+
m_RenderData.changed = m_RenderData.popupShown = false;
204210

205211
// Discard current buffer if size doesn't match
206212
// This happens when resizing the browser as OnPaint is called asynchronously
@@ -281,6 +287,9 @@ void CWebView::UpdateTexture()
281287
pSurface->UnlockRect();
282288
}
283289
}
290+
291+
m_RenderData.cefThreadState = ECefThreadState::Running;
292+
m_RenderData.cefThreadCv.notify_all();
284293
}
285294

286295
void CWebView::ExecuteJavascript(const SString& strJavascriptCode)
@@ -447,6 +456,8 @@ void CWebView::Resize(const CVector2D& size)
447456
// Send resize event to CEF
448457
if (m_pWebView)
449458
m_pWebView->GetHost()->WasResized();
459+
460+
ResumeCefThread();
450461
}
451462

452463
CVector2D CWebView::GetSize()
@@ -709,6 +720,10 @@ void CWebView::OnPaint(CefRefPtr<CefBrowser> browser, CefRenderHandler::PaintEle
709720
m_RenderData.height = height;
710721
m_RenderData.dirtyRects = dirtyRects;
711722
m_RenderData.changed = true;
723+
724+
// Wait for the main thread to handle drawing the texture
725+
m_RenderData.cefThreadState = ECefThreadState::Wait;
726+
m_RenderData.cefThreadCv.wait(lock, [&](){ return m_RenderData.cefThreadState == ECefThreadState::Running; });
712727
}
713728

714729
////////////////////////////////////////////////////////////////////
@@ -1069,3 +1084,14 @@ void CWebView::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefF
10691084
// Show no context menu
10701085
model->Clear();
10711086
}
1087+
1088+
void CWebView::ResumeCefThread()
1089+
{
1090+
{
1091+
// It's recommended to unlock a mutex before the cv notifying to avoid a possible pessimization
1092+
std::unique_lock<std::mutex> lock(m_RenderData.dataMutex);
1093+
m_RenderData.cefThreadState = ECefThreadState::Running;
1094+
}
1095+
1096+
m_RenderData.cefThreadCv.notify_all();
1097+
}

Client/cefweb/CWebView.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
#define MTA_CEF_USERAGENT "Multi Theft Auto: San Andreas Client " MTA_DM_BUILDTAG_LONG
3232

33+
enum class ECefThreadState
34+
{
35+
Running = 0, // CEF thread is currently running
36+
Wait // CEF thread is waiting for the main thread
37+
};
38+
3339
class CWebView : public CWebViewInterface,
3440
private CefClient,
3541
private CefRenderHandler,
@@ -173,6 +179,8 @@ class CWebView : public CWebViewInterface,
173179
CefRefPtr<CefMenuModel> model) override;
174180

175181
private:
182+
void ResumeCefThread();
183+
176184
CefRefPtr<CefBrowser> m_pWebView;
177185
CWebBrowserItem* m_pWebBrowserRenderItem;
178186

@@ -192,6 +200,8 @@ class CWebView : public CWebViewInterface,
192200
{
193201
bool changed = false;
194202
std::mutex dataMutex;
203+
ECefThreadState cefThreadState = ECefThreadState::Running;
204+
std::condition_variable cefThreadCv;
195205

196206
const void* buffer;
197207
int width, height;

0 commit comments

Comments
 (0)