Fix a rare crash when running simulate on macOS.
The bug was introduced in 2f0fb1e4ef. The glfwSwapBuffers call should occur on the main UI thread, not the CoreVideo DisplayLink thread.
PiperOrigin-RevId: 517172202
Change-Id: I2491f2194765019748198cd0cc61ca9263331b01
This commit is contained in:
committed by
Copybara-Service
parent
230e2780de
commit
81ba9ec54a
@@ -175,14 +175,10 @@ bool GlfwAdapter::ShouldCloseWindow() const {
|
||||
void GlfwAdapter::SwapBuffers() {
|
||||
#ifdef __APPLE__
|
||||
if (core_video_.has_value()) {
|
||||
core_video_->EnqueueSwap();
|
||||
core_video_->WaitForSwap();
|
||||
} else {
|
||||
Glfw().glfwSwapBuffers(window_);
|
||||
core_video_->WaitForDisplayRefresh();
|
||||
}
|
||||
#else
|
||||
Glfw().glfwSwapBuffers(window_);
|
||||
#endif
|
||||
Glfw().glfwSwapBuffers(window_);
|
||||
}
|
||||
|
||||
void GlfwAdapter::ToggleFullscreen() {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#error "This header only works on macOS."
|
||||
#endif
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
@@ -37,8 +38,8 @@ class GlfwCoreVideo {
|
||||
public:
|
||||
GlfwCoreVideo(GLFWwindow* window);
|
||||
~GlfwCoreVideo();
|
||||
void EnqueueSwap();
|
||||
void WaitForSwap();
|
||||
|
||||
void WaitForDisplayRefresh();
|
||||
int DisplayLinkCallback();
|
||||
void UpdateDisplayLink();
|
||||
|
||||
@@ -46,7 +47,7 @@ class GlfwCoreVideo {
|
||||
GLFWwindow* window_;
|
||||
CVDisplayLinkRef display_link_;
|
||||
|
||||
bool second_buffer_has_content_;
|
||||
std::atomic_bool waiting_;
|
||||
std::mutex mu_;
|
||||
std::condition_variable cond_;
|
||||
};
|
||||
|
||||
@@ -45,23 +45,16 @@ GlfwCoreVideo::~GlfwCoreVideo() {
|
||||
CVDisplayLinkRelease(display_link_);
|
||||
}
|
||||
|
||||
void GlfwCoreVideo::EnqueueSwap() {
|
||||
void GlfwCoreVideo::WaitForDisplayRefresh() {
|
||||
std::unique_lock lock(mu_);
|
||||
second_buffer_has_content_ = true;
|
||||
}
|
||||
|
||||
void GlfwCoreVideo::WaitForSwap() {
|
||||
if (second_buffer_has_content_) {
|
||||
std::unique_lock lock(mu_);
|
||||
cond_.wait(lock, [this]() { return !this->second_buffer_has_content_; });
|
||||
}
|
||||
waiting_.store(true);
|
||||
cond_.wait(lock, [this]() { return !this->waiting_; });
|
||||
}
|
||||
|
||||
int GlfwCoreVideo::DisplayLinkCallback() {
|
||||
if (second_buffer_has_content_) {
|
||||
if (waiting_.load()) {
|
||||
std::unique_lock lock(mu_);
|
||||
Glfw().glfwSwapBuffers(window_);
|
||||
second_buffer_has_content_ = false;
|
||||
waiting_.store(false);
|
||||
cond_.notify_one();
|
||||
}
|
||||
return kCVReturnSuccess;
|
||||
|
||||
Reference in New Issue
Block a user