Update base::StartsWith calls to new form.
[chromium-blink-merge.git] / ash / touch / touch_transformer_controller.cc
blob5f2f0fe43f2da5e93dc716dbcd14b66e88c00f5e
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.
5 #include "ash/touch/touch_transformer_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/host/ash_window_tree_host.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/shell.h"
12 #include "ui/aura/window_tree_host.h"
13 #include "ui/display/chromeos/display_configurator.h"
14 #include "ui/display/types/display_snapshot.h"
15 #include "ui/events/devices/device_data_manager.h"
17 namespace ash {
19 namespace {
21 DisplayManager* GetDisplayManager() {
22 return Shell::GetInstance()->display_manager();
25 ui::TouchscreenDevice FindTouchscreenById(int id) {
26 const std::vector<ui::TouchscreenDevice>& touchscreens =
27 ui::DeviceDataManager::GetInstance()->touchscreen_devices();
28 for (const auto& touchscreen : touchscreens) {
29 if (touchscreen.id == id)
30 return touchscreen;
33 return ui::TouchscreenDevice();
36 } // namespace
38 // This is to compute the scale ratio for the TouchEvent's radius. The
39 // configured resolution of the display is not always the same as the touch
40 // screen's reporting resolution, e.g. the display could be set as
41 // 1920x1080 while the touchscreen is reporting touch position range at
42 // 32767x32767. Touch radius is reported in the units the same as touch position
43 // so we need to scale the touch radius to be compatible with the display's
44 // resolution. We compute the scale as
45 // sqrt of (display_area / touchscreen_area)
46 double TouchTransformerController::GetTouchResolutionScale(
47 const DisplayInfo& touch_display,
48 const ui::TouchscreenDevice& touch_device) const {
49 if (touch_device.id == ui::InputDevice::kInvalidId ||
50 touch_device.size.IsEmpty() ||
51 touch_display.bounds_in_native().size().IsEmpty())
52 return 1.0;
54 double display_area = touch_display.bounds_in_native().size().GetArea();
55 double touch_area = touch_device.size.GetArea();
56 double ratio = std::sqrt(display_area / touch_area);
58 VLOG(2) << "Display size: "
59 << touch_display.bounds_in_native().size().ToString()
60 << ", Touchscreen size: " << touch_device.size.ToString()
61 << ", Touch radius scale ratio: " << ratio;
62 return ratio;
65 gfx::Transform TouchTransformerController::GetTouchTransform(
66 const DisplayInfo& display,
67 const DisplayInfo& touch_display,
68 const ui::TouchscreenDevice& touchscreen,
69 const gfx::Size& framebuffer_size) const {
70 gfx::SizeF current_size = display.bounds_in_native().size();
71 gfx::SizeF touch_native_size = touch_display.GetNativeModeSize();
72 #if defined(USE_OZONE)
73 gfx::SizeF touch_area = touchscreen.size;
74 #elif defined(USE_X11)
75 // On X11 touches are reported in the framebuffer coordinate space.
76 gfx::SizeF touch_area = framebuffer_size;
77 #endif
79 gfx::Transform ctm;
81 if (current_size.IsEmpty() || touch_native_size.IsEmpty() ||
82 touch_area.IsEmpty() || touchscreen.id == ui::InputDevice::kInvalidId)
83 return ctm;
85 #if defined(USE_OZONE)
86 // Translate the touch so that it falls within the display bounds.
87 ctm.Translate(display.bounds_in_native().x(),
88 display.bounds_in_native().y());
89 #endif
91 // Take care of panel fitting only if supported. Panel fitting is emulated in
92 // software mirroring mode (display != touch_display).
93 // If panel fitting is enabled then the aspect ratio is preserved and the
94 // display is scaled acordingly. In this case blank regions would be present
95 // in order to center the displayed area.
96 if (display.is_aspect_preserving_scaling() ||
97 display.id() != touch_display.id()) {
98 float touch_native_ar =
99 touch_native_size.width() / touch_native_size.height();
100 float current_ar = current_size.width() / current_size.height();
102 if (current_ar > touch_native_ar) { // Letterboxing
103 ctm.Translate(
104 0, (1 - current_ar / touch_native_ar) * 0.5 * current_size.height());
105 ctm.Scale(1, current_ar / touch_native_ar);
106 } else if (touch_native_ar > current_ar) { // Pillarboxing
107 ctm.Translate(
108 (1 - touch_native_ar / current_ar) * 0.5 * current_size.width(), 0);
109 ctm.Scale(touch_native_ar / current_ar, 1);
113 // Take care of scaling between touchscreen area and display resolution.
114 ctm.Scale(current_size.width() / touch_area.width(),
115 current_size.height() / touch_area.height());
116 return ctm;
119 TouchTransformerController::TouchTransformerController() {
120 Shell::GetInstance()->display_controller()->AddObserver(this);
123 TouchTransformerController::~TouchTransformerController() {
124 Shell::GetInstance()->display_controller()->RemoveObserver(this);
127 void TouchTransformerController::UpdateTouchRadius(
128 const DisplayInfo& display) const {
129 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
130 device_manager->UpdateTouchRadiusScale(
131 display.touch_device_id(),
132 GetTouchResolutionScale(display,
133 FindTouchscreenById(display.touch_device_id())));
136 void TouchTransformerController::UpdateTouchTransform(
137 int64_t target_display_id,
138 const DisplayInfo& touch_display,
139 const DisplayInfo& target_display) const {
140 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
141 gfx::Size fb_size =
142 Shell::GetInstance()->display_configurator()->framebuffer_size();
143 device_manager->UpdateTouchInfoForDisplay(
144 target_display_id, touch_display.touch_device_id(),
145 GetTouchTransform(target_display, touch_display,
146 FindTouchscreenById(touch_display.touch_device_id()),
147 fb_size));
150 void TouchTransformerController::UpdateTouchTransformer() const {
151 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
152 device_manager->ClearTouchDeviceAssociations();
154 // Display IDs and DisplayInfo for mirror or extended mode.
155 int64 display1_id = gfx::Display::kInvalidDisplayID;
156 int64 display2_id = gfx::Display::kInvalidDisplayID;
157 DisplayInfo display1;
158 DisplayInfo display2;
159 // Display ID and DisplayInfo for single display mode.
160 int64 single_display_id = gfx::Display::kInvalidDisplayID;
161 DisplayInfo single_display;
163 DisplayController* display_controller =
164 Shell::GetInstance()->display_controller();
165 DisplayManager* display_manager = GetDisplayManager();
166 if (display_manager->num_connected_displays() == 0) {
167 return;
168 } else if (display_manager->num_connected_displays() == 1 ||
169 display_manager->IsInUnifiedMode()) {
170 single_display_id = display_manager->first_display_id();
171 DCHECK(single_display_id != gfx::Display::kInvalidDisplayID);
172 single_display = display_manager->GetDisplayInfo(single_display_id);
173 UpdateTouchRadius(single_display);
174 } else {
175 DisplayIdPair id_pair = display_manager->GetCurrentDisplayIdPair();
176 display1_id = id_pair.first;
177 display2_id = id_pair.second;
178 DCHECK(display1_id != gfx::Display::kInvalidDisplayID &&
179 display2_id != gfx::Display::kInvalidDisplayID);
180 display1 = display_manager->GetDisplayInfo(display1_id);
181 display2 = display_manager->GetDisplayInfo(display2_id);
182 UpdateTouchRadius(display1);
183 UpdateTouchRadius(display2);
186 gfx::Size fb_size =
187 Shell::GetInstance()->display_configurator()->framebuffer_size();
189 if (display_manager->IsInMirrorMode()) {
190 int64_t primary_display_id = display_controller->GetPrimaryDisplayId();
191 if (GetDisplayManager()->SoftwareMirroringEnabled()) {
192 // In extended but software mirroring mode, there is a WindowTreeHost for
193 // each display, but all touches are forwarded to the primary root
194 // window's WindowTreeHost.
195 DisplayInfo target_display =
196 primary_display_id == display1_id ? display1 : display2;
197 UpdateTouchTransform(target_display.id(), display1, target_display);
198 UpdateTouchTransform(target_display.id(), display2, target_display);
199 } else {
200 // In mirror mode, there is just one WindowTreeHost and two displays. Make
201 // the WindowTreeHost accept touch events from both displays.
202 UpdateTouchTransform(primary_display_id, display1, display1);
203 UpdateTouchTransform(primary_display_id, display2, display2);
205 return;
208 if (display_manager->num_connected_displays() > 1) {
209 // In actual extended mode, each display is associated with one
210 // WindowTreeHost.
211 UpdateTouchTransform(display1_id, display1, display1);
212 UpdateTouchTransform(display2_id, display2, display2);
213 return;
216 // Single display mode. The WindowTreeHost has one associated display id.
217 UpdateTouchTransform(single_display_id, single_display, single_display);
220 void TouchTransformerController::OnDisplaysInitialized() {
221 UpdateTouchTransformer();
224 void TouchTransformerController::OnDisplayConfigurationChanged() {
225 UpdateTouchTransformer();
228 } // namespace ash