blob: ed06aa7cb2549d15703ddf2b055244a25495a184 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2020 The Chromium Authors
Mario Bianuccif190dbab2020-07-28 21:12:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Mario Bianuccia538c5d2021-03-15 20:40:235#include "ui/gfx/delegated_ink_point.h"
Mario Bianuccif190dbab2020-07-28 21:12:146
7#include <inttypes.h>
8
9#include "base/strings/stringprintf.h"
Sahir Vellani15782292024-05-02 22:09:0910#include "base/trace_event/trace_event.h"
Mario Bianuccide90327a2021-08-05 19:21:1711#include "ui/gfx/delegated_ink_metadata.h"
Mario Bianuccif190dbab2020-07-28 21:12:1412
Mario Bianuccia538c5d2021-03-15 20:40:2313namespace gfx {
Mario Bianuccif190dbab2020-07-28 21:12:1414
Mario Bianuccide90327a2021-08-05 19:21:1715bool DelegatedInkPoint::MatchesDelegatedInkMetadata(
16 const DelegatedInkMetadata* metadata) const {
Sahir Vellani15782292024-05-02 22:09:0917 if (!metadata) {
18 return false;
19 }
Mario Bianuccide90327a2021-08-05 19:21:1720 // The maximum difference allowed when comparing a DelegatedInkPoint |point_|
21 // to the |point_| on a DelegatedInkMetadata. Some precision loss can occur
22 // when moving between coordinate spaces in the browser and renderer,
23 // particularly when the device scale factor is not a whole number. This can
24 // result in a DelegatedInkMetadata and DelegatedInkPoint having been created
25 // from the same point, but having a very small difference. When this occurs,
26 // we can safely ignore that they are slightly different and use the point for
27 // a delegated ink trail anyway, since it is a very small difference and will
Sahir Vellania06859b2024-05-07 16:23:0828 // only be visible for a single frame. The allowed difference is the square
29 // root of 2 - which is derived from the maximum allowed difference of 1px in
30 // each direction.
31 // TODO(crbug.com/338250110) Consider removing this tolerance.
32 constexpr float kEpsilon = 1.4143f;
Sahir Vellani15782292024-05-02 22:09:0933 TRACE_EVENT_INSTANT2("delegated_ink_trails",
34 "DelegatedInkPoint::MatchesDelegatedInkMetadata",
35 TRACE_EVENT_SCOPE_THREAD, "metadata",
36 metadata->ToString(), "point", ToString());
37 return timestamp_ == metadata->timestamp() &&
Mario Bianuccide90327a2021-08-05 19:21:1738 point_.IsWithinDistance(metadata->point(), kEpsilon);
39}
40
Mario Bianuccif190dbab2020-07-28 21:12:1441std::string DelegatedInkPoint::ToString() const {
Mario Bianucci5621d8b2021-01-28 01:47:3942 return base::StringPrintf("point: %s, timestamp: %" PRId64 ", pointer_id: %d",
Mario Bianuccif190dbab2020-07-28 21:12:1443 point_.ToString().c_str(),
Mario Bianucci5621d8b2021-01-28 01:47:3944 timestamp_.since_origin().InMicroseconds(),
45 pointer_id_);
Mario Bianuccif190dbab2020-07-28 21:12:1446}
47
Mario Bianuccia538c5d2021-03-15 20:40:2348} // namespace gfx