Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / display / cursor_window_controller.cc
1 // Copyright 2014 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/cursor_window_controller.h"
6
7 #include "ash/display/display_controller.h"
8 #include "ash/display/mirror_window_controller.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/window_delegate.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/base/cursor/cursors_aura.h"
16 #include "ui/base/hit_test.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/compositor/dip_util.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/display.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/image/image_skia_operations.h"
23
24 namespace ash {
25
26 class CursorWindowDelegate : public aura::WindowDelegate {
27  public:
28   CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {}
29   ~CursorWindowDelegate() override {}
30
31   // aura::WindowDelegate overrides:
32   gfx::Size GetMinimumSize() const override { return size_; }
33   gfx::Size GetMaximumSize() const override { return size_; }
34   void OnBoundsChanged(const gfx::Rect& old_bounds,
35                        const gfx::Rect& new_bounds) override {}
36   gfx::NativeCursor GetCursor(const gfx::Point& point) override {
37     return gfx::kNullCursor;
38   }
39   int GetNonClientComponent(const gfx::Point& point) const override {
40     return HTNOWHERE;
41   }
42   bool ShouldDescendIntoChildForEventHandling(
43       aura::Window* child,
44       const gfx::Point& location) override {
45     return false;
46   }
47   bool CanFocus() override { return false; }
48   void OnCaptureLost() override {}
49   void OnPaint(gfx::Canvas* canvas) override {
50     canvas->DrawImageInt(cursor_image_, 0, 0);
51   }
52   void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
53   void OnWindowDestroying(aura::Window* window) override {}
54   void OnWindowDestroyed(aura::Window* window) override {}
55   void OnWindowTargetVisibilityChanged(bool visible) override {}
56   bool HasHitTestMask() const override { return false; }
57   void GetHitTestMask(gfx::Path* mask) const override {}
58
59   // Sets cursor compositing mode on/off.
60   void SetCursorCompositingEnabled(bool enabled) {
61     is_cursor_compositing_enabled_ = enabled;
62   }
63
64   // Sets the cursor image for the |display|'s scale factor.
65   void SetCursorImage(const gfx::ImageSkia& image,
66                       const gfx::Display& display) {
67     float scale_factor = display.device_scale_factor();
68     const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor);
69     if (!is_cursor_compositing_enabled_) {
70       // Note that mirror window's scale factor is always 1.0f, therefore we
71       // need to take 2x's image and paint as if it's 1x image.
72       size_ = image_rep.pixel_size();
73       cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap());
74     } else {
75       size_ = image.size();
76       cursor_image_ = gfx::ImageSkia(
77           gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor));
78     }
79   }
80
81   const gfx::Size size() const { return size_; }
82
83  private:
84   bool is_cursor_compositing_enabled_;
85   gfx::ImageSkia cursor_image_;
86   gfx::Size size_;
87
88   DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
89 };
90
91 CursorWindowController::CursorWindowController()
92     : is_cursor_compositing_enabled_(false),
93       container_(NULL),
94       cursor_type_(ui::kCursorNone),
95       cursor_set_(ui::CURSOR_SET_NORMAL),
96       cursor_rotation_(gfx::Display::ROTATE_0),
97       delegate_(new CursorWindowDelegate()) {
98 }
99
100 CursorWindowController::~CursorWindowController() {
101   SetContainer(NULL);
102 }
103
104 void CursorWindowController::SetCursorCompositingEnabled(bool enabled) {
105   if (is_cursor_compositing_enabled_ != enabled) {
106     is_cursor_compositing_enabled_ = enabled;
107     delegate_->SetCursorCompositingEnabled(enabled);
108     UpdateCursorImage();
109     UpdateContainer();
110   }
111 }
112
113 void CursorWindowController::UpdateContainer() {
114   if (is_cursor_compositing_enabled_) {
115     gfx::Screen* screen = Shell::GetScreen();
116     gfx::Display display = screen->GetDisplayNearestPoint(
117         screen->GetCursorScreenPoint());
118     DCHECK(display.is_valid());
119     if (display.is_valid())
120       SetDisplay(display);
121   } else {
122     aura::Window* mirror_window = Shell::GetInstance()->
123         display_controller()->
124         mirror_window_controller()->
125         GetWindow();
126     if (mirror_window)
127       display_ = Shell::GetScreen()->GetPrimaryDisplay();
128     SetContainer(mirror_window);
129   }
130 }
131
132 void CursorWindowController::SetDisplay(const gfx::Display& display) {
133   if (!is_cursor_compositing_enabled_)
134     return;
135
136   display_ = display;
137   aura::Window* root_window = Shell::GetInstance()->display_controller()->
138       GetRootWindowForDisplayId(display.id());
139   if (!root_window)
140     return;
141
142   SetContainer(GetRootWindowController(root_window)->GetContainer(
143       kShellWindowId_MouseCursorContainer));
144   SetBoundsInScreen(display.bounds());
145 }
146
147 void CursorWindowController::UpdateLocation() {
148   if (!cursor_window_)
149     return;
150   gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
151   if (!is_cursor_compositing_enabled_) {
152     Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point);
153   } else {
154     point.Offset(-bounds_in_screen_.x(), -bounds_in_screen_.y());
155   }
156   point.Offset(-hot_point_.x(), -hot_point_.y());
157   gfx::Rect bounds = cursor_window_->bounds();
158   bounds.set_origin(point);
159   cursor_window_->SetBounds(bounds);
160 }
161
162 void CursorWindowController::SetCursor(gfx::NativeCursor cursor) {
163   if (cursor_type_ == cursor.native_type() &&
164       cursor_rotation_ == display_.rotation())
165     return;
166   cursor_type_ = cursor.native_type();
167   cursor_rotation_ = display_.rotation();
168   UpdateCursorImage();
169 }
170
171 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) {
172   cursor_set_ = cursor_set;
173   UpdateCursorImage();
174 }
175
176 void CursorWindowController::SetVisibility(bool visible) {
177   if (!cursor_window_)
178     return;
179   if (visible)
180     cursor_window_->Show();
181   else
182     cursor_window_->Hide();
183 }
184
185 void CursorWindowController::SetContainer(aura::Window* container) {
186   if (container_ == container)
187     return;
188   container_ = container;
189   if (!container) {
190     cursor_window_.reset();
191     return;
192   }
193
194   // Reusing the window does not work when the display is disconnected.
195   // Just creates a new one instead. crbug.com/384218.
196   cursor_window_.reset(new aura::Window(delegate_.get()));
197   cursor_window_->SetTransparent(true);
198   cursor_window_->Init(aura::WINDOW_LAYER_TEXTURED);
199   cursor_window_->set_ignore_events(true);
200   cursor_window_->set_owned_by_parent(false);
201   UpdateCursorImage();
202
203   container->AddChild(cursor_window_.get());
204   cursor_window_->Show();
205   SetBoundsInScreen(container->bounds());
206 }
207
208 void CursorWindowController::SetBoundsInScreen(const gfx::Rect& bounds) {
209   bounds_in_screen_ = bounds;
210   UpdateLocation();
211 }
212
213 void CursorWindowController::UpdateCursorImage() {
214   int resource_id;
215   // TODO(hshi): support custom cursor set.
216   if (!ui::GetCursorDataFor(cursor_set_,
217                             cursor_type_,
218                             display_.device_scale_factor(),
219                             &resource_id,
220                             &hot_point_)) {
221     return;
222   }
223   const gfx::ImageSkia* image =
224       ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
225   gfx::ImageSkia rotated = *image;
226   if (!is_cursor_compositing_enabled_) {
227     switch (cursor_rotation_) {
228       case gfx::Display::ROTATE_0:
229         break;
230       case gfx::Display::ROTATE_90:
231         rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
232             *image, SkBitmapOperations::ROTATION_90_CW);
233         hot_point_.SetPoint(
234             rotated.width() - hot_point_.y(),
235             hot_point_.x());
236         break;
237       case gfx::Display::ROTATE_180:
238         rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
239             *image, SkBitmapOperations::ROTATION_180_CW);
240         hot_point_.SetPoint(
241             rotated.height() - hot_point_.x(),
242             rotated.width() - hot_point_.y());
243         break;
244       case gfx::Display::ROTATE_270:
245         rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
246             *image, SkBitmapOperations::ROTATION_270_CW);
247         hot_point_.SetPoint(
248             hot_point_.y(),
249             rotated.height() - hot_point_.x());
250         break;
251     }
252   } else {
253     hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
254                                        hot_point_);
255   }
256   delegate_->SetCursorImage(rotated, display_);
257   if (cursor_window_) {
258     cursor_window_->SetBounds(gfx::Rect(delegate_->size()));
259     cursor_window_->SchedulePaintInRect(
260         gfx::Rect(cursor_window_->bounds().size()));
261     UpdateLocation();
262   }
263 }
264
265 }  // namespace ash