61432f60c14b8b54e7a8c980c4d07dab3bbafbd0
[platform/framework/web/crosswalk.git] / src / ash / display / display_info.cc
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/window_tree_host.h"
20 #include "ui/gfx/win/dpi.h"
21 #endif
22
23 namespace ash {
24 namespace {
25
26 bool use_125_dsf_for_ui_scaling = false;
27
28 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
29 // Returns true when |bounds| is found.
30 bool GetDisplayBounds(
31     const std::string& spec, gfx::Rect* bounds, float* device_scale_factor) {
32   int width = 0;
33   int height = 0;
34   int x = 0;
35   int y = 0;
36   if (sscanf(spec.c_str(), "%dx%d*%f",
37              &width, &height, device_scale_factor) >= 2 ||
38       sscanf(spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
39              device_scale_factor) >= 4) {
40     bounds->SetRect(x, y, width, height);
41     return true;
42   }
43   return false;
44 }
45
46 }  // namespace
47
48 DisplayMode::DisplayMode()
49     : refresh_rate(0.0f),
50       interlaced(false),
51       native(false),
52       ui_scale(1.0f),
53       device_scale_factor(1.0f) {}
54
55 DisplayMode::DisplayMode(const gfx::Size& size,
56                          float refresh_rate,
57                          bool interlaced,
58                          bool native)
59     : size(size),
60       refresh_rate(refresh_rate),
61       interlaced(interlaced),
62       native(native),
63       ui_scale(1.0f),
64       device_scale_factor(1.0f) {}
65
66 gfx::Size DisplayMode::GetSizeInDIP() const {
67   gfx::SizeF size_dip(size);
68   size_dip.Scale(ui_scale);
69   size_dip.Scale(1.0f / device_scale_factor);
70   return gfx::ToFlooredSize(size_dip);
71 }
72
73 bool DisplayMode::IsEquivalent(const DisplayMode& other) const {
74   const float kEpsilon = 0.0001f;
75   return size == other.size &&
76       std::abs(ui_scale - other.ui_scale) < kEpsilon &&
77       std::abs(device_scale_factor - other.device_scale_factor) < kEpsilon;
78 }
79
80 // satic
81 DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
82   return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
83 }
84
85 // static
86
87 // static
88 void DisplayInfo::SetUse125DSFForUIScaling(bool enable) {
89   use_125_dsf_for_ui_scaling = enable;
90 }
91
92 // static
93 DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
94                                               int64 id) {
95   // Default bounds for a display.
96   const int kDefaultHostWindowX = 200;
97   const int kDefaultHostWindowY = 200;
98   const int kDefaultHostWindowWidth = 1366;
99   const int kDefaultHostWindowHeight = 768;
100
101   // Use larger than max int to catch overflow early.
102   static int64 synthesized_display_id = 2200000000LL;
103
104 #if defined(OS_WIN)
105   gfx::Rect bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
106 #else
107   gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
108                              kDefaultHostWindowWidth, kDefaultHostWindowHeight);
109 #endif
110   std::string main_spec = spec;
111
112   float ui_scale = 1.0f;
113   std::vector<std::string> parts;
114   if (Tokenize(main_spec, "@", &parts) == 2) {
115     double scale_in_double = 0;
116     if (base::StringToDouble(parts[1], &scale_in_double))
117       ui_scale = scale_in_double;
118     main_spec = parts[0];
119   }
120
121   size_t count = Tokenize(main_spec, "/", &parts);
122   gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
123   bool has_overscan = false;
124   if (count) {
125     main_spec = parts[0];
126     if (count >= 2) {
127       std::string options = parts[1];
128       for (size_t i = 0; i < options.size(); ++i) {
129         char c = options[i];
130         switch (c) {
131           case 'o':
132             has_overscan = true;
133             break;
134           case 'r':  // rotate 90 degrees to 'right'.
135             rotation = gfx::Display::ROTATE_90;
136             break;
137           case 'u':  // 180 degrees, 'u'pside-down.
138             rotation = gfx::Display::ROTATE_180;
139             break;
140           case 'l':  // rotate 90 degrees to 'left'.
141             rotation = gfx::Display::ROTATE_270;
142             break;
143         }
144       }
145     }
146   }
147
148   float device_scale_factor = 1.0f;
149   if (!GetDisplayBounds(main_spec, &bounds_in_native, &device_scale_factor)) {
150 #if defined(OS_WIN)
151     if (gfx::IsHighDPIEnabled()) {
152       device_scale_factor = gfx::GetDPIScale();
153     }
154 #endif
155   }
156
157   std::vector<DisplayMode> display_modes;
158   if (Tokenize(main_spec, "#", &parts) == 2) {
159     size_t native_mode = 0;
160     int largest_area = -1;
161     float highest_refresh_rate = -1.0f;
162     main_spec = parts[0];
163     std::string resolution_list = parts[1];
164     count = Tokenize(resolution_list, "|", &parts);
165     for (size_t i = 0; i < count; ++i) {
166       DisplayMode mode;
167       gfx::Rect mode_bounds;
168       std::vector<std::string> resolution;
169       Tokenize(parts[i], "%", &resolution);
170       if (GetDisplayBounds(
171               resolution[0], &mode_bounds, &mode.device_scale_factor)) {
172         mode.size = mode_bounds.size();
173         if (resolution.size() > 1)
174           sscanf(resolution[1].c_str(), "%f", &mode.refresh_rate);
175         if (mode.size.GetArea() >= largest_area &&
176             mode.refresh_rate > highest_refresh_rate) {
177           // Use mode with largest area and highest refresh rate as native.
178           largest_area = mode.size.GetArea();
179           highest_refresh_rate = mode.refresh_rate;
180           native_mode = i;
181         }
182         display_modes.push_back(mode);
183       }
184     }
185     display_modes[native_mode].native = true;
186   }
187
188   if (id == gfx::Display::kInvalidDisplayID)
189     id = synthesized_display_id++;
190   DisplayInfo display_info(
191       id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
192   display_info.set_device_scale_factor(device_scale_factor);
193   display_info.set_rotation(rotation);
194   display_info.set_configured_ui_scale(ui_scale);
195   display_info.SetBounds(bounds_in_native);
196   display_info.set_display_modes(display_modes);
197
198   // To test the overscan, it creates the default 5% overscan.
199   if (has_overscan) {
200     int width = bounds_in_native.width() / device_scale_factor / 40;
201     int height = bounds_in_native.height() / device_scale_factor / 40;
202     display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
203     display_info.UpdateDisplaySize();
204   }
205
206   DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
207            << ", spec=" << spec;
208   return display_info;
209 }
210
211 DisplayInfo::DisplayInfo()
212     : id_(gfx::Display::kInvalidDisplayID),
213       has_overscan_(false),
214       rotation_(gfx::Display::ROTATE_0),
215       touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
216       touch_device_id_(0),
217       device_scale_factor_(1.0f),
218       overscan_insets_in_dip_(0, 0, 0, 0),
219       configured_ui_scale_(1.0f),
220       native_(false),
221       is_aspect_preserving_scaling_(false),
222       color_profile_(ui::COLOR_PROFILE_STANDARD) {
223 }
224
225 DisplayInfo::DisplayInfo(int64 id,
226                          const std::string& name,
227                          bool has_overscan)
228     : id_(id),
229       name_(name),
230       has_overscan_(has_overscan),
231       rotation_(gfx::Display::ROTATE_0),
232       touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
233       touch_device_id_(0),
234       device_scale_factor_(1.0f),
235       overscan_insets_in_dip_(0, 0, 0, 0),
236       configured_ui_scale_(1.0f),
237       native_(false),
238       color_profile_(ui::COLOR_PROFILE_STANDARD) {
239 }
240
241 DisplayInfo::~DisplayInfo() {
242 }
243
244 void DisplayInfo::Copy(const DisplayInfo& native_info) {
245   DCHECK(id_ == native_info.id_);
246   name_ = native_info.name_;
247   has_overscan_ = native_info.has_overscan_;
248
249   DCHECK(!native_info.bounds_in_native_.IsEmpty());
250   bounds_in_native_ = native_info.bounds_in_native_;
251   size_in_pixel_ = native_info.size_in_pixel_;
252   device_scale_factor_ = native_info.device_scale_factor_;
253   display_modes_ = native_info.display_modes_;
254   touch_support_ = native_info.touch_support_;
255   touch_device_id_ = native_info.touch_device_id_;
256
257   // Copy overscan_insets_in_dip_ if it's not empty. This is for test
258   // cases which use "/o" annotation which sets the overscan inset
259   // to native, and that overscan has to be propagated. This does not
260   // happen on the real environment.
261   if (!native_info.overscan_insets_in_dip_.empty())
262     overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
263
264   // Rotation_ and ui_scale_ color_profile_ are given by preference,
265   // or unit tests. Don't copy if this native_info came from
266   // DisplayChangeObserver.
267   if (!native_info.native()) {
268     rotation_ = native_info.rotation_;
269     configured_ui_scale_ = native_info.configured_ui_scale_;
270     color_profile_ = native_info.color_profile();
271   }
272
273   available_color_profiles_ = native_info.available_color_profiles();
274
275   // Don't copy insets as it may be given by preference.  |rotation_|
276   // is treated as a native so that it can be specified in
277   // |CreateFromSpec|.
278 }
279
280 void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
281   bounds_in_native_ = new_bounds_in_native;
282   size_in_pixel_ = new_bounds_in_native.size();
283   UpdateDisplaySize();
284 }
285
286 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
287   if (use_125_dsf_for_ui_scaling && device_scale_factor_ == 1.25f)
288     return (configured_ui_scale_ == 0.8f) ? 1.25f : 1.0f;
289   if (device_scale_factor_ == configured_ui_scale_)
290     return 1.0f;
291   return device_scale_factor_;
292 }
293
294 float DisplayInfo::GetEffectiveUIScale() const {
295   if (use_125_dsf_for_ui_scaling && device_scale_factor_ == 1.25f)
296     return (configured_ui_scale_ == 0.8f) ? 1.0f : configured_ui_scale_;
297   if (device_scale_factor_ == configured_ui_scale_)
298     return 1.0f;
299   return configured_ui_scale_;
300 }
301
302 void DisplayInfo::UpdateDisplaySize() {
303   size_in_pixel_ = bounds_in_native_.size();
304   if (!overscan_insets_in_dip_.empty()) {
305     gfx::Insets insets_in_pixel =
306         overscan_insets_in_dip_.Scale(device_scale_factor_);
307     size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
308   } else {
309     overscan_insets_in_dip_.Set(0, 0, 0, 0);
310   }
311
312   if (rotation_ == gfx::Display::ROTATE_90 ||
313       rotation_ == gfx::Display::ROTATE_270)
314     size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
315   gfx::SizeF size_f(size_in_pixel_);
316   size_f.Scale(GetEffectiveUIScale());
317   size_in_pixel_ = gfx::ToFlooredSize(size_f);
318 }
319
320 void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
321   overscan_insets_in_dip_ = insets_in_dip;
322 }
323
324 gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
325   return overscan_insets_in_dip_.Scale(device_scale_factor_);
326 }
327
328 gfx::Size DisplayInfo::GetNativeModeSize() const {
329   for (size_t i = 0; i < display_modes_.size(); ++i) {
330     if (display_modes_[i].native)
331       return display_modes_[i].size;
332   }
333
334   return gfx::Size();
335 }
336
337 std::string DisplayInfo::ToString() const {
338   int rotation_degree = static_cast<int>(rotation_) * 90;
339   return base::StringPrintf(
340       "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
341       "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
342       "touch-device-id=%d",
343       static_cast<long long int>(id_),
344       bounds_in_native_.ToString().c_str(),
345       size_in_pixel_.ToString().c_str(),
346       device_scale_factor_,
347       overscan_insets_in_dip_.ToString().c_str(),
348       rotation_degree,
349       configured_ui_scale_,
350       touch_support_ == gfx::Display::TOUCH_SUPPORT_AVAILABLE
351           ? "yes"
352           : touch_support_ == gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
353                 ? "no"
354                 : "unknown",
355       touch_device_id_);
356 }
357
358 std::string DisplayInfo::ToFullString() const {
359   std::string display_modes_str;
360   std::vector<DisplayMode>::const_iterator iter = display_modes_.begin();
361   for (; iter != display_modes_.end(); ++iter) {
362     if (!display_modes_str.empty())
363       display_modes_str += ",";
364     base::StringAppendF(&display_modes_str,
365                         "(%dx%d@%f%c%s)",
366                         iter->size.width(),
367                         iter->size.height(),
368                         iter->refresh_rate,
369                         iter->interlaced ? 'I' : 'P',
370                         iter->native ? "(N)" : "");
371   }
372   return ToString() + ", display_modes==" + display_modes_str;
373 }
374
375 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile) {
376   if (IsColorProfileAvailable(profile))
377     color_profile_ = profile;
378 }
379
380 bool DisplayInfo::IsColorProfileAvailable(
381     ui::ColorCalibrationProfile profile) const {
382   return std::find(available_color_profiles_.begin(),
383                    available_color_profiles_.end(),
384                    profile) != available_color_profiles_.end();
385 }
386
387 }  // namespace ash