emu_window.h 7.6 KB

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