display_info.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_util.h" 12#include "base/stringprintf.h" 13#include "base/strings/string_number_conversions.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 // Don't copy insets as it may be given by preference. |rotation_| 152 // is treated as a native so that it can be specified in 153 // |CreateFromSpec|. 154} 155 156void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_pixel) { 157 bounds_in_pixel_ = new_bounds_in_pixel; 158 size_in_pixel_ = new_bounds_in_pixel.size(); 159 UpdateDisplaySize(); 160} 161 162void DisplayInfo::UpdateDisplaySize() { 163 size_in_pixel_ = bounds_in_pixel_.size(); 164 if (has_custom_overscan_insets_) { 165 gfx::Insets insets_in_pixel = 166 overscan_insets_in_dip_.Scale(device_scale_factor_); 167 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height()); 168 } else if (has_overscan_) { 169 // Currently we assume 5% overscan and hope for the best if TV claims it 170 // overscan, but doesn't expose how much. 171 // TODO(oshima): The insets has to be applied after rotation. 172 // Fix this. 173 int width = bounds_in_pixel_.width() / 40; 174 int height = bounds_in_pixel_.height() / 40; 175 gfx::Insets insets_in_pixel(height, width, height, width); 176 overscan_insets_in_dip_ = 177 insets_in_pixel.Scale(1.0 / device_scale_factor_); 178 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height()); 179 } else { 180 overscan_insets_in_dip_.Set(0, 0, 0, 0); 181 } 182 183 if (rotation_ == gfx::Display::ROTATE_90 || 184 rotation_ == gfx::Display::ROTATE_270) 185 size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width()); 186 gfx::SizeF size_f(size_in_pixel_); 187 size_f.Scale(ui_scale_); 188 size_in_pixel_ = gfx::ToFlooredSize(size_f); 189} 190 191void DisplayInfo::SetOverscanInsets(bool custom, 192 const gfx::Insets& insets_in_dip) { 193 has_custom_overscan_insets_ = custom; 194 overscan_insets_in_dip_ = insets_in_dip; 195} 196 197gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const { 198 return overscan_insets_in_dip_.Scale(device_scale_factor_); 199} 200 201std::string DisplayInfo::ToString() const { 202 int rotation_degree = static_cast<int>(rotation_) * 90; 203 return base::StringPrintf( 204 "DisplayInfo[%lld] bounds=%s, size=%s, scale=%f, " 205 "overscan=%s, rotation=%d, ui-scale=%f", 206 static_cast<long long int>(id_), 207 bounds_in_pixel_.ToString().c_str(), 208 size_in_pixel_.ToString().c_str(), 209 device_scale_factor_, 210 overscan_insets_in_dip_.ToString().c_str(), 211 rotation_degree, 212 ui_scale_); 213} 214 215} // namespace internal 216} // namespace ash 217