mirror_window_controller.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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()->
191        CreateReflector(Shell::GetPrimaryRootWindow()->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->mirrored_display().is_valid());
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    delete capture_client;
233
234    root_window_->RemoveRootWindowObserver(
235        Shell::GetInstance()->display_controller());
236    root_window_->RemoveRootWindowObserver(this);
237    root_window_.reset();
238    cursor_window_ = NULL;
239  }
240}
241
242void MirrorWindowController::UpdateCursorLocation() {
243  if (cursor_window_) {
244    // TODO(oshima): Rotate cursor image (including hotpoint).
245    gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
246    Shell::GetPrimaryRootWindow()->ConvertPointToHost(&point);
247    point.Offset(-hot_point_.x(), -hot_point_.y());
248    gfx::Rect bounds = cursor_window_->bounds();
249    bounds.set_origin(point);
250    cursor_window_->SetBounds(bounds);
251  }
252}
253
254void MirrorWindowController::SetMirroredCursor(gfx::NativeCursor cursor) {
255  const gfx::Display& display = Shell::GetScreen()->GetPrimaryDisplay();
256  if (current_cursor_type_ == cursor.native_type() &&
257      current_cursor_rotation_ == display.rotation())
258    return;
259  current_cursor_type_ = cursor.native_type();
260  current_cursor_rotation_ = display.rotation();
261  int resource_id;
262  bool success = ui::GetCursorDataFor(
263      ui::CURSOR_SET_NORMAL,  // Not support custom cursor set.
264      current_cursor_type_,
265      display.device_scale_factor(),
266      &resource_id,
267      &hot_point_);
268  if (!success)
269    return;
270  const gfx::ImageSkia* image =
271      ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
272  gfx::ImageSkia rotated = *image;
273  switch (current_cursor_rotation_) {
274    case gfx::Display::ROTATE_0:
275      break;
276    case gfx::Display::ROTATE_90:
277      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
278          *image, SkBitmapOperations::ROTATION_90_CW);
279      hot_point_.SetPoint(
280          rotated.width() - hot_point_.y(),
281          hot_point_.x());
282      break;
283    case gfx::Display::ROTATE_180:
284      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
285          *image, SkBitmapOperations::ROTATION_180_CW);
286      hot_point_.SetPoint(
287          rotated.height() - hot_point_.x(),
288          rotated.width() - hot_point_.y());
289      break;
290    case gfx::Display::ROTATE_270:
291      rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
292          *image, SkBitmapOperations::ROTATION_270_CW);
293      hot_point_.SetPoint(
294          hot_point_.y(),
295          rotated.height() - hot_point_.x());
296      break;
297  }
298  cursor_window_delegate_->SetCursorImage(rotated, display);
299
300  if (cursor_window_) {
301    cursor_window_->SetBounds(gfx::Rect(cursor_window_delegate_->size()));
302    cursor_window_->SchedulePaintInRect(
303        gfx::Rect(cursor_window_->bounds().size()));
304    UpdateCursorLocation();
305  }
306}
307
308void MirrorWindowController::SetMirroredCursorVisibility(bool visible) {
309  if (cursor_window_)
310    visible ? cursor_window_->Show() : cursor_window_->Hide();
311}
312
313void MirrorWindowController::OnRootWindowHostResized(
314    const aura::RootWindow* root) {
315  // Do not use |old_size| as it contains RootWindow's (but not host's) size,
316  // and this parameter wil be removed soon.
317  if (mirror_window_host_size_ == root->GetHostSize())
318    return;
319  mirror_window_host_size_ = root->GetHostSize();
320  reflector_->OnMirroringCompositorResized();
321  root_window_->SetRootWindowTransformer(
322      CreateRootWindowTransformer().Pass());
323  UpdateCursorLocation();
324}
325
326
327scoped_ptr<aura::RootWindowTransformer>
328MirrorWindowController::CreateRootWindowTransformer() const {
329  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
330  const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
331      display_manager->mirrored_display().id());
332  const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
333      Shell::GetScreen()->GetPrimaryDisplay().id());
334  DCHECK(display_manager->mirrored_display().is_valid());
335  return scoped_ptr<aura::RootWindowTransformer>(
336      internal::CreateRootWindowTransformerForMirroredDisplay(
337          source_display_info,
338          mirror_display_info));
339}
340
341}  // namespace internal
342}  // namespace ash
343