1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/input/render_widget_host_view_input.h"
#include "base/notreached.h"
#include "components/input/render_widget_host_input_event_router.h"
#include "ui/gfx/geometry/dip_util.h"
namespace input {
RenderWidgetHostViewInput::RenderWidgetHostViewInput() = default;
RenderWidgetHostViewInput::~RenderWidgetHostViewInput() = default;
void RenderWidgetHostViewInput::NotifyObserversAboutShutdown() {
// Note: RenderWidgetHostInputEventRouter is an observer, and uses the
// following notification to remove this view from its surface owners map.
for (auto& observer : observers_) {
observer.OnRenderWidgetHostViewInputDestroyed(this);
}
// All observers are required to disconnect after they are notified.
CHECK(observers_.empty());
}
void RenderWidgetHostViewInput::ProcessAckedTouchEvent(
const input::TouchEventWithLatencyInfo& touch,
blink::mojom::InputEventResultState ack_result) {
DUMP_WILL_BE_NOTREACHED();
}
viz::FrameSinkId RenderWidgetHostViewInput::GetRootFrameSinkId() {
return viz::FrameSinkId();
}
bool RenderWidgetHostViewInput::ScreenRectIsUnstableFor(
const blink::WebInputEvent& event) {
return false;
}
bool RenderWidgetHostViewInput::ScreenRectIsUnstableForIOv2For(
const blink::WebInputEvent& event) {
return false;
}
gfx::PointF RenderWidgetHostViewInput::TransformPointToRootCoordSpaceF(
const gfx::PointF& point) const {
return point;
}
gfx::PointF RenderWidgetHostViewInput::TransformRootPointToViewCoordSpace(
const gfx::PointF& point) {
return point;
}
bool RenderWidgetHostViewInput::TransformPointToLocalCoordSpace(
const gfx::PointF& point,
const viz::FrameSinkId& original_frame_sink_id,
gfx::PointF* transformed_point) {
viz::FrameSinkId target_frame_sink_id = GetFrameSinkId();
if (!original_frame_sink_id.is_valid() || !target_frame_sink_id.is_valid()) {
return false;
}
if (original_frame_sink_id == target_frame_sink_id) {
return true;
}
if (!GetViewRenderInputRouter() || !GetViewRenderInputRouter()->delegate()) {
return false;
}
auto* router = GetViewRenderInputRouter()->delegate()->GetInputEventRouter();
if (!router) {
return false;
}
*transformed_point = point;
return TransformPointToTargetCoordSpace(
router->FindViewFromFrameSinkId(original_frame_sink_id),
router->FindViewFromFrameSinkId(target_frame_sink_id), point,
transformed_point);
}
bool RenderWidgetHostViewInput::TransformPointToCoordSpaceForView(
const gfx::PointF& point,
input::RenderWidgetHostViewInput* target_view,
gfx::PointF* transformed_point) {
NOTREACHED();
}
bool RenderWidgetHostViewInput::GetTransformToViewCoordSpace(
input::RenderWidgetHostViewInput* target_view,
gfx::Transform* transform) {
CHECK(transform);
if (target_view == this) {
transform->MakeIdentity();
return true;
}
viz::FrameSinkId root_frame_sink_id = GetRootFrameSinkId();
if (!root_frame_sink_id.is_valid()) {
return false;
}
const auto& display_hit_test_query_map = GetDisplayHitTestQuery();
const auto iter = display_hit_test_query_map.find(root_frame_sink_id);
if (iter == display_hit_test_query_map.end()) {
return false;
}
viz::HitTestQuery* query = iter->second.get();
gfx::Transform transform_this_to_root;
if (GetFrameSinkId() != root_frame_sink_id) {
gfx::Transform transform_root_to_this;
if (!query->GetTransformToTarget(GetFrameSinkId(),
&transform_root_to_this)) {
return false;
}
if (!transform_root_to_this.GetInverse(&transform_this_to_root)) {
return false;
}
}
gfx::Transform transform_root_to_target;
if (!query->GetTransformToTarget(target_view->GetFrameSinkId(),
&transform_root_to_target)) {
return false;
}
// TODO(wjmaclean): In TransformPointToTargetCoordSpace the device scale
// factor is taken from the original view ... does that matter? Presumably
// all the views have the same dsf.
float device_scale_factor = GetDeviceScaleFactor();
gfx::Transform transform_to_pixel;
transform_to_pixel.Scale(device_scale_factor, device_scale_factor);
gfx::Transform transform_from_pixel;
transform_from_pixel.Scale(1.f / device_scale_factor,
1.f / device_scale_factor);
// Note: gfx::Transform includes optimizations to early-out for scale = 1 or
// concatenating an identity matrix, so we don't add those checks here.
transform->MakeIdentity();
transform->PostConcat(transform_to_pixel);
transform->PostConcat(transform_this_to_root);
transform->PostConcat(transform_root_to_target);
transform->PostConcat(transform_from_pixel);
return true;
}
input::RenderWidgetHostViewInput*
RenderWidgetHostViewInput::GetParentViewInput() {
return nullptr;
}
blink::mojom::InputEventResultState RenderWidgetHostViewInput::FilterInputEvent(
const blink::WebInputEvent& input_event) {
// By default, input events are simply forwarded to the renderer.
return blink::mojom::InputEventResultState::kNotConsumed;
}
void RenderWidgetHostViewInput::WheelEventAck(
const blink::WebMouseWheelEvent& event,
blink::mojom::InputEventResultState ack_result) {}
void RenderWidgetHostViewInput::GestureEventAck(
const blink::WebGestureEvent& event,
blink::mojom::InputEventResultSource ack_source,
blink::mojom::InputEventResultState ack_result) {}
void RenderWidgetHostViewInput::ChildDidAckGestureEvent(
const blink::WebGestureEvent& event,
blink::mojom::InputEventResultState ack_result) {}
void RenderWidgetHostViewInput::DisplayCursor(const ui::Cursor& cursor) {
return;
}
input::CursorManager* RenderWidgetHostViewInput::GetCursorManager() {
return nullptr;
}
void RenderWidgetHostViewInput::TransformPointToRootSurface(
gfx::PointF* point) {
return;
}
int RenderWidgetHostViewInput::GetMouseWheelMinimumGranularity() const {
// Most platforms can specify the floating-point delta in the wheel event so
// they don't have a minimum granularity. Android is currently the only
// platform that overrides this.
return 0;
}
void RenderWidgetHostViewInput::AddObserver(
input::RenderWidgetHostViewInputObserver* observer) {
observers_.AddObserver(observer);
}
void RenderWidgetHostViewInput::RemoveObserver(
input::RenderWidgetHostViewInputObserver* observer) {
observers_.RemoveObserver(observer);
}
void RenderWidgetHostViewInput::StopFling() {
if (!GetViewRenderInputRouter()) {
return;
}
GetViewRenderInputRouter()->StopFling();
// In case of scroll bubbling tells the child's fling controller which is in
// charge of generating GSUs to stop flinging.
if (GetViewRenderInputRouter()->delegate() &&
GetViewRenderInputRouter()->delegate()->GetInputEventRouter()) {
GetViewRenderInputRouter()->delegate()->GetInputEventRouter()->StopFling();
}
}
void RenderWidgetHostViewInput::StopFlingingIfNecessary(
const blink::WebGestureEvent& event,
blink::mojom::InputEventResultState ack_result) {
// Reset view_stopped_flinging_for_test_ at the beginning of the scroll
// sequence.
if (event.GetType() == blink::WebInputEvent::Type::kGestureScrollBegin) {
view_stopped_flinging_for_test_ = false;
}
bool processed = blink::mojom::InputEventResultState::kConsumed == ack_result;
if (!processed &&
event.GetType() == blink::WebInputEvent::Type::kGestureScrollUpdate &&
event.data.scroll_update.inertial_phase ==
blink::WebGestureEvent::InertialPhaseState::kMomentum &&
event.SourceDevice() != blink::WebGestureDevice::kSyntheticAutoscroll) {
StopFling();
view_stopped_flinging_for_test_ = true;
}
}
void RenderWidgetHostViewInput::ForwardTouchpadZoomEventIfNecessary(
const blink::WebGestureEvent& event,
blink::mojom::InputEventResultState ack_result) {
if (!event.IsTouchpadZoomEvent()) {
return;
}
if (!event.NeedsWheelEvent()) {
return;
}
switch (event.GetType()) {
case blink::WebInputEvent::Type::kGesturePinchBegin:
// Don't send the begin event until we get the first unconsumed update, so
// that we elide pinch gesture steams consisting of only a begin and end.
pending_touchpad_pinch_begin_ = event;
pending_touchpad_pinch_begin_->SetNeedsWheelEvent(false);
break;
case blink::WebInputEvent::Type::kGesturePinchUpdate:
if (ack_result != blink::mojom::InputEventResultState::kConsumed &&
!event.data.pinch_update.zoom_disabled) {
if (pending_touchpad_pinch_begin_) {
GetViewRenderInputRouter()->ForwardGestureEvent(
*pending_touchpad_pinch_begin_);
pending_touchpad_pinch_begin_.reset();
}
// Now that the synthetic wheel event has gone unconsumed, we have the
// pinch event actually change the page scale.
blink::WebGestureEvent pinch_event(event);
pinch_event.SetNeedsWheelEvent(false);
GetViewRenderInputRouter()->ForwardGestureEvent(pinch_event);
}
break;
case blink::WebInputEvent::Type::kGesturePinchEnd:
if (pending_touchpad_pinch_begin_) {
pending_touchpad_pinch_begin_.reset();
} else {
blink::WebGestureEvent pinch_end_event(event);
pinch_end_event.SetNeedsWheelEvent(false);
GetViewRenderInputRouter()->ForwardGestureEvent(pinch_end_event);
}
break;
case blink::WebInputEvent::Type::kGestureDoubleTap:
if (ack_result != blink::mojom::InputEventResultState::kConsumed) {
blink::WebGestureEvent double_tap(event);
double_tap.SetNeedsWheelEvent(false);
// TODO(mcnee): Support double-tap zoom gesture for OOPIFs. For now,
// we naively send this to the main frame. If this is over an OOPIF,
// then the iframe element will incorrectly be used for the scale
// calculation rather than the element in the OOPIF.
// https://crbug.com/758348
GetViewRenderInputRouter()->ForwardGestureEvent(double_tap);
}
break;
default:
NOTREACHED();
}
}
// TODO(wjmaclean): Would it simplify this function if we re-implemented it
// using GetTransformToViewCoordSpace()?
bool RenderWidgetHostViewInput::TransformPointToTargetCoordSpace(
input::RenderWidgetHostViewInput* original_view,
input::RenderWidgetHostViewInput* target_view,
const gfx::PointF& point,
gfx::PointF* transformed_point) const {
CHECK(original_view);
CHECK(target_view);
viz::FrameSinkId root_frame_sink_id = original_view->GetRootFrameSinkId();
if (!root_frame_sink_id.is_valid()) {
return false;
}
const auto& display_hit_test_query_map = GetDisplayHitTestQuery();
const auto iter = display_hit_test_query_map.find(root_frame_sink_id);
if (iter == display_hit_test_query_map.end()) {
return false;
}
viz::HitTestQuery* query = iter->second.get();
std::vector<viz::FrameSinkId> target_ancestors;
target_ancestors.push_back(target_view->GetFrameSinkId());
input::RenderWidgetHostViewInput* cur_view = target_view;
while (cur_view->GetParentViewInput()) {
cur_view = cur_view->GetParentViewInput();
if (!cur_view) {
return false;
}
target_ancestors.push_back(cur_view->GetFrameSinkId());
}
if (target_ancestors.back() != root_frame_sink_id) {
target_ancestors.push_back(root_frame_sink_id);
}
float device_scale_factor = original_view->GetDeviceScaleFactor();
CHECK_GT(device_scale_factor, 0.0f);
// TODO(crbug.com/41460959): Optimize so that |point_in_pixels| doesn't need
// to be in the coordinate space of the root surface in HitTestQuery.
gfx::Transform transform_root_to_original;
query->GetTransformToTarget(original_view->GetFrameSinkId(),
&transform_root_to_original);
const std::optional<gfx::PointF> point_in_pixels =
transform_root_to_original.InverseMapPoint(
gfx::ConvertPointToPixels(point, device_scale_factor));
if (!point_in_pixels.has_value()) {
return false;
}
gfx::PointF transformed_point_in_physical_pixels;
if (!query->TransformLocationForTarget(
target_ancestors, *point_in_pixels,
&transformed_point_in_physical_pixels)) {
return false;
}
*transformed_point = gfx::ConvertPointToDips(
transformed_point_in_physical_pixels, device_scale_factor);
return true;
}
} // namespace input
|