OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/exo/pointer.h" | 5 #include "components/exo/pointer.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/shell_window_ids.h" | 8 #include "ash/shell_window_ids.h" |
| 9 #include "components/exo/buffer.h" |
9 #include "components/exo/pointer_delegate.h" | 10 #include "components/exo/pointer_delegate.h" |
10 #include "components/exo/surface.h" | 11 #include "components/exo/surface.h" |
| 12 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/aura/env.h" |
11 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 16 #include "ui/base/cursor/cursors_aura.h" |
| 17 #include "ui/compositor/compositor.h" |
12 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
13 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
14 | 20 |
15 namespace exo { | 21 namespace exo { |
| 22 namespace { |
| 23 |
| 24 scoped_ptr<Buffer> CreateDefaultCursor(gfx::Point* hotspot) { |
| 25 ui::Cursor cursor(ui::kCursorPointer); |
| 26 cursor.set_device_scale_factor(1.0f); |
| 27 |
| 28 SkBitmap bitmap; |
| 29 if (!ui::GetCursorBitmap(cursor, &bitmap, hotspot)) { |
| 30 LOG(ERROR) << "Failed to load default cursor bitmap"; |
| 31 return nullptr; |
| 32 } |
| 33 |
| 34 DCHECK_EQ(bitmap.colorType(), kBGRA_8888_SkColorType); |
| 35 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer = |
| 36 aura::Env::GetInstance() |
| 37 ->context_factory() |
| 38 ->GetGpuMemoryBufferManager() |
| 39 ->AllocateGpuMemoryBuffer(gfx::SkISizeToSize(bitmap.dimensions()), |
| 40 gfx::BufferFormat::BGRA_8888, |
| 41 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE); |
| 42 bool rv = gpu_memory_buffer->Map(); |
| 43 DCHECK(rv); |
| 44 int stride = gpu_memory_buffer->stride(0 /* plane */); |
| 45 DCHECK_GT(stride, 0); |
| 46 bitmap.copyPixelsTo(gpu_memory_buffer->memory(0 /* plane */), |
| 47 stride * bitmap.height(), stride); |
| 48 gpu_memory_buffer->Unmap(); |
| 49 |
| 50 return make_scoped_ptr(new Buffer(std::move(gpu_memory_buffer))); |
| 51 } |
| 52 |
| 53 } // namespace |
16 | 54 |
17 //////////////////////////////////////////////////////////////////////////////// | 55 //////////////////////////////////////////////////////////////////////////////// |
18 // Pointer, public: | 56 // Pointer, public: |
19 | 57 |
20 Pointer::Pointer(PointerDelegate* delegate) | 58 Pointer::Pointer(PointerDelegate* delegate) |
21 : delegate_(delegate), surface_(nullptr), focus_(nullptr) { | 59 : delegate_(delegate), surface_(nullptr), focus_(nullptr) { |
22 ash::Shell::GetInstance()->AddPreTargetHandler(this); | 60 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
23 } | 61 } |
24 | 62 |
25 Pointer::~Pointer() { | 63 Pointer::~Pointer() { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 case ui::ET_MOUSE_EXITED: | 171 case ui::ET_MOUSE_EXITED: |
134 case ui::ET_MOUSE_CAPTURE_CHANGED: | 172 case ui::ET_MOUSE_CAPTURE_CHANGED: |
135 break; | 173 break; |
136 default: | 174 default: |
137 NOTREACHED(); | 175 NOTREACHED(); |
138 break; | 176 break; |
139 } | 177 } |
140 | 178 |
141 // Update cursor widget to reflect current focus and pointer location. | 179 // Update cursor widget to reflect current focus and pointer location. |
142 if (focus_) { | 180 if (focus_) { |
143 if (!widget_) | 181 if (!widget_) { |
144 CreatePointerWidget(); | 182 CreatePointerWidget(); |
| 183 |
| 184 // Create default pointer surface. This will be used until a different |
| 185 // pointer surface is set using SetCursor(). |
| 186 default_cursor_ = CreateDefaultCursor(&hotspot_); |
| 187 default_surface_.reset(new Surface); |
| 188 surface_ = default_surface_.get(); |
| 189 surface_->SetSurfaceDelegate(this); |
| 190 surface_->AddSurfaceObserver(this); |
| 191 widget_->GetNativeWindow()->AddChild(surface_); |
| 192 surface_->Attach(default_cursor_.get()); |
| 193 surface_->Commit(); |
| 194 surface_->Show(); |
| 195 } |
145 widget_->SetBounds(gfx::Rect( | 196 widget_->SetBounds(gfx::Rect( |
146 focus_->GetBoundsInScreen().origin() + location_.OffsetFromOrigin(), | 197 focus_->GetBoundsInScreen().origin() + location_.OffsetFromOrigin(), |
147 gfx::Size(1, 1))); | 198 gfx::Size(1, 1))); |
148 if (!widget_->IsVisible()) | 199 if (!widget_->IsVisible()) |
149 widget_->Show(); | 200 widget_->Show(); |
150 } else { | 201 } else { |
151 if (widget_ && widget_->IsVisible()) | 202 if (widget_ && widget_->IsVisible()) |
152 widget_->Hide(); | 203 widget_->Hide(); |
153 } | 204 } |
154 } | 205 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 Surface* Pointer::GetEffectiveTargetForEvent(ui::Event* event) const { | 258 Surface* Pointer::GetEffectiveTargetForEvent(ui::Event* event) const { |
208 Surface* target = | 259 Surface* target = |
209 Surface::AsSurface(static_cast<aura::Window*>(event->target())); | 260 Surface::AsSurface(static_cast<aura::Window*>(event->target())); |
210 if (!target) | 261 if (!target) |
211 return nullptr; | 262 return nullptr; |
212 | 263 |
213 return delegate_->CanAcceptPointerEventsForSurface(target) ? target : nullptr; | 264 return delegate_->CanAcceptPointerEventsForSurface(target) ? target : nullptr; |
214 } | 265 } |
215 | 266 |
216 } // namespace exo | 267 } // namespace exo |
OLD | NEW |