display_info.cc 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#include <stdio.h>
6#include <string>
7#include <vector>
8
9#include "ash/display/display_info.h"
10#include "base/logging.h"
11#include "base/strings/string_number_conversions.h"
12#include "base/strings/string_util.h"
13#include "base/strings/stringprintf.h"
14#include "ui/gfx/display.h"
15#include "ui/gfx/size_conversions.h"
16#include "ui/gfx/size_f.h"
17
18#if defined(OS_WIN)
19#include "ui/aura/root_window_host.h"
20#endif
21
22namespace ash {
23namespace internal {
24
25Resolution::Resolution(const gfx::Size& size, bool interlaced)
26    : size(size),
27      interlaced(interlaced) {
28}
29
30// satic
31DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
32  return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
33}
34
35// static
36DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
37                                              int64 id) {
38  // Default bounds for a display.
39  const int kDefaultHostWindowX = 200;
40  const int kDefaultHostWindowY = 200;
41  const int kDefaultHostWindowWidth = 1366;
42  const int kDefaultHostWindowHeight = 768;
43
44  // Use larger than max int to catch overflow early.
45  static int64 synthesized_display_id = 2200000000LL;
46
47#if defined(OS_WIN)
48  gfx::Rect bounds_in_native(aura::RootWindowHost::GetNativeScreenSize());
49#else
50  gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
51                             kDefaultHostWindowWidth, kDefaultHostWindowHeight);
52#endif
53  std::string main_spec = spec;
54
55  float ui_scale = 1.0f;
56  std::vector<std::string> parts;
57  if (Tokenize(main_spec, "@", &parts) == 2) {
58    double scale_in_double = 0;
59    if (base::StringToDouble(parts[1], &scale_in_double))
60      ui_scale = scale_in_double;
61    main_spec = parts[0];
62  }
63
64  size_t count = Tokenize(main_spec, "/", &parts);
65  gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
66  bool has_overscan = false;
67  if (count) {
68    main_spec = parts[0];
69    if (count >= 2) {
70      std::string options = parts[1];
71      for (size_t i = 0; i < options.size(); ++i) {
72        char c = options[i];
73        switch (c) {
74          case 'o':
75            has_overscan = true;
76            break;
77          case 'r':  // rotate 90 degrees to 'right'.
78            rotation = gfx::Display::ROTATE_90;
79            break;
80          case 'u':  // 180 degrees, 'u'pside-down.
81            rotation = gfx::Display::ROTATE_180;
82            break;
83          case 'l':  // rotate 90 degrees to 'left'.
84            rotation = gfx::Display::ROTATE_270;
85            break;
86        }
87      }
88    }
89  }
90
91  int x = 0, y = 0, width, height;
92  float device_scale_factor = 1.0f;
93  if (sscanf(main_spec.c_str(), "%dx%d*%f",
94             &width, &height, &device_scale_factor) >= 2 ||
95      sscanf(main_spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
96             &device_scale_factor) >= 4) {
97    bounds_in_native.SetRect(x, y, width, height);
98  }
99
100  std::vector<Resolution> resolutions;
101  if (Tokenize(main_spec, "#", &parts) == 2) {
102    main_spec = parts[0];
103    std::string resolution_list = parts[1];
104    count = Tokenize(resolution_list, "|", &parts);
105    for (size_t i = 0; i < count; ++i) {
106      std::string resolution = parts[i];
107      int width, height;
108      if (sscanf(resolution.c_str(), "%dx%d", &width, &height) == 2)
109        resolutions.push_back(Resolution(gfx::Size(width, height), false));
110    }
111  }
112
113  if (id == gfx::Display::kInvalidDisplayID)
114    id = synthesized_display_id++;
115  DisplayInfo display_info(
116      id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
117  display_info.set_device_scale_factor(device_scale_factor);
118  display_info.set_rotation(rotation);
119  display_info.set_ui_scale(ui_scale);
120  display_info.SetBounds(bounds_in_native);
121  display_info.set_resolutions(resolutions);
122
123  // To test the overscan, it creates the default 5% overscan.
124  if (has_overscan) {
125    int width = bounds_in_native.width() / device_scale_factor / 40;
126    int height = bounds_in_native.height() / device_scale_factor / 40;
127    display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
128    display_info.UpdateDisplaySize();
129  }
130
131  DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
132           << ", spec=" << spec;
133  return display_info;
134}
135
136DisplayInfo::DisplayInfo()
137    : id_(gfx::Display::kInvalidDisplayID),
138      has_overscan_(false),
139      rotation_(gfx::Display::ROTATE_0),
140      device_scale_factor_(1.0f),
141      overscan_insets_in_dip_(0, 0, 0, 0),
142      ui_scale_(1.0f),
143      native_(false) {
144}
145
146DisplayInfo::DisplayInfo(int64 id,
147                         const std::string& name,
148                         bool has_overscan)
149    : id_(id),
150      name_(name),
151      has_overscan_(has_overscan),
152      rotation_(gfx::Display::ROTATE_0),
153      device_scale_factor_(1.0f),
154      overscan_insets_in_dip_(0, 0, 0, 0),
155      ui_scale_(1.0f),
156      native_(false) {
157}
158
159DisplayInfo::~DisplayInfo() {
160}
161
162void DisplayInfo::Copy(const DisplayInfo& native_info) {
163  DCHECK(id_ == native_info.id_);
164  name_ = native_info.name_;
165  has_overscan_ = native_info.has_overscan_;
166
167  DCHECK(!native_info.bounds_in_native_.IsEmpty());
168  bounds_in_native_ = native_info.bounds_in_native_;
169  size_in_pixel_ = native_info.size_in_pixel_;
170  device_scale_factor_ = native_info.device_scale_factor_;
171  resolutions_ = native_info.resolutions_;
172
173  // Copy overscan_insets_in_dip_ if it's not empty. This is for test
174  // cases which use "/o" annotation which sets the overscan inset
175  // to native, and that overscan has to be propagated. This does not
176  // happen on the real environment.
177  if (!native_info.overscan_insets_in_dip_.empty())
178    overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
179
180  // Rotation_ and ui_scale_ are given by preference, or unit
181  // tests. Don't copy if this native_info came from
182  // DisplayChangeObserver.
183  if (!native_info.native()) {
184    rotation_ = native_info.rotation_;
185    ui_scale_ = native_info.ui_scale_;
186  }
187  // Don't copy insets as it may be given by preference.  |rotation_|
188  // is treated as a native so that it can be specified in
189  // |CreateFromSpec|.
190}
191
192void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
193  bounds_in_native_ = new_bounds_in_native;
194  size_in_pixel_ = new_bounds_in_native.size();
195  UpdateDisplaySize();
196}
197
198void DisplayInfo::UpdateDisplaySize() {
199  size_in_pixel_ = bounds_in_native_.size();
200  if (!overscan_insets_in_dip_.empty()) {
201    gfx::Insets insets_in_pixel =
202        overscan_insets_in_dip_.Scale(device_scale_factor_);
203    size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
204  } else {
205    overscan_insets_in_dip_.Set(0, 0, 0, 0);
206  }
207
208  if (rotation_ == gfx::Display::ROTATE_90 ||
209      rotation_ == gfx::Display::ROTATE_270)
210    size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
211  gfx::SizeF size_f(size_in_pixel_);
212  size_f.Scale(ui_scale_);
213  size_in_pixel_ = gfx::ToFlooredSize(size_f);
214}
215
216void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
217  overscan_insets_in_dip_ = insets_in_dip;
218}
219
220gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
221  return overscan_insets_in_dip_.Scale(device_scale_factor_);
222}
223
224std::string DisplayInfo::ToString() const {
225  int rotation_degree = static_cast<int>(rotation_) * 90;
226  return base::StringPrintf(
227      "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
228      "overscan=%s, rotation=%d, ui-scale=%f",
229      static_cast<long long int>(id_),
230      bounds_in_native_.ToString().c_str(),
231      size_in_pixel_.ToString().c_str(),
232      device_scale_factor_,
233      overscan_insets_in_dip_.ToString().c_str(),
234      rotation_degree,
235      ui_scale_);
236}
237
238std::string DisplayInfo::ToFullString() const {
239  std::string resolutions_str;
240  std::vector<Resolution>::const_iterator iter = resolutions_.begin();
241  for (; iter != resolutions_.end(); ++iter) {
242    if (!resolutions_str.empty())
243      resolutions_str += ",";
244    resolutions_str += iter->size.ToString();
245    if (iter->interlaced)
246      resolutions_str += "(i)";
247  }
248  return ToString() + ", resolutions=" + resolutions_str;
249}
250
251}  // namespace internal
252}  // namespace ash
253