mirror_window_controller.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/mirror_window_controller.h"
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <X11/Xlib.h>
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Xlib.h defines RootWindow.
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#undef RootWindow
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/display/cursor_window_controller.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ash/display/display_controller.h"
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/display_info.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/display_manager.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/display/root_window_transformers.h"
19a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ash/host/ash_window_tree_host.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ash/host/ash_window_tree_host_init_params.h"
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ash/host/root_window_transformer.h"
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ash/root_window_settings.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/shell.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/aura/client/capture_client.h"
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "ui/aura/env.h"
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/aura/window_delegate.h"
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ui/aura/window_tree_host.h"
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/layout.h"
31558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "ui/compositor/reflector.h"
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/gfx/canvas.h"
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/gfx/native_widget_types.h"
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(USE_X11)
3668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/gfx/x/x11_types.h"
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace ash {
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Mirror window shouldn't handle input events.
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void DisableInput(XID window) {
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  long event_mask = ExposureMask | VisibilityChangeMask |
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      StructureNotifyMask | PropertyChangeMask;
4768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  XSelectInput(gfx::GetXDisplay(), window, event_mask);
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NoneCaptureClient : public aura::client::CaptureClient {
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NoneCaptureClient() {}
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~NoneCaptureClient() {}
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Does a capture on the |window|.
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void SetCapture(aura::Window* window) OVERRIDE {}
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Releases a capture from the |window|.
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ReleaseCapture(aura::Window* window) OVERRIDE {}
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the current capture window.
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual aura::Window* GetCaptureWindow() OVERRIDE {
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return NULL;
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual aura::Window* GetGlobalCaptureWindow() OVERRIDE {
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return NULL;
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NoneCaptureClient);
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MirrorWindowController::MirrorWindowController() {}
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)MirrorWindowController::~MirrorWindowController() {
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Make sure the root window gets deleted before cursor_window_delegate.
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Close();
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) {
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static int mirror_host_count = 0;
85a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (!ash_host_.get()) {
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    AshWindowTreeHostInitParams init_params;
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    init_params.initial_bounds = display_info.bounds_in_native();
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ash_host_.reset(AshWindowTreeHost::Create(init_params));
89a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
90a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->window()->SetName(
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringPrintf("MirrorRootWindow-%d", mirror_host_count++));
92a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->compositor()->SetBackgroundColor(SK_ColorBLACK);
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // No need to remove the observer because the DisplayController outlives the
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // host.
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->AddObserver(Shell::GetInstance()->display_controller());
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->AddObserver(this);
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(oshima): TouchHUD is using idkey.
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    InitRootWindowSettings(host->window())->display_id = display_info.id();
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->InitHost();
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    DisableInput(host->GetAcceleratedWidget());
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::client::SetCaptureClient(host->window(), new NoneCaptureClient());
105a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->Show();
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(oshima): Start mirroring.
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    aura::Window* mirror_window = new aura::Window(NULL);
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mirror_window->Init(aura::WINDOW_LAYER_TEXTURED);
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->window()->AddChild(mirror_window);
111a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    mirror_window->SetBounds(host->window()->bounds());
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    mirror_window->Show();
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    reflector_ = aura::Env::GetInstance()->context_factory()->CreateReflector(
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        Shell::GetPrimaryRootWindow()->GetHost()->compositor(),
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        mirror_window->layer());
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    GetRootWindowSettings(host->window())->display_id = display_info.id();
119a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->SetBounds(display_info.bounds_in_native());
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      Shell::GetScreen()->GetPrimaryDisplay().id());
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(display_manager->IsMirrored());
126a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<RootWindowTransformer> transformer(
127c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      CreateRootWindowTransformerForMirroredDisplay(source_display_info,
128c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                    display_info));
129a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ash_host_->SetRootWindowTransformer(transformer.Pass());
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void MirrorWindowController::UpdateWindow() {
133a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (ash_host_.get()) {
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DisplayManager* display_manager = Shell::GetInstance()->display_manager();
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        display_manager->mirrored_display_id());
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    UpdateWindow(mirror_display_info);
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void MirrorWindowController::Close() {
142a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (ash_host_.get()) {
143a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    aura::Env::GetInstance()->context_factory()->RemoveReflector(reflector_);
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    reflector_ = NULL;
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    NoneCaptureClient* capture_client = static_cast<NoneCaptureClient*>(
147a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        aura::client::GetCaptureClient(host->window()));
148a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::client::SetCaptureClient(host->window(), NULL);
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    delete capture_client;
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
151a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->RemoveObserver(Shell::GetInstance()->display_controller());
152a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->RemoveObserver(this);
153a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    ash_host_.reset();
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void MirrorWindowController::OnHostResized(const aura::WindowTreeHost* host) {
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (mirror_window_host_size_ == host->GetBounds().size())
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  mirror_window_host_size_ = host->GetBounds().size();
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  reflector_->OnMirroringCompositorResized();
162a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ash_host_->SetRootWindowTransformer(CreateRootWindowTransformer().Pass());
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Shell::GetInstance()->display_controller()->cursor_window_controller()->
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      UpdateLocation();
1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
167a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochaura::Window* MirrorWindowController::GetWindow() {
168a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return ash_host_.get() ? ash_host_->AsWindowTreeHost()->window() : NULL;
169a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
171a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochscoped_ptr<RootWindowTransformer>
1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)MirrorWindowController::CreateRootWindowTransformer() const {
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      display_manager->mirrored_display_id());
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      Shell::GetScreen()->GetPrimaryDisplay().id());
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(display_manager->IsMirrored());
179a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return scoped_ptr<RootWindowTransformer>(
180c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      CreateRootWindowTransformerForMirroredDisplay(source_display_info,
181c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                    mirror_display_info));
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace ash
185