mirror_window_controller.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#include "ash/display/mirror_window_controller.h"
6
7#if defined(USE_X11)
8#include <X11/Xlib.h>
9
10// Xlib.h defines RootWindow.
11#undef RootWindow
12#endif
13
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"
36
37#if defined(USE_X11)
38#include "ui/gfx/x/x11_types.h"
39#endif
40
41namespace ash {
42namespace internal {
43namespace {
44
45#if defined(USE_X11)
46// Mirror window shouldn't handle input events.
47void DisableInput(XID window) {
48  long event_mask = ExposureMask | VisibilityChangeMask |
49      StructureNotifyMask | PropertyChangeMask;
50  XSelectInput(gfx::GetXDisplay(), window, event_mask);
51}
52#endif
53
54class NoneCaptureClient : public aura::client::CaptureClient {
55 public:
56  NoneCaptureClient() {}
57  virtual ~NoneCaptureClient() {}
58
59 private:
60  // Does a capture on the |window|.
61  virtual void SetCapture(aura::Window* window) OVERRIDE {}
62
63  // Releases a capture from the |window|.
64  virtual void ReleaseCapture(aura::Window* window) OVERRIDE {}
65
66  // Returns the current capture window.
67  virtual aura::Window* GetCaptureWindow() OVERRIDE {
68    return NULL;
69  }
70
71  DISALLOW_COPY_AND_ASSIGN(NoneCaptureClient);
72};
73
74}  // namespace
75
76class CursorWindowDelegate : public aura::WindowDelegate {
77 public:
78  CursorWindowDelegate() {}
79  virtual ~CursorWindowDelegate() {}
80
81  // aura::WindowDelegate overrides:
82  virtual gfx::Size GetMinimumSize() const OVERRIDE {
83    return size_;
84  }
85  virtual gfx::Size GetMaximumSize() const OVERRIDE {
86    return size_;
87  }
88  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
89                               const gfx::Rect& new_bounds) OVERRIDE {
90  }
91  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
92    return gfx::kNullCursor;
93  }
94  virtual int GetNonClientComponent(
95      const gfx::Point& point) const OVERRIDE {
96    return HTNOWHERE;
97  }
98  virtual bool ShouldDescendIntoChildForEventHandling(
99      aura::Window* child,
100      const gfx::Point& location) OVERRIDE {
101    return false;
102  }
103  virtual bool CanFocus() OVERRIDE {
104    return false;
105  }
106  virtual void OnCaptureLost() OVERRIDE {
107  }
108  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
109    canvas->DrawImageInt(cursor_image_, 0, 0);
110  }
111  virtual void OnDeviceScaleFactorChanged(
112      float device_scale_factor) OVERRIDE {
113  }
114  virtual void OnWindowDestroying() OVERRIDE {}
115  virtual void OnWindowDestroyed() OVERRIDE {}
116  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
117  }
118  virtual bool HasHitTestMask() const OVERRIDE {
119    return false;
120  }
121  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
122  virtual void DidRecreateLayer(ui::Layer* old_layer,
123                                ui::Layer* new_layer) OVERRIDE {}
124
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());
134  }
135
136  const gfx::Size size() const { return size_; }
137
138 private:
139  gfx::ImageSkia cursor_image_;
140  gfx::Size size_;
141
142  DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
143};
144
145MirrorWindowController::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) {
150}
151
152MirrorWindowController::~MirrorWindowController() {
153  // Make sure the root window gets deleted before cursor_window_delegate.
154  Close();
155}
156
157void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) {
158  static int mirror_root_window_count = 0;
159
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();
177#if defined(USE_X11)
178    DisableInput(root_window_->GetAcceleratedWidget());
179#endif
180
181    aura::client::SetCaptureClient(root_window_.get(), new NoneCaptureClient());
182    root_window_->ShowRootWindow();
183
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());
193
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();
199  } else {
200    GetRootWindowSettings(root_window_.get())->display_id = display_info.id();
201    root_window_->SetHostBounds(display_info.bounds_in_native());
202  }
203
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(
210          source_display_info,
211          display_info));
212  root_window_->SetRootWindowTransformer(transformer.Pass());
213
214  UpdateCursorLocation();
215}
216
217void 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);
223  }
224}
225
226void MirrorWindowController::Close() {
227  if (root_window_.get()) {
228    ui::ContextFactory::GetInstance()->RemoveReflector(reflector_);
229    reflector_ = NULL;
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;
234
235    root_window_->RemoveRootWindowObserver(
236        Shell::GetInstance()->display_controller());
237    root_window_->RemoveRootWindowObserver(this);
238    root_window_.reset();
239    cursor_window_ = NULL;
240  }
241}
242
243void 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);
252  }
253}
254
255void 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())
259    return;
260  current_cursor_type_ = cursor.native_type();
261  current_cursor_rotation_ = display.rotation();
262  int resource_id;
263  bool success = ui::GetCursorDataFor(
264      ui::CURSOR_SET_NORMAL,  // Not support custom cursor set.
265      current_cursor_type_,
266      display.device_scale_factor(),
267      &resource_id,
268      &hot_point_);
269  if (!success)
270    return;
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:
276      break;
277    case gfx::Display::ROTATE_90:
278      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
279          *image, SkBitmapOperations::ROTATION_90_CW);
280      hot_point_.SetPoint(
281          rotated.width() - hot_point_.y(),
282          hot_point_.x());
283      break;
284    case gfx::Display::ROTATE_180:
285      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
286          *image, SkBitmapOperations::ROTATION_180_CW);
287      hot_point_.SetPoint(
288          rotated.height() - hot_point_.x(),
289          rotated.width() - hot_point_.y());
290      break;
291    case gfx::Display::ROTATE_270:
292      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
293          *image, SkBitmapOperations::ROTATION_270_CW);
294      hot_point_.SetPoint(
295          hot_point_.y(),
296          rotated.height() - hot_point_.x());
297      break;
298  }
299  cursor_window_delegate_->SetCursorImage(rotated, display);
300
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();
306  }
307}
308
309void MirrorWindowController::SetMirroredCursorVisibility(bool visible) {
310  if (cursor_window_)
311    visible ? cursor_window_->Show() : cursor_window_->Hide();
312}
313
314void 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())
319    return;
320  mirror_window_host_size_ = root->GetHostSize();
321  reflector_->OnMirroringCompositorResized();
322  root_window_->SetRootWindowTransformer(
323      CreateRootWindowTransformer().Pass());
324  UpdateCursorLocation();
325}
326
327
328scoped_ptr<aura::RootWindowTransformer>
329MirrorWindowController::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(
338          source_display_info,
339          mirror_display_info));
340}
341
342}  // namespace internal
343}  // namespace ash
344