PlayerQuickItem.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "PlayerQuickItem.h"
  2. #include <stdexcept>
  3. #include <QOpenGLContext>
  4. #include <QtGui/QOpenGLFramebufferObject>
  5. #include <QtQuick/QQuickWindow>
  6. #include <QOpenGLFunctions>
  7. #include "QsLog.h"
  8. #include "utils/Utils.h"
  9. #ifdef Q_OS_WIN32
  10. #include <windows.h>
  11. #include <d3d9.h>
  12. typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT);
  13. static IDirect3DDevice9* d3ddevice;
  14. // This must be run before the konvergo main window switches to FS mode.
  15. void initD3DDevice(void)
  16. {
  17. // Boilerplate for creating a "blank" D3D device.
  18. // Most of this is copied from FFmpeg (LGPL).
  19. pDirect3DCreate9 *createD3D = NULL;
  20. HRESULT hr;
  21. D3DPRESENT_PARAMETERS d3dpp = {};
  22. D3DDISPLAYMODE d3ddm;
  23. UINT adapter = D3DADAPTER_DEFAULT;
  24. HMODULE d3dlib = LoadLibraryW(L"d3d9.dll");
  25. if (!d3dlib) {
  26. QLOG_ERROR() << "Failed to load D3D9 library";
  27. return;
  28. }
  29. createD3D = (pDirect3DCreate9 *)GetProcAddress(d3dlib, "Direct3DCreate9");
  30. if (!createD3D) {
  31. QLOG_ERROR() << "Failed to locate Direct3DCreate9";
  32. return;
  33. }
  34. IDirect3D9 *d3d9 = createD3D(D3D_SDK_VERSION);
  35. if (!d3d9) {
  36. QLOG_ERROR() << "Failed to create IDirect3D object";
  37. return;
  38. }
  39. IDirect3D9_GetAdapterDisplayMode(d3d9, adapter, &d3ddm);
  40. d3dpp.Windowed = TRUE;
  41. d3dpp.BackBufferWidth = 640;
  42. d3dpp.BackBufferHeight = 480;
  43. d3dpp.BackBufferCount = 0;
  44. d3dpp.BackBufferFormat = d3ddm.Format;
  45. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  46. d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
  47. hr = IDirect3D9_CreateDevice(d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(),
  48. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
  49. &d3dpp, &d3ddevice);
  50. if (FAILED(hr)) {
  51. QLOG_ERROR() << "Failed to create Direct3D device";
  52. return;
  53. }
  54. QLOG_INFO() << "Successfully created a Direct3D device";
  55. };
  56. // Special libmpv-specific pseudo extension for better behavior with OpenGL
  57. // fullscreen modes. This is needed with some drivers which do not allow the
  58. // libmpv DXVA code to create a new D3D device.
  59. static void* __stdcall MPGetD3DInterface(const char* name)
  60. {
  61. QLOG_INFO() << "Asking for " << qPrintable(QString::fromUtf8((name)));
  62. if (strcmp(name, "IDirect3DDevice9") == 0)
  63. {
  64. QLOG_INFO() << "Returning device " << (void *)d3ddevice;
  65. IDirect3DDevice9_AddRef(d3ddevice);
  66. return (void *)d3ddevice;
  67. }
  68. return NULL;
  69. }
  70. #endif
  71. ///////////////////////////////////////////////////////////////////////////////////////////////////
  72. static void* get_proc_address(void* ctx, const char* name)
  73. {
  74. Q_UNUSED(ctx);
  75. QOpenGLContext* glctx = QOpenGLContext::currentContext();
  76. if (!glctx)
  77. return NULL;
  78. void *res = (void *)glctx->getProcAddress(QByteArray(name));
  79. #ifdef Q_OS_WIN32
  80. if (strcmp(name, "glMPGetD3DInterface") == 0)
  81. {
  82. return (void *)&MPGetD3DInterface;
  83. }
  84. // wglGetProcAddress(), which is used by Qt, does not always resolve all
  85. // builtin functions with all drivers (only extensions). Qt compensates this
  86. // for a degree, but does this only for functions Qt happens to need. So
  87. // we need our own falback as well.
  88. if (!res)
  89. {
  90. HMODULE handle = (HMODULE)QOpenGLContext::openGLModuleHandle();
  91. if (handle)
  92. res = (void *)GetProcAddress(handle, name);
  93. }
  94. #endif
  95. return res;
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////
  98. PlayerRenderer::PlayerRenderer(mpv::qt::Handle mpv, QQuickWindow* window)
  99. : m_mpv(mpv), m_mpvGL(0), m_window(window), m_size()
  100. {
  101. m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////
  104. bool PlayerRenderer::init()
  105. {
  106. const char *extensions = "";
  107. #ifdef Q_OS_WIN32
  108. // For the custom IDirect3DDevice9 hack above.
  109. extensions = "GL_MP_D3D_interfaces";
  110. #endif
  111. return mpv_opengl_cb_init_gl(m_mpvGL, extensions, get_proc_address, NULL) >= 0;
  112. }
  113. ///////////////////////////////////////////////////////////////////////////////////////////////////
  114. PlayerRenderer::~PlayerRenderer()
  115. {
  116. // Keep in mind that the m_mpv handle must be held until this is done.
  117. if (m_mpvGL)
  118. mpv_opengl_cb_uninit_gl(m_mpvGL);
  119. }
  120. ///////////////////////////////////////////////////////////////////////////////////////////////////
  121. void PlayerRenderer::render()
  122. {
  123. int fbo = m_window->renderTargetId();
  124. m_window->resetOpenGLState();
  125. // The negative height signals to mpv that the video should be flipped
  126. // (according to the flipped OpenGL coordinate system).
  127. mpv_opengl_cb_draw(m_mpvGL, fbo, m_size.width(), -m_size.height());
  128. m_window->resetOpenGLState();
  129. }
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////
  131. void PlayerRenderer::swap()
  132. {
  133. mpv_opengl_cb_report_flip(m_mpvGL, 0);
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////
  136. PlayerQuickItem::PlayerQuickItem(QQuickItem* parent)
  137. : QQuickItem(parent), m_mpvGL(NULL), m_renderer(NULL)
  138. {
  139. connect(this, &QQuickItem::windowChanged, this, &PlayerQuickItem::onWindowChanged, Qt::DirectConnection);
  140. connect(this, &PlayerQuickItem::onFatalError, this, &PlayerQuickItem::onHandleFatalError, Qt::QueuedConnection);
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////////////////////////
  143. PlayerQuickItem::~PlayerQuickItem()
  144. {
  145. if (m_mpvGL)
  146. mpv_opengl_cb_set_update_callback(m_mpvGL, NULL, NULL);
  147. }
  148. ///////////////////////////////////////////////////////////////////////////////////////////////////
  149. void PlayerQuickItem::onWindowChanged(QQuickWindow* win)
  150. {
  151. if (win)
  152. {
  153. connect(win, &QQuickWindow::beforeSynchronizing, this, &PlayerQuickItem::onSynchronize, Qt::DirectConnection);
  154. connect(win, &QQuickWindow::sceneGraphInvalidated, this, &PlayerQuickItem::onInvalidate, Qt::DirectConnection);
  155. connect(this, &PlayerQuickItem::onUpdate, win, &QQuickWindow::update, Qt::QueuedConnection);
  156. }
  157. }
  158. ///////////////////////////////////////////////////////////////////////////////////////////////////
  159. void PlayerQuickItem::onHandleFatalError(QString message)
  160. {
  161. throw FatalException(message);
  162. }
  163. ///////////////////////////////////////////////////////////////////////////////////////////////////
  164. void PlayerQuickItem::onSynchronize()
  165. {
  166. if (!m_renderer && m_mpv)
  167. {
  168. m_renderer = new PlayerRenderer(m_mpv, window());
  169. if (!m_renderer->init())
  170. {
  171. delete m_renderer;
  172. m_renderer = NULL;
  173. emit onFatalError(tr("Could not initialize OpenGL."));
  174. return;
  175. }
  176. connect(window(), &QQuickWindow::beforeRendering, m_renderer, &PlayerRenderer::render, Qt::DirectConnection);
  177. connect(window(), &QQuickWindow::frameSwapped, m_renderer, &PlayerRenderer::swap, Qt::DirectConnection);
  178. window()->setPersistentOpenGLContext(true);
  179. window()->setPersistentSceneGraph(true);
  180. window()->setClearBeforeRendering(false);
  181. }
  182. if (m_renderer)
  183. m_renderer->m_size = window()->size() * window()->devicePixelRatio();
  184. }
  185. ///////////////////////////////////////////////////////////////////////////////////////////////////
  186. void PlayerQuickItem::onInvalidate()
  187. {
  188. if (m_renderer)
  189. delete m_renderer;
  190. m_renderer = NULL;
  191. }
  192. ///////////////////////////////////////////////////////////////////////////////////////////////////
  193. void PlayerQuickItem::on_update(void *ctx)
  194. {
  195. PlayerQuickItem *self = (PlayerQuickItem *)ctx;
  196. emit self->onUpdate();
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////////////////////////
  199. void PlayerQuickItem::initMpv(PlayerComponent* player)
  200. {
  201. m_mpv = player->getMpvHandle();
  202. m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
  203. if (!m_mpvGL)
  204. throw FatalException(tr("OpenGL not enabled in libmpv."));
  205. mpv_opengl_cb_set_update_callback(m_mpvGL, on_update, (void *)this);
  206. connect(player, &PlayerComponent::windowVisible, this, &QQuickItem::setVisible);
  207. window()->update();
  208. }