1
0

qt-display.cpp 5.0 KB

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