Skip to content

Commit

Permalink
Fix QT display
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed Jan 6, 2025
1 parent 89fa5ea commit 26cb6b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
49 changes: 19 additions & 30 deletions qt-display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@

class SurfaceEventFilter : public QObject {
OBSQTDisplay *display;
int mTimerId;

public:
SurfaceEventFilter(OBSQTDisplay *src) : display(src), mTimerId(0) {}
SurfaceEventFilter(OBSQTDisplay *src) : QObject(src), display(src) {}

protected:
bool eventFilter(QObject *obj, QEvent *event) override
Expand All @@ -40,10 +39,7 @@ class SurfaceEventFilter : public QObject {

switch (surfaceEvent->surfaceEventType()) {
case QPlatformSurfaceEvent::SurfaceCreated:
if (display->windowHandle()->isExposed())
createOBSDisplay();
else
mTimerId = startTimer(67); // Arbitrary
display->RestoreDisplay();
break;
case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
display->DestroyDisplay();
Expand All @@ -54,29 +50,16 @@ class SurfaceEventFilter : public QObject {

break;
case QEvent::Expose:
createOBSDisplay();
display->RestoreDisplay();
break;
default:
break;
}

return result;
}

void timerEvent(QTimerEvent *) override { createOBSDisplay(display->isVisible()); }

private:
void createOBSDisplay(bool force = false)
{
display->CreateDisplay(force);
if (mTimerId > 0) {
killTimer(mTimerId);
mTimerId = 0;
}
}
};


static inline long long color_to_int(const QColor &color)
{
auto shift = [&](unsigned val, int shift_left) {
Expand Down Expand Up @@ -126,10 +109,7 @@ OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags) : QWidget(par
connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
connect(windowHandle(), &QWindow::screenChanged, screenChanged);

#ifdef ENABLE_WAYLAND
if (obs_get_nix_platform() == OBS_NIX_PLATFORM_WAYLAND)
windowHandle()->installEventFilter(new SurfaceEventFilter(this));
#endif
windowHandle()->installEventFilter(new SurfaceEventFilter(this));
}

QColor OBSQTDisplay::GetDisplayBackgroundColor() const
Expand All @@ -152,7 +132,7 @@ void OBSQTDisplay::UpdateDisplayBackgroundColor()
obs_display_set_background_color(display, backgroundColor);
}

bool QTToGSWindow(QWindow *window, gs_window &gswindow)
static bool QTToGSWindow(QWindow *window, gs_window &gswindow)
{
bool success = true;

Expand All @@ -161,27 +141,36 @@ bool QTToGSWindow(QWindow *window, gs_window &gswindow)
#elif __APPLE__
gswindow.view = (id)window->winId();
#else
if (obs_get_nix_platform() == OBS_NIX_PLATFORM_X11_EGL) {
switch (obs_get_nix_platform()) {
case OBS_NIX_PLATFORM_X11_EGL:
gswindow.id = (uint32_t)window->winId();
gswindow.display = obs_get_nix_platform_display();
}
break;
#ifdef ENABLE_WAYLAND
if (obs_get_nix_platform() == OBS_NIX_PLATFORM_WAYLAND) {
case OBS_NIX_PLATFORM_WAYLAND: {
QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
gswindow.display = native->nativeResourceForWindow("surface", window);
success = gswindow.display != nullptr;
break;
}
#endif
default:
success = false;
break;
}
#endif
return success;
}

void OBSQTDisplay::CreateDisplay(bool force)
void OBSQTDisplay::CreateDisplay()
{
if (display)
return;

if (!windowHandle()->isExposed() && !force)
if (destroying)
return;

if (!windowHandle()->isExposed())
return;

QSize size = GetPixelSize(this);
Expand Down
14 changes: 12 additions & 2 deletions qt-display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OBSQTDisplay : public QWidget {
QColor displayBackgroundColor MEMBER backgroundColor READ GetDisplayBackgroundColor WRITE SetDisplayBackgroundColor)

OBSDisplay display;
bool destroying = false;

virtual void paintEvent(QPaintEvent *event) override;
virtual void moveEvent(QMoveEvent *event) override;
Expand Down Expand Up @@ -38,8 +39,17 @@ class OBSQTDisplay : public QWidget {
QColor GetDisplayBackgroundColor() const;
void SetDisplayBackgroundColor(const QColor &color);
void UpdateDisplayBackgroundColor();
void CreateDisplay(bool force = false);
void DestroyDisplay() { display = nullptr; };
void CreateDisplay();
void DestroyDisplay()
{
display = nullptr;
destroying = true;
};
void RestoreDisplay()
{
destroying = false;
CreateDisplay();
};

void OnMove();
void OnDisplayChange();
Expand Down

0 comments on commit 26cb6b2

Please sign in to comment.