OpenGLDetect.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <QtGlobal>
  2. #include <QSurfaceFormat>
  3. #include <QCoreApplication>
  4. #include <QOpenGLContext>
  5. #include <QDebug>
  6. #include <mpv/client.h>
  7. #include "QtHelper.h"
  8. #include "OpenGLDetect.h"
  9. #if defined(Q_OS_MAC)
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. void detectOpenGLEarly()
  12. {
  13. // Request OpenGL 4.1 if possible on OSX, otherwise it defaults to 2.0
  14. // This needs to be done before we create the QGuiApplication
  15. //
  16. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  17. format.setMajorVersion(3);
  18. format.setMinorVersion(2);
  19. format.setProfile(QSurfaceFormat::CoreProfile);
  20. QSurfaceFormat::setDefaultFormat(format);
  21. }
  22. ///////////////////////////////////////////////////////////////////////////////////////////////////
  23. void detectOpenGLLate()
  24. {
  25. }
  26. #elif defined(Q_OS_LINUX)
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // Attempt to reuse mpv's code for detecting whether we want GLX or EGL (which
  29. // is tricky to do because of hardware decoding concerns). This is not pretty,
  30. // but quite effective and without having to duplicate too much GLX/EGL code.
  31. static QString probeHwdecInterop()
  32. {
  33. auto mpv = mpv::qt::Handle::FromRawHandle(mpv_create());
  34. if (!mpv)
  35. return "";
  36. mpv::qt::set_property(mpv, "hwdec-preload", "auto");
  37. // Actually creating a window is required. There is currently no way to keep
  38. // this window hidden or invisible.
  39. mpv::qt::set_property(mpv, "force-window", true);
  40. // As a mitigation, put the window in the top/right corner, and make it as
  41. // small as possible by forcing 1x1 size and removing window borders.
  42. mpv::qt::set_property(mpv, "geometry", "1x1+0+0");
  43. mpv::qt::set_property(mpv, "border", false);
  44. if (mpv_initialize(mpv) < 0)
  45. return "";
  46. return mpv::qt::get_property(mpv, "hwdec-interop").toString();
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////
  49. void detectOpenGLEarly()
  50. {
  51. // The putenv call must happen before Qt initializes its platform stuff.
  52. if (probeHwdecInterop() == "vaapi-egl")
  53. qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. void detectOpenGLLate()
  57. {
  58. }
  59. #elif defined(Q_OS_WIN)
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////
  61. void detectOpenGLEarly()
  62. {
  63. }
  64. /////////////////////////////////////////////////////////////////////////////////////////
  65. void detectOpenGLLate()
  66. {
  67. if (!QCoreApplication::testAttribute(Qt::AA_UseOpenGLES))
  68. return;
  69. // Workaround for broken QSGDefaultDistanceFieldGlyphCache::resizeTexture in ES 3 mode
  70. qputenv("QML_USE_GLYPHCACHE_WORKAROUND", "1");
  71. QList<int> versions = { 3, 2 };
  72. for (auto version : versions)
  73. {
  74. qInfo() << "Trying GLES version" << version;
  75. QSurfaceFormat fmt = QSurfaceFormat::defaultFormat();
  76. fmt.setMajorVersion(version);
  77. #ifdef HAVE_OPTIMALORIENTATION
  78. fmt.setOption(QSurfaceFormat::UseOptimalOrientation);
  79. #endif
  80. QOpenGLContext ctx;
  81. ctx.setFormat(fmt);
  82. if (ctx.create())
  83. {
  84. qInfo() << "Using GLES version" << version;
  85. QSurfaceFormat::setDefaultFormat(fmt);
  86. break;
  87. }
  88. }
  89. }
  90. #else
  91. ///////////////////////////////////////////////////////////////////////////////////////////////////
  92. void detectOpenGLEarly()
  93. {
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////////////////////////
  96. void detectOpenGLLate()
  97. {
  98. }
  99. #endif