emu_window.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <tuple>
  7. #include <utility>
  8. #include "common/common_types.h"
  9. #include "core/frontend/framebuffer_layout.h"
  10. namespace Core::Frontend {
  11. /**
  12. * Represents a graphics context that can be used for background computation or drawing. If the
  13. * graphics backend doesn't require the context, then the implementation of these methods can be
  14. * stubs
  15. */
  16. class GraphicsContext {
  17. public:
  18. virtual ~GraphicsContext();
  19. /// Makes the graphics context current for the caller thread
  20. virtual void MakeCurrent() = 0;
  21. /// Releases (dunno if this is the "right" word) the context from the caller thread
  22. virtual void DoneCurrent() = 0;
  23. };
  24. /**
  25. * Abstraction class used to provide an interface between emulation code and the frontend
  26. * (e.g. SDL, QGLWidget, GLFW, etc...).
  27. *
  28. * Design notes on the interaction between EmuWindow and the emulation core:
  29. * - Generally, decisions on anything visible to the user should be left up to the GUI.
  30. * For example, the emulation core should not try to dictate some window title or size.
  31. * This stuff is not the core's business and only causes problems with regards to thread-safety
  32. * anyway.
  33. * - Under certain circumstances, it may be desirable for the core to politely request the GUI
  34. * to set e.g. a minimum window size. However, the GUI should always be free to ignore any
  35. * such hints.
  36. * - EmuWindow may expose some of its state as read-only to the emulation core, however care
  37. * should be taken to make sure the provided information is self-consistent. This requires
  38. * some sort of synchronization (most of this is still a TODO).
  39. * - DO NOT TREAT THIS CLASS AS A GUI TOOLKIT ABSTRACTION LAYER. That's not what it is. Please
  40. * re-read the upper points again and think about it if you don't see this.
  41. */
  42. class EmuWindow : public GraphicsContext {
  43. public:
  44. /// Data structure to store emuwindow configuration
  45. struct WindowConfig {
  46. bool fullscreen = false;
  47. int res_width = 0;
  48. int res_height = 0;
  49. std::pair<unsigned, unsigned> min_client_area_size;
  50. };
  51. /// Polls window events
  52. virtual void PollEvents() = 0;
  53. /**
  54. * Returns a GraphicsContext that the frontend provides that is shared with the emu window. This
  55. * context can be used from other threads for background graphics computation. If the frontend
  56. * is using a graphics backend that doesn't need anything specific to run on a different thread,
  57. * then it can use a stubbed implemenation for GraphicsContext.
  58. *
  59. * If the return value is null, then the core should assume that the frontend cannot provide a
  60. * Shared Context
  61. */
  62. virtual std::unique_ptr<GraphicsContext> CreateSharedContext() const {
  63. return nullptr;
  64. }
  65. /// Returns if window is shown (not minimized)
  66. virtual bool IsShown() const = 0;
  67. /// Retrieves Vulkan specific handlers from the window
  68. virtual void RetrieveVulkanHandlers(void* get_instance_proc_addr, void* instance,
  69. void* surface) const = 0;
  70. /**
  71. * Signal that a touch pressed event has occurred (e.g. mouse click pressed)
  72. * @param framebuffer_x Framebuffer x-coordinate that was pressed
  73. * @param framebuffer_y Framebuffer y-coordinate that was pressed
  74. */
  75. void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y);
  76. /// Signal that a touch released event has occurred (e.g. mouse click released)
  77. void TouchReleased();
  78. /**
  79. * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window)
  80. * @param framebuffer_x Framebuffer x-coordinate
  81. * @param framebuffer_y Framebuffer y-coordinate
  82. */
  83. void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y);
  84. /**
  85. * Returns currently active configuration.
  86. * @note Accesses to the returned object need not be consistent because it may be modified in
  87. * another thread
  88. */
  89. const WindowConfig& GetActiveConfig() const {
  90. return active_config;
  91. }
  92. /**
  93. * Requests the internal configuration to be replaced by the specified argument at some point in
  94. * the future.
  95. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  96. * loop. Hence there is no guarantee on when the requested configuration will be active.
  97. */
  98. void SetConfig(const WindowConfig& val) {
  99. config = val;
  100. }
  101. /**
  102. * Gets the framebuffer layout (width, height, and screen regions)
  103. * @note This method is thread-safe
  104. */
  105. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  106. return framebuffer_layout;
  107. }
  108. /**
  109. * Convenience method to update the current frame layout
  110. * Read from the current settings to determine which layout to use.
  111. */
  112. void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
  113. protected:
  114. EmuWindow();
  115. virtual ~EmuWindow();
  116. /**
  117. * Processes any pending configuration changes from the last SetConfig call.
  118. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  119. * field changed.
  120. * @note Implementations will usually want to call this from the GUI thread.
  121. * @todo Actually call this in existing implementations.
  122. */
  123. void ProcessConfigurationChanges() {
  124. // TODO: For proper thread safety, we should eventually implement a proper
  125. // multiple-writer/single-reader queue...
  126. if (config.min_client_area_size != active_config.min_client_area_size) {
  127. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  128. config.min_client_area_size = active_config.min_client_area_size;
  129. }
  130. }
  131. /**
  132. * Update framebuffer layout with the given parameter.
  133. * @note EmuWindow implementations will usually use this in window resize event handlers.
  134. */
  135. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  136. framebuffer_layout = layout;
  137. }
  138. /**
  139. * Update internal client area size with the given parameter.
  140. * @note EmuWindow implementations will usually use this in window resize event handlers.
  141. */
  142. void NotifyClientAreaSizeChanged(const std::pair<unsigned, unsigned>& size) {
  143. client_area_width = size.first;
  144. client_area_height = size.second;
  145. }
  146. private:
  147. /**
  148. * Handler called when the minimal client area was requested to be changed via SetConfig.
  149. * For the request to be honored, EmuWindow implementations will usually reimplement this
  150. * function.
  151. */
  152. virtual void OnMinimalClientAreaChangeRequest(std::pair<unsigned, unsigned>) {
  153. // By default, ignore this request and do nothing.
  154. }
  155. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  156. unsigned client_area_width; ///< Current client width, should be set by window impl.
  157. unsigned client_area_height; ///< Current client height, should be set by window impl.
  158. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  159. /// ProcessConfigurationChanges)
  160. WindowConfig active_config; ///< Internal active configuration
  161. class TouchState;
  162. std::shared_ptr<TouchState> touch_state;
  163. /**
  164. * Clip the provided coordinates to be inside the touchscreen area.
  165. */
  166. std::tuple<unsigned, unsigned> ClipToTouchScreen(unsigned new_x, unsigned new_y) const;
  167. };
  168. } // namespace Core::Frontend