qt-display.cpp 5.0 KB

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