1
0

OBSQTDisplay.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "OBSQTDisplay.hpp"
  2. #include <utility/display-helpers.hpp>
  3. #include <utility/SurfaceEventFilter.hpp>
  4. #if !defined(_WIN32) && !defined(__APPLE__)
  5. #include <obs-nix-platform.h>
  6. #endif
  7. #include <QWindow>
  8. #ifdef ENABLE_WAYLAND
  9. #include <QApplication>
  10. #if QT_VERSION < QT_VERSION_CHECK(6, 9, 0)
  11. #include <qpa/qplatformnativeinterface.h>
  12. #endif
  13. #endif
  14. #ifdef _WIN32
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <Windows.h>
  17. #endif
  18. #include "moc_OBSQTDisplay.cpp"
  19. static inline long long color_to_int(const QColor &color)
  20. {
  21. auto shift = [&](unsigned val, int shift) {
  22. return ((val & 0xff) << shift);
  23. };
  24. return shift(color.red(), 0) | shift(color.green(), 8) | shift(color.blue(), 16) | shift(color.alpha(), 24);
  25. }
  26. static inline QColor rgba_to_color(uint32_t rgba)
  27. {
  28. return QColor::fromRgb(rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
  29. }
  30. static bool QTToGSWindow(QWindow *window, gs_window &gswindow)
  31. {
  32. bool success = true;
  33. #ifdef _WIN32
  34. gswindow.hwnd = (HWND)window->winId();
  35. #elif __APPLE__
  36. gswindow.view = (id)window->winId();
  37. #else
  38. switch (obs_get_nix_platform()) {
  39. case OBS_NIX_PLATFORM_X11_EGL:
  40. gswindow.id = window->winId();
  41. gswindow.display = obs_get_nix_platform_display();
  42. break;
  43. #ifdef ENABLE_WAYLAND
  44. case OBS_NIX_PLATFORM_WAYLAND: {
  45. #if QT_VERSION < QT_VERSION_CHECK(6, 9, 0)
  46. QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
  47. gswindow.display = native->nativeResourceForWindow("surface", window);
  48. #else
  49. gswindow.display = (void *)window->winId();
  50. #endif
  51. success = gswindow.display != nullptr;
  52. break;
  53. }
  54. #endif
  55. default:
  56. success = false;
  57. break;
  58. }
  59. #endif
  60. return success;
  61. }
  62. OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags)
  63. {
  64. setAttribute(Qt::WA_PaintOnScreen);
  65. setAttribute(Qt::WA_StaticContents);
  66. setAttribute(Qt::WA_NoSystemBackground);
  67. setAttribute(Qt::WA_OpaquePaintEvent);
  68. setAttribute(Qt::WA_DontCreateNativeAncestors);
  69. setAttribute(Qt::WA_NativeWindow);
  70. auto windowVisible = [this](bool visible) {
  71. if (!visible) {
  72. #if !defined(_WIN32) && !defined(__APPLE__)
  73. display = nullptr;
  74. #endif
  75. return;
  76. }
  77. if (!display) {
  78. CreateDisplay();
  79. } else {
  80. QSize size = GetPixelSize(this);
  81. obs_display_resize(display, size.width(), size.height());
  82. }
  83. };
  84. auto screenChanged = [this](QScreen *) {
  85. CreateDisplay();
  86. QSize size = GetPixelSize(this);
  87. obs_display_resize(display, size.width(), size.height());
  88. };
  89. connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
  90. connect(windowHandle(), &QWindow::screenChanged, screenChanged);
  91. windowHandle()->installEventFilter(new SurfaceEventFilter(this));
  92. }
  93. QColor OBSQTDisplay::GetDisplayBackgroundColor() const
  94. {
  95. return rgba_to_color(backgroundColor);
  96. }
  97. void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
  98. {
  99. uint32_t newBackgroundColor = (uint32_t)color_to_int(color);
  100. if (newBackgroundColor != backgroundColor) {
  101. backgroundColor = newBackgroundColor;
  102. UpdateDisplayBackgroundColor();
  103. }
  104. }
  105. void OBSQTDisplay::UpdateDisplayBackgroundColor()
  106. {
  107. obs_display_set_background_color(display, backgroundColor);
  108. }
  109. void OBSQTDisplay::CreateDisplay()
  110. {
  111. if (display)
  112. return;
  113. if (destroying)
  114. return;
  115. if (!windowHandle()->isExposed())
  116. return;
  117. QSize size = GetPixelSize(this);
  118. gs_init_data info = {};
  119. info.cx = size.width();
  120. info.cy = size.height();
  121. info.format = GS_BGRA;
  122. info.zsformat = GS_ZS_NONE;
  123. if (!QTToGSWindow(windowHandle(), info.window))
  124. return;
  125. display = obs_display_create(&info, backgroundColor);
  126. emit DisplayCreated(this);
  127. }
  128. void OBSQTDisplay::paintEvent(QPaintEvent *event)
  129. {
  130. CreateDisplay();
  131. QWidget::paintEvent(event);
  132. }
  133. void OBSQTDisplay::moveEvent(QMoveEvent *event)
  134. {
  135. QWidget::moveEvent(event);
  136. OnMove();
  137. }
  138. bool OBSQTDisplay::nativeEvent(const QByteArray &, void *message, qintptr *)
  139. {
  140. #ifdef _WIN32
  141. const MSG &msg = *static_cast<MSG *>(message);
  142. switch (msg.message) {
  143. case WM_DISPLAYCHANGE:
  144. OnDisplayChange();
  145. }
  146. #else
  147. UNUSED_PARAMETER(message);
  148. #endif
  149. return false;
  150. }
  151. void OBSQTDisplay::resizeEvent(QResizeEvent *event)
  152. {
  153. QWidget::resizeEvent(event);
  154. CreateDisplay();
  155. if (isVisible() && display) {
  156. QSize size = GetPixelSize(this);
  157. obs_display_resize(display, size.width(), size.height());
  158. }
  159. emit DisplayResized();
  160. }
  161. QPaintEngine *OBSQTDisplay::paintEngine() const
  162. {
  163. return nullptr;
  164. }
  165. void OBSQTDisplay::OnMove()
  166. {
  167. if (display)
  168. obs_display_update_color_space(display);
  169. }
  170. void OBSQTDisplay::OnDisplayChange()
  171. {
  172. if (display)
  173. obs_display_update_color_space(display);
  174. }