emu_window.h 7.5 KB

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