display_info.cc 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#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/string_number_conversions.h"
12#include "base/string_util.h"
13#include "base/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
25// satic
26DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
27  return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
28}
29
30// static
31DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
32                                              int64 id) {
33  // Default bounds for a display.
34  const int kDefaultHostWindowX = 200;
35  const int kDefaultHostWindowY = 200;
36  const int kDefaultHostWindowWidth = 1366;
37  const int kDefaultHostWindowHeight = 768;
38
39  static int64 synthesized_display_id = 1000;
40
41#if defined(OS_WIN)
42  gfx::Rect bounds(aura::RootWindowHost::GetNativeScreenSize());
43#else
44  gfx::Rect bounds(kDefaultHostWindowX, kDefaultHostWindowY,
45                   kDefaultHostWindowWidth, kDefaultHostWindowHeight);
46#endif
47  std::string main_spec = spec;
48
49  float ui_scale = 1.0f;
50  std::vector<std::string> parts;
51  if (Tokenize(main_spec, "@", &parts) == 2) {
52    double scale_in_double = 0;
53    if (base::StringToDouble(parts[1], &scale_in_double))
54      ui_scale = scale_in_double;
55    main_spec = parts[0];
56  }
57
58  size_t count = Tokenize(main_spec, "/", &parts);
59  gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
60  bool has_overscan = false;
61  if (count) {
62    main_spec = parts[0];
63    if (count >= 2) {
64      std::string options = parts[1];
65      for (size_t i = 0; i < options.size(); ++i) {
66        char c = options[i];
67        switch (c) {
68          case 'o':
69            has_overscan = true;
70            break;
71          case 'r':  // rotate 90 degrees to 'right'.
72            rotation = gfx::Display::ROTATE_90;
73            break;
74          case 'u':  // 180 degrees, 'u'pside-down.
75            rotation = gfx::Display::ROTATE_180;
76            break;
77          case 'l':  // rotate 90 degrees to 'left'.
78            rotation = gfx::Display::ROTATE_270;
79            break;
80        }
81      }
82    }
83  }
84
85  int x = 0, y = 0, width, height;
86  float device_scale_factor = 1.0f;
87  if (sscanf(main_spec.c_str(), "%dx%d*%f",
88             &width, &height, &device_scale_factor) >= 2 ||
89      sscanf(main_spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
90             &device_scale_factor) >= 4) {
91    bounds.SetRect(x, y, width, height);
92  }
93  if (id == gfx::Display::kInvalidDisplayID)
94    id = synthesized_display_id++;
95  DisplayInfo display_info(
96      id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
97  display_info.set_device_scale_factor(device_scale_factor);
98  display_info.set_rotation(rotation);
99  display_info.set_ui_scale(ui_scale);
100  display_info.SetBounds(bounds);
101  DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
102           << ", spec=" << spec;
103  return display_info;
104}
105
106DisplayInfo::DisplayInfo()
107    : id_(gfx::Display::kInvalidDisplayID),
108      has_overscan_(false),
109      rotation_(gfx::Display::ROTATE_0),
110      device_scale_factor_(1.0f),
111      overscan_insets_in_dip_(0, 0, 0, 0),
112      has_custom_overscan_insets_(false),
113      ui_scale_(1.0f),
114      native_(false) {
115}
116
117DisplayInfo::DisplayInfo(int64 id,
118                         const std::string& name,
119                         bool has_overscan)
120    : id_(id),
121      name_(name),
122      has_overscan_(has_overscan),
123      rotation_(gfx::Display::ROTATE_0),
124      device_scale_factor_(1.0f),
125      overscan_insets_in_dip_(0, 0, 0, 0),
126      has_custom_overscan_insets_(false),
127      ui_scale_(1.0f),
128      native_(false) {
129}
130
131DisplayInfo::~DisplayInfo() {
132}
133
134void DisplayInfo::Copy(const DisplayInfo& native_info) {
135  DCHECK(id_ == native_info.id_);
136  name_ = native_info.name_;
137  has_overscan_ = native_info.has_overscan_;
138
139  DCHECK(!native_info.bounds_in_pixel_.IsEmpty());
140  bounds_in_pixel_ = native_info.bounds_in_pixel_;
141  size_in_pixel_ = native_info.size_in_pixel_;
142  device_scale_factor_ = native_info.device_scale_factor_;
143
144  // Rotation_ and ui_scale_ are given by preference, or unit
145  // tests. Don't copy if this native_info came from
146  // DisplayChangeObserverX11.
147  if (!native_info.native()) {
148    rotation_ = native_info.rotation_;
149    ui_scale_ = native_info.ui_scale_;
150  }
151  // It makes little sense to scale beyond the original
152  // resolution. This guard is to protect applying
153  // ui_scale to an external display whose DPI has changed
154  // from 2.0 to 1.0 for some reason.
155  if (ui_scale_ > device_scale_factor_)
156    ui_scale_ = 1.0f;
157
158  // Don't copy insets as it may be given by preference.  |rotation_|
159  // is treated as a native so that it can be specified in
160  // |CreateFromSpec|.
161}
162
163void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_pixel) {
164  bounds_in_pixel_ = new_bounds_in_pixel;
165  size_in_pixel_ = new_bounds_in_pixel.size();
166  UpdateDisplaySize();
167}
168
169void DisplayInfo::UpdateDisplaySize() {
170  size_in_pixel_ = bounds_in_pixel_.size();
171  if (has_custom_overscan_insets_) {
172    gfx::Insets insets_in_pixel =
173        overscan_insets_in_dip_.Scale(device_scale_factor_);
174    size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
175  } else if (has_overscan_) {
176    // Currently we assume 5% overscan and hope for the best if TV claims it
177    // overscan, but doesn't expose how much.
178    // TODO(oshima): The insets has to be applied after rotation.
179    // Fix this.
180    int width = bounds_in_pixel_.width() / 40;
181    int height = bounds_in_pixel_.height() / 40;
182    gfx::Insets insets_in_pixel(height, width, height, width);
183    overscan_insets_in_dip_ =
184        insets_in_pixel.Scale(1.0 / device_scale_factor_);
185    size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
186  } else {
187    overscan_insets_in_dip_.Set(0, 0, 0, 0);
188  }
189
190  if (rotation_ == gfx::Display::ROTATE_90 ||
191      rotation_ == gfx::Display::ROTATE_270)
192    size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
193  gfx::SizeF size_f(size_in_pixel_);
194  size_f.Scale(ui_scale_);
195  size_in_pixel_ = gfx::ToFlooredSize(size_f);
196}
197
198void DisplayInfo::SetOverscanInsets(bool custom,
199                                    const gfx::Insets& insets_in_dip) {
200  has_custom_overscan_insets_ = custom;
201  overscan_insets_in_dip_ = insets_in_dip;
202}
203
204gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
205  return overscan_insets_in_dip_.Scale(device_scale_factor_);
206}
207
208std::string DisplayInfo::ToString() const {
209  int rotation_degree = static_cast<int>(rotation_) * 90;
210  return base::StringPrintf(
211      "DisplayInfo[%lld] bounds=%s, size=%s, scale=%f, "
212      "overscan=%s, rotation=%d, ui-scale=%f",
213      static_cast<long long int>(id_),
214      bounds_in_pixel_.ToString().c_str(),
215      size_in_pixel_.ToString().c_str(),
216      device_scale_factor_,
217      overscan_insets_in_dip_.ToString().c_str(),
218      rotation_degree,
219      ui_scale_);
220}
221
222}  // namespace internal
223}  // namespace ash
224