screen.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_DISPLAY_SCREEN_H_
  5. #define UI_DISPLAY_SCREEN_H_
  6. #include <memory>
  7. #include <set>
  8. #include <vector>
  9. #include "base/location.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/values.h"
  12. #include "build/build_config.h"
  13. #include "ui/display/display.h"
  14. #include "ui/display/display_export.h"
  15. #include "ui/display/screen_infos.h"
  16. #include "ui/gfx/gpu_extra_info.h"
  17. #include "ui/gfx/native_widget_types.h"
  18. namespace base {
  19. class TimeDelta;
  20. } // namespace base
  21. namespace gfx {
  22. class Point;
  23. class Rect;
  24. } // namespace gfx
  25. namespace display {
  26. class DisplayObserver;
  27. // A utility class for getting various info about screen size, displays,
  28. // cursor position, etc.
  29. //
  30. // Also, can notify DisplayObservers about global workspace changes. The
  31. // availability of that functionality depends on a platform.
  32. //
  33. // Note that this class does not represent an individual display connected to a
  34. // computer -- see the Display class for that. A single Screen object exists
  35. // regardless of the number of connected displays.
  36. class DISPLAY_EXPORT Screen {
  37. public:
  38. Screen();
  39. Screen(const Screen&) = delete;
  40. Screen& operator=(const Screen&) = delete;
  41. virtual ~Screen();
  42. // Retrieves the single Screen object; this may be null if it's not already
  43. // created, except for IOS where it creates a native screen instance
  44. // automatically.
  45. static Screen* GetScreen();
  46. // Returns whether a Screen singleton exists or not.
  47. static bool HasScreen();
  48. // [Deprecated] as a public method. Do not use this.
  49. // Sets the global screen. Returns the previously installed screen, if any.
  50. // NOTE: this does not take ownership of |screen|. Tests must be sure to reset
  51. // any state they install.
  52. static Screen* SetScreenInstance(Screen* instance,
  53. const base::Location& location = FROM_HERE);
  54. // Returns the current absolute position of the mouse pointer.
  55. virtual gfx::Point GetCursorScreenPoint() = 0;
  56. // Allows tests to override the cursor point location on the screen.
  57. virtual void SetCursorScreenPointForTesting(const gfx::Point& point);
  58. // Returns true if the cursor is directly over |window|.
  59. virtual bool IsWindowUnderCursor(gfx::NativeWindow window) = 0;
  60. // Returns the window at the given screen coordinate |point|.
  61. virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) = 0;
  62. // Finds the topmost visible chrome window at |screen_point|. This should
  63. // return nullptr if |screen_point| is in another program's window which
  64. // occludes the topmost chrome window. Ignores the windows in |ignore|, which
  65. // contain windows such as the tab being dragged right now.
  66. virtual gfx::NativeWindow GetLocalProcessWindowAtPoint(
  67. const gfx::Point& point,
  68. const std::set<gfx::NativeWindow>& ignore) = 0;
  69. // Returns the number of displays. Mirrored displays are excluded; this
  70. // method is intended to return the number of distinct, usable displays.
  71. // The value returned must be at least 1, as GetAllDisplays returns a fake
  72. // display if there are no displays in the system.
  73. virtual int GetNumDisplays() const = 0;
  74. // Returns the list of displays that are currently available.
  75. // Screen subclasses must return at least one Display, even if it is fake.
  76. virtual const std::vector<Display>& GetAllDisplays() const = 0;
  77. // Returns the display nearest the specified window.
  78. // If the window is NULL or the window is not rooted to a display this will
  79. // return the primary display.
  80. virtual Display GetDisplayNearestWindow(gfx::NativeWindow window) const = 0;
  81. // Returns the display nearest the specified view. It may still use the window
  82. // that contains the view (i.e. if a window is spread over two displays,
  83. // the location of the view within that window won't influence the result).
  84. virtual Display GetDisplayNearestView(gfx::NativeView view) const;
  85. // Returns the display nearest the specified DIP |point|.
  86. virtual Display GetDisplayNearestPoint(const gfx::Point& point) const = 0;
  87. // Returns the display that most closely intersects the DIP rect |match_rect|.
  88. virtual Display GetDisplayMatching(const gfx::Rect& match_rect) const = 0;
  89. // Returns the primary display. It is guaranteed that this will return a
  90. // display with a valid display ID even if there is no display connected.
  91. // A real display will be reported via DisplayObserver when it is connected.
  92. virtual Display GetPrimaryDisplay() const = 0;
  93. // Returns a suggested display to use when creating a new window. On most
  94. // platforms just returns the primary display.
  95. Display GetDisplayForNewWindows() const;
  96. // Sets the suggested display to use when creating a new window.
  97. void SetDisplayForNewWindows(int64_t display_id);
  98. // Returns ScreenInfos, attempting to set the current ScreenInfo to the
  99. // display corresponding to `nearest_id`. The returned result is guaranteed
  100. // to be non-empty. This function also performs fallback to ensure the result
  101. // also has a valid current ScreenInfo and exactly one primary ScreenInfo
  102. // (both of which may or may not be `nearest_id`).
  103. display::ScreenInfos GetScreenInfosNearestDisplay(int64_t nearest_id) const;
  104. #if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX)
  105. // Object which suspends the platform-specific screensaver for the duration of
  106. // its existence.
  107. class ScreenSaverSuspender {
  108. public:
  109. ScreenSaverSuspender() = default;
  110. ScreenSaverSuspender(const ScreenSaverSuspender&) = delete;
  111. ScreenSaverSuspender& operator=(const ScreenSaverSuspender&) = delete;
  112. // Causes the platform-specific screensaver to be un-suspended iff this is
  113. // the last remaining instance.
  114. virtual ~ScreenSaverSuspender() = 0;
  115. };
  116. // Suspends the platform-specific screensaver until the returned
  117. // |ScreenSaverSuspender| is destructed, or returns nullptr if suspension
  118. // failed. This method allows stacking multiple overlapping calls, such that
  119. // the platform-specific screensaver will not be un-suspended until all
  120. // returned |ScreenSaverSuspender| instances have been destructed.
  121. virtual std::unique_ptr<ScreenSaverSuspender> SuspendScreenSaver();
  122. #endif // BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX)
  123. // Returns whether the screensaver is currently running.
  124. virtual bool IsScreenSaverActive() const;
  125. // Calculates idle time.
  126. virtual base::TimeDelta CalculateIdleTime() const;
  127. // Adds/Removes display observers.
  128. virtual void AddObserver(DisplayObserver* observer) = 0;
  129. virtual void RemoveObserver(DisplayObserver* observer) = 0;
  130. // Converts |screen_rect| to DIP coordinates in the context of |window|
  131. // clamping to the enclosing rect if the coordinates do not fall on pixel
  132. // boundaries. If |window| is null, the primary display is used as the
  133. // context.
  134. virtual gfx::Rect ScreenToDIPRectInWindow(gfx::NativeWindow window,
  135. const gfx::Rect& screen_rect) const;
  136. // Converts |dip_rect| to screen coordinates in the context of |window|
  137. // clamping to the enclosing rect if the coordinates do not fall on pixel
  138. // boundaries. If |window| is null, the primary display is used as the
  139. // context.
  140. virtual gfx::Rect DIPToScreenRectInWindow(gfx::NativeWindow window,
  141. const gfx::Rect& dip_rect) const;
  142. // Returns true if the display with |display_id| is found and returns that
  143. // display in |display|. Otherwise returns false and |display| remains
  144. // untouched.
  145. bool GetDisplayWithDisplayId(int64_t display_id, Display* display) const;
  146. virtual void SetPanelRotationForTesting(int64_t display_id,
  147. Display::Rotation rotation);
  148. // Depending on a platform, a client can listen to global workspace changes
  149. // by implementing and setting self as a DisplayObserver. It is also possible
  150. // to get current workspace through the GetCurrentWorkspace method.
  151. virtual std::string GetCurrentWorkspace();
  152. // Returns human readable description of the window manager, desktop, and
  153. // other system properties related to the compositing.
  154. virtual base::Value::List GetGpuExtraInfo(
  155. const gfx::GpuExtraInfo& gpu_extra_info);
  156. protected:
  157. void set_shutdown(bool shutdown) { shutdown_ = shutdown; }
  158. private:
  159. friend class ScopedDisplayForNewWindows;
  160. // Used to temporarily override the value from SetDisplayForNewWindows() by
  161. // creating an instance of ScopedDisplayForNewWindows. Call with
  162. // |kInvalidDisplayId| to unset.
  163. void SetScopedDisplayForNewWindows(int64_t display_id);
  164. static gfx::NativeWindow GetWindowForView(gfx::NativeView view);
  165. // A flag indicates that the instance is a special one used during shutdown.
  166. bool shutdown_ = false;
  167. int64_t display_id_for_new_windows_;
  168. int64_t scoped_display_id_for_new_windows_ = display::kInvalidDisplayId;
  169. #if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX)
  170. uint32_t screen_saver_suspension_count_ = 0;
  171. #endif // BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX)
  172. };
  173. // TODO(crbug.com/1317416): Make this static private member of
  174. // ScopedNativeScreen.
  175. DISPLAY_EXPORT Screen* CreateNativeScreen();
  176. // Android does not have `CreateNativeScreen()`.
  177. #if !BUILDFLAG(IS_ANDROID)
  178. // ScopedNativeScreen creates a native screen if there is no screen created yet
  179. // (e.g. by a unit test).
  180. class DISPLAY_EXPORT ScopedNativeScreen {
  181. public:
  182. explicit ScopedNativeScreen(const base::Location& location = FROM_HERE);
  183. ScopedNativeScreen(const ScopedNativeScreen&) = delete;
  184. ScopedNativeScreen& operator=(const ScopedNativeScreen&) = delete;
  185. virtual ~ScopedNativeScreen();
  186. // Create and initialize the screen instance if the screen instance does not
  187. // exist yet.
  188. void MaybeInit(const base::Location& location = FROM_HERE);
  189. void Shutdown();
  190. Screen* screen() { return screen_.get(); }
  191. virtual Screen* CreateScreen();
  192. protected:
  193. explicit ScopedNativeScreen(bool call_maybe_init,
  194. const base::Location& location = FROM_HERE);
  195. private:
  196. bool maybe_init_called_{false};
  197. std::unique_ptr<Screen> screen_;
  198. };
  199. #endif
  200. } // namespace display
  201. #endif // UI_DISPLAY_SCREEN_H_