bootmanager.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <glad/glad.h>
  4. #include <QApplication>
  5. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  6. #include <QCameraImageCapture>
  7. #include <QCameraInfo>
  8. #endif
  9. #include <QHBoxLayout>
  10. #include <QMessageBox>
  11. #include <QPainter>
  12. #include <QScreen>
  13. #include <QString>
  14. #include <QStringList>
  15. #include <QWindow>
  16. #ifdef HAS_OPENGL
  17. #include <QOffscreenSurface>
  18. #include <QOpenGLContext>
  19. #endif
  20. #if !defined(WIN32)
  21. #include <qpa/qplatformnativeinterface.h>
  22. #endif
  23. #include <fmt/format.h>
  24. #include "common/assert.h"
  25. #include "common/microprofile.h"
  26. #include "common/scm_rev.h"
  27. #include "common/settings.h"
  28. #include "core/core.h"
  29. #include "core/cpu_manager.h"
  30. #include "core/frontend/framebuffer_layout.h"
  31. #include "input_common/drivers/camera.h"
  32. #include "input_common/drivers/keyboard.h"
  33. #include "input_common/drivers/mouse.h"
  34. #include "input_common/drivers/tas_input.h"
  35. #include "input_common/drivers/touch_screen.h"
  36. #include "input_common/main.h"
  37. #include "video_core/renderer_base.h"
  38. #include "yuzu/bootmanager.h"
  39. #include "yuzu/main.h"
  40. static Core::Frontend::WindowSystemType GetWindowSystemType();
  41. EmuThread::EmuThread(Core::System& system) : m_system{system} {}
  42. EmuThread::~EmuThread() = default;
  43. void EmuThread::run() {
  44. const char* name = "EmuControlThread";
  45. MicroProfileOnThreadCreate(name);
  46. Common::SetCurrentThreadName(name);
  47. auto& gpu = m_system.GPU();
  48. auto stop_token = m_stop_source.get_token();
  49. m_system.RegisterHostThread();
  50. // Main process has been loaded. Make the context current to this thread and begin GPU and CPU
  51. // execution.
  52. gpu.ObtainContext();
  53. emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);
  54. if (Settings::values.use_disk_shader_cache.GetValue()) {
  55. m_system.Renderer().ReadRasterizer()->LoadDiskResources(
  56. m_system.GetCurrentProcessProgramID(), stop_token,
  57. [this](VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total) {
  58. emit LoadProgress(stage, value, total);
  59. });
  60. }
  61. emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0);
  62. gpu.ReleaseContext();
  63. gpu.Start();
  64. m_system.GetCpuManager().OnGpuReady();
  65. if (m_system.DebuggerEnabled()) {
  66. m_system.InitializeDebugger();
  67. }
  68. while (!stop_token.stop_requested()) {
  69. std::unique_lock lk{m_should_run_mutex};
  70. if (m_should_run) {
  71. m_system.Run();
  72. m_is_running.store(true);
  73. m_is_running.notify_all();
  74. Common::CondvarWait(m_should_run_cv, lk, stop_token, [&] { return !m_should_run; });
  75. } else {
  76. m_system.Pause();
  77. m_is_running.store(false);
  78. m_is_running.notify_all();
  79. EmulationPaused(lk);
  80. Common::CondvarWait(m_should_run_cv, lk, stop_token, [&] { return m_should_run; });
  81. EmulationResumed(lk);
  82. }
  83. }
  84. // Shutdown the main emulated process
  85. m_system.DetachDebugger();
  86. m_system.ShutdownMainProcess();
  87. #if MICROPROFILE_ENABLED
  88. MicroProfileOnThreadExit();
  89. #endif
  90. }
  91. // Unlock while emitting signals so that the main thread can
  92. // continue pumping events.
  93. void EmuThread::EmulationPaused(std::unique_lock<std::mutex>& lk) {
  94. lk.unlock();
  95. emit DebugModeEntered();
  96. lk.lock();
  97. }
  98. void EmuThread::EmulationResumed(std::unique_lock<std::mutex>& lk) {
  99. lk.unlock();
  100. emit DebugModeLeft();
  101. lk.lock();
  102. }
  103. #ifdef HAS_OPENGL
  104. class OpenGLSharedContext : public Core::Frontend::GraphicsContext {
  105. public:
  106. /// Create the original context that should be shared from
  107. explicit OpenGLSharedContext(QSurface* surface_) : surface{surface_} {
  108. QSurfaceFormat format;
  109. format.setVersion(4, 6);
  110. format.setProfile(QSurfaceFormat::CompatibilityProfile);
  111. format.setOption(QSurfaceFormat::FormatOption::DeprecatedFunctions);
  112. if (Settings::values.renderer_debug) {
  113. format.setOption(QSurfaceFormat::FormatOption::DebugContext);
  114. }
  115. // TODO: expose a setting for buffer value (ie default/single/double/triple)
  116. format.setSwapBehavior(QSurfaceFormat::DefaultSwapBehavior);
  117. format.setSwapInterval(0);
  118. context = std::make_unique<QOpenGLContext>();
  119. context->setFormat(format);
  120. if (!context->create()) {
  121. LOG_ERROR(Frontend, "Unable to create main openGL context");
  122. }
  123. }
  124. /// Create the shared contexts for rendering and presentation
  125. explicit OpenGLSharedContext(QOpenGLContext* share_context, QSurface* main_surface = nullptr) {
  126. // disable vsync for any shared contexts
  127. auto format = share_context->format();
  128. format.setSwapInterval(main_surface ? Settings::values.use_vsync.GetValue() : 0);
  129. context = std::make_unique<QOpenGLContext>();
  130. context->setShareContext(share_context);
  131. context->setFormat(format);
  132. if (!context->create()) {
  133. LOG_ERROR(Frontend, "Unable to create shared openGL context");
  134. }
  135. if (!main_surface) {
  136. offscreen_surface = std::make_unique<QOffscreenSurface>(nullptr);
  137. offscreen_surface->setFormat(format);
  138. offscreen_surface->create();
  139. surface = offscreen_surface.get();
  140. } else {
  141. surface = main_surface;
  142. }
  143. }
  144. ~OpenGLSharedContext() {
  145. DoneCurrent();
  146. }
  147. void SwapBuffers() override {
  148. context->swapBuffers(surface);
  149. }
  150. void MakeCurrent() override {
  151. // We can't track the current state of the underlying context in this wrapper class because
  152. // Qt may make the underlying context not current for one reason or another. In particular,
  153. // the WebBrowser uses GL, so it seems to conflict if we aren't careful.
  154. // Instead of always just making the context current (which does not have any caching to
  155. // check if the underlying context is already current) we can check for the current context
  156. // in the thread local data by calling `currentContext()` and checking if its ours.
  157. if (QOpenGLContext::currentContext() != context.get()) {
  158. context->makeCurrent(surface);
  159. }
  160. }
  161. void DoneCurrent() override {
  162. context->doneCurrent();
  163. }
  164. QOpenGLContext* GetShareContext() {
  165. return context.get();
  166. }
  167. const QOpenGLContext* GetShareContext() const {
  168. return context.get();
  169. }
  170. private:
  171. // Avoid using Qt parent system here since we might move the QObjects to new threads
  172. // As a note, this means we should avoid using slots/signals with the objects too
  173. std::unique_ptr<QOpenGLContext> context;
  174. std::unique_ptr<QOffscreenSurface> offscreen_surface{};
  175. QSurface* surface;
  176. };
  177. #endif
  178. class DummyContext : public Core::Frontend::GraphicsContext {};
  179. class RenderWidget : public QWidget {
  180. public:
  181. explicit RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) {
  182. setAttribute(Qt::WA_NativeWindow);
  183. setAttribute(Qt::WA_PaintOnScreen);
  184. if (GetWindowSystemType() == Core::Frontend::WindowSystemType::Wayland) {
  185. setAttribute(Qt::WA_DontCreateNativeAncestors);
  186. }
  187. }
  188. virtual ~RenderWidget() = default;
  189. QPaintEngine* paintEngine() const override {
  190. return nullptr;
  191. }
  192. private:
  193. GRenderWindow* render_window;
  194. };
  195. struct OpenGLRenderWidget : public RenderWidget {
  196. explicit OpenGLRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  197. windowHandle()->setSurfaceType(QWindow::OpenGLSurface);
  198. }
  199. void SetContext(std::unique_ptr<Core::Frontend::GraphicsContext>&& context_) {
  200. context = std::move(context_);
  201. }
  202. private:
  203. std::unique_ptr<Core::Frontend::GraphicsContext> context;
  204. };
  205. struct VulkanRenderWidget : public RenderWidget {
  206. explicit VulkanRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  207. windowHandle()->setSurfaceType(QWindow::VulkanSurface);
  208. }
  209. };
  210. struct NullRenderWidget : public RenderWidget {
  211. explicit NullRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {}
  212. };
  213. static Core::Frontend::WindowSystemType GetWindowSystemType() {
  214. // Determine WSI type based on Qt platform.
  215. QString platform_name = QGuiApplication::platformName();
  216. if (platform_name == QStringLiteral("windows"))
  217. return Core::Frontend::WindowSystemType::Windows;
  218. else if (platform_name == QStringLiteral("xcb"))
  219. return Core::Frontend::WindowSystemType::X11;
  220. else if (platform_name == QStringLiteral("wayland"))
  221. return Core::Frontend::WindowSystemType::Wayland;
  222. else if (platform_name == QStringLiteral("wayland-egl"))
  223. return Core::Frontend::WindowSystemType::Wayland;
  224. else if (platform_name == QStringLiteral("cocoa"))
  225. return Core::Frontend::WindowSystemType::Cocoa;
  226. else if (platform_name == QStringLiteral("android"))
  227. return Core::Frontend::WindowSystemType::Android;
  228. LOG_CRITICAL(Frontend, "Unknown Qt platform {}!", platform_name.toStdString());
  229. return Core::Frontend::WindowSystemType::Windows;
  230. }
  231. static Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) {
  232. Core::Frontend::EmuWindow::WindowSystemInfo wsi;
  233. wsi.type = GetWindowSystemType();
  234. // Our Win32 Qt external doesn't have the private API.
  235. #if defined(WIN32) || defined(__APPLE__)
  236. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  237. #else
  238. QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
  239. wsi.display_connection = pni->nativeResourceForWindow("display", window);
  240. if (wsi.type == Core::Frontend::WindowSystemType::Wayland)
  241. wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr;
  242. else
  243. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  244. #endif
  245. wsi.render_surface_scale = window ? static_cast<float>(window->devicePixelRatio()) : 1.0f;
  246. return wsi;
  247. }
  248. GRenderWindow::GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  249. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_,
  250. Core::System& system_)
  251. : QWidget(parent),
  252. emu_thread(emu_thread_), input_subsystem{std::move(input_subsystem_)}, system{system_} {
  253. setWindowTitle(QStringLiteral("yuzu %1 | %2-%3")
  254. .arg(QString::fromUtf8(Common::g_build_name),
  255. QString::fromUtf8(Common::g_scm_branch),
  256. QString::fromUtf8(Common::g_scm_desc)));
  257. setAttribute(Qt::WA_AcceptTouchEvents);
  258. auto* layout = new QHBoxLayout(this);
  259. layout->setContentsMargins(0, 0, 0, 0);
  260. setLayout(layout);
  261. input_subsystem->Initialize();
  262. this->setMouseTracking(true);
  263. strict_context_required = QGuiApplication::platformName() == QStringLiteral("wayland") ||
  264. QGuiApplication::platformName() == QStringLiteral("wayland-egl");
  265. connect(this, &GRenderWindow::FirstFrameDisplayed, parent, &GMainWindow::OnLoadComplete);
  266. connect(this, &GRenderWindow::ExecuteProgramSignal, parent, &GMainWindow::OnExecuteProgram,
  267. Qt::QueuedConnection);
  268. connect(this, &GRenderWindow::ExitSignal, parent, &GMainWindow::OnExit, Qt::QueuedConnection);
  269. connect(this, &GRenderWindow::TasPlaybackStateChanged, parent, &GMainWindow::OnTasStateChanged);
  270. }
  271. void GRenderWindow::ExecuteProgram(std::size_t program_index) {
  272. emit ExecuteProgramSignal(program_index);
  273. }
  274. void GRenderWindow::Exit() {
  275. emit ExitSignal();
  276. }
  277. GRenderWindow::~GRenderWindow() {
  278. input_subsystem->Shutdown();
  279. }
  280. void GRenderWindow::OnFrameDisplayed() {
  281. input_subsystem->GetTas()->UpdateThread();
  282. const InputCommon::TasInput::TasState new_tas_state =
  283. std::get<0>(input_subsystem->GetTas()->GetStatus());
  284. if (!first_frame) {
  285. last_tas_state = new_tas_state;
  286. first_frame = true;
  287. emit FirstFrameDisplayed();
  288. }
  289. if (new_tas_state != last_tas_state) {
  290. last_tas_state = new_tas_state;
  291. emit TasPlaybackStateChanged();
  292. }
  293. }
  294. bool GRenderWindow::IsShown() const {
  295. return !isMinimized();
  296. }
  297. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  298. //
  299. // Older versions get the window size (density independent pixels),
  300. // and hence, do not support DPI scaling ("retina" displays).
  301. // The result will be a viewport that is smaller than the extent of the window.
  302. void GRenderWindow::OnFramebufferSizeChanged() {
  303. // Screen changes potentially incur a change in screen DPI, hence we should update the
  304. // framebuffer size
  305. const qreal pixel_ratio = windowPixelRatio();
  306. const u32 width = this->width() * pixel_ratio;
  307. const u32 height = this->height() * pixel_ratio;
  308. UpdateCurrentFramebufferLayout(width, height);
  309. }
  310. void GRenderWindow::BackupGeometry() {
  311. geometry = QWidget::saveGeometry();
  312. }
  313. void GRenderWindow::RestoreGeometry() {
  314. // We don't want to back up the geometry here (obviously)
  315. QWidget::restoreGeometry(geometry);
  316. }
  317. void GRenderWindow::restoreGeometry(const QByteArray& geometry_) {
  318. // Make sure users of this class don't need to deal with backing up the geometry themselves
  319. QWidget::restoreGeometry(geometry_);
  320. BackupGeometry();
  321. }
  322. QByteArray GRenderWindow::saveGeometry() {
  323. // If we are a top-level widget, store the current geometry
  324. // otherwise, store the last backup
  325. if (parent() == nullptr) {
  326. return QWidget::saveGeometry();
  327. }
  328. return geometry;
  329. }
  330. qreal GRenderWindow::windowPixelRatio() const {
  331. return devicePixelRatioF();
  332. }
  333. std::pair<u32, u32> GRenderWindow::ScaleTouch(const QPointF& pos) const {
  334. const qreal pixel_ratio = windowPixelRatio();
  335. return {static_cast<u32>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
  336. static_cast<u32>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
  337. }
  338. void GRenderWindow::closeEvent(QCloseEvent* event) {
  339. emit Closed();
  340. QWidget::closeEvent(event);
  341. }
  342. int GRenderWindow::QtKeyToSwitchKey(Qt::Key qt_key) {
  343. static constexpr std::array<std::pair<Qt::Key, Settings::NativeKeyboard::Keys>, 106> key_map = {
  344. std::pair<Qt::Key, Settings::NativeKeyboard::Keys>{Qt::Key_A, Settings::NativeKeyboard::A},
  345. {Qt::Key_A, Settings::NativeKeyboard::A},
  346. {Qt::Key_B, Settings::NativeKeyboard::B},
  347. {Qt::Key_C, Settings::NativeKeyboard::C},
  348. {Qt::Key_D, Settings::NativeKeyboard::D},
  349. {Qt::Key_E, Settings::NativeKeyboard::E},
  350. {Qt::Key_F, Settings::NativeKeyboard::F},
  351. {Qt::Key_G, Settings::NativeKeyboard::G},
  352. {Qt::Key_H, Settings::NativeKeyboard::H},
  353. {Qt::Key_I, Settings::NativeKeyboard::I},
  354. {Qt::Key_J, Settings::NativeKeyboard::J},
  355. {Qt::Key_K, Settings::NativeKeyboard::K},
  356. {Qt::Key_L, Settings::NativeKeyboard::L},
  357. {Qt::Key_M, Settings::NativeKeyboard::M},
  358. {Qt::Key_N, Settings::NativeKeyboard::N},
  359. {Qt::Key_O, Settings::NativeKeyboard::O},
  360. {Qt::Key_P, Settings::NativeKeyboard::P},
  361. {Qt::Key_Q, Settings::NativeKeyboard::Q},
  362. {Qt::Key_R, Settings::NativeKeyboard::R},
  363. {Qt::Key_S, Settings::NativeKeyboard::S},
  364. {Qt::Key_T, Settings::NativeKeyboard::T},
  365. {Qt::Key_U, Settings::NativeKeyboard::U},
  366. {Qt::Key_V, Settings::NativeKeyboard::V},
  367. {Qt::Key_W, Settings::NativeKeyboard::W},
  368. {Qt::Key_X, Settings::NativeKeyboard::X},
  369. {Qt::Key_Y, Settings::NativeKeyboard::Y},
  370. {Qt::Key_Z, Settings::NativeKeyboard::Z},
  371. {Qt::Key_1, Settings::NativeKeyboard::N1},
  372. {Qt::Key_2, Settings::NativeKeyboard::N2},
  373. {Qt::Key_3, Settings::NativeKeyboard::N3},
  374. {Qt::Key_4, Settings::NativeKeyboard::N4},
  375. {Qt::Key_5, Settings::NativeKeyboard::N5},
  376. {Qt::Key_6, Settings::NativeKeyboard::N6},
  377. {Qt::Key_7, Settings::NativeKeyboard::N7},
  378. {Qt::Key_8, Settings::NativeKeyboard::N8},
  379. {Qt::Key_9, Settings::NativeKeyboard::N9},
  380. {Qt::Key_0, Settings::NativeKeyboard::N0},
  381. {Qt::Key_Return, Settings::NativeKeyboard::Return},
  382. {Qt::Key_Escape, Settings::NativeKeyboard::Escape},
  383. {Qt::Key_Backspace, Settings::NativeKeyboard::Backspace},
  384. {Qt::Key_Tab, Settings::NativeKeyboard::Tab},
  385. {Qt::Key_Space, Settings::NativeKeyboard::Space},
  386. {Qt::Key_Minus, Settings::NativeKeyboard::Minus},
  387. {Qt::Key_Plus, Settings::NativeKeyboard::Plus},
  388. {Qt::Key_questiondown, Settings::NativeKeyboard::Plus},
  389. {Qt::Key_BracketLeft, Settings::NativeKeyboard::OpenBracket},
  390. {Qt::Key_BraceLeft, Settings::NativeKeyboard::OpenBracket},
  391. {Qt::Key_BracketRight, Settings::NativeKeyboard::CloseBracket},
  392. {Qt::Key_BraceRight, Settings::NativeKeyboard::CloseBracket},
  393. {Qt::Key_Bar, Settings::NativeKeyboard::Pipe},
  394. {Qt::Key_Dead_Tilde, Settings::NativeKeyboard::Tilde},
  395. {Qt::Key_Ntilde, Settings::NativeKeyboard::Semicolon},
  396. {Qt::Key_Semicolon, Settings::NativeKeyboard::Semicolon},
  397. {Qt::Key_Apostrophe, Settings::NativeKeyboard::Quote},
  398. {Qt::Key_Dead_Grave, Settings::NativeKeyboard::Backquote},
  399. {Qt::Key_Comma, Settings::NativeKeyboard::Comma},
  400. {Qt::Key_Period, Settings::NativeKeyboard::Period},
  401. {Qt::Key_Slash, Settings::NativeKeyboard::Slash},
  402. {Qt::Key_CapsLock, Settings::NativeKeyboard::CapsLockKey},
  403. {Qt::Key_F1, Settings::NativeKeyboard::F1},
  404. {Qt::Key_F2, Settings::NativeKeyboard::F2},
  405. {Qt::Key_F3, Settings::NativeKeyboard::F3},
  406. {Qt::Key_F4, Settings::NativeKeyboard::F4},
  407. {Qt::Key_F5, Settings::NativeKeyboard::F5},
  408. {Qt::Key_F6, Settings::NativeKeyboard::F6},
  409. {Qt::Key_F7, Settings::NativeKeyboard::F7},
  410. {Qt::Key_F8, Settings::NativeKeyboard::F8},
  411. {Qt::Key_F9, Settings::NativeKeyboard::F9},
  412. {Qt::Key_F10, Settings::NativeKeyboard::F10},
  413. {Qt::Key_F11, Settings::NativeKeyboard::F11},
  414. {Qt::Key_F12, Settings::NativeKeyboard::F12},
  415. {Qt::Key_Print, Settings::NativeKeyboard::PrintScreen},
  416. {Qt::Key_ScrollLock, Settings::NativeKeyboard::ScrollLockKey},
  417. {Qt::Key_Pause, Settings::NativeKeyboard::Pause},
  418. {Qt::Key_Insert, Settings::NativeKeyboard::Insert},
  419. {Qt::Key_Home, Settings::NativeKeyboard::Home},
  420. {Qt::Key_PageUp, Settings::NativeKeyboard::PageUp},
  421. {Qt::Key_Delete, Settings::NativeKeyboard::Delete},
  422. {Qt::Key_End, Settings::NativeKeyboard::End},
  423. {Qt::Key_PageDown, Settings::NativeKeyboard::PageDown},
  424. {Qt::Key_Right, Settings::NativeKeyboard::Right},
  425. {Qt::Key_Left, Settings::NativeKeyboard::Left},
  426. {Qt::Key_Down, Settings::NativeKeyboard::Down},
  427. {Qt::Key_Up, Settings::NativeKeyboard::Up},
  428. {Qt::Key_NumLock, Settings::NativeKeyboard::NumLockKey},
  429. // Numpad keys are missing here
  430. {Qt::Key_F13, Settings::NativeKeyboard::F13},
  431. {Qt::Key_F14, Settings::NativeKeyboard::F14},
  432. {Qt::Key_F15, Settings::NativeKeyboard::F15},
  433. {Qt::Key_F16, Settings::NativeKeyboard::F16},
  434. {Qt::Key_F17, Settings::NativeKeyboard::F17},
  435. {Qt::Key_F18, Settings::NativeKeyboard::F18},
  436. {Qt::Key_F19, Settings::NativeKeyboard::F19},
  437. {Qt::Key_F20, Settings::NativeKeyboard::F20},
  438. {Qt::Key_F21, Settings::NativeKeyboard::F21},
  439. {Qt::Key_F22, Settings::NativeKeyboard::F22},
  440. {Qt::Key_F23, Settings::NativeKeyboard::F23},
  441. {Qt::Key_F24, Settings::NativeKeyboard::F24},
  442. // {Qt::..., Settings::NativeKeyboard::KPComma},
  443. // {Qt::..., Settings::NativeKeyboard::Ro},
  444. {Qt::Key_Hiragana_Katakana, Settings::NativeKeyboard::KatakanaHiragana},
  445. {Qt::Key_yen, Settings::NativeKeyboard::Yen},
  446. {Qt::Key_Henkan, Settings::NativeKeyboard::Henkan},
  447. {Qt::Key_Muhenkan, Settings::NativeKeyboard::Muhenkan},
  448. // {Qt::..., Settings::NativeKeyboard::NumPadCommaPc98},
  449. {Qt::Key_Hangul, Settings::NativeKeyboard::HangulEnglish},
  450. {Qt::Key_Hangul_Hanja, Settings::NativeKeyboard::Hanja},
  451. {Qt::Key_Katakana, Settings::NativeKeyboard::KatakanaKey},
  452. {Qt::Key_Hiragana, Settings::NativeKeyboard::HiraganaKey},
  453. {Qt::Key_Zenkaku_Hankaku, Settings::NativeKeyboard::ZenkakuHankaku},
  454. // Modifier keys are handled by the modifier property
  455. };
  456. for (const auto& [qkey, nkey] : key_map) {
  457. if (qt_key == qkey) {
  458. return nkey;
  459. }
  460. }
  461. return Settings::NativeKeyboard::None;
  462. }
  463. int GRenderWindow::QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers) {
  464. int modifier = 0;
  465. if ((qt_modifiers & Qt::KeyboardModifier::ShiftModifier) != 0) {
  466. modifier |= 1 << Settings::NativeKeyboard::LeftShift;
  467. }
  468. if ((qt_modifiers & Qt::KeyboardModifier::ControlModifier) != 0) {
  469. modifier |= 1 << Settings::NativeKeyboard::LeftControl;
  470. }
  471. if ((qt_modifiers & Qt::KeyboardModifier::AltModifier) != 0) {
  472. modifier |= 1 << Settings::NativeKeyboard::LeftAlt;
  473. }
  474. if ((qt_modifiers & Qt::KeyboardModifier::MetaModifier) != 0) {
  475. modifier |= 1 << Settings::NativeKeyboard::LeftMeta;
  476. }
  477. // TODO: These keys can't be obtained with Qt::KeyboardModifier
  478. // if ((qt_modifiers & 0x10) != 0) {
  479. // modifier |= 1 << Settings::NativeKeyboard::RightShift;
  480. // }
  481. // if ((qt_modifiers & 0x20) != 0) {
  482. // modifier |= 1 << Settings::NativeKeyboard::RightControl;
  483. // }
  484. // if ((qt_modifiers & 0x40) != 0) {
  485. // modifier |= 1 << Settings::NativeKeyboard::RightAlt;
  486. // }
  487. // if ((qt_modifiers & 0x80) != 0) {
  488. // modifier |= 1 << Settings::NativeKeyboard::RightMeta;
  489. // }
  490. // if ((qt_modifiers & 0x100) != 0) {
  491. // modifier |= 1 << Settings::NativeKeyboard::CapsLock;
  492. // }
  493. // if ((qt_modifiers & 0x200) != 0) {
  494. // modifier |= 1 << Settings::NativeKeyboard::NumLock;
  495. // }
  496. // if ((qt_modifiers & ???) != 0) {
  497. // modifier |= 1 << Settings::NativeKeyboard::ScrollLock;
  498. // }
  499. // if ((qt_modifiers & ???) != 0) {
  500. // modifier |= 1 << Settings::NativeKeyboard::Katakana;
  501. // }
  502. // if ((qt_modifiers & ???) != 0) {
  503. // modifier |= 1 << Settings::NativeKeyboard::Hiragana;
  504. // }
  505. return modifier;
  506. }
  507. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  508. /**
  509. * This feature can be enhanced with the following functions, but they do not provide
  510. * cross-platform behavior.
  511. *
  512. * event->nativeVirtualKey() can distinguish between keys on the numpad.
  513. * event->nativeModifiers() can distinguish between left and right keys and numlock,
  514. * capslock, scroll lock.
  515. */
  516. if (!event->isAutoRepeat()) {
  517. const auto modifier = QtModifierToSwitchModifier(event->modifiers());
  518. const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
  519. input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
  520. input_subsystem->GetKeyboard()->PressKeyboardKey(key);
  521. // This is used for gamepads that can have any key mapped
  522. input_subsystem->GetKeyboard()->PressKey(event->key());
  523. }
  524. }
  525. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  526. /**
  527. * This feature can be enhanced with the following functions, but they do not provide
  528. * cross-platform behavior.
  529. *
  530. * event->nativeVirtualKey() can distinguish between keys on the numpad.
  531. * event->nativeModifiers() can distinguish between left and right buttons and numlock,
  532. * capslock, scroll lock.
  533. */
  534. if (!event->isAutoRepeat()) {
  535. const auto modifier = QtModifierToSwitchModifier(event->modifiers());
  536. const auto key = QtKeyToSwitchKey(Qt::Key(event->key()));
  537. input_subsystem->GetKeyboard()->SetKeyboardModifiers(modifier);
  538. input_subsystem->GetKeyboard()->ReleaseKeyboardKey(key);
  539. // This is used for gamepads that can have any key mapped
  540. input_subsystem->GetKeyboard()->ReleaseKey(event->key());
  541. }
  542. }
  543. InputCommon::MouseButton GRenderWindow::QtButtonToMouseButton(Qt::MouseButton button) {
  544. switch (button) {
  545. case Qt::LeftButton:
  546. return InputCommon::MouseButton::Left;
  547. case Qt::RightButton:
  548. return InputCommon::MouseButton::Right;
  549. case Qt::MiddleButton:
  550. return InputCommon::MouseButton::Wheel;
  551. case Qt::BackButton:
  552. return InputCommon::MouseButton::Backward;
  553. case Qt::ForwardButton:
  554. return InputCommon::MouseButton::Forward;
  555. case Qt::TaskButton:
  556. return InputCommon::MouseButton::Task;
  557. default:
  558. return InputCommon::MouseButton::Extra;
  559. }
  560. }
  561. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  562. // Touch input is handled in TouchBeginEvent
  563. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  564. return;
  565. }
  566. // Qt sometimes returns the parent coordinates. To avoid this we read the global mouse
  567. // coordinates and map them to the current render area
  568. const auto pos = mapFromGlobal(QCursor::pos());
  569. const auto [x, y] = ScaleTouch(pos);
  570. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  571. const auto button = QtButtonToMouseButton(event->button());
  572. input_subsystem->GetMouse()->PressButton(x, y, touch_x, touch_y, button);
  573. emit MouseActivity();
  574. }
  575. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  576. // Touch input is handled in TouchUpdateEvent
  577. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  578. return;
  579. }
  580. // Qt sometimes returns the parent coordinates. To avoid this we read the global mouse
  581. // coordinates and map them to the current render area
  582. const auto pos = mapFromGlobal(QCursor::pos());
  583. const auto [x, y] = ScaleTouch(pos);
  584. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  585. const int center_x = width() / 2;
  586. const int center_y = height() / 2;
  587. input_subsystem->GetMouse()->MouseMove(x, y, touch_x, touch_y, center_x, center_y);
  588. if (Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  589. QCursor::setPos(mapToGlobal(QPoint{center_x, center_y}));
  590. }
  591. emit MouseActivity();
  592. }
  593. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  594. // Touch input is handled in TouchEndEvent
  595. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  596. return;
  597. }
  598. const auto button = QtButtonToMouseButton(event->button());
  599. input_subsystem->GetMouse()->ReleaseButton(button);
  600. }
  601. void GRenderWindow::wheelEvent(QWheelEvent* event) {
  602. const int x = event->angleDelta().x();
  603. const int y = event->angleDelta().y();
  604. input_subsystem->GetMouse()->MouseWheelChange(x, y);
  605. }
  606. void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
  607. QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
  608. for (const auto& touch_point : touch_points) {
  609. const auto [x, y] = ScaleTouch(touch_point.pos());
  610. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  611. input_subsystem->GetTouchScreen()->TouchPressed(touch_x, touch_y, touch_point.id());
  612. }
  613. }
  614. void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
  615. QList<QTouchEvent::TouchPoint> touch_points = event->touchPoints();
  616. input_subsystem->GetTouchScreen()->ClearActiveFlag();
  617. for (const auto& touch_point : touch_points) {
  618. const auto [x, y] = ScaleTouch(touch_point.pos());
  619. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  620. input_subsystem->GetTouchScreen()->TouchMoved(touch_x, touch_y, touch_point.id());
  621. }
  622. input_subsystem->GetTouchScreen()->ReleaseInactiveTouch();
  623. }
  624. void GRenderWindow::TouchEndEvent() {
  625. input_subsystem->GetTouchScreen()->ReleaseAllTouch();
  626. }
  627. void GRenderWindow::InitializeCamera() {
  628. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  629. constexpr auto camera_update_ms = std::chrono::milliseconds{50}; // (50ms, 20Hz)
  630. if (!Settings::values.enable_ir_sensor) {
  631. return;
  632. }
  633. bool camera_found = false;
  634. const QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
  635. for (const QCameraInfo& cameraInfo : cameras) {
  636. if (Settings::values.ir_sensor_device.GetValue() == cameraInfo.deviceName().toStdString() ||
  637. Settings::values.ir_sensor_device.GetValue() == "Auto") {
  638. camera = std::make_unique<QCamera>(cameraInfo);
  639. if (!camera->isCaptureModeSupported(QCamera::CaptureMode::CaptureViewfinder) &&
  640. !camera->isCaptureModeSupported(QCamera::CaptureMode::CaptureStillImage)) {
  641. LOG_ERROR(Frontend,
  642. "Camera doesn't support CaptureViewfinder or CaptureStillImage");
  643. continue;
  644. }
  645. camera_found = true;
  646. break;
  647. }
  648. }
  649. if (!camera_found) {
  650. return;
  651. }
  652. camera_capture = std::make_unique<QCameraImageCapture>(camera.get());
  653. if (!camera_capture->isCaptureDestinationSupported(
  654. QCameraImageCapture::CaptureDestination::CaptureToBuffer)) {
  655. LOG_ERROR(Frontend, "Camera doesn't support saving to buffer");
  656. return;
  657. }
  658. const auto camera_width = input_subsystem->GetCamera()->getImageWidth();
  659. const auto camera_height = input_subsystem->GetCamera()->getImageHeight();
  660. camera_data.resize(camera_width * camera_height);
  661. camera_capture->setCaptureDestination(QCameraImageCapture::CaptureDestination::CaptureToBuffer);
  662. connect(camera_capture.get(), &QCameraImageCapture::imageCaptured, this,
  663. &GRenderWindow::OnCameraCapture);
  664. camera->unload();
  665. if (camera->isCaptureModeSupported(QCamera::CaptureMode::CaptureViewfinder)) {
  666. camera->setCaptureMode(QCamera::CaptureViewfinder);
  667. } else if (camera->isCaptureModeSupported(QCamera::CaptureMode::CaptureStillImage)) {
  668. camera->setCaptureMode(QCamera::CaptureStillImage);
  669. }
  670. camera->load();
  671. camera->start();
  672. pending_camera_snapshots = 0;
  673. is_virtual_camera = false;
  674. camera_timer = std::make_unique<QTimer>();
  675. connect(camera_timer.get(), &QTimer::timeout, [this] { RequestCameraCapture(); });
  676. // This timer should be dependent of camera resolution 5ms for every 100 pixels
  677. camera_timer->start(camera_update_ms);
  678. #endif
  679. }
  680. void GRenderWindow::FinalizeCamera() {
  681. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  682. if (camera_timer) {
  683. camera_timer->stop();
  684. }
  685. if (camera) {
  686. camera->unload();
  687. }
  688. #endif
  689. }
  690. void GRenderWindow::RequestCameraCapture() {
  691. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  692. if (!Settings::values.enable_ir_sensor) {
  693. return;
  694. }
  695. // If the camera doesn't capture, test for virtual cameras
  696. if (pending_camera_snapshots > 5) {
  697. is_virtual_camera = true;
  698. }
  699. // Virtual cameras like obs need to reset the camera every capture
  700. if (is_virtual_camera) {
  701. camera->stop();
  702. camera->start();
  703. }
  704. pending_camera_snapshots++;
  705. camera_capture->capture();
  706. #endif
  707. }
  708. void GRenderWindow::OnCameraCapture(int requestId, const QImage& img) {
  709. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  710. // TODO: Capture directly in the format and resolution needed
  711. const auto camera_width = input_subsystem->GetCamera()->getImageWidth();
  712. const auto camera_height = input_subsystem->GetCamera()->getImageHeight();
  713. const auto converted =
  714. img.scaled(static_cast<int>(camera_width), static_cast<int>(camera_height),
  715. Qt::AspectRatioMode::IgnoreAspectRatio,
  716. Qt::TransformationMode::SmoothTransformation)
  717. .mirrored(false, true);
  718. if (camera_data.size() != camera_width * camera_height) {
  719. camera_data.resize(camera_width * camera_height);
  720. }
  721. std::memcpy(camera_data.data(), converted.bits(), camera_width * camera_height * sizeof(u32));
  722. input_subsystem->GetCamera()->SetCameraData(camera_width, camera_height, camera_data);
  723. pending_camera_snapshots = 0;
  724. #endif
  725. }
  726. bool GRenderWindow::event(QEvent* event) {
  727. if (event->type() == QEvent::TouchBegin) {
  728. TouchBeginEvent(static_cast<QTouchEvent*>(event));
  729. return true;
  730. } else if (event->type() == QEvent::TouchUpdate) {
  731. TouchUpdateEvent(static_cast<QTouchEvent*>(event));
  732. return true;
  733. } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
  734. TouchEndEvent();
  735. return true;
  736. }
  737. return QWidget::event(event);
  738. }
  739. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  740. QWidget::focusOutEvent(event);
  741. input_subsystem->GetKeyboard()->ReleaseAllKeys();
  742. input_subsystem->GetMouse()->ReleaseAllButtons();
  743. input_subsystem->GetTouchScreen()->ReleaseAllTouch();
  744. }
  745. void GRenderWindow::resizeEvent(QResizeEvent* event) {
  746. QWidget::resizeEvent(event);
  747. OnFramebufferSizeChanged();
  748. }
  749. std::unique_ptr<Core::Frontend::GraphicsContext> GRenderWindow::CreateSharedContext() const {
  750. #ifdef HAS_OPENGL
  751. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  752. auto c = static_cast<OpenGLSharedContext*>(main_context.get());
  753. // Bind the shared contexts to the main surface in case the backend wants to take over
  754. // presentation
  755. return std::make_unique<OpenGLSharedContext>(c->GetShareContext(),
  756. child_widget->windowHandle());
  757. }
  758. #endif
  759. return std::make_unique<DummyContext>();
  760. }
  761. bool GRenderWindow::InitRenderTarget() {
  762. ReleaseRenderTarget();
  763. {
  764. // Create a dummy render widget so that Qt
  765. // places the render window at the correct position.
  766. const RenderWidget dummy_widget{this};
  767. }
  768. first_frame = false;
  769. switch (Settings::values.renderer_backend.GetValue()) {
  770. case Settings::RendererBackend::OpenGL:
  771. if (!InitializeOpenGL()) {
  772. return false;
  773. }
  774. break;
  775. case Settings::RendererBackend::Vulkan:
  776. if (!InitializeVulkan()) {
  777. return false;
  778. }
  779. break;
  780. case Settings::RendererBackend::Null:
  781. InitializeNull();
  782. break;
  783. }
  784. // Update the Window System information with the new render target
  785. window_info = GetWindowSystemInfo(child_widget->windowHandle());
  786. child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  787. layout()->addWidget(child_widget);
  788. // Reset minimum required size to avoid resizing issues on the main window after restarting.
  789. setMinimumSize(1, 1);
  790. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  791. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  792. OnFramebufferSizeChanged();
  793. BackupGeometry();
  794. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  795. if (!LoadOpenGL()) {
  796. return false;
  797. }
  798. }
  799. return true;
  800. }
  801. void GRenderWindow::ReleaseRenderTarget() {
  802. if (child_widget) {
  803. layout()->removeWidget(child_widget);
  804. child_widget->deleteLater();
  805. child_widget = nullptr;
  806. }
  807. main_context.reset();
  808. }
  809. void GRenderWindow::CaptureScreenshot(const QString& screenshot_path) {
  810. auto& renderer = system.Renderer();
  811. const f32 res_scale = Settings::values.resolution_info.up_factor;
  812. if (renderer.IsScreenshotPending()) {
  813. LOG_WARNING(Render,
  814. "A screenshot is already requested or in progress, ignoring the request");
  815. return;
  816. }
  817. const Layout::FramebufferLayout layout{Layout::FrameLayoutFromResolutionScale(res_scale)};
  818. screenshot_image = QImage(QSize(layout.width, layout.height), QImage::Format_RGB32);
  819. renderer.RequestScreenshot(
  820. screenshot_image.bits(),
  821. [=, this](bool invert_y) {
  822. const std::string std_screenshot_path = screenshot_path.toStdString();
  823. if (screenshot_image.mirrored(false, invert_y).save(screenshot_path)) {
  824. LOG_INFO(Frontend, "Screenshot saved to \"{}\"", std_screenshot_path);
  825. } else {
  826. LOG_ERROR(Frontend, "Failed to save screenshot to \"{}\"", std_screenshot_path);
  827. }
  828. },
  829. layout);
  830. }
  831. bool GRenderWindow::IsLoadingComplete() const {
  832. return first_frame;
  833. }
  834. void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  835. setMinimumSize(minimal_size.first, minimal_size.second);
  836. }
  837. bool GRenderWindow::InitializeOpenGL() {
  838. #ifdef HAS_OPENGL
  839. if (!QOpenGLContext::supportsThreadedOpenGL()) {
  840. QMessageBox::warning(this, tr("OpenGL not available!"),
  841. tr("OpenGL shared contexts are not supported."));
  842. return false;
  843. }
  844. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  845. // WA_DontShowOnScreen, WA_DeleteOnClose
  846. auto child = new OpenGLRenderWidget(this);
  847. child_widget = child;
  848. child_widget->windowHandle()->create();
  849. auto context = std::make_shared<OpenGLSharedContext>(child->windowHandle());
  850. main_context = context;
  851. child->SetContext(
  852. std::make_unique<OpenGLSharedContext>(context->GetShareContext(), child->windowHandle()));
  853. return true;
  854. #else
  855. QMessageBox::warning(this, tr("OpenGL not available!"),
  856. tr("yuzu has not been compiled with OpenGL support."));
  857. return false;
  858. #endif
  859. }
  860. bool GRenderWindow::InitializeVulkan() {
  861. auto child = new VulkanRenderWidget(this);
  862. child_widget = child;
  863. child_widget->windowHandle()->create();
  864. main_context = std::make_unique<DummyContext>();
  865. return true;
  866. }
  867. void GRenderWindow::InitializeNull() {
  868. child_widget = new NullRenderWidget(this);
  869. main_context = std::make_unique<DummyContext>();
  870. }
  871. bool GRenderWindow::LoadOpenGL() {
  872. auto context = CreateSharedContext();
  873. auto scope = context->Acquire();
  874. if (!gladLoadGL()) {
  875. QMessageBox::warning(
  876. this, tr("Error while initializing OpenGL!"),
  877. tr("Your GPU may not support OpenGL, or you do not have the latest graphics driver."));
  878. return false;
  879. }
  880. const QString renderer =
  881. QString::fromUtf8(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
  882. if (!GLAD_GL_VERSION_4_6) {
  883. LOG_ERROR(Frontend, "GPU does not support OpenGL 4.6: {}", renderer.toStdString());
  884. QMessageBox::warning(this, tr("Error while initializing OpenGL 4.6!"),
  885. tr("Your GPU may not support OpenGL 4.6, or you do not have the "
  886. "latest graphics driver.<br><br>GL Renderer:<br>%1")
  887. .arg(renderer));
  888. return false;
  889. }
  890. QStringList unsupported_gl_extensions = GetUnsupportedGLExtensions();
  891. if (!unsupported_gl_extensions.empty()) {
  892. QMessageBox::warning(
  893. this, tr("Error while initializing OpenGL!"),
  894. tr("Your GPU may not support one or more required OpenGL extensions. Please ensure you "
  895. "have the latest graphics driver.<br><br>GL Renderer:<br>%1<br><br>Unsupported "
  896. "extensions:<br>%2")
  897. .arg(renderer)
  898. .arg(unsupported_gl_extensions.join(QStringLiteral("<br>"))));
  899. return false;
  900. }
  901. return true;
  902. }
  903. QStringList GRenderWindow::GetUnsupportedGLExtensions() const {
  904. QStringList unsupported_ext;
  905. // Extensions required to support some texture formats.
  906. if (!GLAD_GL_EXT_texture_compression_s3tc) {
  907. unsupported_ext.append(QStringLiteral("EXT_texture_compression_s3tc"));
  908. }
  909. if (!GLAD_GL_ARB_texture_compression_rgtc) {
  910. unsupported_ext.append(QStringLiteral("ARB_texture_compression_rgtc"));
  911. }
  912. if (!unsupported_ext.empty()) {
  913. const std::string gl_renderer{reinterpret_cast<const char*>(glGetString(GL_RENDERER))};
  914. LOG_ERROR(Frontend, "GPU does not support all required extensions: {}", gl_renderer);
  915. }
  916. for (const QString& ext : unsupported_ext) {
  917. LOG_ERROR(Frontend, "Unsupported GL extension: {}", ext.toStdString());
  918. }
  919. return unsupported_ext;
  920. }
  921. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread_) {
  922. emu_thread = emu_thread_;
  923. }
  924. void GRenderWindow::OnEmulationStopping() {
  925. emu_thread = nullptr;
  926. }
  927. void GRenderWindow::showEvent(QShowEvent* event) {
  928. QWidget::showEvent(event);
  929. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  930. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  931. Qt::UniqueConnection);
  932. }
  933. bool GRenderWindow::eventFilter(QObject* object, QEvent* event) {
  934. if (event->type() == QEvent::HoverMove) {
  935. if (Settings::values.mouse_panning || Settings::values.mouse_enabled) {
  936. auto* hover_event = static_cast<QMouseEvent*>(event);
  937. mouseMoveEvent(hover_event);
  938. return false;
  939. }
  940. emit MouseActivity();
  941. }
  942. return false;
  943. }