Take ownership of Sqlite.* histograms.
[chromium-blink-merge.git] / ash / display / event_transformation_handler.cc
blobcd9961a954e93ea39b8c95e06070b7019935c523
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.
5 #include "ash/display/event_transformation_handler.h"
7 #include <cmath>
9 #include "ash/display/display_info.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/shell.h"
12 #include "ash/wm/coordinate_conversion.h"
13 #include "ash/wm/window_util.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/compositor/dip_util.h"
17 #include "ui/events/event.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
21 #if defined(OS_CHROMEOS)
22 #include "ui/display/chromeos/display_configurator.h"
23 #endif // defined(OS_CHROMEOS)
25 namespace ash {
26 namespace {
28 // Boost factor for non-integrated displays.
29 const float kBoostForNonIntegrated = 1.20f;
31 #if defined(OS_CHROMEOS)
32 gfx::Size GetNativeModeSize(const DisplayInfo& info) {
33 const std::vector<DisplayMode>& modes = info.display_modes();
34 for (size_t i = 0; i < modes.size(); ++i) {
35 if (modes[i].native)
36 return modes[i].size;
39 return gfx::Size();
42 float GetMirroredDisplayAreaRatio(const gfx::Size& current_size,
43 const gfx::Size& native_size) {
44 float area_ratio = 1.0f;
46 if (current_size.IsEmpty() || native_size.IsEmpty())
47 return area_ratio;
49 float width_ratio = static_cast<float>(current_size.width()) /
50 static_cast<float>(native_size.width());
51 float height_ratio = static_cast<float>(current_size.height()) /
52 static_cast<float>(native_size.height());
54 area_ratio = width_ratio * height_ratio;
55 return area_ratio;
58 // Key of the map is the touch display's id, and the value of the map is the
59 // touch display's area ratio in mirror mode defined as:
60 // mirror_mode_area / native_mode_area.
61 // This is used for scaling touch event's radius when the touch display is in
62 // mirror mode: new_touch_radius = sqrt(area_ratio) * old_touch_radius
63 std::map<int, float> GetMirroredDisplayAreaRatioMap() {
64 std::map<int, float> area_ratios;
65 DisplayManager* display_manager =
66 ash::Shell::GetInstance()->display_manager();
67 const std::vector<gfx::Display>& displays = display_manager->displays();
68 for (size_t i = 0; i < displays.size(); ++i) {
69 const DisplayInfo& info = display_manager->GetDisplayInfo(
70 displays[i].id());
71 const gfx::Size current_size = info.size_in_pixel();
72 const gfx::Size native_size = GetNativeModeSize(info);
74 if (info.touch_support() == gfx::Display::TOUCH_SUPPORT_AVAILABLE &&
75 current_size != native_size &&
76 info.is_aspect_preserving_scaling()) {
77 area_ratios[info.touch_device_id()] = GetMirroredDisplayAreaRatio(
78 current_size, native_size);
82 return area_ratios;
84 #endif // defined(OS_CHROMEOS)
86 } // namespace
88 EventTransformationHandler::EventTransformationHandler()
89 : transformation_mode_(TRANSFORM_AUTO) {
92 EventTransformationHandler::~EventTransformationHandler() {
95 void EventTransformationHandler::OnScrollEvent(ui::ScrollEvent* event) {
96 if (transformation_mode_ == TRANSFORM_NONE)
97 return;
99 // It is unnecessary to scale the event for the device scale factor since
100 // the event locations etc. are already in DIP.
101 gfx::Point point_in_screen(event->location());
102 aura::Window* target = static_cast<aura::Window*>(event->target());
103 wm::ConvertPointToScreen(target, &point_in_screen);
104 const gfx::Display& display =
105 Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen);
107 // Apply some additional scaling if the display is non-integrated.
108 if (!display.IsInternal())
109 event->Scale(kBoostForNonIntegrated);
112 #if defined(OS_CHROMEOS)
113 // This is to scale the TouchEvent's radius when the touch display is in
114 // mirror mode. TouchEvent's radius is often reported in the touchscreen's
115 // native resolution. In mirror mode, the touch display could be configured
116 // at a lower resolution. We scale down the radius using the ratio defined as
117 // the sqrt of
118 // (mirror_width * mirror_height) / (native_width * native_height)
119 void EventTransformationHandler::OnTouchEvent(ui::TouchEvent* event) {
120 // Check display_configurator's output_state instead of checking
121 // DisplayManager::IsMirrored() because the compositor based mirroring
122 // won't cause the scaling issue.
123 if (ash::Shell::GetInstance()->display_configurator()->display_state() !=
124 ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR)
125 return;
127 const std::map<int, float> area_ratio_map = GetMirroredDisplayAreaRatioMap();
129 // TODO(miletus): When there are more than 1 touchscreen (e.g. Link connected
130 // to an external touchscreen), the correct way to do is to have a way
131 // to find out which touchscreen is the event originating from and use the
132 // area ratio of that touchscreen to scale the event's radius.
133 // Tracked here crbug.com/233245
134 if (area_ratio_map.size() != 1) {
135 LOG(ERROR) << "Mirroring mode with " << area_ratio_map.size()
136 << " touch display found";
137 return;
140 float area_ratio_sqrt = std::sqrt(area_ratio_map.begin()->second);
141 event->set_radius_x(event->radius_x() * area_ratio_sqrt);
142 event->set_radius_y(event->radius_y() * area_ratio_sqrt);
144 #endif // defined(OS_CHROMEOS)
146 } // namespace ash