qt-display.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags)
  20. : QWidget(parent, flags)
  21. {
  22. setAttribute(Qt::WA_PaintOnScreen);
  23. setAttribute(Qt::WA_StaticContents);
  24. setAttribute(Qt::WA_NoSystemBackground);
  25. setAttribute(Qt::WA_OpaquePaintEvent);
  26. setAttribute(Qt::WA_DontCreateNativeAncestors);
  27. setAttribute(Qt::WA_NativeWindow);
  28. auto windowVisible = [this] (bool visible)
  29. {
  30. if (!visible)
  31. return;
  32. if (!display) {
  33. CreateDisplay();
  34. } else {
  35. QSize size = GetPixelSize(this);
  36. obs_display_resize(display, size.width(), size.height());
  37. }
  38. };
  39. auto sizeChanged = [this] (QScreen*)
  40. {
  41. CreateDisplay();
  42. QSize size = GetPixelSize(this);
  43. obs_display_resize(display, size.width(), size.height());
  44. };
  45. connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
  46. connect(windowHandle(), &QWindow::screenChanged, sizeChanged);
  47. this->setProperty("themeID", "displayBackgroundColor");
  48. }
  49. void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
  50. {
  51. backgroundColor = (uint32_t)color_to_int(color);
  52. obs_display_set_background_color(display, backgroundColor);
  53. }
  54. void OBSQTDisplay::CreateDisplay()
  55. {
  56. if (display || !windowHandle()->isExposed())
  57. return;
  58. QSize size = GetPixelSize(this);
  59. gs_init_data info = {};
  60. info.cx = size.width();
  61. info.cy = size.height();
  62. info.format = GS_RGBA;
  63. info.zsformat = GS_ZS_NONE;
  64. QTToGSWindow(winId(), info.window);
  65. display = obs_display_create(&info, backgroundColor);
  66. emit DisplayCreated(this);
  67. }
  68. void OBSQTDisplay::resizeEvent(QResizeEvent *event)
  69. {
  70. QWidget::resizeEvent(event);
  71. CreateDisplay();
  72. if (isVisible() && display) {
  73. QSize size = GetPixelSize(this);
  74. obs_display_resize(display, size.width(), size.height());
  75. }
  76. emit DisplayResized();
  77. }
  78. void OBSQTDisplay::paintEvent(QPaintEvent *event)
  79. {
  80. CreateDisplay();
  81. QWidget::paintEvent(event);
  82. }
  83. QPaintEngine *OBSQTDisplay::paintEngine() const
  84. {
  85. return nullptr;
  86. }