display_manager.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Copyright (c) 2012 The Chromium Authors. All rights reserved.
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Use of this source code is governed by a BSD-style license that can be
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// found in the LICENSE file.
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/display/display_manager.h"
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <cmath>
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <set>
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string>
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <vector>
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/ash_switches.h"
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/display/display_layout_store.h"
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/display/screen_ash.h"
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/screen_util.h"
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/shell.h"
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/auto_reset.h"
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/command_line.h"
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/logging.h"
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/metrics/histogram.h"
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/string_number_conversions.h"
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/string_split.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/stringprintf.h"
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/utf_string_conversions.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "grit/ash_strings.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/base/l10n/l10n_util.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/base/layout.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/base/resource/resource_bundle.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/gfx/display.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/gfx/rect.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/gfx/screen.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/gfx/size_conversions.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(USE_X11)
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ui/base/x/x11_util.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(OS_CHROMEOS)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ash/display/output_configurator_animation.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/sys_info.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(OS_WIN)
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/win/windows_version.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallnamespace ash {
48e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Halltypedef std::vector<gfx::Display> DisplayList;
49e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Halltypedef std::vector<DisplayInfo> DisplayInfoList;
50e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
51e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallnamespace {
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// We need to keep this in order for unittests to tell if
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// the object in gfx::Screen::GetScreenByType is for shutdown.
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallgfx::Screen* screen_for_shutdown = NULL;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// The number of pixels to overlap between the primary and secondary displays,
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// in case that the offset value is too large.
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst int kMinimumOverlapForInvalidOffset = 100;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// List of value UI Scale values. Scales for 2x are equivalent to 640,
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 2560 pixel width 2x density display. Please see crbug.com/233375
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// for the full list of resolutions.
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst float kUIScalesFor2x[] =
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct DisplaySortFunctor {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool operator()(const gfx::Display& a, const gfx::Display& b) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return a.id() < b.id();
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct DisplayInfoSortFunctor {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool operator()(const DisplayInfo& a, const DisplayInfo& b) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return a.id() < b.id();
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct DisplayModeMatcher {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayModeMatcher(const gfx::Size& size) : size(size) {}
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool operator()(const DisplayMode& mode) { return mode.size == size; }
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  gfx::Size size;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct ScaleComparator {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  explicit ScaleComparator(float s) : scale(s) {}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool operator()(float s) const {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const float kEpsilon = 0.0001f;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return std::abs(scale - s) < kEpsilon;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  float scale;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallgfx::Display& GetInvalidDisplay() {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  static gfx::Display* invalid_display = new gfx::Display();
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return *invalid_display;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid MaybeInitInternalDisplay(int64 id) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  CommandLine* command_line = CommandLine::ForCurrentProcess();
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (command_line->HasSwitch(switches::kAshUseFirstDisplayAsInternal))
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gfx::Display::SetInternalDisplayId(id);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Scoped objects used to either create or close the non desktop window
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// at specific timing.
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallclass NonDesktopDisplayUpdater {
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall public:
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  NonDesktopDisplayUpdater(DisplayManager* manager,
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           DisplayManager::Delegate* delegate)
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      : manager_(manager),
116e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        delegate_(delegate),
117e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        enabled_(manager_->second_display_mode() != DisplayManager::EXTENDED &&
118e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                 manager_->non_desktop_display().is_valid()) {
119e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  }
120e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
121e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  ~NonDesktopDisplayUpdater() {
122e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if (!delegate_)
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (enabled_) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DisplayInfo display_info = manager_->GetDisplayInfo(
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          manager_->non_desktop_display().id());
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      delegate_->CreateOrUpdateNonDesktopDisplay(display_info);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      delegate_->CloseNonDesktopDisplay();
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool enabled() const { return enabled_; }
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall private:
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayManager* manager_;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayManager::Delegate* delegate_;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bool enabled_;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DISALLOW_COPY_AND_ASSIGN(NonDesktopDisplayUpdater);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}  // namespace
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusing std::string;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusing std::vector;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDisplayManager::DisplayManager()
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    : delegate_(NULL),
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      screen_ash_(new ScreenAsh),
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      screen_(screen_ash_.get()),
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      layout_store_(new DisplayLayoutStore),
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      first_display_id_(gfx::Display::kInvalidDisplayID),
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      num_connected_displays_(0),
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      force_bounds_changed_(false),
156e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      change_display_upon_host_resize_(false),
157e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      second_display_mode_(EXTENDED),
158e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      mirrored_display_id_(gfx::Display::kInvalidDisplayID) {
159e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall#if defined(OS_CHROMEOS)
160e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  change_display_upon_host_resize_ = !base::SysInfo::IsRunningOnChromeOS();
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayInfo::SetAllowUpgradeToHighDPI(
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ui::ResourceBundle::GetSharedInstance().GetMaxScaleFactor() ==
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ui::SCALE_FACTOR_200P);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
167e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                                 screen_ash_.get());
168e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  gfx::Screen* current_native =
169e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no native, or the native was for shutdown,
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // use ash's screen.
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!current_native ||
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current_native == screen_for_shutdown) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   screen_ash_.get());
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
177e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
179e4c5d95ed37611acc6a186522315195b4ebfb9efJesse HallDisplayManager::~DisplayManager() {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
181e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
182e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall// static
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstd::vector<float> DisplayManager::GetScalesForDisplay(
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const DisplayInfo& info) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  std::vector<float> ret;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (info.device_scale_factor() == 2.0f) {
187e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    ret.assign(kUIScalesFor2x, kUIScalesFor2x + arraysize(kUIScalesFor2x));
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return ret;
189e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  }
190e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  switch (info.bounds_in_native().width()) {
191e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    case 1280:
192e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      ret.assign(kUIScalesFor1280,
193e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                 kUIScalesFor1280 + arraysize(kUIScalesFor1280));
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 1366:
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret.assign(kUIScalesFor1366,
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 kUIScalesFor1366 + arraysize(kUIScalesFor1366));
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret.assign(kUIScalesFor1280,
201e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                 kUIScalesFor1280 + arraysize(kUIScalesFor1280));
202e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall#if defined(OS_CHROMEOS)
203e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      if (base::SysInfo::IsRunningOnChromeOS())
204e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        NOTREACHED() << "Unknown resolution:" << info.ToString();
205e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall#endif
206e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  }
207e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  return ret;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// static
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallfloat DisplayManager::GetNextUIScale(const DisplayInfo& info, bool up) {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  float scale = info.configured_ui_scale();
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  std::vector<float> scales = GetScalesForDisplay(info);
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (size_t i = 0; i < scales.size(); ++i) {
215e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if (ScaleComparator(scales[i])(scale)) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (up && i != scales.size() - 1)
217e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        return scales[i + 1];
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!up && i != 0)
219e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        return scales[i - 1];
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return scales[i];
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
223e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  // Fallback to 1.0f if the |scale| wasn't in the list.
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1.0f;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbool DisplayManager::InitFromCommandLine() {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayInfoList info_list;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  CommandLine* command_line = CommandLine::ForCurrentProcess();
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!command_line->HasSwitch(switches::kAshHostWindowBounds))
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return false;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  const string size_str =
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      command_line->GetSwitchValueASCII(switches::kAshHostWindowBounds);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vector<string> parts;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  base::SplitString(size_str, ',', &parts);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (vector<string>::const_iterator iter = parts.begin();
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       iter != parts.end(); ++iter) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info_list.push_back(DisplayInfo::CreateFromSpec(*iter));
239e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  }
240e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  MaybeInitInternalDisplay(info_list[0].id());
241e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  if (info_list.size() > 1 &&
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      command_line->HasSwitch(switches::kAshEnableSoftwareMirroring)) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SetSecondDisplayMode(MIRRORING);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  OnNativeDisplaysChanged(info_list);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return true;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DisplayManager::InitDefaultDisplay() {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayInfoList info_list;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  info_list.push_back(DisplayInfo::CreateFromSpec(std::string()));
252e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  MaybeInitInternalDisplay(info_list[0].id());
253e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall  OnNativeDisplaysChanged(info_list);
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// static
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DisplayManager::UpdateDisplayBoundsForLayoutById(
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const DisplayLayout& layout,
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const gfx::Display& primary_display,
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int64 secondary_display_id) {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DCHECK_NE(gfx::Display::kInvalidDisplayID, secondary_display_id);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UpdateDisplayBoundsForLayout(
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      layout, primary_display,
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      Shell::GetInstance()->display_manager()->
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      FindDisplayForId(secondary_display_id));
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbool DisplayManager::IsActiveDisplay(const gfx::Display& display) const {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (DisplayList::const_iterator iter = displays_.begin();
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       iter != displays_.end(); ++iter) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((*iter).id() == display.id())
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return true;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return false;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbool DisplayManager::HasInternalDisplay() const {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return gfx::Display::InternalDisplayId() != gfx::Display::kInvalidDisplayID;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbool DisplayManager::IsInternalDisplayId(int64 id) const {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return gfx::Display::InternalDisplayId() == id;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDisplayLayout DisplayManager::GetCurrentDisplayLayout() {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DCHECK_EQ(2U, num_connected_displays());
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Invert if the primary was swapped.
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (num_connected_displays() > 1) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DisplayIdPair pair = GetCurrentDisplayIdPair();
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  NOTREACHED() << "DisplayLayout is requested for single display";
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // On release build, just fallback to default instead of blowing up.
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayLayout layout =
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      layout_store_->default_display_layout();
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  layout.primary_id = displays_[0].id();
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return layout;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDisplayIdPair DisplayManager::GetCurrentDisplayIdPair() const {
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (IsMirrored()) {
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (software_mirroring_enabled()) {
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      CHECK_EQ(2u, num_connected_displays());
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // This comment is to make it easy to distinguish the crash
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // between two checks.
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      CHECK_EQ(1u, displays_.size());
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return std::make_pair(displays_[0].id(), mirrored_display_id_);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    CHECK_GE(2u, displays_.size());
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int64 id_at_zero = displays_[0].id();
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (id_at_zero == gfx::Display::InternalDisplayId() ||
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        id_at_zero == first_display_id()) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return std::make_pair(id_at_zero, displays_[1].id());
315e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    } else {
316e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall      return std::make_pair(displays_[1].id(), id_at_zero);
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DisplayManager::SetLayoutForCurrentDisplays(
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const DisplayLayout& layout_relative_to_primary) {
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DCHECK_EQ(2U, GetNumDisplays());
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (GetNumDisplays() < 2)
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  const gfx::Display& primary = screen_->GetPrimaryDisplay();
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  const DisplayIdPair pair = GetCurrentDisplayIdPair();
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Invert if the primary was swapped.
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayLayout to_set = pair.first == primary.id() ?
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      layout_relative_to_primary : layout_relative_to_primary.Invert();
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DisplayLayout current_layout =
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      layout_store_->GetRegisteredDisplayLayout(pair);
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (to_set.position != current_layout.position ||
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      to_set.offset != current_layout.offset) {
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    to_set.primary_id = primary.id();
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    layout_store_->RegisterLayoutForDisplayIdPair(
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pair.first, pair.second, to_set);
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (delegate_)
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      delegate_->PreDisplayConfigurationChange(false);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // PreDisplayConfigurationChange(false);
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // TODO(oshima): Call UpdateDisplays instead.
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const DisplayLayout layout = GetCurrentDisplayLayout();
344    UpdateDisplayBoundsForLayoutById(
345        layout, primary,
346        ScreenUtil::GetSecondaryDisplay().id());
347
348    // Primary's bounds stay the same. Just notify bounds change
349    // on the secondary.
350    screen_ash_->NotifyBoundsChanged(
351        ScreenUtil::GetSecondaryDisplay());
352    if (delegate_)
353      delegate_->PostDisplayConfigurationChange();
354  }
355}
356
357const gfx::Display& DisplayManager::GetDisplayForId(int64 id) const {
358  gfx::Display* display =
359      const_cast<DisplayManager*>(this)->FindDisplayForId(id);
360  return display ? *display : GetInvalidDisplay();
361}
362
363const gfx::Display& DisplayManager::FindDisplayContainingPoint(
364    const gfx::Point& point_in_screen) const {
365  for (DisplayList::const_iterator iter = displays_.begin();
366       iter != displays_.end(); ++iter) {
367    const gfx::Display& display = *iter;
368    if (display.bounds().Contains(point_in_screen))
369      return display;
370  }
371  return GetInvalidDisplay();
372}
373
374bool DisplayManager::UpdateWorkAreaOfDisplay(int64 display_id,
375                                             const gfx::Insets& insets) {
376  gfx::Display* display = FindDisplayForId(display_id);
377  DCHECK(display);
378  gfx::Rect old_work_area = display->work_area();
379  display->UpdateWorkAreaFromInsets(insets);
380  return old_work_area != display->work_area();
381}
382
383void DisplayManager::SetOverscanInsets(int64 display_id,
384                                       const gfx::Insets& insets_in_dip) {
385  display_info_[display_id].SetOverscanInsets(insets_in_dip);
386  DisplayInfoList display_info_list;
387  for (DisplayList::const_iterator iter = displays_.begin();
388       iter != displays_.end(); ++iter) {
389    display_info_list.push_back(GetDisplayInfo(iter->id()));
390  }
391  AddMirrorDisplayInfoIfAny(&display_info_list);
392  UpdateDisplays(display_info_list);
393}
394
395void DisplayManager::SetDisplayRotation(int64 display_id,
396                                        gfx::Display::Rotation rotation) {
397  DisplayInfoList display_info_list;
398  for (DisplayList::const_iterator iter = displays_.begin();
399       iter != displays_.end(); ++iter) {
400    DisplayInfo info = GetDisplayInfo(iter->id());
401    if (info.id() == display_id) {
402      if (info.rotation() == rotation)
403        return;
404      info.set_rotation(rotation);
405    }
406    display_info_list.push_back(info);
407  }
408  AddMirrorDisplayInfoIfAny(&display_info_list);
409  if (virtual_keyboard_root_window_enabled() &&
410      display_id == non_desktop_display_.id()) {
411    DisplayInfo info = GetDisplayInfo(display_id);
412    info.set_rotation(rotation);
413    display_info_list.push_back(info);
414  }
415  UpdateDisplays(display_info_list);
416}
417
418void DisplayManager::SetDisplayUIScale(int64 display_id,
419                                       float ui_scale) {
420  if (!IsDisplayUIScalingEnabled() ||
421      gfx::Display::InternalDisplayId() != display_id) {
422    return;
423  }
424
425  DisplayInfoList display_info_list;
426  for (DisplayList::const_iterator iter = displays_.begin();
427       iter != displays_.end(); ++iter) {
428    DisplayInfo info = GetDisplayInfo(iter->id());
429    if (info.id() == display_id) {
430      if (info.configured_ui_scale() == ui_scale)
431        return;
432      std::vector<float> scales = GetScalesForDisplay(info);
433      ScaleComparator comparator(ui_scale);
434      if (std::find_if(scales.begin(), scales.end(), comparator) ==
435          scales.end()) {
436        return;
437      }
438      info.set_configured_ui_scale(ui_scale);
439    }
440    display_info_list.push_back(info);
441  }
442  AddMirrorDisplayInfoIfAny(&display_info_list);
443  UpdateDisplays(display_info_list);
444}
445
446void DisplayManager::SetDisplayResolution(int64 display_id,
447                                          const gfx::Size& resolution) {
448  DCHECK_NE(gfx::Display::InternalDisplayId(), display_id);
449  if (gfx::Display::InternalDisplayId() == display_id)
450    return;
451  const DisplayInfo& display_info = GetDisplayInfo(display_id);
452  const std::vector<DisplayMode>& modes = display_info.display_modes();
453  DCHECK_NE(0u, modes.size());
454  std::vector<DisplayMode>::const_iterator iter =
455      std::find_if(modes.begin(), modes.end(), DisplayModeMatcher(resolution));
456  if (iter == modes.end()) {
457    LOG(WARNING) << "Unsupported resolution was requested:"
458                 << resolution.ToString();
459    return;
460  }
461  display_modes_[display_id] = *iter;
462#if defined(OS_CHROMEOS)
463  if (base::SysInfo::IsRunningOnChromeOS())
464    Shell::GetInstance()->display_configurator()->OnConfigurationChanged();
465#endif
466}
467
468void DisplayManager::RegisterDisplayProperty(
469    int64 display_id,
470    gfx::Display::Rotation rotation,
471    float ui_scale,
472    const gfx::Insets* overscan_insets,
473    const gfx::Size& resolution_in_pixels,
474    ui::ColorCalibrationProfile color_profile) {
475  if (display_info_.find(display_id) == display_info_.end())
476    display_info_[display_id] = DisplayInfo(display_id, std::string(), false);
477
478  display_info_[display_id].set_rotation(rotation);
479  display_info_[display_id].SetColorProfile(color_profile);
480  // Just in case the preference file was corrupted.
481  if (0.5f <= ui_scale && ui_scale <= 2.0f)
482    display_info_[display_id].set_configured_ui_scale(ui_scale);
483  if (overscan_insets)
484    display_info_[display_id].SetOverscanInsets(*overscan_insets);
485  if (!resolution_in_pixels.IsEmpty()) {
486    // Default refresh rate, until OnNativeDisplaysChanged() updates us with the
487    // actual display info, is 60 Hz.
488    display_modes_[display_id] =
489        DisplayMode(resolution_in_pixels, 60.0f, false, false);
490  }
491}
492
493bool DisplayManager::GetSelectedModeForDisplayId(int64 id,
494                                                 DisplayMode* mode_out) const {
495  std::map<int64, DisplayMode>::const_iterator iter = display_modes_.find(id);
496  if (iter == display_modes_.end())
497    return false;
498  *mode_out = iter->second;
499  return true;
500}
501
502bool DisplayManager::IsDisplayUIScalingEnabled() const {
503  return GetDisplayIdForUIScaling() != gfx::Display::kInvalidDisplayID;
504}
505
506gfx::Insets DisplayManager::GetOverscanInsets(int64 display_id) const {
507  std::map<int64, DisplayInfo>::const_iterator it =
508      display_info_.find(display_id);
509  return (it != display_info_.end()) ?
510      it->second.overscan_insets_in_dip() : gfx::Insets();
511}
512
513void DisplayManager::SetColorCalibrationProfile(
514    int64 display_id,
515    ui::ColorCalibrationProfile profile) {
516#if defined(OS_CHROMEOS)
517  if (!display_info_[display_id].IsColorProfileAvailable(profile))
518    return;
519
520  if (delegate_)
521    delegate_->PreDisplayConfigurationChange(false);
522  // Just sets color profile if it's not running on ChromeOS (like tests).
523  if (!base::SysInfo::IsRunningOnChromeOS() ||
524      Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
525          display_id, profile)) {
526    display_info_[display_id].SetColorProfile(profile);
527    UMA_HISTOGRAM_ENUMERATION(
528        "ChromeOS.Display.ColorProfile", profile, ui::NUM_COLOR_PROFILES);
529  }
530  if (delegate_)
531    delegate_->PostDisplayConfigurationChange();
532#endif
533}
534
535void DisplayManager::OnNativeDisplaysChanged(
536    const std::vector<DisplayInfo>& updated_displays) {
537  if (updated_displays.empty()) {
538    VLOG(1) << "OnNativeDisplayChanged(0): # of current displays="
539            << displays_.size();
540    // If the device is booted without display, or chrome is started
541    // without --ash-host-window-bounds on linux desktop, use the
542    // default display.
543    if (displays_.empty()) {
544      std::vector<DisplayInfo> init_displays;
545      init_displays.push_back(DisplayInfo::CreateFromSpec(std::string()));
546      MaybeInitInternalDisplay(init_displays[0].id());
547      OnNativeDisplaysChanged(init_displays);
548    } else {
549      // Otherwise don't update the displays when all displays are disconnected.
550      // This happens when:
551      // - the device is idle and powerd requested to turn off all displays.
552      // - the device is suspended. (kernel turns off all displays)
553      // - the internal display's brightness is set to 0 and no external
554      //   display is connected.
555      // - the internal display's brightness is 0 and external display is
556      //   disconnected.
557      // The display will be updated when one of displays is turned on, and the
558      // display list will be updated correctly.
559    }
560    return;
561  }
562  first_display_id_ = updated_displays[0].id();
563  std::set<gfx::Point> origins;
564
565  if (updated_displays.size() == 1) {
566    VLOG(1) << "OnNativeDisplaysChanged(1):" << updated_displays[0].ToString();
567  } else {
568    VLOG(1) << "OnNativeDisplaysChanged(" << updated_displays.size()
569            << ") [0]=" << updated_displays[0].ToString()
570            << ", [1]=" << updated_displays[1].ToString();
571  }
572
573  bool internal_display_connected = false;
574  num_connected_displays_ = updated_displays.size();
575  mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
576  non_desktop_display_ = gfx::Display();
577  DisplayInfoList new_display_info_list;
578  for (DisplayInfoList::const_iterator iter = updated_displays.begin();
579       iter != updated_displays.end();
580       ++iter) {
581    if (!internal_display_connected)
582      internal_display_connected = IsInternalDisplayId(iter->id());
583    // Mirrored monitors have the same origins.
584    gfx::Point origin = iter->bounds_in_native().origin();
585    if (origins.find(origin) != origins.end()) {
586      InsertAndUpdateDisplayInfo(*iter);
587      mirrored_display_id_ = iter->id();
588    } else {
589      origins.insert(origin);
590      new_display_info_list.push_back(*iter);
591    }
592
593    const gfx::Size& resolution = iter->bounds_in_native().size();
594    const std::vector<DisplayMode>& display_modes = iter->display_modes();
595    // This is empty the displays are initialized from InitFromCommandLine.
596    if (!display_modes.size())
597      continue;
598    std::vector<DisplayMode>::const_iterator display_modes_iter =
599        std::find_if(display_modes.begin(),
600                     display_modes.end(),
601                     DisplayModeMatcher(resolution));
602    // Update the actual resolution selected as the resolution request may fail.
603    if (display_modes_iter == display_modes.end())
604      display_modes_.erase(iter->id());
605    else if (display_modes_.find(iter->id()) != display_modes_.end())
606      display_modes_[iter->id()] = *display_modes_iter;
607  }
608  if (HasInternalDisplay() &&
609      !internal_display_connected &&
610      display_info_.find(gfx::Display::InternalDisplayId()) ==
611      display_info_.end()) {
612    DisplayInfo internal_display_info(
613        gfx::Display::InternalDisplayId(),
614        l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME),
615        false  /*Internal display must not have overscan */);
616    internal_display_info.SetBounds(gfx::Rect(0, 0, 800, 600));
617    display_info_[gfx::Display::InternalDisplayId()] = internal_display_info;
618  }
619  UpdateDisplays(new_display_info_list);
620}
621
622void DisplayManager::UpdateDisplays() {
623  DisplayInfoList display_info_list;
624  for (DisplayList::const_iterator iter = displays_.begin();
625       iter != displays_.end(); ++iter) {
626    display_info_list.push_back(GetDisplayInfo(iter->id()));
627  }
628  AddMirrorDisplayInfoIfAny(&display_info_list);
629  UpdateDisplays(display_info_list);
630}
631
632void DisplayManager::UpdateDisplays(
633    const std::vector<DisplayInfo>& updated_display_info_list) {
634#if defined(OS_WIN)
635  if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
636    DCHECK_EQ(1u, updated_display_info_list.size()) <<
637        "Multiple display test does not work on Win8 bots. Please "
638        "skip (don't disable) the test using SupportsMultipleDisplays()";
639  }
640#endif
641
642  DisplayInfoList new_display_info_list = updated_display_info_list;
643  std::sort(displays_.begin(), displays_.end(), DisplaySortFunctor());
644  std::sort(new_display_info_list.begin(),
645            new_display_info_list.end(),
646            DisplayInfoSortFunctor());
647  DisplayList removed_displays;
648  std::vector<size_t> changed_display_indices;
649  std::vector<size_t> added_display_indices;
650
651  DisplayList::iterator curr_iter = displays_.begin();
652  DisplayInfoList::const_iterator new_info_iter = new_display_info_list.begin();
653
654  DisplayList new_displays;
655
656  // Use the internal display or 1st as the mirror source, then scale
657  // the root window so that it matches the external display's
658  // resolution. This is necessary in order for scaling to work while
659  // mirrored.
660  int64 non_desktop_display_id = gfx::Display::kInvalidDisplayID;
661
662  if (second_display_mode_ != EXTENDED && new_display_info_list.size() == 2) {
663    bool zero_is_source =
664        first_display_id_ == new_display_info_list[0].id() ||
665        gfx::Display::InternalDisplayId() == new_display_info_list[0].id();
666    if (second_display_mode_ == MIRRORING) {
667      mirrored_display_id_ = new_display_info_list[zero_is_source ? 1 : 0].id();
668      non_desktop_display_id = mirrored_display_id_;
669    } else {
670      // TODO(oshima|bshe): The virtual keyboard is currently assigned to
671      // the 1st display.
672      non_desktop_display_id =
673          new_display_info_list[zero_is_source ? 0 : 1].id();
674    }
675  }
676
677  while (curr_iter != displays_.end() ||
678         new_info_iter != new_display_info_list.end()) {
679    if (new_info_iter != new_display_info_list.end() &&
680        non_desktop_display_id == new_info_iter->id()) {
681      DisplayInfo info = *new_info_iter;
682      info.SetOverscanInsets(gfx::Insets());
683      InsertAndUpdateDisplayInfo(info);
684      non_desktop_display_ =
685          CreateDisplayFromDisplayInfoById(non_desktop_display_id);
686      ++new_info_iter;
687      // Remove existing external display if it is going to be used as
688      // non desktop.
689      if (curr_iter != displays_.end() &&
690          curr_iter->id() == non_desktop_display_id) {
691        removed_displays.push_back(*curr_iter);
692        ++curr_iter;
693      }
694      continue;
695    }
696
697    if (curr_iter == displays_.end()) {
698      // more displays in new list.
699      added_display_indices.push_back(new_displays.size());
700      InsertAndUpdateDisplayInfo(*new_info_iter);
701      new_displays.push_back(
702          CreateDisplayFromDisplayInfoById(new_info_iter->id()));
703      ++new_info_iter;
704    } else if (new_info_iter == new_display_info_list.end()) {
705      // more displays in current list.
706      removed_displays.push_back(*curr_iter);
707      ++curr_iter;
708    } else if (curr_iter->id() == new_info_iter->id()) {
709      const gfx::Display& current_display = *curr_iter;
710      // Copy the info because |CreateDisplayFromInfo| updates the instance.
711      const DisplayInfo current_display_info =
712          GetDisplayInfo(current_display.id());
713      InsertAndUpdateDisplayInfo(*new_info_iter);
714      gfx::Display new_display =
715          CreateDisplayFromDisplayInfoById(new_info_iter->id());
716      const DisplayInfo& new_display_info = GetDisplayInfo(new_display.id());
717
718      bool host_window_bounds_changed =
719          current_display_info.bounds_in_native() !=
720          new_display_info.bounds_in_native();
721
722      if (force_bounds_changed_ ||
723          host_window_bounds_changed ||
724          (current_display.device_scale_factor() !=
725           new_display.device_scale_factor()) ||
726          (current_display_info.size_in_pixel() !=
727           new_display.GetSizeInPixel()) ||
728          (current_display.rotation() != new_display.rotation())) {
729        changed_display_indices.push_back(new_displays.size());
730      }
731
732      new_display.UpdateWorkAreaFromInsets(current_display.GetWorkAreaInsets());
733      new_displays.push_back(new_display);
734      ++curr_iter;
735      ++new_info_iter;
736    } else if (curr_iter->id() < new_info_iter->id()) {
737      // more displays in current list between ids, which means it is deleted.
738      removed_displays.push_back(*curr_iter);
739      ++curr_iter;
740    } else {
741      // more displays in new list between ids, which means it is added.
742      added_display_indices.push_back(new_displays.size());
743      InsertAndUpdateDisplayInfo(*new_info_iter);
744      new_displays.push_back(
745          CreateDisplayFromDisplayInfoById(new_info_iter->id()));
746      ++new_info_iter;
747    }
748  }
749
750  scoped_ptr<NonDesktopDisplayUpdater> non_desktop_display_updater(
751      new NonDesktopDisplayUpdater(this, delegate_));
752
753  // Do not update |displays_| if there's nothing to be updated. Without this,
754  // it will not update the display layout, which causes the bug
755  // http://crbug.com/155948.
756  if (changed_display_indices.empty() && added_display_indices.empty() &&
757      removed_displays.empty()) {
758    return;
759  }
760  // Clear focus if the display has been removed, but don't clear focus if
761  // the destkop has been moved from one display to another
762  // (mirror -> docked, docked -> single internal).
763  bool clear_focus =
764      !removed_displays.empty() &&
765      !(removed_displays.size() == 1 && added_display_indices.size() == 1);
766  if (delegate_)
767    delegate_->PreDisplayConfigurationChange(clear_focus);
768
769  size_t updated_index;
770  if (UpdateSecondaryDisplayBoundsForLayout(&new_displays, &updated_index) &&
771      std::find(added_display_indices.begin(),
772                added_display_indices.end(),
773                updated_index) == added_display_indices.end() &&
774      std::find(changed_display_indices.begin(),
775                changed_display_indices.end(),
776                updated_index) == changed_display_indices.end()) {
777    changed_display_indices.push_back(updated_index);
778  }
779
780  displays_ = new_displays;
781
782  base::AutoReset<bool> resetter(&change_display_upon_host_resize_, false);
783
784  // Temporarily add displays to be removed because display object
785  // being removed are accessed during shutting down the root.
786  displays_.insert(displays_.end(), removed_displays.begin(),
787                   removed_displays.end());
788
789  for (DisplayList::const_reverse_iterator iter = removed_displays.rbegin();
790       iter != removed_displays.rend(); ++iter) {
791    screen_ash_->NotifyDisplayRemoved(displays_.back());
792    displays_.pop_back();
793  }
794  // Close the non desktop window here to avoid creating two compositor on
795  // one display.
796  if (!non_desktop_display_updater->enabled())
797    non_desktop_display_updater.reset();
798  for (std::vector<size_t>::iterator iter = added_display_indices.begin();
799       iter != added_display_indices.end(); ++iter) {
800    screen_ash_->NotifyDisplayAdded(displays_[*iter]);
801  }
802  // Create the non destkop window after all displays are added so that
803  // it can mirror the display newly added. This can happen when switching
804  // from dock mode to software mirror mode.
805  non_desktop_display_updater.reset();
806  for (std::vector<size_t>::iterator iter = changed_display_indices.begin();
807       iter != changed_display_indices.end(); ++iter) {
808    screen_ash_->NotifyBoundsChanged(displays_[*iter]);
809  }
810  if (delegate_)
811    delegate_->PostDisplayConfigurationChange();
812
813#if defined(USE_X11) && defined(OS_CHROMEOS)
814  if (!changed_display_indices.empty() && base::SysInfo::IsRunningOnChromeOS())
815    ui::ClearX11DefaultRootWindow();
816#endif
817}
818
819const gfx::Display& DisplayManager::GetDisplayAt(size_t index) const {
820  DCHECK_LT(index, displays_.size());
821  return displays_[index];
822}
823
824const gfx::Display& DisplayManager::GetPrimaryDisplayCandidate() const {
825  if (GetNumDisplays() == 1)
826    return displays_[0];
827  DisplayLayout layout = layout_store_->GetRegisteredDisplayLayout(
828      GetCurrentDisplayIdPair());
829  return GetDisplayForId(layout.primary_id);
830}
831
832size_t DisplayManager::GetNumDisplays() const {
833  return displays_.size();
834}
835
836bool DisplayManager::IsMirrored() const {
837  return mirrored_display_id_ != gfx::Display::kInvalidDisplayID;
838}
839
840const DisplayInfo& DisplayManager::GetDisplayInfo(int64 display_id) const {
841  DCHECK_NE(gfx::Display::kInvalidDisplayID, display_id);
842
843  std::map<int64, DisplayInfo>::const_iterator iter =
844      display_info_.find(display_id);
845  CHECK(iter != display_info_.end()) << display_id;
846  return iter->second;
847}
848
849std::string DisplayManager::GetDisplayNameForId(int64 id) {
850  if (id == gfx::Display::kInvalidDisplayID)
851    return l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
852
853  std::map<int64, DisplayInfo>::const_iterator iter = display_info_.find(id);
854  if (iter != display_info_.end() && !iter->second.name().empty())
855    return iter->second.name();
856
857  return base::StringPrintf("Display %d", static_cast<int>(id));
858}
859
860int64 DisplayManager::GetDisplayIdForUIScaling() const {
861  // UI Scaling is effective only on internal display.
862  int64 display_id = gfx::Display::InternalDisplayId();
863#if defined(OS_WIN)
864  display_id = first_display_id();
865#endif
866  return display_id;
867}
868
869void DisplayManager::SetMirrorMode(bool mirrored) {
870  if (num_connected_displays() <= 1)
871    return;
872
873#if defined(OS_CHROMEOS)
874  if (base::SysInfo::IsRunningOnChromeOS()) {
875    ui::MultipleDisplayState new_state =
876        mirrored ? ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR :
877                   ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED;
878    Shell::GetInstance()->display_configurator()->SetDisplayMode(new_state);
879    return;
880  }
881#endif
882  // This is fallback path to emulate mirroroing on desktop.
883  SetSecondDisplayMode(mirrored ? MIRRORING : EXTENDED);
884  DisplayInfoList display_info_list;
885  int count = 0;
886  for (std::map<int64, DisplayInfo>::const_iterator iter =
887           display_info_.begin();
888       count < 2; ++iter, ++count) {
889    display_info_list.push_back(GetDisplayInfo(iter->second.id()));
890  }
891  UpdateDisplays(display_info_list);
892#if defined(OS_CHROMEOS)
893  if (Shell::GetInstance()->output_configurator_animation()) {
894    Shell::GetInstance()->output_configurator_animation()->
895        StartFadeInAnimation();
896  }
897#endif
898}
899
900void DisplayManager::AddRemoveDisplay() {
901  DCHECK(!displays_.empty());
902  std::vector<DisplayInfo> new_display_info_list;
903  const DisplayInfo& first_display = GetDisplayInfo(displays_[0].id());
904  new_display_info_list.push_back(first_display);
905  // Add if there is only one display connected.
906  if (num_connected_displays() == 1) {
907    // Layout the 2nd display below the primary as with the real device.
908    gfx::Rect host_bounds = first_display.bounds_in_native();
909    new_display_info_list.push_back(DisplayInfo::CreateFromSpec(
910        base::StringPrintf(
911            "%d+%d-500x400", host_bounds.x(), host_bounds.bottom())));
912  }
913  num_connected_displays_ = new_display_info_list.size();
914  mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
915  non_desktop_display_ = gfx::Display();
916  UpdateDisplays(new_display_info_list);
917}
918
919void DisplayManager::ToggleDisplayScaleFactor() {
920  DCHECK(!displays_.empty());
921  std::vector<DisplayInfo> new_display_info_list;
922  for (DisplayList::const_iterator iter = displays_.begin();
923       iter != displays_.end(); ++iter) {
924    DisplayInfo display_info = GetDisplayInfo(iter->id());
925    display_info.set_device_scale_factor(
926        display_info.device_scale_factor() == 1.0f ? 2.0f : 1.0f);
927    new_display_info_list.push_back(display_info);
928  }
929  AddMirrorDisplayInfoIfAny(&new_display_info_list);
930  UpdateDisplays(new_display_info_list);
931}
932
933#if defined(OS_CHROMEOS)
934void DisplayManager::SetSoftwareMirroring(bool enabled) {
935  // TODO(oshima|bshe): Support external display on the system
936  // that has virtual keyboard display.
937  if (second_display_mode_ == VIRTUAL_KEYBOARD)
938    return;
939  SetSecondDisplayMode(enabled ? MIRRORING : EXTENDED);
940}
941#endif
942
943void DisplayManager::SetSecondDisplayMode(SecondDisplayMode mode) {
944  second_display_mode_ = mode;
945  mirrored_display_id_ = gfx::Display::kInvalidDisplayID;
946  non_desktop_display_ = gfx::Display();
947}
948
949bool DisplayManager::UpdateDisplayBounds(int64 display_id,
950                                         const gfx::Rect& new_bounds) {
951  if (change_display_upon_host_resize_) {
952    display_info_[display_id].SetBounds(new_bounds);
953    // Don't notify observers if the mirrored window has changed.
954    if (software_mirroring_enabled() && mirrored_display_id_ == display_id)
955      return false;
956    gfx::Display* display = FindDisplayForId(display_id);
957    display->SetSize(display_info_[display_id].size_in_pixel());
958    screen_ash_->NotifyBoundsChanged(*display);
959    return true;
960  }
961  return false;
962}
963
964void DisplayManager::CreateMirrorWindowIfAny() {
965  NonDesktopDisplayUpdater updater(this, delegate_);
966}
967
968void DisplayManager::CreateScreenForShutdown() const {
969  bool native_is_ash =
970      gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE) ==
971      screen_ash_.get();
972  delete screen_for_shutdown;
973  screen_for_shutdown = screen_ash_->CloneForShutdown();
974  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE,
975                                 screen_for_shutdown);
976  if (native_is_ash) {
977    gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
978                                   screen_for_shutdown);
979  }
980}
981
982gfx::Display* DisplayManager::FindDisplayForId(int64 id) {
983  for (DisplayList::iterator iter = displays_.begin();
984       iter != displays_.end(); ++iter) {
985    if ((*iter).id() == id)
986      return &(*iter);
987  }
988  DLOG(WARNING) << "Could not find display:" << id;
989  return NULL;
990}
991
992void DisplayManager::AddMirrorDisplayInfoIfAny(
993    std::vector<DisplayInfo>* display_info_list) {
994  if (software_mirroring_enabled() && IsMirrored())
995    display_info_list->push_back(GetDisplayInfo(mirrored_display_id_));
996}
997
998void DisplayManager::InsertAndUpdateDisplayInfo(const DisplayInfo& new_info) {
999  std::map<int64, DisplayInfo>::iterator info =
1000      display_info_.find(new_info.id());
1001  if (info != display_info_.end()) {
1002    info->second.Copy(new_info);
1003  } else {
1004    display_info_[new_info.id()] = new_info;
1005    display_info_[new_info.id()].set_native(false);
1006  }
1007  display_info_[new_info.id()].UpdateDisplaySize();
1008
1009  OnDisplayInfoUpdated(display_info_[new_info.id()]);
1010}
1011
1012void DisplayManager::OnDisplayInfoUpdated(const DisplayInfo& display_info) {
1013#if defined(OS_CHROMEOS)
1014  ui::ColorCalibrationProfile color_profile = display_info.color_profile();
1015  if (color_profile != ui::COLOR_PROFILE_STANDARD) {
1016    Shell::GetInstance()->display_configurator()->SetColorCalibrationProfile(
1017        display_info.id(), color_profile);
1018  }
1019#endif
1020}
1021
1022gfx::Display DisplayManager::CreateDisplayFromDisplayInfoById(int64 id) {
1023  DCHECK(display_info_.find(id) != display_info_.end());
1024  const DisplayInfo& display_info = display_info_[id];
1025
1026  gfx::Display new_display(display_info.id());
1027  gfx::Rect bounds_in_native(display_info.size_in_pixel());
1028  float device_scale_factor = display_info.GetEffectiveDeviceScaleFactor();
1029
1030  // Simply set the origin to (0,0).  The primary display's origin is
1031  // always (0,0) and the secondary display's bounds will be updated
1032  // in |UpdateSecondaryDisplayBoundsForLayout| called in |UpdateDisplay|.
1033  new_display.SetScaleAndBounds(
1034      device_scale_factor, gfx::Rect(bounds_in_native.size()));
1035  new_display.set_rotation(display_info.rotation());
1036  new_display.set_touch_support(display_info.touch_support());
1037  return new_display;
1038}
1039
1040bool DisplayManager::UpdateSecondaryDisplayBoundsForLayout(
1041    DisplayList* displays,
1042    size_t* updated_index) const {
1043  if (displays->size() != 2U)
1044    return false;
1045
1046  int64 id_at_zero = displays->at(0).id();
1047  DisplayIdPair pair =
1048      (id_at_zero == first_display_id_ ||
1049       id_at_zero == gfx::Display::InternalDisplayId()) ?
1050      std::make_pair(id_at_zero, displays->at(1).id()) :
1051      std::make_pair(displays->at(1).id(), id_at_zero);
1052  DisplayLayout layout =
1053      layout_store_->ComputeDisplayLayoutForDisplayIdPair(pair);
1054
1055  // Ignore if a user has a old format (should be extremely rare)
1056  // and this will be replaced with DCHECK.
1057  if (layout.primary_id != gfx::Display::kInvalidDisplayID) {
1058    size_t primary_index, secondary_index;
1059    if (displays->at(0).id() == layout.primary_id) {
1060      primary_index = 0;
1061      secondary_index = 1;
1062    } else {
1063      primary_index = 1;
1064      secondary_index = 0;
1065    }
1066    // This function may be called before the secondary display is
1067    // registered. The bounds is empty in that case and will
1068    // return true.
1069    gfx::Rect bounds =
1070        GetDisplayForId(displays->at(secondary_index).id()).bounds();
1071    UpdateDisplayBoundsForLayout(
1072        layout, displays->at(primary_index), &displays->at(secondary_index));
1073    *updated_index = secondary_index;
1074    return bounds != displays->at(secondary_index).bounds();
1075  }
1076  return false;
1077}
1078
1079// static
1080void DisplayManager::UpdateDisplayBoundsForLayout(
1081    const DisplayLayout& layout,
1082    const gfx::Display& primary_display,
1083    gfx::Display* secondary_display) {
1084  DCHECK_EQ("0,0", primary_display.bounds().origin().ToString());
1085
1086  const gfx::Rect& primary_bounds = primary_display.bounds();
1087  const gfx::Rect& secondary_bounds = secondary_display->bounds();
1088  gfx::Point new_secondary_origin = primary_bounds.origin();
1089
1090  DisplayLayout::Position position = layout.position;
1091
1092  // Ignore the offset in case the secondary display doesn't share edges with
1093  // the primary display.
1094  int offset = layout.offset;
1095  if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
1096    offset = std::min(
1097        offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
1098    offset = std::max(
1099        offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
1100  } else {
1101    offset = std::min(
1102        offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
1103    offset = std::max(
1104        offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
1105  }
1106  switch (position) {
1107    case DisplayLayout::TOP:
1108      new_secondary_origin.Offset(offset, -secondary_bounds.height());
1109      break;
1110    case DisplayLayout::RIGHT:
1111      new_secondary_origin.Offset(primary_bounds.width(), offset);
1112      break;
1113    case DisplayLayout::BOTTOM:
1114      new_secondary_origin.Offset(offset, primary_bounds.height());
1115      break;
1116    case DisplayLayout::LEFT:
1117      new_secondary_origin.Offset(-secondary_bounds.width(), offset);
1118      break;
1119  }
1120  gfx::Insets insets = secondary_display->GetWorkAreaInsets();
1121  secondary_display->set_bounds(
1122      gfx::Rect(new_secondary_origin, secondary_bounds.size()));
1123  secondary_display->UpdateWorkAreaFromInsets(insets);
1124}
1125
1126}  // namespace ash
1127