qt-display.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static inline long long color_to_int(QColor color)
  9. {
  10. auto shift = [&](unsigned val, int shift)
  11. {
  12. return ((val & 0xff) << shift);
  13. };
  14. return shift(color.red(), 0) |
  15. shift(color.green(), 8) |
  16. shift(color.blue(), 16) |
  17. shift(color.alpha(), 24);
  18. }
  19. static inline QColor rgba_to_color(uint32_t rgba)
  20. {
  21. return QColor::fromRgb(rgba & 0xFF,
  22. (rgba >> 8) & 0xFF,
  23. (rgba >> 16) & 0xFF,
  24. (rgba >> 24) & 0xFF);
  25. }
  26. OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags)
  27. : QWidget(parent, flags)
  28. {
  29. setAttribute(Qt::WA_PaintOnScreen);
  30. setAttribute(Qt::WA_StaticContents);
  31. setAttribute(Qt::WA_NoSystemBackground);
  32. setAttribute(Qt::WA_OpaquePaintEvent);
  33. setAttribute(Qt::WA_DontCreateNativeAncestors);
  34. setAttribute(Qt::WA_NativeWindow);
  35. auto windowVisible = [this] (bool visible)
  36. {
  37. if (!visible)
  38. return;
  39. if (!display) {
  40. CreateDisplay();
  41. } else {
  42. QSize size = GetPixelSize(this);
  43. obs_display_resize(display, size.width(), size.height());
  44. }
  45. };
  46. auto sizeChanged = [this] (QScreen*)
  47. {
  48. CreateDisplay();
  49. QSize size = GetPixelSize(this);
  50. obs_display_resize(display, size.width(), size.height());
  51. };
  52. connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
  53. connect(windowHandle(), &QWindow::screenChanged, sizeChanged);
  54. }
  55. QColor OBSQTDisplay::GetDisplayBackgroundColor() const
  56. {
  57. return rgba_to_color(backgroundColor);
  58. }
  59. void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
  60. {
  61. uint32_t newBackgroundColor = (uint32_t)color_to_int(color);
  62. if (newBackgroundColor != backgroundColor) {
  63. backgroundColor = newBackgroundColor;
  64. UpdateDisplayBackgroundColor();
  65. }
  66. }
  67. void OBSQTDisplay::UpdateDisplayBackgroundColor()
  68. {
  69. obs_display_set_background_color(display, backgroundColor);
  70. }
  71. void OBSQTDisplay::CreateDisplay()
  72. {
  73. if (display || !windowHandle()->isExposed())
  74. return;
  75. QSize size = GetPixelSize(this);
  76. gs_init_data info = {};
  77. info.cx = size.width();
  78. info.cy = size.height();
  79. info.format = GS_RGBA;
  80. info.zsformat = GS_ZS_NONE;
  81. QTToGSWindow(winId(), info.window);
  82. display = obs_display_create(&info, backgroundColor);
  83. emit DisplayCreated(this);
  84. }
  85. void OBSQTDisplay::resizeEvent(QResizeEvent *event)
  86. {
  87. QWidget::resizeEvent(event);
  88. CreateDisplay();
  89. if (isVisible() && display) {
  90. QSize size = GetPixelSize(this);
  91. obs_display_resize(display, size.width(), size.height());
  92. }
  93. emit DisplayResized();
  94. }
  95. void OBSQTDisplay::paintEvent(QPaintEvent *event)
  96. {
  97. CreateDisplay();
  98. QWidget::paintEvent(event);
  99. }
  100. QPaintEngine *OBSQTDisplay::paintEngine() const
  101. {
  102. return nullptr;
  103. }