1 // Copyright (c) 2013 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 5 #ifndef UI_DISPLAY_MANAGER_MANAGED_DISPLAY_INFO_H_ 6 #define UI_DISPLAY_MANAGER_MANAGED_DISPLAY_INFO_H_ 7 8 #include <stdint.h> 9 #include <algorithm> 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 #include "base/files/file_path.h" 15 #include "ui/display/display.h" 16 #include "ui/display/manager/display_manager_export.h" 17 #include "ui/display/types/display_constants.h" 18 #include "ui/gfx/geometry/insets.h" 19 #include "ui/gfx/geometry/rect.h" 20 21 namespace display { 22 23 // A class that represents the display's mode info. 24 class DISPLAY_MANAGER_EXPORT ManagedDisplayMode { 25 public: 26 ManagedDisplayMode(); 27 explicit ManagedDisplayMode(const gfx::Size& size); 28 29 ManagedDisplayMode(const gfx::Size& size, 30 float refresh_rate, 31 bool is_interlaced, 32 bool native); 33 34 ManagedDisplayMode(const gfx::Size& size, 35 float refresh_rate, 36 bool is_interlaced, 37 bool native, 38 float device_scale_factor); 39 40 ~ManagedDisplayMode(); 41 ManagedDisplayMode(const ManagedDisplayMode& other); 42 ManagedDisplayMode& operator=(const ManagedDisplayMode& other); 43 bool operator==(const ManagedDisplayMode& other) const; 44 45 // Returns the size in DIP which is visible to the user. 46 gfx::Size GetSizeInDIP() const; 47 48 // Returns true if |other| has same size and scale factors. 49 bool IsEquivalent(const ManagedDisplayMode& other) const; 50 size()51 const gfx::Size& size() const { return size_; } is_interlaced()52 bool is_interlaced() const { return is_interlaced_; } refresh_rate()53 float refresh_rate() const { return refresh_rate_; } 54 native()55 bool native() const { return native_; } 56 57 // Missing from ui::ManagedDisplayMode device_scale_factor()58 float device_scale_factor() const { return device_scale_factor_; } 59 60 std::string ToString() const; 61 62 private: 63 gfx::Size size_; // Physical pixel size of the display. 64 float refresh_rate_ = 0.0f; // Refresh rate of the display, in Hz. 65 bool is_interlaced_ = false; // True if mode is interlaced. 66 bool native_ = false; // True if mode is native mode of the display. 67 float device_scale_factor_ = 1.0f; // The device scale factor of the mode. 68 }; 69 70 inline bool operator!=(const ManagedDisplayMode& lhs, 71 const ManagedDisplayMode& rhs) { 72 return !(lhs == rhs); 73 } 74 75 // ManagedDisplayInfo contains metadata for each display. This is used to create 76 // |Display| as well as to maintain extra infomation to manage displays in ash 77 // environment. This class is intentionally made copiable. 78 class DISPLAY_MANAGER_EXPORT ManagedDisplayInfo { 79 public: 80 using ManagedDisplayModeList = std::vector<ManagedDisplayMode>; 81 82 // Creates a ManagedDisplayInfo from string spec. 100+200-1440x800 creates 83 // display 84 // whose size is 1440x800 at the location (100, 200) in host coordinates. 85 // The format is 86 // 87 // [origin-]widthxheight[*device_scale_factor][#resolutions list] 88 // [/<properties>][@zoom-factor] 89 // 90 // where [] are optional: 91 // - |origin| is given in x+y- format. 92 // - |device_scale_factor| is either 2 or 1 (or empty). 93 // - properties can combination of 'o', which adds default overscan insets 94 // (5%), and one rotation property where 'r' is 90 degree clock-wise 95 // (to the 'r'ight) 'u' is 180 degrees ('u'pside-down) and 'l' is 96 // 270 degrees (to the 'l'eft). 97 // - zoom-factor is floating value, e.g. @1.5 or @1.25. 98 // - |resolution list| is the list of size that is given in 99 // |width x height [% refresh_rate]| separated by '|'. 100 // 101 // A couple of examples: 102 // "100x100" 103 // 100x100 window at 0,0 origin. 1x device scale factor. no overscan. 104 // no rotation. 1.0 zoom factor. 105 // "5+5-300x200*2" 106 // 300x200 window at 5,5 origin. 2x device scale factor. 107 // no overscan, no rotation. 1.0 zoom factor. 108 // "300x200/ol" 109 // 300x200 window at 0,0 origin. 1x device scale factor. 110 // with 5% overscan. rotated to left (90 degree counter clockwise). 111 // 1.0 zoom factor. 112 // "10+20-300x200/u@1.5" 113 // 300x200 window at 10,20 origin. 1x device scale factor. 114 // no overscan. flipped upside-down (180 degree) and 1.5 zoom factor. 115 // "200x100#300x200|200x100%59.0|100x100%60" 116 // 200x100 window at 0,0 origin, with 3 possible resolutions, 117 // 300x200, 200x100 at 59 Hz, and 100x100 at 60 Hz. 118 static ManagedDisplayInfo CreateFromSpec(const std::string& spec); 119 120 // Creates a ManagedDisplayInfo from string spec using given |id|. 121 static ManagedDisplayInfo CreateFromSpecWithID(const std::string& spec, 122 int64_t id); 123 124 ManagedDisplayInfo(); 125 ManagedDisplayInfo(int64_t id, const std::string& name, bool has_overscan); 126 ManagedDisplayInfo(const ManagedDisplayInfo& other); 127 ~ManagedDisplayInfo(); 128 id()129 int64_t id() const { return id_; } 130 131 // The name of the display. name()132 const std::string& name() const { return name_; } 133 134 // The path to the display device in the sysfs filesystem. set_sys_path(const base::FilePath & sys_path)135 void set_sys_path(const base::FilePath& sys_path) { sys_path_ = sys_path; } sys_path()136 const base::FilePath& sys_path() const { return sys_path_; } 137 138 // True if the display EDID has the overscan flag. This does not create the 139 // actual overscan automatically, but used in the message. has_overscan()140 bool has_overscan() const { return has_overscan_; } 141 set_touch_support(Display::TouchSupport support)142 void set_touch_support(Display::TouchSupport support) { 143 touch_support_ = support; 144 } touch_support()145 Display::TouchSupport touch_support() const { return touch_support_; } 146 147 // Gets/Sets the device scale factor of the display. 148 // TODO(oshima): Rename this to |default_device_scale_factor|. device_scale_factor()149 float device_scale_factor() const { return device_scale_factor_; } set_device_scale_factor(float scale)150 void set_device_scale_factor(float scale) { device_scale_factor_ = scale; } 151 zoom_factor()152 float zoom_factor() const { return zoom_factor_; } set_zoom_factor(float zoom_factor)153 void set_zoom_factor(float zoom_factor) { zoom_factor_ = zoom_factor; } 154 refresh_rate()155 float refresh_rate() const { return refresh_rate_; } set_refresh_rate(float refresh_rate)156 void set_refresh_rate(float refresh_rate) { refresh_rate_ = refresh_rate; } is_interlaced()157 bool is_interlaced() const { return is_interlaced_; } set_is_interlaced(bool is_interlaced)158 void set_is_interlaced(bool is_interlaced) { is_interlaced_ = is_interlaced; } 159 160 // Gets/Sets the device DPI of the display. device_dpi()161 float device_dpi() const { return device_dpi_; } set_device_dpi(float dpi)162 void set_device_dpi(float dpi) { device_dpi_ = dpi; } 163 panel_orientation()164 PanelOrientation panel_orientation() const { return panel_orientation_; } set_panel_orientation(PanelOrientation panel_orientation)165 void set_panel_orientation(PanelOrientation panel_orientation) { 166 panel_orientation_ = panel_orientation; 167 } 168 169 // The native bounds for the display. The size of this can be 170 // different from the |size_in_pixel| when overscan insets are set. bounds_in_native()171 const gfx::Rect& bounds_in_native() const { return bounds_in_native_; } 172 173 // The size for the display in pixels with the rotation taking into 174 // account. size_in_pixel()175 const gfx::Size& size_in_pixel() const { return size_in_pixel_; } 176 177 // The original size for the display in pixel, without rotation, but 178 // |panel_orientation_| taking into account. 179 gfx::Size GetSizeInPixelWithPanelOrientation() const; 180 181 // The overscan insets for the display in DIP. overscan_insets_in_dip()182 const gfx::Insets& overscan_insets_in_dip() const { 183 return overscan_insets_in_dip_; 184 } 185 186 // Sets the rotation for the given |source|. Setting a new rotation will also 187 // have it become the active rotation. 188 void SetRotation(Display::Rotation rotation, Display::RotationSource source); 189 190 // Returns the currently active rotation for this display. 191 Display::Rotation GetActiveRotation() const; 192 193 // Returns the currently active rotation for this display with the panel 194 // orientation adjustment applied. 195 Display::Rotation GetLogicalActiveRotation() const; 196 197 // Returns the source which set the active rotation for this display. active_rotation_source()198 Display::RotationSource active_rotation_source() const { 199 return active_rotation_source_; 200 } 201 202 // Returns the rotation set by a given |source|. 203 Display::Rotation GetRotation(Display::RotationSource source) const; 204 205 // Returns the ui scale and device scale factor actually used to create 206 // display that chrome sees. This is |device_scale_factor| x |zoom_factor_|. 207 // TODO(oshima): Rename to |GetDeviceScaleFactor()|. 208 float GetEffectiveDeviceScaleFactor() const; 209 210 // Copy the display info except for fields that can be modified by a user 211 // (|rotation_|). |rotation_| is copied when the |another_info| isn't native 212 // one. 213 void Copy(const ManagedDisplayInfo& another_info); 214 215 // Update the |bounds_in_native_| and |size_in_pixel_| using 216 // given |bounds_in_native|. 217 void SetBounds(const gfx::Rect& bounds_in_native); 218 219 // Update the |bounds_in_native| according to the current overscan 220 // and rotation settings. 221 void UpdateDisplaySize(); 222 223 // Sets/Clears the overscan insets. 224 void SetOverscanInsets(const gfx::Insets& insets_in_dip); 225 gfx::Insets GetOverscanInsetsInPixel() const; 226 227 // Sets/Gets the flag to clear overscan insets. clear_overscan_insets()228 bool clear_overscan_insets() const { return clear_overscan_insets_; } set_clear_overscan_insets(bool clear)229 void set_clear_overscan_insets(bool clear) { clear_overscan_insets_ = clear; } 230 set_native(bool native)231 void set_native(bool native) { native_ = native; } native()232 bool native() const { return native_; } 233 set_from_native_platform(bool from_native_platform)234 void set_from_native_platform(bool from_native_platform) { 235 from_native_platform_ = from_native_platform; 236 } from_native_platform()237 bool from_native_platform() const { return from_native_platform_; } 238 display_modes()239 const ManagedDisplayModeList& display_modes() const { return display_modes_; } 240 // Sets the display mode list. The mode list will be sorted for the 241 // display. 242 void SetManagedDisplayModes(const ManagedDisplayModeList& display_modes); 243 244 // Returns the native mode size. If a native mode is not present, return an 245 // empty size. 246 gfx::Size GetNativeModeSize() const; 247 display_color_spaces()248 const gfx::DisplayColorSpaces& display_color_spaces() const { 249 return display_color_spaces_; 250 } set_display_color_spaces(const gfx::DisplayColorSpaces & display_color_spaces)251 void set_display_color_spaces( 252 const gfx::DisplayColorSpaces& display_color_spaces) { 253 display_color_spaces_ = display_color_spaces; 254 } 255 bits_per_channel()256 uint32_t bits_per_channel() const { return bits_per_channel_; } set_bits_per_channel(uint32_t bits_per_channel)257 void set_bits_per_channel(uint32_t bits_per_channel) { 258 bits_per_channel_ = bits_per_channel; 259 } 260 is_aspect_preserving_scaling()261 bool is_aspect_preserving_scaling() const { 262 return is_aspect_preserving_scaling_; 263 } 264 set_is_aspect_preserving_scaling(bool value)265 void set_is_aspect_preserving_scaling(bool value) { 266 is_aspect_preserving_scaling_ = value; 267 } 268 269 // Maximum cursor size in native pixels. maximum_cursor_size()270 const gfx::Size& maximum_cursor_size() const { return maximum_cursor_size_; } set_maximum_cursor_size(const gfx::Size & size)271 void set_maximum_cursor_size(const gfx::Size& size) { 272 maximum_cursor_size_ = size; 273 } 274 manufacturer_id()275 const std::string& manufacturer_id() const { return manufacturer_id_; } set_manufacturer_id(const std::string & id)276 void set_manufacturer_id(const std::string& id) { manufacturer_id_ = id; } 277 product_id()278 const std::string& product_id() const { return product_id_; } set_product_id(const std::string & id)279 void set_product_id(const std::string& id) { product_id_ = id; } 280 year_of_manufacture()281 int32_t year_of_manufacture() const { return year_of_manufacture_; } set_year_of_manufacture(int32_t year)282 void set_year_of_manufacture(int32_t year) { year_of_manufacture_ = year; } 283 284 // Returns a string representation of the ManagedDisplayInfo, excluding 285 // display modes. 286 std::string ToString() const; 287 288 // Returns a string representation of the ManagedDisplayInfo, including 289 // display modes. 290 std::string ToFullString() const; 291 292 private: 293 // Return the rotation with the panel orientation applied. 294 Display::Rotation GetRotationWithPanelOrientation( 295 Display::Rotation rotation) const; 296 297 int64_t id_; 298 std::string name_; 299 std::string manufacturer_id_; 300 std::string product_id_; 301 int32_t year_of_manufacture_; 302 base::FilePath sys_path_; 303 bool has_overscan_; 304 std::map<Display::RotationSource, Display::Rotation> rotations_; 305 Display::RotationSource active_rotation_source_; 306 Display::TouchSupport touch_support_; 307 308 // This specifies the device's pixel density. (For example, a display whose 309 // DPI is higher than the threshold is considered to have device_scale_factor 310 // = 2.0 on Chrome OS). This is used by the graphics layer to choose and draw 311 // appropriate images and scale layers properly. 312 float device_scale_factor_; 313 gfx::Rect bounds_in_native_; 314 315 // This specifies the device's DPI. 316 float device_dpi_; 317 318 // Orientation of the panel relative to natural device orientation. 319 display::PanelOrientation panel_orientation_; 320 321 // The size of the display in use. The size can be different from the size 322 // of |bounds_in_native_| if the display has overscan insets and/or rotation. 323 gfx::Size size_in_pixel_; 324 // TODO(oshima): Change this to store pixel. 325 gfx::Insets overscan_insets_in_dip_; 326 327 // The zoom level currently applied to the display. This value is appended 328 // multiplicatively to the device scale factor to get the effecting scaling 329 // for a display. 330 float zoom_factor_; 331 332 // The value of the current display mode refresh rate. 333 float refresh_rate_; 334 335 // True if the current display mode is interlaced (i.e. the display's odd 336 // and even lines are scanned alternately in two interwoven rasterized lines). 337 bool is_interlaced_; 338 339 // True if this comes from native platform (DisplayChangeObserver). 340 bool from_native_platform_; 341 342 // True if current mode is native mode of the display. 343 bool native_; 344 345 // True if the display is configured to preserve the aspect ratio. When the 346 // display is configured in a non-native mode, only parts of the display will 347 // be used such that the aspect ratio is preserved. 348 bool is_aspect_preserving_scaling_; 349 350 // True if the displays' overscan inset should be cleared. This is 351 // to distinguish the empty overscan insets from native display info. 352 bool clear_overscan_insets_; 353 354 // The list of modes supported by this display. 355 ManagedDisplayModeList display_modes_; 356 357 // Maximum cursor size. 358 gfx::Size maximum_cursor_size_; 359 360 // Colorimetry information of the Display. 361 gfx::DisplayColorSpaces display_color_spaces_; 362 363 // Bit depth of every channel, extracted from its EDID, usually 8, but can be 364 // 0 if EDID says so or if the EDID (retrieval) was faulty. 365 uint32_t bits_per_channel_; 366 367 // If you add a new member, you need to update Copy(). 368 }; 369 370 // Resets the synthesized display id for testing. This 371 // is necessary to avoid overflowing the output index. 372 void DISPLAY_MANAGER_EXPORT ResetDisplayIdForTest(); 373 374 // Generates a fake, synthesized display ID that will be used when the 375 // |kInvalidDisplayId| is passed to |ManagedDisplayInfo| constructor. 376 int64_t DISPLAY_MANAGER_EXPORT GetNextSynthesizedDisplayId(int64_t id); 377 378 } // namespace display 379 380 #endif // UI_DISPLAY_MANAGER_MANAGED_DISPLAY_INFO_H_ 381