display_info.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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][#resolutions list]
40  //     [/<properties>][@ui-scale]
41  //
42  // where [] are optional:
43  // - |origin| is given in x+y- format.
44  // - |device_scale_factor| is either 2 or 1 (or empty).
45  // - properties can combination of 'o', which adds default overscan insets
46  //   (5%), and one rotation property where 'r' is 90 degree clock-wise
47  //   (to the 'r'ight) 'u' is 180 degrees ('u'pside-down) and 'l' is
48  //   270 degrees (to the 'l'eft).
49  // - ui-scale is floating value, e.g. @1.5 or @1.25.
50  // - |resolution list| is the list of size that is given in
51  //   |width x height| separated by '|'.
52  //
53  // A couple of examples:
54  // "100x100"
55  //      100x100 window at 0,0 origin. 1x device scale factor. no overscan.
56  //      no rotation. 1.0 ui scale.
57  // "5+5-300x200*2"
58  //      300x200 window at 5,5 origin. 2x device scale factor.
59  //      no overscan, no rotation. 1.0 ui scale.
60  // "300x200/ol"
61  //      300x200 window at 0,0 origin. 1x device scale factor.
62  //      with 5% overscan. rotated to left (90 degree counter clockwise).
63  //      1.0 ui scale.
64  // "10+20-300x200/u@1.5"
65  //      300x200 window at 10,20 origin. 1x device scale factor.
66  //      no overscan. flipped upside-down (180 degree) and 1.5 ui scale.
67  // "200x100#300x200|200x100|100x100"
68  //      200x100 window at 0,0 origin, with 3 possible resolutions,
69  //      300x200, 200x100 and 100x100.
70  static DisplayInfo CreateFromSpec(const std::string& spec);
71
72  // Creates a DisplayInfo from string spec using given |id|.
73  static DisplayInfo CreateFromSpecWithID(const std::string& spec,
74                                          int64 id);
75
76  DisplayInfo();
77  DisplayInfo(int64 id, const std::string& name, bool has_overscan);
78  ~DisplayInfo();
79
80  int64 id() const { return id_; }
81
82  // The name of the display.
83  const std::string& name() const { return name_; }
84
85  // True if the display EDID has the overscan flag. This does not create the
86  // actual overscan automatically, but used in the message.
87  bool has_overscan() const { return has_overscan_; }
88
89  void set_rotation(gfx::Display::Rotation rotation) { rotation_ = rotation; }
90  gfx::Display::Rotation rotation() const { return rotation_; }
91
92  // Gets/Sets the device scale factor of the display.
93  float device_scale_factor() const { return device_scale_factor_; }
94  void set_device_scale_factor(float scale) { device_scale_factor_ = scale; }
95
96  // The native bounds for the display. The size of this can be different from
97  // the |size_in_pixel| when overscan insets are set and/or |ui_scale_| is set.
98  const gfx::Rect bounds_in_native() const {
99    return bounds_in_native_;
100  }
101
102  // The size for the display in pixels.
103  const gfx::Size& size_in_pixel() const { return size_in_pixel_; }
104
105  // The overscan insets for the display in DIP.
106  const gfx::Insets& overscan_insets_in_dip() const {
107    return overscan_insets_in_dip_;
108  }
109
110  float ui_scale() const { return ui_scale_; }
111  void set_ui_scale(float scale) { ui_scale_ = scale; }
112
113  // Copy the display info except for fields that can be modified by a user
114  // (|rotation_| and |ui_scale_|). |rotation_| and |ui_scale_| are copied
115  // when the |another_info| isn't native one.
116  void Copy(const DisplayInfo& another_info);
117
118  // Update the |bounds_in_native_| and |size_in_pixel_| using
119  // given |bounds_in_native|.
120  void SetBounds(const gfx::Rect& bounds_in_native);
121
122  // Update the |bounds_in_native| according to the current overscan
123  // and rotation settings.
124  void UpdateDisplaySize();
125
126  // Sets/Clears the overscan insets.
127  void SetOverscanInsets(const gfx::Insets& insets_in_dip);
128  gfx::Insets GetOverscanInsetsInPixel() const;
129
130  void set_native(bool native) { native_ = native; }
131  bool native() const { return native_; }
132
133  const std::vector<Resolution>& resolutions() const {
134    return resolutions_;
135  }
136  void set_resolutions(std::vector<Resolution>& resolution) {
137    resolutions_.swap(resolution);
138  }
139
140  // Returns a string representation of the DisplayInfo
141  // excluding resolutions.
142  std::string ToString() const;
143
144  // Returns a string representation of the DisplayInfo
145  // including resolutions.
146  std::string ToFullString() const;
147
148 private:
149  int64 id_;
150  std::string name_;
151  bool has_overscan_;
152  gfx::Display::Rotation rotation_;
153
154  // This specifies the device's pixel density. (For example, a
155  // display whose DPI is higher than the threshold is considered to have
156  // device_scale_factor = 2.0 on Chrome OS).  This is used by the
157  // grapics layer to choose and draw appropriate images and scale
158  // layers properly.
159  float device_scale_factor_;
160  gfx::Rect bounds_in_native_;
161
162  // The size of the display in use. The size can be different from the size
163  // of |bounds_in_native_| if the display has overscan insets and/or rotation.
164  gfx::Size size_in_pixel_;
165  gfx::Insets overscan_insets_in_dip_;
166
167  // The pixel scale of the display. This is used to simply expand (or
168  // shrink) the desktop over the native display resolution (useful in
169  // HighDPI display).  Note that this should not be confused with the
170  // device scale factor, which specifies the pixel density of the
171  // display.
172  float ui_scale_;
173
174  // True if this comes from native platform (DisplayChangeObserver).
175  bool native_;
176
177  // The list of resolutions supported by this display.
178  std::vector<Resolution> resolutions_;
179};
180
181}  // namespace internal
182}  // namespace ash
183
184#endif  //  ASH_DISPLAY_DISPLAY_INFO_H_
185