qt-display.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "qt-display.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include "display-helpers.hpp"
  4. #include <QWindow>
  5. #include <QScreen>
  6. #include <QResizeEvent>
  7. #include <QShowEvent>
  8. #include <obs-config.h>
  9. #ifdef ENABLE_WAYLAND
  10. #include <obs-nix-platform.h>
  11. class SurfaceEventFilter : public QObject {
  12. OBSQTDisplay *display;
  13. int mTimerId;
  14. public:
  15. SurfaceEventFilter(OBSQTDisplay *src) : display(src), mTimerId(0) {}
  16. protected:
  17. bool eventFilter(QObject *obj, QEvent *event) override
  18. {
  19. bool result = QObject::eventFilter(obj, event);
  20. QPlatformSurfaceEvent *surfaceEvent;
  21. switch (event->type()) {
  22. case QEvent::PlatformSurface:
  23. surfaceEvent =
  24. static_cast<QPlatformSurfaceEvent *>(event);
  25. if (surfaceEvent->surfaceEventType() !=
  26. QPlatformSurfaceEvent::SurfaceCreated)
  27. return result;
  28. if (display->windowHandle()->isExposed())
  29. createOBSDisplay();
  30. else
  31. mTimerId = startTimer(67); // Arbitrary
  32. break;
  33. case QEvent::Expose:
  34. createOBSDisplay();
  35. break;
  36. default:
  37. break;
  38. }
  39. return result;
  40. }
  41. void timerEvent(QTimerEvent *) { createOBSDisplay(true); }
  42. private:
  43. void createOBSDisplay(bool force = false)
  44. {
  45. display->CreateDisplay(force);
  46. if (mTimerId > 0) {
  47. killTimer(mTimerId);
  48. mTimerId = 0;
  49. }
  50. }
  51. };
  52. #endif
  53. static inline long long color_to_int(const QColor &color)
  54. {
  55. auto shift = [&](unsigned val, int shift) {
  56. return ((val & 0xff) << shift);
  57. };
  58. return shift(color.red(), 0) | shift(color.green(), 8) |
  59. shift(color.blue(), 16) | shift(color.alpha(), 24);
  60. }
  61. static inline QColor rgba_to_color(uint32_t rgba)
  62. {
  63. return QColor::fromRgb(rgba & 0xFF, (rgba >> 8) & 0xFF,
  64. (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
  65. }
  66. OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags)
  67. : QWidget(parent, flags)
  68. {
  69. setAttribute(Qt::WA_PaintOnScreen);
  70. setAttribute(Qt::WA_StaticContents);
  71. setAttribute(Qt::WA_NoSystemBackground);
  72. setAttribute(Qt::WA_OpaquePaintEvent);
  73. setAttribute(Qt::WA_DontCreateNativeAncestors);
  74. setAttribute(Qt::WA_NativeWindow);
  75. auto windowVisible = [this](bool visible) {
  76. if (!visible) {
  77. #if !defined(_WIN32) && !defined(__APPLE__)
  78. display = nullptr;
  79. #endif
  80. return;
  81. }
  82. if (!display) {
  83. CreateDisplay();
  84. } else {
  85. QSize size = GetPixelSize(this);
  86. obs_display_resize(display, size.width(),
  87. size.height());
  88. }
  89. };
  90. auto screenChanged = [this](QScreen *) {
  91. CreateDisplay();
  92. QSize size = GetPixelSize(this);
  93. obs_display_resize(display, size.width(), size.height());
  94. };
  95. connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
  96. connect(windowHandle(), &QWindow::screenChanged, screenChanged);
  97. #ifdef ENABLE_WAYLAND
  98. if (obs_get_nix_platform() == OBS_NIX_PLATFORM_WAYLAND)
  99. windowHandle()->installEventFilter(
  100. new SurfaceEventFilter(this));
  101. #endif
  102. }
  103. QColor OBSQTDisplay::GetDisplayBackgroundColor() const
  104. {
  105. return rgba_to_color(backgroundColor);
  106. }
  107. void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
  108. {
  109. uint32_t newBackgroundColor = (uint32_t)color_to_int(color);
  110. if (newBackgroundColor != backgroundColor) {
  111. backgroundColor = newBackgroundColor;
  112. UpdateDisplayBackgroundColor();
  113. }
  114. }
  115. void OBSQTDisplay::UpdateDisplayBackgroundColor()
  116. {
  117. obs_display_set_background_color(display, backgroundColor);
  118. }
  119. void OBSQTDisplay::CreateDisplay(bool force)
  120. {
  121. if (display)
  122. return;
  123. if (!windowHandle()->isExposed() && !force)
  124. return;
  125. QSize size = GetPixelSize(this);
  126. gs_init_data info = {};
  127. info.cx = size.width();
  128. info.cy = size.height();
  129. info.format = GS_BGRA;
  130. info.zsformat = GS_ZS_NONE;
  131. if (!QTToGSWindow(windowHandle(), info.window))
  132. return;
  133. display = obs_display_create(&info, backgroundColor);
  134. emit DisplayCreated(this);
  135. }
  136. void OBSQTDisplay::resizeEvent(QResizeEvent *event)
  137. {
  138. QWidget::resizeEvent(event);
  139. CreateDisplay();
  140. if (isVisible() && display) {
  141. QSize size = GetPixelSize(this);
  142. obs_display_resize(display, size.width(), size.height());
  143. }
  144. emit DisplayResized();
  145. }
  146. void OBSQTDisplay::paintEvent(QPaintEvent *event)
  147. {
  148. CreateDisplay();
  149. QWidget::paintEvent(event);
  150. }
  151. QPaintEngine *OBSQTDisplay::paintEngine() const
  152. {
  153. return nullptr;
  154. }