emu_window.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <memory>
  6. #include <utility>
  7. #include "common/common_types.h"
  8. #include "core/frontend/framebuffer_layout.h"
  9. namespace Core::Frontend {
  10. /// Information for the Graphics Backends signifying what type of screen pointer is in
  11. /// WindowInformation
  12. enum class WindowSystemType {
  13. Headless,
  14. Windows,
  15. X11,
  16. Wayland,
  17. };
  18. /**
  19. * Represents a drawing context that supports graphics operations.
  20. */
  21. class GraphicsContext {
  22. public:
  23. virtual ~GraphicsContext();
  24. /// Inform the driver to swap the front/back buffers and present the current image
  25. virtual void SwapBuffers() {}
  26. /// Makes the graphics context current for the caller thread
  27. virtual void MakeCurrent() {}
  28. /// Releases (dunno if this is the "right" word) the context from the caller thread
  29. virtual void DoneCurrent() {}
  30. class Scoped {
  31. public:
  32. [[nodiscard]] explicit Scoped(GraphicsContext& context_) : context(context_) {
  33. context.MakeCurrent();
  34. }
  35. ~Scoped() {
  36. if (active) {
  37. context.DoneCurrent();
  38. }
  39. }
  40. /// In the event that context was destroyed before the Scoped is destroyed, this provides a
  41. /// mechanism to prevent calling a destroyed object's method during the deconstructor
  42. void Cancel() {
  43. active = false;
  44. }
  45. private:
  46. GraphicsContext& context;
  47. bool active{true};
  48. };
  49. /// Calls MakeCurrent on the context and calls DoneCurrent when the scope for the returned value
  50. /// ends
  51. [[nodiscard]] Scoped Acquire() {
  52. return Scoped{*this};
  53. }
  54. };
  55. /**
  56. * Abstraction class used to provide an interface between emulation code and the frontend
  57. * (e.g. SDL, QGLWidget, GLFW, etc...).
  58. *
  59. * Design notes on the interaction between EmuWindow and the emulation core:
  60. * - Generally, decisions on anything visible to the user should be left up to the GUI.
  61. * For example, the emulation core should not try to dictate some window title or size.
  62. * This stuff is not the core's business and only causes problems with regards to thread-safety
  63. * anyway.
  64. * - Under certain circumstances, it may be desirable for the core to politely request the GUI
  65. * to set e.g. a minimum window size. However, the GUI should always be free to ignore any
  66. * such hints.
  67. * - EmuWindow may expose some of its state as read-only to the emulation core, however care
  68. * should be taken to make sure the provided information is self-consistent. This requires
  69. * some sort of synchronization (most of this is still a TODO).
  70. * - DO NOT TREAT THIS CLASS AS A GUI TOOLKIT ABSTRACTION LAYER. That's not what it is. Please
  71. * re-read the upper points again and think about it if you don't see this.
  72. */
  73. class EmuWindow {
  74. public:
  75. /// Data structure to store emuwindow configuration
  76. struct WindowConfig {
  77. bool fullscreen = false;
  78. int res_width = 0;
  79. int res_height = 0;
  80. std::pair<u32, u32> min_client_area_size;
  81. };
  82. /// Data describing host window system information
  83. struct WindowSystemInfo {
  84. // Window system type. Determines which GL context or Vulkan WSI is used.
  85. WindowSystemType type = WindowSystemType::Headless;
  86. // Connection to a display server. This is used on X11 and Wayland platforms.
  87. void* display_connection = nullptr;
  88. // Render surface. This is a pointer to the native window handle, which depends
  89. // on the platform. e.g. HWND for Windows, Window for X11. If the surface is
  90. // set to nullptr, the video backend will run in headless mode.
  91. void* render_surface = nullptr;
  92. // Scale of the render surface. For hidpi systems, this will be >1.
  93. float render_surface_scale = 1.0f;
  94. };
  95. /// Called from GPU thread when a frame is displayed.
  96. virtual void OnFrameDisplayed() {}
  97. /**
  98. * Returns a GraphicsContext that the frontend provides to be used for rendering.
  99. */
  100. virtual std::unique_ptr<GraphicsContext> CreateSharedContext() const = 0;
  101. /// Returns if window is shown (not minimized)
  102. virtual bool IsShown() const = 0;
  103. /**
  104. * Returns currently active configuration.
  105. * @note Accesses to the returned object need not be consistent because it may be modified in
  106. * another thread
  107. */
  108. const WindowConfig& GetActiveConfig() const {
  109. return active_config;
  110. }
  111. /**
  112. * Requests the internal configuration to be replaced by the specified argument at some point in
  113. * the future.
  114. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  115. * loop. Hence there is no guarantee on when the requested configuration will be active.
  116. */
  117. void SetConfig(const WindowConfig& val) {
  118. config = val;
  119. }
  120. /**
  121. * Returns system information about the drawing area.
  122. */
  123. const WindowSystemInfo& GetWindowInfo() const {
  124. return window_info;
  125. }
  126. /**
  127. * Gets the framebuffer layout (width, height, and screen regions)
  128. * @note This method is thread-safe
  129. */
  130. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  131. return framebuffer_layout;
  132. }
  133. /**
  134. * Convenience method to update the current frame layout
  135. * Read from the current settings to determine which layout to use.
  136. */
  137. void UpdateCurrentFramebufferLayout(u32 width, u32 height);
  138. protected:
  139. explicit EmuWindow();
  140. virtual ~EmuWindow();
  141. /**
  142. * Processes any pending configuration changes from the last SetConfig call.
  143. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  144. * field changed.
  145. * @note Implementations will usually want to call this from the GUI thread.
  146. * @todo Actually call this in existing implementations.
  147. */
  148. void ProcessConfigurationChanges() {
  149. // TODO: For proper thread safety, we should eventually implement a proper
  150. // multiple-writer/single-reader queue...
  151. if (config.min_client_area_size != active_config.min_client_area_size) {
  152. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  153. config.min_client_area_size = active_config.min_client_area_size;
  154. }
  155. }
  156. /**
  157. * Update framebuffer layout with the given parameter.
  158. * @note EmuWindow implementations will usually use this in window resize event handlers.
  159. */
  160. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  161. framebuffer_layout = layout;
  162. }
  163. /**
  164. * Update internal client area size with the given parameter.
  165. * @note EmuWindow implementations will usually use this in window resize event handlers.
  166. */
  167. void NotifyClientAreaSizeChanged(std::pair<u32, u32> size) {
  168. client_area_width = size.first;
  169. client_area_height = size.second;
  170. }
  171. /**
  172. * Converts a screen postion into the equivalent touchscreen position.
  173. */
  174. std::pair<f32, f32> MapToTouchScreen(u32 framebuffer_x, u32 framebuffer_y) const;
  175. WindowSystemInfo window_info;
  176. private:
  177. /**
  178. * Handler called when the minimal client area was requested to be changed via SetConfig.
  179. * For the request to be honored, EmuWindow implementations will usually reimplement this
  180. * function.
  181. */
  182. virtual void OnMinimalClientAreaChangeRequest(std::pair<u32, u32>) {
  183. // By default, ignore this request and do nothing.
  184. }
  185. /**
  186. * Clip the provided coordinates to be inside the touchscreen area.
  187. */
  188. std::pair<u32, u32> ClipToTouchScreen(u32 new_x, u32 new_y) const;
  189. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  190. u32 client_area_width; ///< Current client width, should be set by window impl.
  191. u32 client_area_height; ///< Current client height, should be set by window impl.
  192. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  193. /// ProcessConfigurationChanges)
  194. WindowConfig active_config; ///< Internal active configuration
  195. };
  196. } // namespace Core::Frontend