window_tree_host.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window_tree_host.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/debug/trace_event.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/client/capture_client.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/client/cursor_client.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/env.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_targeter.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_tree_host_observer.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/base/view_prop.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/compositor/dip_util.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/compositor/layer.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/display.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/insets.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/gfx/point.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/point3_f.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/point_conversions.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/screen.h"
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/size_conversions.h"
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace aura {
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const char kWindowTreeHostForAcceleratedWidget[] =
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    "__AURA_WINDOW_TREE_HOST_ACCELERATED_WIDGET__";
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)float GetDeviceScaleFactorFromDisplay(Window* window) {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Display display = gfx::Screen::GetScreenFor(window)->
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GetDisplayNearestWindow(window);
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(display.is_valid());
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return display.device_scale_factor();
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// WindowTreeHost, public:
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)WindowTreeHost::~WindowTreeHost() {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!compositor_) << "compositor must be destroyed before root window";
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_ANDROID)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// static
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WindowTreeHost* WindowTreeHost::Create(const gfx::Rect& bounds) {
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This is only hit for tests and ash, right now these aren't an issue so
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // adding the CHECK.
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(sky): decide if we want a factory.
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(false);
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return NULL;
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// static
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WindowTreeHost* WindowTreeHost::GetForAcceleratedWidget(
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    gfx::AcceleratedWidget widget) {
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reinterpret_cast<WindowTreeHost*>(
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ui::ViewProp::GetValue(widget, kWindowTreeHostForAcceleratedWidget));
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::InitHost() {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InitCompositor();
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UpdateRootWindowSize(GetBounds().size());
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Env::GetInstance()->NotifyHostInitialized(this);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  window()->Show();
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::InitCompositor() {
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  compositor_->SetScaleAndSize(GetDeviceScaleFactorFromDisplay(window()),
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               GetBounds().size());
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  compositor_->SetRootLayer(window()->layer());
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::AddObserver(WindowTreeHostObserver* observer) {
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  observers_.AddObserver(observer);
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::RemoveObserver(WindowTreeHostObserver* observer) {
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  observers_.RemoveObserver(observer);
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventProcessor* WindowTreeHost::event_processor() {
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return dispatcher();
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)gfx::Transform WindowTreeHost::GetRootTransform() const {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  float scale = ui::GetDeviceScaleFactor(window()->layer());
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Transform transform;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  transform.Scale(scale, scale);
92a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  transform *= window()->layer()->transform();
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return transform;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid WindowTreeHost::SetRootTransform(const gfx::Transform& transform) {
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  window()->SetTransform(transform);
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  UpdateRootWindowSize(GetBounds().size());
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)gfx::Transform WindowTreeHost::GetInverseRootTransform() const {
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::Transform invert;
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::Transform transform = GetRootTransform();
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (!transform.GetInverse(&invert))
105a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return transform;
106a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return invert;
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::UpdateRootWindowSize(const gfx::Size& host_size) {
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::Rect bounds(host_size);
111a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  gfx::RectF new_bounds(ui::ConvertRectToDIP(window()->layer(), bounds));
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  window()->layer()->transform().TransformRect(&new_bounds);
113a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  window()->SetBounds(gfx::Rect(gfx::ToFlooredSize(new_bounds.size())));
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::ConvertPointToNativeScreen(gfx::Point* point) const {
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ConvertPointToHost(point);
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point location = GetLocationOnNativeScreen();
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  point->Offset(location.x(), location.y());
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::ConvertPointFromNativeScreen(gfx::Point* point) const {
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gfx::Point location = GetLocationOnNativeScreen();
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  point->Offset(-location.x(), -location.y());
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ConvertPointFromHost(point);
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::ConvertPointToHost(gfx::Point* point) const {
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point3F point_3f(*point);
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetRootTransform().TransformPoint(&point_3f);
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *point = gfx::ToFlooredPoint(point_3f.AsPointF());
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::ConvertPointFromHost(gfx::Point* point) const {
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point3F point_3f(*point);
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetInverseRootTransform().TransformPoint(&point_3f);
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *point = gfx::ToFlooredPoint(point_3f.AsPointF());
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::SetCursor(gfx::NativeCursor cursor) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  last_cursor_ = cursor;
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A lot of code seems to depend on NULL cursors actually showing an arrow,
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // so just pass everything along to the host.
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetCursorNative(cursor);
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::OnCursorVisibilityChanged(bool show) {
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Clear any existing mouse hover effects when the cursor becomes invisible.
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Note we do not need to dispatch a mouse enter when the cursor becomes
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // visible because that can only happen in response to a mouse event, which
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // will trigger its own mouse enter.
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!show) {
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    dispatcher()->DispatchMouseExitAtPoint(
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        dispatcher()->GetLastMouseLocationInRoot());
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OnCursorVisibilityChangedNative(show);
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::MoveCursorTo(const gfx::Point& location_in_dip) {
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point host_location(location_in_dip);
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ConvertPointToHost(&host_location);
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MoveCursorToInternal(location_in_dip, host_location);
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::MoveCursorToHostLocation(const gfx::Point& host_location) {
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point root_location(host_location);
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ConvertPointFromHost(&root_location);
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MoveCursorToInternal(root_location, host_location);
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// WindowTreeHost, protected:
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)WindowTreeHost::WindowTreeHost()
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : window_(new Window(NULL)),
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      last_cursor_(ui::kCursorNull) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::DestroyCompositor() {
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  compositor_.reset();
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::DestroyDispatcher() {
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  delete window_;
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  window_ = NULL;
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  dispatcher_.reset();
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(beng): this comment is no longer quite valid since this function
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // isn't called from WED, and WED isn't a subclass of Window. So it seems
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // like we could just rely on ~Window now.
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Destroy child windows while we're still valid. This is also done by
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // ~Window, but by that time any calls to virtual methods overriden here (such
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // as GetRootWindow()) result in Window's implementation. By destroying here
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // we ensure GetRootWindow() still returns this.
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //window()->RemoveOrDestroyChildren();
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::CreateCompositor(
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::AcceleratedWidget accelerated_widget) {
201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(Env::GetInstance());
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ui::ContextFactory* context_factory = Env::GetInstance()->context_factory();
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(context_factory);
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  compositor_.reset(
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new ui::Compositor(GetAcceleratedWidget(), context_factory));
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(beng): I think this setup should probably all move to a "accelerated
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // widget available" function.
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!dispatcher()) {
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    window()->Init(WINDOW_LAYER_NOT_DRAWN);
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    window()->set_host(this);
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    window()->SetName("RootWindow");
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    window()->SetEventTargeter(
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        scoped_ptr<ui::EventTargeter>(new WindowTargeter()));
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    prop_.reset(new ui::ViewProp(GetAcceleratedWidget(),
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 kWindowTreeHostForAcceleratedWidget,
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 this));
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    dispatcher_.reset(new WindowEventDispatcher(this));
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::OnHostMoved(const gfx::Point& new_location) {
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT1("ui", "WindowTreeHost::OnHostMoved",
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "origin", new_location.ToString());
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FOR_EACH_OBSERVER(WindowTreeHostObserver, observers_,
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    OnHostMoved(this, new_location));
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::OnHostResized(const gfx::Size& new_size) {
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The compositor should have the same size as the native root window host.
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Get the latest scale from display because it might have been changed.
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  compositor_->SetScaleAndSize(GetDeviceScaleFactorFromDisplay(window()),
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               new_size);
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Size layer_size = GetBounds().size();
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The layer, and the observers should be notified of the
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // transformed size of the root window.
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UpdateRootWindowSize(layer_size);
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FOR_EACH_OBSERVER(WindowTreeHostObserver, observers_, OnHostResized(this));
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::OnHostCloseRequested() {
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FOR_EACH_OBSERVER(WindowTreeHostObserver, observers_,
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                    OnHostCloseRequested(this));
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::OnHostActivated() {
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Env::GetInstance()->NotifyHostActivated(this);
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowTreeHost::OnHostLostWindowCapture() {
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* capture_window = client::GetCaptureWindow(window());
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (capture_window && capture_window->GetRootWindow() == window())
254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    capture_window->ReleaseCapture();
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
256a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowTreeHost, private:
259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void WindowTreeHost::MoveCursorToInternal(const gfx::Point& root_location,
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                          const gfx::Point& host_location) {
2625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  last_cursor_request_position_in_host_ = host_location;
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MoveCursorToNative(host_location);
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  client::CursorClient* cursor_client = client::GetCursorClient(window());
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (cursor_client) {
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Display& display =
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        gfx::Screen::GetScreenFor(window())->GetDisplayNearestWindow(window());
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cursor_client->SetDisplay(display);
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  dispatcher()->OnCursorMovedToRootLocation(root_location);
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
273f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace aura
274