display.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_DISPLAY_H_
  5. #define UI_DISPLAY_DISPLAY_H_
  6. #include <stdint.h>
  7. #include "mojo/public/cpp/bindings/struct_traits.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "ui/display/display_export.h"
  10. #include "ui/display/types/display_constants.h"
  11. #include "ui/gfx/display_color_spaces.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. namespace display {
  14. namespace mojom {
  15. class DisplayDataView;
  16. }
  17. // This class typically, but does not always, correspond to a physical display
  18. // connected to the system. A fake Display may exist on a headless system, or a
  19. // Display may correspond to a remote, virtual display.
  20. //
  21. // Note: The screen and display currently uses pixel coordinate
  22. // system. For platforms that support DIP (density independent pixel),
  23. // |bounds()| and |work_area| will return values in DIP coordinate
  24. // system, not in backing pixels.
  25. class DISPLAY_EXPORT Display final {
  26. public:
  27. // Screen Rotation in clock-wise degrees.
  28. // This enum corresponds to DisplayRotationDefaultProto::Rotation in
  29. // components/policy/proto/chrome_device_policy.proto.
  30. enum Rotation {
  31. ROTATE_0 = 0,
  32. ROTATE_90,
  33. ROTATE_180,
  34. ROTATE_270,
  35. };
  36. // The display rotation can have multiple causes for change. A user can set a
  37. // preference. On devices with accelerometers, they can change the rotation.
  38. // RotationSource allows for the tracking of a Rotation per source of the
  39. // change. ACTIVE is the current rotation of the display. Rotation changes not
  40. // due to an accelerometer, nor the user, are to use this source directly.
  41. // UNKNOWN is when no rotation source has been provided.
  42. enum class RotationSource {
  43. ACCELEROMETER = 0,
  44. ACTIVE,
  45. USER,
  46. UNKNOWN,
  47. };
  48. // Touch support for the display.
  49. enum class TouchSupport {
  50. UNKNOWN,
  51. AVAILABLE,
  52. UNAVAILABLE,
  53. };
  54. // Accelerometer support for the display.
  55. enum class AccelerometerSupport {
  56. UNKNOWN,
  57. AVAILABLE,
  58. UNAVAILABLE,
  59. };
  60. // Creates a display with kInvalidDisplayId as default.
  61. Display();
  62. explicit Display(int64_t id);
  63. Display(int64_t id, const gfx::Rect& bounds);
  64. Display(const Display& other);
  65. ~Display();
  66. // Returns a valid display with default parameters and ID set to
  67. // |kDefaultDisplayId| which is used when there's no actual display connected
  68. // to the device.
  69. static Display GetDefaultDisplay();
  70. // Returns the forced device scale factor, which is given by
  71. // "--force-device-scale-factor".
  72. static float GetForcedDeviceScaleFactor();
  73. // Indicates if a device scale factor is being explicitly enforced from the
  74. // command line via "--force-device-scale-factor".
  75. static bool HasForceDeviceScaleFactor();
  76. // Returns the forced raster color profile, which is given by
  77. // "--force-raster-color-profile".
  78. static gfx::ColorSpace GetForcedRasterColorProfile();
  79. // Indicates if a raster color profile is being explicitly enforced from the
  80. // command line via "--force-raster-color-profile".
  81. static bool HasForceRasterColorProfile();
  82. // Indicates if the display color profile being forced should be ensured to
  83. // be in use by the operating system as well.
  84. static bool HasEnsureForcedColorProfile();
  85. // Resets the caches used to determine if a device scale factor is being
  86. // forced from the command line via "--force-device-scale-factor", and thus
  87. // ensures that the command line is reevaluated.
  88. static void ResetForceDeviceScaleFactorForTesting();
  89. // Resets the cache and sets a new force device scale factor.
  90. static void SetForceDeviceScaleFactor(double dsf);
  91. // Converts the given angle to its corresponding Rotation. The angle is in
  92. // degrees, and the only valid values are 0, 90, 180, and 270.
  93. // TODO(crbug.com/840189): we should never need to convert degrees to a
  94. // Rotation if we were to Rotations internally and only converted to numeric
  95. // values when required.
  96. static Rotation DegreesToRotation(int degrees);
  97. // This is the analog to DegreesToRotation and converts a Rotation to a
  98. // numeric representation.
  99. static int RotationToDegrees(Rotation rotation);
  100. // Returns true if |degrees| is compatible with DegreesToRotation. I.e., that
  101. // it is 0, 90, 180, or 270.
  102. static bool IsValidRotation(int degrees);
  103. // Sets/Gets unique identifier associated with the display.
  104. // -1 means invalid display and it doesn't not exit.
  105. int64_t id() const { return id_; }
  106. void set_id(int64_t id) { id_ = id; }
  107. // Gets/Sets the display's bounds in Screen's coordinates.
  108. const gfx::Rect& bounds() const { return bounds_; }
  109. void set_bounds(const gfx::Rect& bounds) { bounds_ = bounds; }
  110. // Gets/Sets the display's work area in Screen's coordinates.
  111. const gfx::Rect& work_area() const { return work_area_; }
  112. void set_work_area(const gfx::Rect& work_area) { work_area_ = work_area; }
  113. // Output device's pixel scale factor. This specifies how much the
  114. // UI should be scaled when the actual output has more pixels than
  115. // standard displays (which is around 100~120dpi.) The potential return
  116. // values depend on each platforms.
  117. float device_scale_factor() const { return device_scale_factor_; }
  118. void set_device_scale_factor(float scale) { device_scale_factor_ = scale; }
  119. Rotation rotation() const { return rotation_; }
  120. void set_rotation(Rotation rotation) { rotation_ = rotation; }
  121. int RotationAsDegree() const;
  122. void SetRotationAsDegree(int rotation);
  123. // Panel's native rotation. This is same as |rotation()| in normal case.
  124. Rotation panel_rotation() const {
  125. return panel_rotation_ ? *panel_rotation_ : rotation_;
  126. }
  127. void set_panel_rotation(Rotation rotation) { panel_rotation_ = rotation; }
  128. int PanelRotationAsDegree() const;
  129. TouchSupport touch_support() const { return touch_support_; }
  130. void set_touch_support(TouchSupport support) { touch_support_ = support; }
  131. AccelerometerSupport accelerometer_support() const {
  132. return accelerometer_support_;
  133. }
  134. void set_accelerometer_support(AccelerometerSupport support) {
  135. accelerometer_support_ = support;
  136. }
  137. // Utility functions that just return the size of display and work area.
  138. const gfx::Size& size() const { return bounds_.size(); }
  139. const gfx::Size& work_area_size() const { return work_area_.size(); }
  140. // Returns the work area insets.
  141. gfx::Insets GetWorkAreaInsets() const;
  142. // Sets the device scale factor and display bounds in pixel. This
  143. // updates the work area using the same insets between old bounds and
  144. // work area.
  145. void SetScaleAndBounds(float device_scale_factor,
  146. const gfx::Rect& bounds_in_pixel);
  147. // Sets the device scale factor while respecting forced scale factor and other
  148. // constraints. Use this over set_device_scale_factor() unless you need to
  149. // forcefully overwrite the scale.
  150. void SetScale(float device_scale_factor);
  151. // Sets the display's size. This updates the work area using the same insets
  152. // between old bounds and work area.
  153. void SetSize(const gfx::Size& size_in_pixel);
  154. // Computes and updates the display's work are using
  155. // |work_area_insets| and the bounds.
  156. void UpdateWorkAreaFromInsets(const gfx::Insets& work_area_insets);
  157. // Returns the display's size in pixel coordinates.
  158. gfx::Size GetSizeInPixel() const;
  159. void set_size_in_pixels(const gfx::Size& size) { size_in_pixels_ = size; }
  160. // Returns a string representation of the display;
  161. std::string ToString() const;
  162. // True if the display contains valid display id.
  163. bool is_valid() const { return id_ != kInvalidDisplayId; }
  164. // True if the display corresponds to internal panel.
  165. bool IsInternal() const;
  166. // [Deprecated] Use `display::GetInternalDisplayIds()`.
  167. // Gets an id of display corresponding to internal panel.
  168. static int64_t InternalDisplayId();
  169. // Maximum cursor size in native pixels.
  170. const gfx::Size& maximum_cursor_size() const { return maximum_cursor_size_; }
  171. void set_maximum_cursor_size(const gfx::Size& size) {
  172. maximum_cursor_size_ = size;
  173. }
  174. // The color spaces used by the display.
  175. // TODO(b/226163383): Rename to SetColorSpaces
  176. const gfx::DisplayColorSpaces& color_spaces() const { return color_spaces_; }
  177. void set_color_spaces(const gfx::DisplayColorSpaces& color_spaces);
  178. // Return true if the display orientation is landscape.
  179. bool is_landscape() const { return bounds_.width() >= bounds_.height(); }
  180. // Default values for color_depth and depth_per_component.
  181. static constexpr int kDefaultBitsPerPixel = 24;
  182. static constexpr int kDefaultBitsPerComponent = 8;
  183. // The following values are abused by media query APIs to detect HDR
  184. // capability.
  185. static constexpr int kHDR10BitsPerPixel = 30;
  186. static constexpr int kHDR10BitsPerComponent = 10;
  187. // The number of bits per pixel. Used by media query APIs.
  188. int color_depth() const { return color_depth_; }
  189. void set_color_depth(int color_depth) {
  190. color_depth_ = color_depth;
  191. }
  192. // The number of bits per color component (all color components are assumed to
  193. // have the same number of bits). Used by media query APIs.
  194. int depth_per_component() const { return depth_per_component_; }
  195. void set_depth_per_component(int depth_per_component) {
  196. depth_per_component_ = depth_per_component;
  197. }
  198. // True if this is a monochrome display (e.g, for accessibility). Used by
  199. // media query APIs.
  200. bool is_monochrome() const { return is_monochrome_; }
  201. void set_is_monochrome(bool is_monochrome) { is_monochrome_ = is_monochrome; }
  202. // The display frequency of the monitor.
  203. int display_frequency() const { return display_frequency_; }
  204. void set_display_frequency(int display_frequency) {
  205. display_frequency_ = display_frequency;
  206. }
  207. // A user-friendly label, determined by the platform.
  208. const std::string& label() const { return label_; }
  209. void set_label(const std::string& label) { label_ = label; }
  210. bool operator==(const Display& rhs) const;
  211. bool operator!=(const Display& rhs) const { return !(*this == rhs); }
  212. private:
  213. friend struct mojo::StructTraits<mojom::DisplayDataView, Display>;
  214. int64_t id_ = kInvalidDisplayId;
  215. gfx::Rect bounds_;
  216. // If non-empty, then should be same size as |bounds_|. Used to avoid rounding
  217. // errors.
  218. gfx::Size size_in_pixels_;
  219. gfx::Rect work_area_;
  220. float device_scale_factor_;
  221. Rotation rotation_ = ROTATE_0;
  222. absl::optional<Rotation> panel_rotation_;
  223. TouchSupport touch_support_ = TouchSupport::UNKNOWN;
  224. AccelerometerSupport accelerometer_support_ = AccelerometerSupport::UNKNOWN;
  225. gfx::Size maximum_cursor_size_;
  226. gfx::DisplayColorSpaces color_spaces_;
  227. int color_depth_;
  228. int depth_per_component_;
  229. bool is_monochrome_ = false;
  230. int display_frequency_ = 0;
  231. std::string label_;
  232. };
  233. } // namespace display
  234. #endif // UI_DISPLAY_DISPLAY_H_