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 "ash/display/mirror_window_controller.h"
10 // Xlib.h defines RootWindow.
14 #include "ash/display/display_controller.h"
15 #include "ash/display/display_info.h"
16 #include "ash/display/display_manager.h"
17 #include "ash/display/root_window_transformers.h"
18 #include "ash/host/root_window_host_factory.h"
19 #include "ash/root_window_settings.h"
20 #include "ash/shell.h"
21 #include "base/strings/stringprintf.h"
22 #include "ui/aura/client/capture_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/root_window_transformer.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/base/cursor/cursors_aura.h"
28 #include "ui/base/hit_test.h"
29 #include "ui/base/layout.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/compositor/reflector.h"
32 #include "ui/gfx/canvas.h"
33 #include "ui/gfx/image/image_skia.h"
34 #include "ui/gfx/image/image_skia_operations.h"
35 #include "ui/gfx/native_widget_types.h"
38 #include "ui/gfx/x/x11_types.h"
46 // Mirror window shouldn't handle input events.
47 void DisableInput(XID window) {
48 long event_mask = ExposureMask | VisibilityChangeMask |
49 StructureNotifyMask | PropertyChangeMask;
50 XSelectInput(gfx::GetXDisplay(), window, event_mask);
54 class NoneCaptureClient : public aura::client::CaptureClient {
56 NoneCaptureClient() {}
57 virtual ~NoneCaptureClient() {}
60 // Does a capture on the |window|.
61 virtual void SetCapture(aura::Window* window) OVERRIDE {}
63 // Releases a capture from the |window|.
64 virtual void ReleaseCapture(aura::Window* window) OVERRIDE {}
66 // Returns the current capture window.
67 virtual aura::Window* GetCaptureWindow() OVERRIDE {
71 DISALLOW_COPY_AND_ASSIGN(NoneCaptureClient);
76 class CursorWindowDelegate : public aura::WindowDelegate {
78 CursorWindowDelegate() {}
79 virtual ~CursorWindowDelegate() {}
81 // aura::WindowDelegate overrides:
82 virtual gfx::Size GetMinimumSize() const OVERRIDE {
85 virtual gfx::Size GetMaximumSize() const OVERRIDE {
88 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
89 const gfx::Rect& new_bounds) OVERRIDE {
91 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
92 return gfx::kNullCursor;
94 virtual int GetNonClientComponent(
95 const gfx::Point& point) const OVERRIDE {
98 virtual bool ShouldDescendIntoChildForEventHandling(
100 const gfx::Point& location) OVERRIDE {
103 virtual bool CanFocus() OVERRIDE {
106 virtual void OnCaptureLost() OVERRIDE {
108 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
109 canvas->DrawImageInt(cursor_image_, 0, 0);
111 virtual void OnDeviceScaleFactorChanged(
112 float device_scale_factor) OVERRIDE {
114 virtual void OnWindowDestroying() OVERRIDE {}
115 virtual void OnWindowDestroyed() OVERRIDE {}
116 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
118 virtual bool HasHitTestMask() const OVERRIDE {
121 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
122 virtual void DidRecreateLayer(ui::Layer* old_layer,
123 ui::Layer* new_layer) OVERRIDE {}
125 // Set the cursor image for the |display|'s scale factor. Note that
126 // mirror window's scale factor is always 1.0f, therefore we need to
127 // take 2x's image and paint as if it's 1x image.
128 void SetCursorImage(const gfx::ImageSkia& image,
129 const gfx::Display& display) {
130 const gfx::ImageSkiaRep& image_rep =
131 image.GetRepresentation(display.device_scale_factor());
132 size_ = image_rep.pixel_size();
133 cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap());
136 const gfx::Size size() const { return size_; }
139 gfx::ImageSkia cursor_image_;
142 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
145 MirrorWindowController::MirrorWindowController()
146 : current_cursor_type_(ui::kCursorNone),
147 current_cursor_rotation_(gfx::Display::ROTATE_0),
148 cursor_window_(NULL),
149 cursor_window_delegate_(new CursorWindowDelegate) {
152 MirrorWindowController::~MirrorWindowController() {
153 // Make sure the root window gets deleted before cursor_window_delegate.
157 void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) {
158 static int mirror_root_window_count = 0;
160 if (!root_window_.get()) {
161 const gfx::Rect& bounds_in_native = display_info.bounds_in_native();
162 aura::RootWindow::CreateParams params(bounds_in_native);
163 params.host = Shell::GetInstance()->root_window_host_factory()->
164 CreateRootWindowHost(bounds_in_native);
165 root_window_.reset(new aura::RootWindow(params));
166 root_window_->SetName(
167 base::StringPrintf("MirrorRootWindow-%d", mirror_root_window_count++));
168 root_window_->compositor()->SetBackgroundColor(SK_ColorBLACK);
169 // No need to remove RootWindowObserver because
170 // the DisplayController object outlives RootWindow objects.
171 root_window_->AddRootWindowObserver(
172 Shell::GetInstance()->display_controller());
173 root_window_->AddRootWindowObserver(this);
174 // TODO(oshima): TouchHUD is using idkey.
175 InitRootWindowSettings(root_window_.get())->display_id = display_info.id();
176 root_window_->Init();
178 DisableInput(root_window_->GetAcceleratedWidget());
181 aura::client::SetCaptureClient(root_window_.get(), new NoneCaptureClient());
182 root_window_->ShowRootWindow();
184 // TODO(oshima): Start mirroring.
185 aura::Window* mirror_window = new aura::Window(NULL);
186 mirror_window->Init(ui::LAYER_TEXTURED);
187 root_window_->AddChild(mirror_window);
188 mirror_window->SetBounds(root_window_->bounds());
189 mirror_window->Show();
190 reflector_ = ui::ContextFactory::GetInstance()->CreateReflector(
191 Shell::GetPrimaryRootWindow()->GetDispatcher()->compositor(),
192 mirror_window->layer());
194 cursor_window_ = new aura::Window(cursor_window_delegate_.get());
195 cursor_window_->SetTransparent(true);
196 cursor_window_->Init(ui::LAYER_TEXTURED);
197 root_window_->AddChild(cursor_window_);
198 cursor_window_->Show();
200 GetRootWindowSettings(root_window_.get())->display_id = display_info.id();
201 root_window_->SetHostBounds(display_info.bounds_in_native());
204 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
205 const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
206 Shell::GetScreen()->GetPrimaryDisplay().id());
207 DCHECK(display_manager->IsMirrored());
208 scoped_ptr<aura::RootWindowTransformer> transformer(
209 internal::CreateRootWindowTransformerForMirroredDisplay(
212 root_window_->SetRootWindowTransformer(transformer.Pass());
214 UpdateCursorLocation();
217 void MirrorWindowController::UpdateWindow() {
218 if (root_window_.get()) {
219 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
220 const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
221 display_manager->mirrored_display_id());
222 UpdateWindow(mirror_display_info);
226 void MirrorWindowController::Close() {
227 if (root_window_.get()) {
228 ui::ContextFactory::GetInstance()->RemoveReflector(reflector_);
230 NoneCaptureClient* capture_client = static_cast<NoneCaptureClient*>(
231 aura::client::GetCaptureClient(root_window_.get()));
232 aura::client::SetCaptureClient(root_window_.get(), NULL);
233 delete capture_client;
235 root_window_->RemoveRootWindowObserver(
236 Shell::GetInstance()->display_controller());
237 root_window_->RemoveRootWindowObserver(this);
238 root_window_.reset();
239 cursor_window_ = NULL;
243 void MirrorWindowController::UpdateCursorLocation() {
244 if (cursor_window_) {
245 // TODO(oshima): Rotate cursor image (including hotpoint).
246 gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
247 Shell::GetPrimaryRootWindow()->GetDispatcher()->ConvertPointToHost(&point);
248 point.Offset(-hot_point_.x(), -hot_point_.y());
249 gfx::Rect bounds = cursor_window_->bounds();
250 bounds.set_origin(point);
251 cursor_window_->SetBounds(bounds);
255 void MirrorWindowController::SetMirroredCursor(gfx::NativeCursor cursor) {
256 const gfx::Display& display = Shell::GetScreen()->GetPrimaryDisplay();
257 if (current_cursor_type_ == cursor.native_type() &&
258 current_cursor_rotation_ == display.rotation())
260 current_cursor_type_ = cursor.native_type();
261 current_cursor_rotation_ = display.rotation();
263 bool success = ui::GetCursorDataFor(
264 ui::CURSOR_SET_NORMAL, // Not support custom cursor set.
265 current_cursor_type_,
266 display.device_scale_factor(),
271 const gfx::ImageSkia* image =
272 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
273 gfx::ImageSkia rotated = *image;
274 switch (current_cursor_rotation_) {
275 case gfx::Display::ROTATE_0:
277 case gfx::Display::ROTATE_90:
278 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
279 *image, SkBitmapOperations::ROTATION_90_CW);
281 rotated.width() - hot_point_.y(),
284 case gfx::Display::ROTATE_180:
285 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
286 *image, SkBitmapOperations::ROTATION_180_CW);
288 rotated.height() - hot_point_.x(),
289 rotated.width() - hot_point_.y());
291 case gfx::Display::ROTATE_270:
292 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
293 *image, SkBitmapOperations::ROTATION_270_CW);
296 rotated.height() - hot_point_.x());
299 cursor_window_delegate_->SetCursorImage(rotated, display);
301 if (cursor_window_) {
302 cursor_window_->SetBounds(gfx::Rect(cursor_window_delegate_->size()));
303 cursor_window_->SchedulePaintInRect(
304 gfx::Rect(cursor_window_->bounds().size()));
305 UpdateCursorLocation();
309 void MirrorWindowController::SetMirroredCursorVisibility(bool visible) {
311 visible ? cursor_window_->Show() : cursor_window_->Hide();
314 void MirrorWindowController::OnRootWindowHostResized(
315 const aura::RootWindow* root) {
316 // Do not use |old_size| as it contains RootWindow's (but not host's) size,
317 // and this parameter wil be removed soon.
318 if (mirror_window_host_size_ == root->GetHostSize())
320 mirror_window_host_size_ = root->GetHostSize();
321 reflector_->OnMirroringCompositorResized();
322 root_window_->SetRootWindowTransformer(
323 CreateRootWindowTransformer().Pass());
324 UpdateCursorLocation();
328 scoped_ptr<aura::RootWindowTransformer>
329 MirrorWindowController::CreateRootWindowTransformer() const {
330 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
331 const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
332 display_manager->mirrored_display_id());
333 const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
334 Shell::GetScreen()->GetPrimaryDisplay().id());
335 DCHECK(display_manager->IsMirrored());
336 return scoped_ptr<aura::RootWindowTransformer>(
337 internal::CreateRootWindowTransformerForMirroredDisplay(
339 mirror_display_info));
342 } // namespace internal