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.
5 #include "ui/aura/window_tree_host.h"
7 #include "base/debug/trace_event.h"
8 #include "ui/aura/client/capture_client.h"
9 #include "ui/aura/client/cursor_client.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/aura/window_targeter.h"
14 #include "ui/aura/window_tree_host_observer.h"
15 #include "ui/base/view_prop.h"
16 #include "ui/compositor/dip_util.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/insets.h"
20 #include "ui/gfx/point.h"
21 #include "ui/gfx/point3_f.h"
22 #include "ui/gfx/point_conversions.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/gfx/size_conversions.h"
28 const char kWindowTreeHostForAcceleratedWidget
[] =
29 "__AURA_WINDOW_TREE_HOST_ACCELERATED_WIDGET__";
31 float GetDeviceScaleFactorFromDisplay(Window
* window
) {
32 gfx::Display display
= gfx::Screen::GetScreenFor(window
)->
33 GetDisplayNearestWindow(window
);
34 DCHECK(display
.is_valid());
35 return display
.device_scale_factor();
38 ////////////////////////////////////////////////////////////////////////////////
39 // WindowTreeHost, public:
41 WindowTreeHost::~WindowTreeHost() {
42 DCHECK(!compositor_
) << "compositor must be destroyed before root window";
45 #if defined(OS_ANDROID)
47 WindowTreeHost
* WindowTreeHost::Create(const gfx::Rect
& bounds
) {
48 // This is only hit for tests and ash, right now these aren't an issue so
50 // TODO(sky): decide if we want a factory.
57 WindowTreeHost
* WindowTreeHost::GetForAcceleratedWidget(
58 gfx::AcceleratedWidget widget
) {
59 return reinterpret_cast<WindowTreeHost
*>(
60 ui::ViewProp::GetValue(widget
, kWindowTreeHostForAcceleratedWidget
));
63 void WindowTreeHost::InitHost() {
65 UpdateRootWindowSize(GetBounds().size());
66 Env::GetInstance()->NotifyHostInitialized(this);
70 void WindowTreeHost::InitCompositor() {
71 compositor_
->SetScaleAndSize(GetDeviceScaleFactorFromDisplay(window()),
73 compositor_
->SetRootLayer(window()->layer());
76 void WindowTreeHost::AddObserver(WindowTreeHostObserver
* observer
) {
77 observers_
.AddObserver(observer
);
80 void WindowTreeHost::RemoveObserver(WindowTreeHostObserver
* observer
) {
81 observers_
.RemoveObserver(observer
);
84 ui::EventProcessor
* WindowTreeHost::event_processor() {
88 gfx::Transform
WindowTreeHost::GetRootTransform() const {
89 float scale
= ui::GetDeviceScaleFactor(window()->layer());
90 gfx::Transform transform
;
91 transform
.Scale(scale
, scale
);
92 transform
*= window()->layer()->transform();
96 void WindowTreeHost::SetRootTransform(const gfx::Transform
& transform
) {
97 window()->SetTransform(transform
);
98 UpdateRootWindowSize(GetBounds().size());
101 gfx::Transform
WindowTreeHost::GetInverseRootTransform() const {
102 gfx::Transform invert
;
103 gfx::Transform transform
= GetRootTransform();
104 if (!transform
.GetInverse(&invert
))
109 void WindowTreeHost::UpdateRootWindowSize(const gfx::Size
& host_size
) {
110 gfx::Rect
bounds(host_size
);
111 gfx::RectF
new_bounds(ui::ConvertRectToDIP(window()->layer(), bounds
));
112 window()->layer()->transform().TransformRect(&new_bounds
);
113 window()->SetBounds(gfx::Rect(gfx::ToFlooredSize(new_bounds
.size())));
116 void WindowTreeHost::ConvertPointToNativeScreen(gfx::Point
* point
) const {
117 ConvertPointToHost(point
);
118 gfx::Point location
= GetLocationOnNativeScreen();
119 point
->Offset(location
.x(), location
.y());
122 void WindowTreeHost::ConvertPointFromNativeScreen(gfx::Point
* point
) const {
123 gfx::Point location
= GetLocationOnNativeScreen();
124 point
->Offset(-location
.x(), -location
.y());
125 ConvertPointFromHost(point
);
128 void WindowTreeHost::ConvertPointToHost(gfx::Point
* point
) const {
129 gfx::Point3F
point_3f(*point
);
130 GetRootTransform().TransformPoint(&point_3f
);
131 *point
= gfx::ToFlooredPoint(point_3f
.AsPointF());
134 void WindowTreeHost::ConvertPointFromHost(gfx::Point
* point
) const {
135 gfx::Point3F
point_3f(*point
);
136 GetInverseRootTransform().TransformPoint(&point_3f
);
137 *point
= gfx::ToFlooredPoint(point_3f
.AsPointF());
140 void WindowTreeHost::SetCursor(gfx::NativeCursor cursor
) {
141 last_cursor_
= cursor
;
142 // A lot of code seems to depend on NULL cursors actually showing an arrow,
143 // so just pass everything along to the host.
144 SetCursorNative(cursor
);
147 void WindowTreeHost::OnCursorVisibilityChanged(bool show
) {
148 // Clear any existing mouse hover effects when the cursor becomes invisible.
149 // Note we do not need to dispatch a mouse enter when the cursor becomes
150 // visible because that can only happen in response to a mouse event, which
151 // will trigger its own mouse enter.
153 ui::EventDispatchDetails details
= dispatcher()->DispatchMouseExitAtPoint(
154 dispatcher()->GetLastMouseLocationInRoot());
155 if (details
.dispatcher_destroyed
)
159 OnCursorVisibilityChangedNative(show
);
162 void WindowTreeHost::MoveCursorTo(const gfx::Point
& location_in_dip
) {
163 gfx::Point
host_location(location_in_dip
);
164 ConvertPointToHost(&host_location
);
165 MoveCursorToInternal(location_in_dip
, host_location
);
168 void WindowTreeHost::MoveCursorToHostLocation(const gfx::Point
& host_location
) {
169 gfx::Point
root_location(host_location
);
170 ConvertPointFromHost(&root_location
);
171 MoveCursorToInternal(root_location
, host_location
);
174 ////////////////////////////////////////////////////////////////////////////////
175 // WindowTreeHost, protected:
177 WindowTreeHost::WindowTreeHost()
178 : window_(new Window(NULL
)),
179 last_cursor_(ui::kCursorNull
) {
182 void WindowTreeHost::DestroyCompositor() {
186 void WindowTreeHost::DestroyDispatcher() {
191 // TODO(beng): this comment is no longer quite valid since this function
192 // isn't called from WED, and WED isn't a subclass of Window. So it seems
193 // like we could just rely on ~Window now.
194 // Destroy child windows while we're still valid. This is also done by
195 // ~Window, but by that time any calls to virtual methods overriden here (such
196 // as GetRootWindow()) result in Window's implementation. By destroying here
197 // we ensure GetRootWindow() still returns this.
198 //window()->RemoveOrDestroyChildren();
201 void WindowTreeHost::CreateCompositor(
202 gfx::AcceleratedWidget accelerated_widget
) {
203 DCHECK(Env::GetInstance());
204 ui::ContextFactory
* context_factory
= Env::GetInstance()->context_factory();
205 DCHECK(context_factory
);
207 new ui::Compositor(GetAcceleratedWidget(), context_factory
));
208 // TODO(beng): I think this setup should probably all move to a "accelerated
209 // widget available" function.
211 window()->Init(WINDOW_LAYER_NOT_DRAWN
);
212 window()->set_host(this);
213 window()->SetName("RootWindow");
214 window()->SetEventTargeter(
215 scoped_ptr
<ui::EventTargeter
>(new WindowTargeter()));
216 prop_
.reset(new ui::ViewProp(GetAcceleratedWidget(),
217 kWindowTreeHostForAcceleratedWidget
,
219 dispatcher_
.reset(new WindowEventDispatcher(this));
223 void WindowTreeHost::OnHostMoved(const gfx::Point
& new_location
) {
224 TRACE_EVENT1("ui", "WindowTreeHost::OnHostMoved",
225 "origin", new_location
.ToString());
227 FOR_EACH_OBSERVER(WindowTreeHostObserver
, observers_
,
228 OnHostMoved(this, new_location
));
231 void WindowTreeHost::OnHostResized(const gfx::Size
& new_size
) {
232 // The compositor should have the same size as the native root window host.
233 // Get the latest scale from display because it might have been changed.
234 compositor_
->SetScaleAndSize(GetDeviceScaleFactorFromDisplay(window()),
237 gfx::Size layer_size
= GetBounds().size();
238 // The layer, and the observers should be notified of the
239 // transformed size of the root window.
240 UpdateRootWindowSize(layer_size
);
241 FOR_EACH_OBSERVER(WindowTreeHostObserver
, observers_
, OnHostResized(this));
244 void WindowTreeHost::OnHostCloseRequested() {
245 FOR_EACH_OBSERVER(WindowTreeHostObserver
, observers_
,
246 OnHostCloseRequested(this));
249 void WindowTreeHost::OnHostActivated() {
250 Env::GetInstance()->NotifyHostActivated(this);
253 void WindowTreeHost::OnHostLostWindowCapture() {
254 Window
* capture_window
= client::GetCaptureWindow(window());
255 if (capture_window
&& capture_window
->GetRootWindow() == window())
256 capture_window
->ReleaseCapture();
259 ////////////////////////////////////////////////////////////////////////////////
260 // WindowTreeHost, private:
262 void WindowTreeHost::MoveCursorToInternal(const gfx::Point
& root_location
,
263 const gfx::Point
& host_location
) {
264 last_cursor_request_position_in_host_
= host_location
;
265 MoveCursorToNative(host_location
);
266 client::CursorClient
* cursor_client
= client::GetCursorClient(window());
268 const gfx::Display
& display
=
269 gfx::Screen::GetScreenFor(window())->GetDisplayNearestWindow(window());
270 cursor_client
->SetDisplay(display
);
272 dispatcher()->OnCursorMovedToRootLocation(root_location
);