Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Mario Bianucci | a538c5d | 2021-03-15 20:40:23 | [diff] [blame] | 5 | #include "ui/gfx/delegated_ink_point.h" |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 6 | |
| 7 | #include <inttypes.h> |
| 8 | |
| 9 | #include "base/strings/stringprintf.h" |
Sahir Vellani | 1578229 | 2024-05-02 22:09:09 | [diff] [blame] | 10 | #include "base/trace_event/trace_event.h" |
Mario Bianucci | de90327a | 2021-08-05 19:21:17 | [diff] [blame] | 11 | #include "ui/gfx/delegated_ink_metadata.h" |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 12 | |
Mario Bianucci | a538c5d | 2021-03-15 20:40:23 | [diff] [blame] | 13 | namespace gfx { |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 14 | |
Mario Bianucci | de90327a | 2021-08-05 19:21:17 | [diff] [blame] | 15 | bool DelegatedInkPoint::MatchesDelegatedInkMetadata( |
| 16 | const DelegatedInkMetadata* metadata) const { |
Sahir Vellani | 1578229 | 2024-05-02 22:09:09 | [diff] [blame] | 17 | if (!metadata) { |
| 18 | return false; |
| 19 | } |
Mario Bianucci | de90327a | 2021-08-05 19:21:17 | [diff] [blame] | 20 | // 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 Vellani | a06859b | 2024-05-07 16:23:08 | [diff] [blame] | 28 | // 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 Vellani | 1578229 | 2024-05-02 22:09:09 | [diff] [blame] | 33 | 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 Bianucci | de90327a | 2021-08-05 19:21:17 | [diff] [blame] | 38 | point_.IsWithinDistance(metadata->point(), kEpsilon); |
| 39 | } |
| 40 | |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 41 | std::string DelegatedInkPoint::ToString() const { |
Mario Bianucci | 5621d8b | 2021-01-28 01:47:39 | [diff] [blame] | 42 | return base::StringPrintf("point: %s, timestamp: %" PRId64 ", pointer_id: %d", |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 43 | point_.ToString().c_str(), |
Mario Bianucci | 5621d8b | 2021-01-28 01:47:39 | [diff] [blame] | 44 | timestamp_.since_origin().InMicroseconds(), |
| 45 | pointer_id_); |
Mario Bianucci | f190dbab | 2020-07-28 21:12:14 | [diff] [blame] | 46 | } |
| 47 | |
Mario Bianucci | a538c5d | 2021-03-15 20:40:23 | [diff] [blame] | 48 | } // namespace gfx |