emu_window.h 8.1 KB

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