OBSQTDisplay.cpp 4.3 KB

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