emu_window.h 7.0 KB

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