Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / client / touch_input_scaler.cc
blob80c749cc1d74b7978e945a1e416b437b64c86cf0
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
3 // found in the LICENSE file.
5 #include "remoting/client/touch_input_scaler.h"
7 #include "base/logging.h"
8 #include "remoting/proto/event.pb.h"
10 namespace remoting {
12 using protocol::TouchEvent;
13 using protocol::TouchEventPoint;
15 namespace {
17 // |value| is the number to be scaled. |output_max| is the output desktop's max
18 // height or width. |input_max| is the input desktop's max height or width.
19 float Scale(float value, int output_max, int input_max) {
20 DCHECK_GT(output_max, 0);
21 DCHECK_GT(input_max, 0);
22 value *= output_max;
23 value /= input_max;
24 return value;
27 // Same as Scale() but |value| will be scaled and clamped using |output_max| and
28 // |input_max|.
29 float ScaleAndClamp(float value, int output_max, int input_max) {
30 value = Scale(value, output_max, input_max);
31 return std::max(0.0f, std::min(static_cast<float>(output_max), value));
34 } // namespace
36 TouchInputScaler::TouchInputScaler(InputStub* input_stub)
37 : InputFilter(input_stub) {}
39 TouchInputScaler::~TouchInputScaler() {}
41 void TouchInputScaler::InjectTouchEvent(const TouchEvent& event) {
42 if (input_size_.is_empty() || output_size_.is_empty())
43 return;
45 // We scale based on the maximum input & output coordinates, rather than the
46 // input and output sizes, so that it's possible to reach the edge of the
47 // output when up-scaling. We also take care to round up or down correctly,
48 // which is important when down-scaling.
49 TouchEvent out_event(event);
50 for (int i = 0; i < out_event.touch_points().size(); ++i) {
51 TouchEventPoint* point = out_event.mutable_touch_points(i);
52 if (point->has_x() || point->has_y()) {
53 DCHECK(point->has_x() && point->has_y());
54 point->set_x(
55 ScaleAndClamp(point->x(), output_size_.width(), input_size_.width()));
56 point->set_y(ScaleAndClamp(point->y(), output_size_.height(),
57 input_size_.height()));
60 // Also scale the touch size. Without scaling, the size on the host will not
61 // be right.
62 // For example
63 // Suppose:
64 // - No size scaling.
65 // - Client is a HiDPI Chromebook device.
66 // - Host is running on a HiDPI Windows device.
67 // With the configuration above, the client will send the logical touch
68 // size to the host, therefore it will be smaller on the host.
69 // This is because a HiDPI Chromebook device (e.g. Pixel) has 2 by 2
70 // physical pixel mapped to a logical pixel.
71 // With scaling, the size would be the same.
72 // TODO(rkuroiwa): Also clamp. Note that point->angle() affects the maximum
73 // size (crbug.com/461526).
74 if (point->has_radius_x() || point->has_radius_y()) {
75 DCHECK(point->has_radius_x() && point->has_radius_y());
76 point->set_radius_x(
77 Scale(point->radius_x(), output_size_.width(), input_size_.width()));
78 point->set_radius_y(Scale(point->radius_y(), output_size_.height(),
79 input_size_.height()));
83 InputFilter::InjectTouchEvent(out_event);
86 } // namespace remoting