display_info.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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 ASH_DISPLAY_DISPLAY_INFO_H_
6#define ASH_DISPLAY_DISPLAY_INFO_H_
7
8#include <string>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "base/gtest_prod_util.h"
13#include "ui/gfx/display.h"
14#include "ui/gfx/insets.h"
15#include "ui/gfx/rect.h"
16
17namespace ash {
18namespace internal {
19
20// A struct that represents the display's resolution and
21// interlaced info.
22struct ASH_EXPORT Resolution {
23  Resolution(const gfx::Size& size, bool interlaced);
24
25  gfx::Size size;
26  bool interlaced;
27};
28
29// DisplayInfo contains metadata for each display. This is used to
30// create |gfx::Display| as well as to maintain extra infomation
31// to manage displays in ash environment.
32// This class is intentionally made copiable.
33class ASH_EXPORT DisplayInfo {
34 public:
35  // Creates a DisplayInfo from string spec. 100+200-1440x800 creates display
36  // whose size is 1440x800 at the location (100, 200) in host coordinates.
37  // The format is
38  //
39  // [origin-]widthxheight[*device_scale_factor][/<properties>][@ui-scale]
40  //
41  // where [] are optional:
42  // - |origin| is given in x+y- format.
43  // - |device_scale_factor| is either 2 or 1 (or empty).
44  // - properties can combination of 'o', which adds default overscan insets
45  //   (5%), and one rotation property where 'r' is 90 degree clock-wise
46  //   (to the 'r'ight) 'u' is 180 degrees ('u'pside-down) and 'l' is
47  //   270 degrees (to the 'l'eft).
48  // - ui-scale is floating value, e.g. @1.5 or @1.25.
49  //
50  // A couple of examples:
51  // "100x100"
52  //      100x100 window at 0,0 origin. 1x device scale factor. no overscan.
53  //      no rotation. 1.0 ui scale.
54  // "5+5-300x200*2"
55  //      300x200 window at 5,5 origin. 2x device scale factor.
56  //      no overscan, no rotation. 1.0 ui scale.
57  // "300x200/ol"
58  //      300x200 window at 0,0 origin. 1x device scale factor.
59  //      with 5% overscan. rotated to left (90 degree counter clockwise).
60  //      1.0 ui scale.
61  // "10+20-300x200/u@1.5"
62  //      300x200 window at 10,20 origin. 1x device scale factor.
63  //      no overscan. flipped upside-down (180 degree) and 1.5 ui scale.
64  static DisplayInfo CreateFromSpec(const std::string& spec);
65
66  // Creates a DisplayInfo from string spec using given |id|.
67  static DisplayInfo CreateFromSpecWithID(const std::string& spec,
68                                          int64 id);
69
70  DisplayInfo();
71  DisplayInfo(int64 id, const std::string& name, bool has_overscan);
72  ~DisplayInfo();
73
74  int64 id() const { return id_; }
75
76  // The name of the display.
77  const std::string& name() const { return name_; }
78
79  // True if the display EDID has the overscan flag. This does not create the
80  // actual overscan automatically, but used in the message.
81  bool has_overscan() const { return has_overscan_; }
82
83  void set_rotation(gfx::Display::Rotation rotation) { rotation_ = rotation; }
84  gfx::Display::Rotation rotation() const { return rotation_; }
85
86  // Gets/Sets the device scale factor of the display.
87  float device_scale_factor() const { return device_scale_factor_; }
88  void set_device_scale_factor(float scale) { device_scale_factor_ = scale; }
89
90  // The bounds_in_pixel for the display. The size of this can be different from
91  // the |size_in_pixel| in case of overscan insets.
92  const gfx::Rect bounds_in_pixel() const {
93    return bounds_in_pixel_;
94  }
95
96  // The size for the display in pixels.
97  const gfx::Size& size_in_pixel() const { return size_in_pixel_; }
98
99  // The overscan insets for the display in DIP.
100  const gfx::Insets& overscan_insets_in_dip() const {
101    return overscan_insets_in_dip_;
102  }
103
104  float ui_scale() const { return ui_scale_; }
105  void set_ui_scale(float scale) { ui_scale_ = scale; }
106
107  // Copy the display info except for fields that can be modified by a user
108  // (|rotation_| and |ui_scale_|). |rotation_| and |ui_scale_| are copied
109  // when the |another_info| isn't native one.
110  void Copy(const DisplayInfo& another_info);
111
112  // Update the |bounds_in_pixel_| and |size_in_pixel_| using
113  // given |bounds_in_pixel|.
114  void SetBounds(const gfx::Rect& bounds_in_pixel);
115
116  // Update the |bounds_in_pixel| according to the current overscan
117  // and rotation settings.
118  void UpdateDisplaySize();
119
120  // Sets/Clears the overscan insets.
121  void SetOverscanInsets(const gfx::Insets& insets_in_dip);
122  gfx::Insets GetOverscanInsetsInPixel() const;
123
124  void set_native(bool native) { native_ = native; }
125  bool native() const { return native_; }
126
127  const std::vector<Resolution>& resolutions() const {
128    return resolutions_;
129  }
130  void set_resolutions(std::vector<Resolution>& resolution) {
131    resolutions_.swap(resolution);
132  }
133
134  // Returns a string representation of the DisplayInfo
135  // excluding resolutions.
136  std::string ToString() const;
137
138  // Returns a string representation of the DisplayInfo
139  // including resolutions.
140  std::string ToFullString() const;
141
142 private:
143  int64 id_;
144  std::string name_;
145  bool has_overscan_;
146  gfx::Display::Rotation rotation_;
147  float device_scale_factor_;
148  gfx::Rect bounds_in_pixel_;
149  // The size of the display in use. The size can be different from the size
150  // of |bounds_in_pixel_| if the display has overscan insets and/or rotation.
151  gfx::Size size_in_pixel_;
152  gfx::Insets overscan_insets_in_dip_;
153
154  // UI scale of the display.
155  float ui_scale_;
156
157  // True if this comes from native platform (DisplayChangeObserverX11).
158  bool native_;
159
160  // The list of resolutions supported by this display.
161  std::vector<Resolution> resolutions_;
162};
163
164}  // namespace internal
165}  // namespace ash
166
167#endif  //  ASH_DISPLAY_DISPLAY_INFO_H_
168