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