Chromium Code Reviews| Index: components/exo/pointer.cc |
| diff --git a/components/exo/pointer.cc b/components/exo/pointer.cc |
| index 073c750192f374b967e3c25061542d8f88136460..1c45aaac98e90817c7d0e86cbcd9394c20d13df1 100644 |
| --- a/components/exo/pointer.cc |
| +++ b/components/exo/pointer.cc |
| @@ -4,6 +4,8 @@ |
| #include "components/exo/pointer.h" |
| +#include <algorithm> |
| + |
| #include "ash/public/cpp/shell_window_ids.h" |
| #include "cc/output/copy_output_request.h" |
| #include "cc/output/copy_output_result.h" |
| @@ -14,6 +16,7 @@ |
| #include "ui/aura/client/cursor_client.h" |
| #include "ui/aura/env.h" |
| #include "ui/aura/window.h" |
| +#include "ui/base/cursor/cursor_util.h" |
| #include "ui/display/manager/display_manager.h" |
| #include "ui/display/manager/managed_display_info.h" |
| #include "ui/display/screen.h" |
| @@ -32,7 +35,13 @@ |
| namespace exo { |
| namespace { |
| -const float kLargeCursorScale = 2.8f; |
| +// TODO(oshima): Some accessibility features, including large cursors, disable |
|
reveman
2017/05/23 17:06:57
please minimize the patch and avoid this change th
Dominik Laskowski
2017/05/24 00:43:04
It's needed due to the new logic for updating scal
reveman
2017/05/25 10:52:55
I'm failing to see why. Please explain.
Dominik Laskowski
2017/05/31 02:06:26
Folded into UpdateCursor.
|
| +// hardware cursors. Ash does not support compositing for custom cursors, so it |
| +// replaces them with the default cursor. As a result, this scale has no effect |
| +// for now. See crbug.com/708378. |
| +float GetCursorScale(ui::CursorSetType cursor_set) { |
| + return cursor_set == ui::CURSOR_SET_LARGE ? 2.8f : 1.0f; |
| +} |
| // Synthesized events typically lack floating point precision so to avoid |
| // generating mouse event jitter we consider the location of these events |
| @@ -44,6 +53,22 @@ bool SameLocation(const ui::LocatedEvent* event, const gfx::PointF& location) { |
| return event->location_f() == location; |
| } |
| +inline const display::ManagedDisplayInfo& GetDisplayInfo( |
| + const display::Display& display) { |
| + return WMHelper::GetInstance()->GetDisplayInfo(display.id()); |
| +} |
| + |
| +display::Display GetCaptureDisplay() { |
|
reveman
2017/05/23 17:06:57
What if no DSF 2.0 display exists but is later add
Dominik Laskowski
2017/05/24 00:43:04
Good point. Capturing at constant pixel size is si
|
| + const auto& displays = display::Screen::GetScreen()->GetAllDisplays(); |
| + DCHECK(!displays.empty()); |
| + return *std::max_element( |
| + displays.begin(), displays.end(), |
| + [](const display::Display& lhs, const display::Display& rhs) -> bool { |
| + return GetDisplayInfo(lhs).device_scale_factor() < |
| + GetDisplayInfo(rhs).device_scale_factor(); |
| + }); |
| +} |
| + |
| } // namespace |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -51,12 +76,15 @@ bool SameLocation(const ui::LocatedEvent* event, const gfx::PointF& location) { |
| Pointer::Pointer(PointerDelegate* delegate) |
| : delegate_(delegate), |
| - cursor_(ui::CursorType::kNull), |
| cursor_capture_source_id_(base::UnguessableToken::Create()), |
| cursor_capture_weak_ptr_factory_(this) { |
| auto* helper = WMHelper::GetInstance(); |
| helper->AddPreTargetHandler(this); |
| helper->AddCursorObserver(this); |
| + |
|
reveman
2017/05/23 17:06:57
why are the following 4 lines needed?
Dominik Laskowski
2017/05/24 00:43:04
To initialize the state updated by OnCursorSetChan
|
| + cursor_scale_ = GetCursorScale(helper->GetCursorSet()); |
| + |
| + OnCursorDisplayChanging(display::Screen::GetScreen()->GetPrimaryDisplay()); |
| } |
| Pointer::~Pointer() { |
| @@ -102,7 +130,8 @@ void Pointer::SetCursor(Surface* surface, const gfx::Point& hotspot) { |
| // snapshot. Where in the tree is not important but we might as well use |
| // the cursor container. |
| WMHelper::GetInstance() |
| - ->GetContainer(ash::kShellWindowId_MouseCursorContainer) |
| + ->GetContainer(GetCaptureDisplay().id(), |
| + ash::kShellWindowId_MouseCursorContainer) |
| ->AddChild(surface_->window()); |
| } |
| cursor_changed = true; |
| @@ -123,14 +152,19 @@ void Pointer::SetCursor(Surface* surface, const gfx::Point& hotspot) { |
| if (surface_) { |
| CaptureCursor(); |
| } else { |
| + cursor_.reset(); |
| cursor_capture_weak_ptr_factory_.InvalidateWeakPtrs(); |
| - cursor_ = ui::CursorType::kNone; |
| - UpdateCursor(); |
| + SetCursor(ui::CursorType::kNone); |
| } |
| } |
| gfx::NativeCursor Pointer::GetCursor() { |
| - return cursor_; |
| + if (focus_) |
| + if (auto* root_window = focus_->window()->GetRootWindow()) |
| + if (auto* cursor_client = aura::client::GetCursorClient(root_window)) |
| + return cursor_client->GetCursor(); |
| + |
| + return ui::CursorType::kNull; |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -150,7 +184,6 @@ void Pointer::OnMouseEvent(ui::MouseEvent* event) { |
| // response to each OnPointerEnter() call. |
| focus_->UnregisterCursorProvider(this); |
| focus_ = nullptr; |
| - cursor_ = ui::CursorType::kNull; |
| cursor_capture_weak_ptr_factory_.InvalidateWeakPtrs(); |
| } |
| // Second generate an enter event if focus moved to a new target. |
| @@ -240,7 +273,6 @@ void Pointer::OnMouseEvent(ui::MouseEvent* event) { |
| } |
| last_event_type_ = event->type(); |
| - UpdateCursorScale(); |
| } |
| void Pointer::OnScrollEvent(ui::ScrollEvent* event) { |
| @@ -251,8 +283,14 @@ void Pointer::OnScrollEvent(ui::ScrollEvent* event) { |
| // WMHelper::CursorObserver overrides: |
| void Pointer::OnCursorSetChanged(ui::CursorSetType cursor_set) { |
| + cursor_scale_ = GetCursorScale(cursor_set); |
| if (focus_) |
| - UpdateCursorScale(); |
| + UpdateCursor(); |
| +} |
| + |
| +void Pointer::OnCursorDisplayChanging(const display::Display& display) { |
| + device_scale_factor_ = GetDisplayInfo(display).device_scale_factor(); |
| + rotation_ = display.rotation(); |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -296,99 +334,82 @@ Surface* Pointer::GetEffectiveTargetForEvent(ui::Event* event) const { |
| return delegate_->CanAcceptPointerEventsForSurface(target) ? target : nullptr; |
| } |
| -void Pointer::UpdateCursorScale() { |
| - DCHECK(focus_); |
| - |
| - display::Screen* screen = display::Screen::GetScreen(); |
| - WMHelper* helper = WMHelper::GetInstance(); |
| - |
| - // Update cursor scale if the effective UI scale has changed. |
| - display::Display display = screen->GetDisplayNearestWindow(focus_->window()); |
| - float scale = helper->GetDisplayInfo(display.id()).GetEffectiveUIScale(); |
| - |
| - if (display::Display::HasInternalDisplay()) { |
| - float primary_device_scale_factor = |
| - screen->GetPrimaryDisplay().device_scale_factor(); |
| - // The size of the cursor surface is the quotient of its physical size and |
| - // the DSF of the primary display. The physical size is proportional to the |
| - // DSF of the internal display. For external displays (and the internal |
| - // display when secondary to a display with a different DSF), scale the |
| - // cursor so its physical size matches with the single display case. |
| - if (!display.IsInternal() || |
| - display.device_scale_factor() != primary_device_scale_factor) { |
| - scale *= primary_device_scale_factor / |
| - helper->GetDisplayInfo(display::Display::InternalDisplayId()) |
| - .device_scale_factor(); |
| - } |
| - } |
| - |
| - if (helper->GetCursorSet() == ui::CURSOR_SET_LARGE) |
| - scale *= kLargeCursorScale; |
| - |
| - if (scale != cursor_scale_) { |
| - cursor_scale_ = scale; |
| - if (surface_) |
| - CaptureCursor(); |
| - } |
| -} |
| - |
| void Pointer::CaptureCursor() { |
| DCHECK(surface_); |
| DCHECK(focus_); |
| - // Set UI scale before submitting capture request. |
| - surface_->window()->layer()->SetTransform( |
| - gfx::GetScaleTransform(gfx::Point(), cursor_scale_)); |
| - |
| - float primary_device_scale_factor = |
| - display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(); |
| + // Surface size is in DIPs, while layer size is in pseudo-DIP units that |
| + // depend on the DSF of the display mode. Scale the layer to capture the |
| + // surface at a constant pixel size converted from DIPs using the DSF of |
| + // the capture display, regardless of its UI scale and display mode DSF. |
| + display::Display display = GetCaptureDisplay(); |
| + const auto& info = GetDisplayInfo(display); |
| + float display_scale = info.GetEffectiveUIScale() * info.device_scale_factor(); |
| + surface_->window()->SetTransform(gfx::GetScaleTransform( |
| + gfx::Point(), display_scale / display.device_scale_factor())); |
| std::unique_ptr<cc::CopyOutputRequest> request = |
| cc::CopyOutputRequest::CreateBitmapRequest( |
| base::Bind(&Pointer::OnCursorCaptured, |
| - cursor_capture_weak_ptr_factory_.GetWeakPtr(), |
| - gfx::ScaleToFlooredPoint( |
| - hotspot_, |
| - // |hotspot_| is in surface coordinate space so apply |
| - // both device scale and UI scale. |
| - cursor_scale_ * primary_device_scale_factor))); |
| + cursor_capture_weak_ptr_factory_.GetWeakPtr())); |
| + |
| request->set_source(cursor_capture_source_id_); |
| surface_->window()->layer()->RequestCopyOfOutput(std::move(request)); |
| } |
| -void Pointer::OnCursorCaptured(const gfx::Point& hotspot, |
| - std::unique_ptr<cc::CopyOutputResult> result) { |
| +void Pointer::OnCursorCaptured(std::unique_ptr<cc::CopyOutputResult> result) { |
| if (!focus_) |
| return; |
| - cursor_ = ui::CursorType::kNone; |
| - if (!result->IsEmpty()) { |
| - DCHECK(result->HasBitmap()); |
| - std::unique_ptr<SkBitmap> bitmap = result->TakeBitmap(); |
| + if (result->IsEmpty()) { |
| + cursor_.reset(); |
| + SetCursor(ui::CursorType::kNone); |
| + return; |
| + } |
| + |
| + DCHECK(result->HasBitmap()); |
| + cursor_ = *result->TakeBitmap(); |
| + UpdateCursor(); |
| +} |
| + |
| +void Pointer::UpdateCursor() { |
| + DCHECK(focus_); |
| + |
| + if (cursor_.drawsNothing()) |
| + return; |
| + |
| + const auto& info = GetDisplayInfo(GetCaptureDisplay()); |
| - ui::PlatformCursor platform_cursor; |
| + SkBitmap bitmap = cursor_; |
| + gfx::Point hotspot = |
| + gfx::ScaleToFlooredPoint(hotspot_, info.device_scale_factor()); |
| + |
| + ui::ScaleAndRotateCursorBitmapAndHotpoint( |
| + cursor_scale_ * device_scale_factor_ / info.device_scale_factor(), |
| + rotation_, &bitmap, &hotspot); |
| + |
| + ui::PlatformCursor platform_cursor; |
| #if defined(USE_OZONE) |
| - // TODO(reveman): Add interface for creating cursors from GpuMemoryBuffers |
| - // and use that here instead of the current bitmap API. crbug.com/686600 |
| - platform_cursor = ui::CursorFactoryOzone::GetInstance()->CreateImageCursor( |
| - *bitmap.get(), hotspot, cursor_scale_); |
| + // TODO(reveman): Add interface for creating cursors from GpuMemoryBuffers |
| + // and use that here instead of the current bitmap API. crbug.com/686600 |
| + platform_cursor = ui::CursorFactoryOzone::GetInstance()->CreateImageCursor( |
| + bitmap, hotspot, 0); |
| #elif defined(USE_X11) |
| - XcursorImage* image = ui::SkBitmapToXcursorImage(bitmap.get(), hotspot); |
| - platform_cursor = ui::CreateReffedCustomXCursor(image); |
| + XcursorImage* image = ui::SkBitmapToXcursorImage(&bitmap, hotspot); |
| + platform_cursor = ui::CreateReffedCustomXCursor(image); |
| #endif |
| - cursor_ = ui::CursorType::kCustom; |
| - cursor_.SetPlatformCursor(platform_cursor); |
| + gfx::NativeCursor cursor = ui::CursorType::kCustom; |
| + cursor.SetPlatformCursor(platform_cursor); |
| #if defined(USE_OZONE) |
| - ui::CursorFactoryOzone::GetInstance()->UnrefImageCursor(platform_cursor); |
| + ui::CursorFactoryOzone::GetInstance()->UnrefImageCursor(platform_cursor); |
| #elif defined(USE_X11) |
| - ui::UnrefCustomXCursor(platform_cursor); |
| + ui::UnrefCustomXCursor(platform_cursor); |
| #endif |
| - } |
| - UpdateCursor(); |
| + SetCursor(cursor); |
| } |
| -void Pointer::UpdateCursor() { |
| +void Pointer::SetCursor(gfx::NativeCursor cursor) { |
| DCHECK(focus_); |
| aura::Window* root_window = focus_->window()->GetRootWindow(); |
| @@ -398,7 +419,7 @@ void Pointer::UpdateCursor() { |
| aura::client::CursorClient* cursor_client = |
| aura::client::GetCursorClient(root_window); |
| if (cursor_client) |
| - cursor_client->SetCursor(cursor_); |
| + cursor_client->SetCursor(cursor); |
| } |
| } // namespace exo |