PlayerQuickItem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "PlayerQuickItem.h"
  2. #include <stdexcept>
  3. #include <QOpenGLContext>
  4. #include <QRunnable>
  5. #include <QtGui/QOpenGLFramebufferObject>
  6. #include <QtQuick/QQuickWindow>
  7. #include <QOpenGLFunctions>
  8. #include "QsLog.h"
  9. #include "utils/Utils.h"
  10. #if defined(Q_OS_WIN32)
  11. #include <windows.h>
  12. #include <d3d9.h>
  13. #include <dwmapi.h>
  14. #include <avrt.h>
  15. typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
  16. static IDirect3DDevice9* d3ddevice;
  17. // This must be run before the konvergo main window switches to FS mode.
  18. void initD3DDevice(void)
  19. {
  20. // Boilerplate for creating a "blank" D3D device.
  21. // Most of this is copied from FFmpeg (LGPL).
  22. pDirect3DCreate9 *createD3D = NULL;
  23. HRESULT hr;
  24. D3DPRESENT_PARAMETERS d3dpp = {};
  25. D3DDISPLAYMODE d3ddm;
  26. UINT adapter = D3DADAPTER_DEFAULT;
  27. HMODULE d3dlib = LoadLibraryW(L"d3d9.dll");
  28. if (!d3dlib) {
  29. QLOG_ERROR() << "Failed to load D3D9 library";
  30. return;
  31. }
  32. createD3D = (pDirect3DCreate9 *)GetProcAddress(d3dlib, "Direct3DCreate9");
  33. if (!createD3D) {
  34. QLOG_ERROR() << "Failed to locate Direct3DCreate9";
  35. return;
  36. }
  37. IDirect3D9 *d3d9 = createD3D(D3D_SDK_VERSION);
  38. if (!d3d9) {
  39. QLOG_ERROR() << "Failed to create IDirect3D object";
  40. return;
  41. }
  42. IDirect3D9_GetAdapterDisplayMode(d3d9, adapter, &d3ddm);
  43. d3dpp.Windowed = TRUE;
  44. d3dpp.BackBufferWidth = 640;
  45. d3dpp.BackBufferHeight = 480;
  46. d3dpp.BackBufferCount = 0;
  47. d3dpp.BackBufferFormat = d3ddm.Format;
  48. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  49. d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
  50. hr = IDirect3D9_CreateDevice(d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
  51. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
  52. &d3dpp, &d3ddevice);
  53. if (FAILED(hr)) {
  54. QLOG_ERROR() << "Failed to create Direct3D device";
  55. return;
  56. }
  57. QLOG_INFO() << "Successfully created a Direct3D device";
  58. };
  59. // Special libmpv-specific pseudo extension for better behavior with OpenGL
  60. // fullscreen modes. This is needed with some drivers which do not allow the
  61. // libmpv DXVA code to create a new D3D device.
  62. static void* __stdcall MPGetNativeDisplay(const char* name)
  63. {
  64. QLOG_INFO() << "Asking for " << qPrintable(QString::fromUtf8((name)));
  65. if (strcmp(name, "IDirect3DDevice9") == 0)
  66. {
  67. QLOG_INFO() << "Returning device " << (void *)d3ddevice;
  68. if (d3ddevice)
  69. IDirect3DDevice9_AddRef(d3ddevice);
  70. return (void *)d3ddevice;
  71. }
  72. return NULL;
  73. }
  74. // defined(Q_OS_WIN32)
  75. #else
  76. // Unsupported or not needed. Also, not using Windows-specific calling convention.
  77. static void* MPGetNativeDisplay(const char* name)
  78. {
  79. return nullptr;
  80. }
  81. #endif
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////
  83. static void* get_proc_address(void* ctx, const char* name)
  84. {
  85. Q_UNUSED(ctx);
  86. QOpenGLContext* glctx = QOpenGLContext::currentContext();
  87. if (!glctx)
  88. return nullptr;
  89. void *res = (void *)glctx->getProcAddress(QByteArray(name));
  90. if (strcmp(name, "glMPGetNativeDisplay") == 0)
  91. {
  92. return (void *)&MPGetNativeDisplay;
  93. }
  94. #ifdef Q_OS_WIN32
  95. // wglGetProcAddress(), which is used by Qt, does not always resolve all
  96. // builtin functions with all drivers (only extensions). Qt compensates this
  97. // for a degree, but does this only for functions Qt happens to need. So
  98. // we need our own falback as well.
  99. if (!res)
  100. {
  101. HMODULE handle = (HMODULE)QOpenGLContext::openGLModuleHandle();
  102. if (handle)
  103. res = (void *)GetProcAddress(handle, name);
  104. }
  105. #endif
  106. return res;
  107. }
  108. namespace {
  109. /////////////////////////////////////////////////////////////////////////////////////////
  110. class RequestRepaintJob : public QRunnable
  111. {
  112. public:
  113. RequestRepaintJob(QQuickWindow *window) : m_window(window) { }
  114. void run() override
  115. {
  116. // QSGThreadedRenderLoop::update has a special code path that will render
  117. // without syncing the render and GUI threads unless asked elsewhere to support
  118. // QQuickAnimator animations. This is currently triggered by the fact that
  119. // QQuickWindow::update() is called from the render thread.
  120. // This allows continuing rendering video while the GUI thread is busy.
  121. //
  122. m_window->update();
  123. }
  124. private:
  125. QQuickWindow *m_window;
  126. };
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////////////////////////
  129. PlayerRenderer::PlayerRenderer(mpv::qt::Handle mpv, QQuickWindow* window)
  130. : m_mpv(mpv), m_mpvGL(nullptr), m_window(window), m_size(), m_hAvrtHandle(nullptr)
  131. {
  132. m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////
  135. bool PlayerRenderer::init()
  136. {
  137. #ifdef Q_OS_WIN32
  138. // Request Multimedia Class Schedule Service.
  139. DwmEnableMMCSS(TRUE);
  140. #endif
  141. mpv_opengl_cb_set_update_callback(m_mpvGL, on_update, (void *)this);
  142. // Signals presence of MPGetNativeDisplay().
  143. const char *extensions = "GL_MP_MPGetNativeDisplay";
  144. return mpv_opengl_cb_init_gl(m_mpvGL, extensions, get_proc_address, nullptr) >= 0;
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////
  147. PlayerRenderer::~PlayerRenderer()
  148. {
  149. // Keep in mind that the m_mpv handle must be held until this is done.
  150. if (m_mpvGL)
  151. mpv_opengl_cb_uninit_gl(m_mpvGL);
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////////////////////////
  154. void PlayerRenderer::render()
  155. {
  156. GLint fbo = 0;
  157. QOpenGLContext::currentContext()->functions()->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
  158. m_window->resetOpenGLState();
  159. // The negative height signals to mpv that the video should be flipped
  160. // (according to the flipped OpenGL coordinate system).
  161. mpv_opengl_cb_draw(m_mpvGL, fbo, m_size.width(), -m_size.height());
  162. m_window->resetOpenGLState();
  163. }
  164. ///////////////////////////////////////////////////////////////////////////////////////////////////
  165. void PlayerRenderer::swap()
  166. {
  167. mpv_opengl_cb_report_flip(m_mpvGL, 0);
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////////////////////////
  170. void PlayerRenderer::onPlaybackActive(bool active)
  171. {
  172. #ifdef Q_OS_WIN32
  173. if (active && !m_hAvrtHandle)
  174. {
  175. DWORD handle = 0;
  176. m_hAvrtHandle = AvSetMmThreadCharacteristicsW(L"Low Latency", &handle);
  177. }
  178. else if (!active && m_hAvrtHandle)
  179. {
  180. AvRevertMmThreadCharacteristics(m_hAvrtHandle);
  181. m_hAvrtHandle = 0;
  182. }
  183. #endif
  184. }
  185. ///////////////////////////////////////////////////////////////////////////////////////////////////
  186. void PlayerRenderer::on_update(void *ctx)
  187. {
  188. PlayerRenderer *self = (PlayerRenderer *)ctx;
  189. // QQuickWindow::scheduleRenderJob is expected to be called from the GUI thread but
  190. // is thread-safe when using the QSGThreadedRenderLoop. We can detect a non-threaded render
  191. // loop by checking if QQuickWindow::beforeSynchronizing was called from the GUI thread
  192. // (which affects the QObject::thread() of the PlayerRenderer).
  193. //
  194. if (self->thread() == self->m_window->thread())
  195. QMetaObject::invokeMethod(self->m_window, "update", Qt::QueuedConnection);
  196. else
  197. self->m_window->scheduleRenderJob(new RequestRepaintJob(self->m_window), QQuickWindow::NoStage);
  198. }
  199. ///////////////////////////////////////////////////////////////////////////////////////////////////
  200. PlayerQuickItem::PlayerQuickItem(QQuickItem* parent)
  201. : QQuickItem(parent), m_mpvGL(nullptr), m_renderer(nullptr)
  202. {
  203. connect(this, &QQuickItem::windowChanged, this, &PlayerQuickItem::onWindowChanged, Qt::DirectConnection);
  204. connect(this, &PlayerQuickItem::onFatalError, this, &PlayerQuickItem::onHandleFatalError, Qt::QueuedConnection);
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////////////////////////
  207. PlayerQuickItem::~PlayerQuickItem()
  208. {
  209. if (m_mpvGL)
  210. mpv_opengl_cb_set_update_callback(m_mpvGL, nullptr, nullptr);
  211. }
  212. ///////////////////////////////////////////////////////////////////////////////////////////////////
  213. void PlayerQuickItem::onWindowChanged(QQuickWindow* win)
  214. {
  215. if (win)
  216. {
  217. connect(win, &QQuickWindow::beforeSynchronizing, this, &PlayerQuickItem::onSynchronize, Qt::DirectConnection);
  218. connect(win, &QQuickWindow::sceneGraphInvalidated, this, &PlayerQuickItem::onInvalidate, Qt::DirectConnection);
  219. }
  220. }
  221. ///////////////////////////////////////////////////////////////////////////////////////////////////
  222. void PlayerQuickItem::onHandleFatalError(QString message)
  223. {
  224. throw FatalException(message);
  225. }
  226. ///////////////////////////////////////////////////////////////////////////////////////////////////
  227. void PlayerQuickItem::onSynchronize()
  228. {
  229. if (!m_renderer && m_mpv)
  230. {
  231. m_renderer = new PlayerRenderer(m_mpv, window());
  232. if (!m_renderer->init())
  233. {
  234. delete m_renderer;
  235. m_renderer = nullptr;
  236. emit onFatalError(tr("Could not initialize OpenGL."));
  237. return;
  238. }
  239. connect(window(), &QQuickWindow::beforeRendering, m_renderer, &PlayerRenderer::render, Qt::DirectConnection);
  240. connect(window(), &QQuickWindow::frameSwapped, m_renderer, &PlayerRenderer::swap, Qt::DirectConnection);
  241. connect(&PlayerComponent::Get(), &PlayerComponent::playbackActive, m_renderer, &PlayerRenderer::onPlaybackActive, Qt::QueuedConnection);
  242. window()->setPersistentOpenGLContext(true);
  243. window()->setPersistentSceneGraph(true);
  244. window()->setClearBeforeRendering(false);
  245. m_debugInfo = "";
  246. QOpenGLContext* glctx = QOpenGLContext::currentContext();
  247. if (glctx && glctx->isValid())
  248. {
  249. m_debugInfo += "\nOpenGL:\n";
  250. int syms[4] = {GL_VENDOR, GL_RENDERER, GL_VERSION, GL_SHADING_LANGUAGE_VERSION};
  251. for (auto sym : syms)
  252. {
  253. auto s = (char *)glctx->functions()->glGetString(sym);
  254. if (s)
  255. m_debugInfo += QString(" ") + QString::fromUtf8(s) + "\n";
  256. }
  257. m_debugInfo += "\n";
  258. }
  259. }
  260. if (m_renderer)
  261. m_renderer->m_size = window()->size() * window()->devicePixelRatio();
  262. }
  263. ///////////////////////////////////////////////////////////////////////////////////////////////////
  264. void PlayerQuickItem::onInvalidate()
  265. {
  266. if (m_renderer)
  267. delete m_renderer;
  268. m_renderer = nullptr;
  269. }
  270. ///////////////////////////////////////////////////////////////////////////////////////////////////
  271. void PlayerQuickItem::initMpv(PlayerComponent* player)
  272. {
  273. m_mpv = player->getMpvHandle();
  274. m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
  275. if (!m_mpvGL)
  276. throw FatalException(tr("OpenGL not enabled in libmpv."));
  277. connect(player, &PlayerComponent::windowVisible, this, &QQuickItem::setVisible);
  278. window()->update();
  279. }