emu_window.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /**
  68. * Signal that a touch pressed event has occurred (e.g. mouse click pressed)
  69. * @param framebuffer_x Framebuffer x-coordinate that was pressed
  70. * @param framebuffer_y Framebuffer y-coordinate that was pressed
  71. */
  72. void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y);
  73. /// Signal that a touch released event has occurred (e.g. mouse click released)
  74. void TouchReleased();
  75. /**
  76. * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window)
  77. * @param framebuffer_x Framebuffer x-coordinate
  78. * @param framebuffer_y Framebuffer y-coordinate
  79. */
  80. void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y);
  81. /**
  82. * Returns currently active configuration.
  83. * @note Accesses to the returned object need not be consistent because it may be modified in
  84. * another thread
  85. */
  86. const WindowConfig& GetActiveConfig() const {
  87. return active_config;
  88. }
  89. /**
  90. * Requests the internal configuration to be replaced by the specified argument at some point in
  91. * the future.
  92. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  93. * loop. Hence there is no guarantee on when the requested configuration will be active.
  94. */
  95. void SetConfig(const WindowConfig& val) {
  96. config = val;
  97. }
  98. /**
  99. * Gets the framebuffer layout (width, height, and screen regions)
  100. * @note This method is thread-safe
  101. */
  102. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  103. return framebuffer_layout;
  104. }
  105. /**
  106. * Convenience method to update the current frame layout
  107. * Read from the current settings to determine which layout to use.
  108. */
  109. void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
  110. protected:
  111. EmuWindow();
  112. virtual ~EmuWindow();
  113. /**
  114. * Processes any pending configuration changes from the last SetConfig call.
  115. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  116. * field changed.
  117. * @note Implementations will usually want to call this from the GUI thread.
  118. * @todo Actually call this in existing implementations.
  119. */
  120. void ProcessConfigurationChanges() {
  121. // TODO: For proper thread safety, we should eventually implement a proper
  122. // multiple-writer/single-reader queue...
  123. if (config.min_client_area_size != active_config.min_client_area_size) {
  124. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  125. config.min_client_area_size = active_config.min_client_area_size;
  126. }
  127. }
  128. /**
  129. * Update framebuffer layout with the given parameter.
  130. * @note EmuWindow implementations will usually use this in window resize event handlers.
  131. */
  132. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  133. framebuffer_layout = layout;
  134. }
  135. /**
  136. * Update internal client area size with the given parameter.
  137. * @note EmuWindow implementations will usually use this in window resize event handlers.
  138. */
  139. void NotifyClientAreaSizeChanged(const std::pair<unsigned, unsigned>& size) {
  140. client_area_width = size.first;
  141. client_area_height = size.second;
  142. }
  143. private:
  144. /**
  145. * Handler called when the minimal client area was requested to be changed via SetConfig.
  146. * For the request to be honored, EmuWindow implementations will usually reimplement this
  147. * function.
  148. */
  149. virtual void OnMinimalClientAreaChangeRequest(std::pair<unsigned, unsigned>) {
  150. // By default, ignore this request and do nothing.
  151. }
  152. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  153. unsigned client_area_width; ///< Current client width, should be set by window impl.
  154. unsigned client_area_height; ///< Current client height, should be set by window impl.
  155. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  156. /// ProcessConfigurationChanges)
  157. WindowConfig active_config; ///< Internal active configuration
  158. class TouchState;
  159. std::shared_ptr<TouchState> touch_state;
  160. /**
  161. * Clip the provided coordinates to be inside the touchscreen area.
  162. */
  163. std::tuple<unsigned, unsigned> ClipToTouchScreen(unsigned new_x, unsigned new_y) const;
  164. };
  165. } // namespace Core::Frontend