OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/display/mirror_window_controller.h" | 5 #include "ash/display/mirror_window_controller.h" |
6 | 6 |
7 #if defined(USE_X11) | 7 #if defined(USE_X11) |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 | 9 |
10 // Xlib.h defines RootWindow. | 10 // Xlib.h defines RootWindow. |
11 #undef RootWindow | 11 #undef RootWindow |
12 #endif | 12 #endif |
13 | 13 |
14 #include "ash/display/display_info.h" | 14 #include "ash/display/display_info.h" |
15 #include "ash/display/display_manager.h" | 15 #include "ash/display/display_manager.h" |
| 16 #include "ash/display/root_window_transformers.h" |
16 #include "ash/host/root_window_host_factory.h" | 17 #include "ash/host/root_window_host_factory.h" |
17 #include "ash/shell.h" | 18 #include "ash/shell.h" |
18 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
19 #include "ui/aura/client/capture_client.h" | 20 #include "ui/aura/client/capture_client.h" |
20 #include "ui/aura/env.h" | 21 #include "ui/aura/env.h" |
21 #include "ui/aura/root_window.h" | 22 #include "ui/aura/root_window.h" |
| 23 #include "ui/aura/root_window_transformer.h" |
22 #include "ui/aura/window_delegate.h" | 24 #include "ui/aura/window_delegate.h" |
23 #include "ui/base/cursor/cursors_aura.h" | 25 #include "ui/base/cursor/cursors_aura.h" |
24 #include "ui/base/hit_test.h" | 26 #include "ui/base/hit_test.h" |
25 #include "ui/base/layout.h" | 27 #include "ui/base/layout.h" |
26 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
27 #include "ui/compositor/compositor.h" | 29 #include "ui/compositor/compositor.h" |
28 #include "ui/gfx/canvas.h" | 30 #include "ui/gfx/canvas.h" |
29 #include "ui/gfx/image/image_skia.h" | 31 #include "ui/gfx/image/image_skia.h" |
30 #include "ui/gfx/native_widget_types.h" | 32 #include "ui/gfx/native_widget_types.h" |
31 | 33 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 67 |
66 } // namespace | 68 } // namespace |
67 | 69 |
68 class CursorWindowDelegate : public aura::WindowDelegate { | 70 class CursorWindowDelegate : public aura::WindowDelegate { |
69 public: | 71 public: |
70 CursorWindowDelegate() {} | 72 CursorWindowDelegate() {} |
71 virtual ~CursorWindowDelegate() {} | 73 virtual ~CursorWindowDelegate() {} |
72 | 74 |
73 // aura::WindowDelegate overrides: | 75 // aura::WindowDelegate overrides: |
74 virtual gfx::Size GetMinimumSize() const OVERRIDE { | 76 virtual gfx::Size GetMinimumSize() const OVERRIDE { |
75 return cursor_image_.size(); | 77 return size_; |
76 } | 78 } |
77 virtual gfx::Size GetMaximumSize() const OVERRIDE { | 79 virtual gfx::Size GetMaximumSize() const OVERRIDE { |
78 return cursor_image_.size(); | 80 return size_; |
79 } | 81 } |
80 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | 82 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, |
81 const gfx::Rect& new_bounds) OVERRIDE { | 83 const gfx::Rect& new_bounds) OVERRIDE { |
82 } | 84 } |
83 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | 85 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { |
84 return gfx::kNullCursor; | 86 return gfx::kNullCursor; |
85 } | 87 } |
86 virtual int GetNonClientComponent( | 88 virtual int GetNonClientComponent( |
87 const gfx::Point& point) const OVERRIDE { | 89 const gfx::Point& point) const OVERRIDE { |
88 return HTNOWHERE; | 90 return HTNOWHERE; |
(...skipping 20 matching lines...) Expand all Loading... |
109 } | 111 } |
110 virtual bool HasHitTestMask() const OVERRIDE { | 112 virtual bool HasHitTestMask() const OVERRIDE { |
111 return false; | 113 return false; |
112 } | 114 } |
113 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} | 115 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} |
114 virtual scoped_refptr<ui::Texture> CopyTexture() OVERRIDE { | 116 virtual scoped_refptr<ui::Texture> CopyTexture() OVERRIDE { |
115 NOTREACHED(); | 117 NOTREACHED(); |
116 return scoped_refptr<ui::Texture>(); | 118 return scoped_refptr<ui::Texture>(); |
117 } | 119 } |
118 | 120 |
119 void SetCursorImage(const gfx::ImageSkia& image) { | 121 // Set the cursor image for the |display|'s scale factor. Note that |
120 cursor_image_ = image; | 122 // mirror window's scale factor is always 1.0f, therefore we need to |
| 123 // take 2x's image and paint as if it's 1x image. |
| 124 void SetCursorImage(const gfx::ImageSkia& image, |
| 125 const gfx::Display& display) { |
| 126 device_scale_factor_ = |
| 127 ui::GetScaleFactorFromScale(display.device_scale_factor()); |
| 128 const gfx::ImageSkiaRep& image_rep = |
| 129 image.GetRepresentation(device_scale_factor_); |
| 130 size_ = image_rep.pixel_size(); |
| 131 cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap()); |
121 } | 132 } |
122 | 133 |
| 134 const gfx::Size size() const { return size_; } |
| 135 |
123 private: | 136 private: |
124 gfx::ImageSkia cursor_image_; | 137 gfx::ImageSkia cursor_image_; |
| 138 ui::ScaleFactor device_scale_factor_; |
| 139 gfx::Size size_; |
125 | 140 |
126 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate); | 141 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate); |
127 }; | 142 }; |
128 | 143 |
129 MirrorWindowController::MirrorWindowController() | 144 MirrorWindowController::MirrorWindowController() |
130 : current_cursor_type_(ui::kCursorNone), | 145 : current_cursor_type_(ui::kCursorNone), |
131 cursor_window_(NULL), | 146 cursor_window_(NULL), |
132 cursor_window_delegate_(new CursorWindowDelegate) { | 147 cursor_window_delegate_(new CursorWindowDelegate) { |
133 } | 148 } |
134 | 149 |
135 MirrorWindowController::~MirrorWindowController() { | 150 MirrorWindowController::~MirrorWindowController() { |
136 // Make sure the root window gets deleted before cursor_window_delegate. | 151 // Make sure the root window gets deleted before cursor_window_delegate. |
137 root_window_.reset(); | 152 Close(); |
138 } | 153 } |
139 | 154 |
140 void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) { | 155 void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) { |
141 static int mirror_root_window_count = 0; | 156 static int mirror_root_window_count = 0; |
| 157 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 158 |
| 159 if (!root_window_.get()) { |
| 160 const gfx::Rect& bounds_in_pixel = display_info.bounds_in_pixel(); |
| 161 aura::RootWindow::CreateParams params(bounds_in_pixel); |
| 162 params.host = Shell::GetInstance()->root_window_host_factory()-> |
| 163 CreateRootWindowHost(bounds_in_pixel); |
| 164 root_window_.reset(new aura::RootWindow(params)); |
| 165 root_window_->SetName( |
| 166 base::StringPrintf("MirrorRootWindow-%d", mirror_root_window_count++)); |
| 167 root_window_->compositor()->SetBackgroundColor(SK_ColorBLACK); |
| 168 // No need to remove RootWindowObserver because |
| 169 // the DisplayManager object outlives RootWindow objects. |
| 170 root_window_->AddRootWindowObserver(display_manager); |
| 171 // TODO(oshima): TouchHUD is using idkey. |
| 172 root_window_->SetProperty(internal::kDisplayIdKey, display_info.id()); |
| 173 root_window_->Init(); |
| 174 #if defined(USE_X11) |
| 175 DisableInput(root_window_->GetAcceleratedWidget()); |
| 176 #endif |
| 177 |
| 178 aura::client::SetCaptureClient(root_window_.get(), new NoneCaptureClient()); |
| 179 root_window_->ShowRootWindow(); |
| 180 |
| 181 // TODO(oshima): Start mirroring. |
| 182 |
| 183 cursor_window_ = new aura::Window(cursor_window_delegate_.get()); |
| 184 cursor_window_->SetTransparent(true); |
| 185 cursor_window_->Init(ui::LAYER_TEXTURED); |
| 186 root_window_->AddChild(cursor_window_); |
| 187 cursor_window_->Show(); |
| 188 } else { |
| 189 root_window_->SetProperty(internal::kDisplayIdKey, display_info.id()); |
| 190 root_window_->SetHostBounds(display_info.bounds_in_pixel()); |
| 191 } |
| 192 |
| 193 const DisplayInfo& source_display_info = display_manager->GetDisplayInfo( |
| 194 Shell::GetScreen()->GetPrimaryDisplay().id()); |
| 195 DCHECK(display_manager->mirrored_display().is_valid()); |
| 196 scoped_ptr<aura::RootWindowTransformer> transformer( |
| 197 internal::CreateRootWindowTransformerForMirroredDisplay( |
| 198 source_display_info, |
| 199 display_info)); |
| 200 root_window_->SetRootWindowTransformer(transformer.Pass()); |
| 201 |
| 202 UpdateCursorLocation(); |
| 203 } |
| 204 |
| 205 void MirrorWindowController::UpdateWindow() { |
142 if (root_window_.get()) { | 206 if (root_window_.get()) { |
143 root_window_->SetHostBounds(display_info.bounds_in_pixel()); | 207 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
144 return; | 208 const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo( |
| 209 display_manager->mirrored_display().id()); |
| 210 UpdateWindow(mirror_display_info); |
145 } | 211 } |
146 Shell* shell = Shell::GetInstance(); | |
147 const gfx::Rect& bounds_in_pixel = display_info.bounds_in_pixel(); | |
148 aura::RootWindow::CreateParams params(bounds_in_pixel); | |
149 params.host = shell->root_window_host_factory()-> | |
150 CreateRootWindowHost(bounds_in_pixel); | |
151 root_window_.reset(new aura::RootWindow(params)); | |
152 root_window_->SetName( | |
153 base::StringPrintf("MirrorRootWindow-%d", mirror_root_window_count++)); | |
154 root_window_->compositor()->SetBackgroundColor(SK_ColorBLACK); | |
155 // No need to remove RootWindowObserver because | |
156 // the DisplayManager object outlives RootWindow objects. | |
157 root_window_->AddRootWindowObserver(shell->display_manager()); | |
158 // TODO(oshima): TouchHUD is using idkey. | |
159 root_window_->SetProperty(internal::kDisplayIdKey, display_info.id()); | |
160 root_window_->Init(); | |
161 #if defined(USE_X11) | |
162 DisableInput(root_window_->GetAcceleratedWidget()); | |
163 #endif | |
164 aura::client::SetCaptureClient(root_window_.get(), new NoneCaptureClient()); | |
165 root_window_->ShowRootWindow(); | |
166 | |
167 cursor_window_ = new aura::Window(cursor_window_delegate_.get()); | |
168 cursor_window_->SetTransparent(true); | |
169 cursor_window_->Init(ui::LAYER_TEXTURED); | |
170 root_window_->AddChild(cursor_window_); | |
171 cursor_window_->Show(); | |
172 } | 212 } |
173 | 213 |
174 void MirrorWindowController::Close() { | 214 void MirrorWindowController::Close() { |
175 if (root_window_.get()) { | 215 if (root_window_.get()) { |
| 216 root_window_->RemoveRootWindowObserver( |
| 217 Shell::GetInstance()->display_manager()); |
176 NoneCaptureClient* capture_client = static_cast<NoneCaptureClient*>( | 218 NoneCaptureClient* capture_client = static_cast<NoneCaptureClient*>( |
177 aura::client::GetCaptureClient(root_window_.get())); | 219 aura::client::GetCaptureClient(root_window_.get())); |
178 delete capture_client; | 220 delete capture_client; |
179 root_window_.reset(); | 221 root_window_.reset(); |
180 cursor_window_ = NULL; | 222 cursor_window_ = NULL; |
181 } | 223 } |
182 } | 224 } |
183 | 225 |
184 void MirrorWindowController::UpdateCursorLocation() { | 226 void MirrorWindowController::UpdateCursorLocation() { |
185 if (cursor_window_) { | 227 if (cursor_window_) { |
| 228 // TODO(oshima): Rotate cursor image (including hotpoint). |
186 gfx::Point point = aura::Env::GetInstance()->last_mouse_location(); | 229 gfx::Point point = aura::Env::GetInstance()->last_mouse_location(); |
| 230 Shell::GetPrimaryRootWindow()->ConvertPointToHost(&point); |
187 point.Offset(-hot_point_.x(), -hot_point_.y()); | 231 point.Offset(-hot_point_.x(), -hot_point_.y()); |
188 gfx::Rect bounds = cursor_window_->bounds(); | 232 gfx::Rect bounds = cursor_window_->bounds(); |
189 bounds.set_origin(point); | 233 bounds.set_origin(point); |
190 cursor_window_->SetBounds(bounds); | 234 cursor_window_->SetBounds(bounds); |
191 } | 235 } |
192 } | 236 } |
193 | 237 |
194 void MirrorWindowController::SetMirroredCursor(gfx::NativeCursor cursor) { | 238 void MirrorWindowController::SetMirroredCursor(gfx::NativeCursor cursor) { |
195 if (current_cursor_type_ == cursor.native_type()) | 239 if (current_cursor_type_ == cursor.native_type()) |
196 return; | 240 return; |
197 current_cursor_type_ = cursor.native_type(); | 241 current_cursor_type_ = cursor.native_type(); |
198 int resource_id; | 242 int resource_id; |
| 243 const gfx::Display& display = Shell::GetScreen()->GetPrimaryDisplay(); |
199 bool success = ui::GetCursorDataFor( | 244 bool success = ui::GetCursorDataFor( |
200 current_cursor_type_, 1.0, &resource_id, &hot_point_); | 245 current_cursor_type_, |
| 246 display.device_scale_factor(), |
| 247 &resource_id, |
| 248 &hot_point_); |
201 if (!success) | 249 if (!success) |
202 return; | 250 return; |
203 const gfx::ImageSkia* image = | 251 const gfx::ImageSkia* image = |
204 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); | 252 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); |
205 cursor_window_delegate_->SetCursorImage(*image); | 253 cursor_window_delegate_->SetCursorImage(*image, display); |
206 if (cursor_window_) { | 254 if (cursor_window_) { |
| 255 cursor_window_->SetBounds(gfx::Rect(cursor_window_delegate_->size())); |
207 cursor_window_->SchedulePaintInRect( | 256 cursor_window_->SchedulePaintInRect( |
208 gfx::Rect(cursor_window_->bounds().size())); | 257 gfx::Rect(cursor_window_->bounds().size())); |
209 UpdateCursorLocation(); | 258 UpdateCursorLocation(); |
210 } | 259 } |
211 } | 260 } |
212 | 261 |
213 void MirrorWindowController::SetMirroredCursorVisibility(bool visible) { | 262 void MirrorWindowController::SetMirroredCursorVisibility(bool visible) { |
214 if (cursor_window_) | 263 if (cursor_window_) |
215 visible ? cursor_window_->Show() : cursor_window_->Hide(); | 264 visible ? cursor_window_->Show() : cursor_window_->Hide(); |
216 } | 265 } |
217 | 266 |
218 } // namespace internal | 267 } // namespace internal |
219 } // namespace ash | 268 } // namespace ash |
OLD | NEW |