mirror_window_controller.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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
37namespace ash {
38namespace internal {
39namespace {
40
41#if defined(USE_X11)
42// Mirror window shouldn't handle input events.
43void DisableInput(XID window) {
44  long event_mask = ExposureMask | VisibilityChangeMask |
45      StructureNotifyMask | PropertyChangeMask;
46  XSelectInput(base::MessagePumpX11::GetDefaultXDisplay(),
47               window, event_mask);
48}
49#endif
50
51class NoneCaptureClient : public aura::client::CaptureClient {
52 public:
53  NoneCaptureClient() {}
54  virtual ~NoneCaptureClient() {}
55
56 private:
57  // Does a capture on the |window|.
58  virtual void SetCapture(aura::Window* window) OVERRIDE {}
59
60  // Releases a capture from the |window|.
61  virtual void ReleaseCapture(aura::Window* window) OVERRIDE {}
62
63  // Returns the current capture window.
64  virtual aura::Window* GetCaptureWindow() OVERRIDE {
65    return NULL;
66  }
67
68  DISALLOW_COPY_AND_ASSIGN(NoneCaptureClient);
69};
70
71}  // namespace
72
73class CursorWindowDelegate : public aura::WindowDelegate {
74 public:
75  CursorWindowDelegate() {}
76  virtual ~CursorWindowDelegate() {}
77
78  // aura::WindowDelegate overrides:
79  virtual gfx::Size GetMinimumSize() const OVERRIDE {
80    return size_;
81  }
82  virtual gfx::Size GetMaximumSize() const OVERRIDE {
83    return size_;
84  }
85  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
86                               const gfx::Rect& new_bounds) OVERRIDE {
87  }
88  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
89    return gfx::kNullCursor;
90  }
91  virtual int GetNonClientComponent(
92      const gfx::Point& point) const OVERRIDE {
93    return HTNOWHERE;
94  }
95  virtual bool ShouldDescendIntoChildForEventHandling(
96      aura::Window* child,
97      const gfx::Point& location) OVERRIDE {
98    return false;
99  }
100  virtual bool CanFocus() OVERRIDE {
101    return false;
102  }
103  virtual void OnCaptureLost() OVERRIDE {
104  }
105  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
106    canvas->DrawImageInt(cursor_image_, 0, 0);
107  }
108  virtual void OnDeviceScaleFactorChanged(
109      float device_scale_factor) OVERRIDE {
110  }
111  virtual void OnWindowDestroying() OVERRIDE {}
112  virtual void OnWindowDestroyed() OVERRIDE {}
113  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
114  }
115  virtual bool HasHitTestMask() const OVERRIDE {
116    return false;
117  }
118  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
119  virtual void DidRecreateLayer(ui::Layer* old_layer,
120                                ui::Layer* new_layer) OVERRIDE {}
121
122  // Set the cursor image for the |display|'s scale factor.  Note that
123  // mirror window's scale factor is always 1.0f, therefore we need to
124  // take 2x's image and paint as if it's 1x image.
125  void SetCursorImage(const gfx::ImageSkia& image,
126                      const gfx::Display& display) {
127    device_scale_factor_ =
128        ui::GetScaleFactorFromScale(display.device_scale_factor());
129    const gfx::ImageSkiaRep& image_rep =
130        image.GetRepresentation(device_scale_factor_);
131    size_ = image_rep.pixel_size();
132    cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap());
133  }
134
135  const gfx::Size size() const { return size_; }
136
137 private:
138  gfx::ImageSkia cursor_image_;
139  ui::ScaleFactor device_scale_factor_;
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_pixel = display_info.bounds_in_pixel();
162    aura::RootWindow::CreateParams params(bounds_in_pixel);
163    params.host = Shell::GetInstance()->root_window_host_factory()->
164        CreateRootWindowHost(bounds_in_pixel);
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_pixel());
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