bootmanager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <condition_variable>
  7. #include <mutex>
  8. #include <QImage>
  9. #include <QThread>
  10. #include <QWidget>
  11. #include "core/core.h"
  12. #include "core/frontend/emu_window.h"
  13. class QKeyEvent;
  14. class QScreen;
  15. class QTouchEvent;
  16. class GGLWidgetInternal;
  17. class GMainWindow;
  18. class GRenderWindow;
  19. class QSurface;
  20. class QOpenGLContext;
  21. namespace VideoCore {
  22. enum class LoadCallbackStage;
  23. }
  24. class EmuThread final : public QThread {
  25. Q_OBJECT
  26. public:
  27. explicit EmuThread(GRenderWindow* render_window);
  28. ~EmuThread() override;
  29. /**
  30. * Start emulation (on new thread)
  31. * @warning Only call when not running!
  32. */
  33. void run() override;
  34. /**
  35. * Steps the emulation thread by a single CPU instruction (if the CPU is not already running)
  36. * @note This function is thread-safe
  37. */
  38. void ExecStep() {
  39. exec_step = true;
  40. running_cv.notify_all();
  41. }
  42. /**
  43. * Sets whether the emulation thread is running or not
  44. * @param running Boolean value, set the emulation thread to running if true
  45. * @note This function is thread-safe
  46. */
  47. void SetRunning(bool running) {
  48. std::unique_lock lock{running_mutex};
  49. this->running = running;
  50. lock.unlock();
  51. running_cv.notify_all();
  52. }
  53. /**
  54. * Check if the emulation thread is running or not
  55. * @return True if the emulation thread is running, otherwise false
  56. * @note This function is thread-safe
  57. */
  58. bool IsRunning() const {
  59. return running;
  60. }
  61. /**
  62. * Requests for the emulation thread to stop running
  63. */
  64. void RequestStop() {
  65. stop_run = true;
  66. SetRunning(false);
  67. }
  68. private:
  69. bool exec_step = false;
  70. bool running = false;
  71. std::atomic_bool stop_run{false};
  72. std::mutex running_mutex;
  73. std::condition_variable running_cv;
  74. GRenderWindow* render_window;
  75. signals:
  76. /**
  77. * Emitted when the CPU has halted execution
  78. *
  79. * @warning When connecting to this signal from other threads, make sure to specify either
  80. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  81. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  82. */
  83. void DebugModeEntered();
  84. /**
  85. * Emitted right before the CPU continues execution
  86. *
  87. * @warning When connecting to this signal from other threads, make sure to specify either
  88. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  89. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  90. */
  91. void DebugModeLeft();
  92. void ErrorThrown(Core::System::ResultStatus, std::string);
  93. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  94. };
  95. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  96. Q_OBJECT
  97. public:
  98. GRenderWindow(GMainWindow* parent, EmuThread* emu_thread);
  99. ~GRenderWindow() override;
  100. // EmuWindow implementation
  101. void SwapBuffers() override;
  102. void MakeCurrent() override;
  103. void DoneCurrent() override;
  104. void PollEvents() override;
  105. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  106. void ForwardKeyPressEvent(QKeyEvent* event);
  107. void ForwardKeyReleaseEvent(QKeyEvent* event);
  108. void BackupGeometry();
  109. void RestoreGeometry();
  110. void restoreGeometry(const QByteArray& geometry); // overridden
  111. QByteArray saveGeometry(); // overridden
  112. qreal GetWindowPixelRatio() const;
  113. std::pair<u32, u32> ScaleTouch(QPointF pos) const;
  114. void closeEvent(QCloseEvent* event) override;
  115. bool event(QEvent* event) override;
  116. void focusOutEvent(QFocusEvent* event) override;
  117. void OnClientAreaResized(u32 width, u32 height);
  118. void InitRenderTarget();
  119. void CaptureScreenshot(u32 res_scale, const QString& screenshot_path);
  120. public slots:
  121. void moveContext(); // overridden
  122. void OnEmulationStarting(EmuThread* emu_thread);
  123. void OnEmulationStopping();
  124. void OnFramebufferSizeChanged();
  125. signals:
  126. /// Emitted when the window is closed
  127. void Closed();
  128. void FirstFrameDisplayed();
  129. private:
  130. void TouchBeginEvent(const QTouchEvent* event);
  131. void TouchUpdateEvent(const QTouchEvent* event);
  132. void TouchEndEvent();
  133. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  134. QWidget* container = nullptr;
  135. GGLWidgetInternal* child = nullptr;
  136. QByteArray geometry;
  137. EmuThread* emu_thread;
  138. // Context that backs the GGLWidgetInternal (and will be used by core to render)
  139. std::unique_ptr<QOpenGLContext> context;
  140. // Context that will be shared between all newly created contexts. This should never be made
  141. // current
  142. std::unique_ptr<QOpenGLContext> shared_context;
  143. /// Temporary storage of the screenshot taken
  144. QImage screenshot_image;
  145. bool first_frame = false;
  146. protected:
  147. void showEvent(QShowEvent* event) override;
  148. };