qt-display.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 _WIN32
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <Windows.h>
  12. #endif
  13. #ifdef ENABLE_WAYLAND
  14. #include <obs-nix-platform.h>
  15. class SurfaceEventFilter : public QObject {
  16. OBSQTDisplay *display;
  17. int mTimerId;
  18. public:
  19. SurfaceEventFilter(OBSQTDisplay *src) : display(src), mTimerId(0) {}
  20. protected:
  21. bool eventFilter(QObject *obj, QEvent *event) override
  22. {
  23. bool result = QObject::eventFilter(obj, event);
  24. QPlatformSurfaceEvent *surfaceEvent;
  25. switch (event->type()) {
  26. case QEvent::PlatformSurface:
  27. surfaceEvent =
  28. static_cast<QPlatformSurfaceEvent *>(event);
  29. if (surfaceEvent->surfaceEventType() !=
  30. QPlatformSurfaceEvent::SurfaceCreated)
  31. return result;
  32. if (display->windowHandle()->isExposed())
  33. createOBSDisplay();
  34. else
  35. mTimerId = startTimer(67); // Arbitrary
  36. break;
  37. case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
  38. display->DestroyDisplay();
  39. break;
  40. case QEvent::Expose:
  41. createOBSDisplay();
  42. break;
  43. default:
  44. break;
  45. }
  46. return result;
  47. }
  48. void timerEvent(QTimerEvent *)
  49. {
  50. createOBSDisplay(display->isVisible());
  51. }
  52. private:
  53. void createOBSDisplay(bool force = false)
  54. {
  55. display->CreateDisplay(force);
  56. if (mTimerId > 0) {
  57. killTimer(mTimerId);
  58. mTimerId = 0;
  59. }
  60. }
  61. };
  62. #endif
  63. static inline long long color_to_int(const QColor &color)
  64. {
  65. auto shift = [&](unsigned val, int shift) {
  66. return ((val & 0xff) << shift);
  67. };
  68. return shift(color.red(), 0) | shift(color.green(), 8) |
  69. shift(color.blue(), 16) | shift(color.alpha(), 24);
  70. }
  71. static inline QColor rgba_to_color(uint32_t rgba)
  72. {
  73. return QColor::fromRgb(rgba & 0xFF, (rgba >> 8) & 0xFF,
  74. (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
  75. }
  76. OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags)
  77. : QWidget(parent, flags)
  78. {
  79. setAttribute(Qt::WA_PaintOnScreen);
  80. setAttribute(Qt::WA_StaticContents);
  81. setAttribute(Qt::WA_NoSystemBackground);
  82. setAttribute(Qt::WA_OpaquePaintEvent);
  83. setAttribute(Qt::WA_DontCreateNativeAncestors);
  84. setAttribute(Qt::WA_NativeWindow);
  85. auto windowVisible = [this](bool visible) {
  86. if (!visible) {
  87. #if !defined(_WIN32) && !defined(__APPLE__)
  88. display = nullptr;
  89. #endif
  90. return;
  91. }
  92. if (!display) {
  93. CreateDisplay();
  94. } else {
  95. QSize size = GetPixelSize(this);
  96. obs_display_resize(display, size.width(),
  97. size.height());
  98. }
  99. };
  100. auto screenChanged = [this](QScreen *) {
  101. CreateDisplay();
  102. QSize size = GetPixelSize(this);
  103. obs_display_resize(display, size.width(), size.height());
  104. };
  105. connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
  106. connect(windowHandle(), &QWindow::screenChanged, screenChanged);
  107. #ifdef ENABLE_WAYLAND
  108. if (obs_get_nix_platform() == OBS_NIX_PLATFORM_WAYLAND)
  109. windowHandle()->installEventFilter(
  110. new SurfaceEventFilter(this));
  111. #endif
  112. }
  113. QColor OBSQTDisplay::GetDisplayBackgroundColor() const
  114. {
  115. return rgba_to_color(backgroundColor);
  116. }
  117. void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
  118. {
  119. uint32_t newBackgroundColor = (uint32_t)color_to_int(color);
  120. if (newBackgroundColor != backgroundColor) {
  121. backgroundColor = newBackgroundColor;
  122. UpdateDisplayBackgroundColor();
  123. }
  124. }
  125. void OBSQTDisplay::UpdateDisplayBackgroundColor()
  126. {
  127. obs_display_set_background_color(display, backgroundColor);
  128. }
  129. void OBSQTDisplay::CreateDisplay(bool force)
  130. {
  131. if (display)
  132. return;
  133. if (!windowHandle()->isExposed() && !force)
  134. return;
  135. QSize size = GetPixelSize(this);
  136. gs_init_data info = {};
  137. info.cx = size.width();
  138. info.cy = size.height();
  139. info.format = GS_BGRA;
  140. info.zsformat = GS_ZS_NONE;
  141. if (!QTToGSWindow(windowHandle(), info.window))
  142. return;
  143. display = obs_display_create(&info, backgroundColor);
  144. emit DisplayCreated(this);
  145. }
  146. void OBSQTDisplay::paintEvent(QPaintEvent *event)
  147. {
  148. CreateDisplay();
  149. QWidget::paintEvent(event);
  150. }
  151. void OBSQTDisplay::moveEvent(QMoveEvent *event)
  152. {
  153. QWidget::moveEvent(event);
  154. OnMove();
  155. }
  156. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  157. bool OBSQTDisplay::nativeEvent(const QByteArray &, void *message, qintptr *)
  158. #else
  159. bool OBSQTDisplay::nativeEvent(const QByteArray &, void *message, long *)
  160. #endif
  161. {
  162. #ifdef _WIN32
  163. const MSG &msg = *static_cast<MSG *>(message);
  164. switch (msg.message) {
  165. case WM_DISPLAYCHANGE:
  166. OnDisplayChange();
  167. }
  168. #else
  169. UNUSED_PARAMETER(message);
  170. #endif
  171. return false;
  172. }
  173. void OBSQTDisplay::resizeEvent(QResizeEvent *event)
  174. {
  175. QWidget::resizeEvent(event);
  176. CreateDisplay();
  177. if (isVisible() && display) {
  178. QSize size = GetPixelSize(this);
  179. obs_display_resize(display, size.width(), size.height());
  180. }
  181. emit DisplayResized();
  182. }
  183. QPaintEngine *OBSQTDisplay::paintEngine() const
  184. {
  185. return nullptr;
  186. }
  187. void OBSQTDisplay::OnMove()
  188. {
  189. if (display)
  190. obs_display_update_color_space(display);
  191. }
  192. void OBSQTDisplay::OnDisplayChange()
  193. {
  194. if (display)
  195. obs_display_update_color_space(display);
  196. }