bootmanager.cpp 41 KB

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