Commit 9d3334b0 authored by Navid Zolghadr's avatar Navid Zolghadr Committed by Commit Bot

Move back pointer event maps to WTF

In an earlier change we changed all WTF maps
to std maps to account for all the values in
the key range (i.e. PointerId). But the solution
of using WTF with a bigger type as key (in this
case int64_t) is better than using std map.

Bug: 921325
Change-Id: Ice58c0fb55ab12138467de9f18fadc33b0815215
Reviewed-on: https://chromium-review.googlesource.com/c/1477755Reviewed-by: default avatarDave Tapuska <dtapuska@chromium.org>
Commit-Queue: Navid Zolghadr <nzolghadr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#633767}
parent c3344361
...@@ -319,7 +319,7 @@ PointerEvent* PointerEventFactory::Create( ...@@ -319,7 +319,7 @@ PointerEvent* PointerEventFactory::Create(
void PointerEventFactory::SetLastPosition(int pointer_id, void PointerEventFactory::SetLastPosition(int pointer_id,
const WebPointerProperties& event) { const WebPointerProperties& event) {
pointer_id_last_position_mapping_[pointer_id] = event.PositionInScreen(); pointer_id_last_position_mapping_.Set(pointer_id, event.PositionInScreen());
} }
void PointerEventFactory::RemoveLastPosition(const int pointer_id) { void PointerEventFactory::RemoveLastPosition(const int pointer_id) {
...@@ -329,10 +329,8 @@ void PointerEventFactory::RemoveLastPosition(const int pointer_id) { ...@@ -329,10 +329,8 @@ void PointerEventFactory::RemoveLastPosition(const int pointer_id) {
FloatPoint PointerEventFactory::GetLastPointerPosition( FloatPoint PointerEventFactory::GetLastPointerPosition(
int pointer_id, int pointer_id,
const WebPointerProperties& event) const { const WebPointerProperties& event) const {
const auto& last_position_iterator = if (pointer_id_last_position_mapping_.Contains(pointer_id))
pointer_id_last_position_mapping_.find(pointer_id); return pointer_id_last_position_mapping_.at(pointer_id);
if (last_position_iterator != pointer_id_last_position_mapping_.end())
return last_position_iterator->second;
// If pointer_id is not in the map, returns the current position so the // If pointer_id is not in the map, returns the current position so the
// movement will be zero. // movement will be zero.
return event.PositionInScreen(); return event.PositionInScreen();
...@@ -341,9 +339,11 @@ FloatPoint PointerEventFactory::GetLastPointerPosition( ...@@ -341,9 +339,11 @@ FloatPoint PointerEventFactory::GetLastPointerPosition(
PointerEvent* PointerEventFactory::CreatePointerCancelEvent( PointerEvent* PointerEventFactory::CreatePointerCancelEvent(
const int pointer_id, const int pointer_id,
TimeTicks platfrom_time_stamp) { TimeTicks platfrom_time_stamp) {
DCHECK(pointer_id_mapping_.find(pointer_id) != pointer_id_mapping_.end()); DCHECK(pointer_id_mapping_.Contains(pointer_id));
pointer_id_mapping_[pointer_id] = PointerAttributes( pointer_id_mapping_.Set(
pointer_id_mapping_.at(pointer_id).incoming_id, false, true); pointer_id,
PointerAttributes(pointer_id_mapping_.at(pointer_id).incoming_id, false,
true));
PointerEventInit* pointer_event_init = PointerEventInit::Create(); PointerEventInit* pointer_event_init = PointerEventInit::Create();
...@@ -452,8 +452,10 @@ void PointerEventFactory::Clear() { ...@@ -452,8 +452,10 @@ void PointerEventFactory::Clear() {
// No need to add it to |pointer_incoming_id_mapping_| as it is not going to // No need to add it to |pointer_incoming_id_mapping_| as it is not going to
// be used with the existing APIs // be used with the existing APIs
primary_id_[ToInt(WebPointerProperties::PointerType::kMouse)] = kMouseId; primary_id_[ToInt(WebPointerProperties::PointerType::kMouse)] = kMouseId;
pointer_id_mapping_[kMouseId] = PointerAttributes( pointer_id_mapping_.insert(
IncomingId(WebPointerProperties::PointerType::kMouse, 0), false, true); kMouseId, PointerAttributes(
IncomingId(WebPointerProperties::PointerType::kMouse, 0),
false, true));
current_id_ = PointerEventFactory::kMouseId + 1; current_id_ = PointerEventFactory::kMouseId + 1;
} }
...@@ -463,15 +465,15 @@ PointerId PointerEventFactory::AddIdAndActiveButtons(const IncomingId p, ...@@ -463,15 +465,15 @@ PointerId PointerEventFactory::AddIdAndActiveButtons(const IncomingId p,
bool hovering) { bool hovering) {
// Do not add extra mouse pointer as it was added in initialization. // Do not add extra mouse pointer as it was added in initialization.
if (p.GetPointerType() == WebPointerProperties::PointerType::kMouse) { if (p.GetPointerType() == WebPointerProperties::PointerType::kMouse) {
pointer_id_mapping_[kMouseId] = pointer_id_mapping_.Set(kMouseId,
PointerAttributes(p, is_active_buttons, true); PointerAttributes(p, is_active_buttons, true));
return kMouseId; return kMouseId;
} }
if (pointer_incoming_id_mapping_.Contains(p)) { if (pointer_incoming_id_mapping_.Contains(p)) {
PointerId mapped_id = pointer_incoming_id_mapping_.at(p); PointerId mapped_id = pointer_incoming_id_mapping_.at(p);
pointer_id_mapping_[mapped_id] = pointer_id_mapping_.Set(mapped_id,
PointerAttributes(p, is_active_buttons, hovering); PointerAttributes(p, is_active_buttons, hovering));
return mapped_id; return mapped_id;
} }
int type_int = p.PointerTypeInt(); int type_int = p.PointerTypeInt();
...@@ -481,15 +483,14 @@ PointerId PointerEventFactory::AddIdAndActiveButtons(const IncomingId p, ...@@ -481,15 +483,14 @@ PointerId PointerEventFactory::AddIdAndActiveButtons(const IncomingId p,
primary_id_[type_int] = mapped_id; primary_id_[type_int] = mapped_id;
id_count_[type_int]++; id_count_[type_int]++;
pointer_incoming_id_mapping_.insert(p, mapped_id); pointer_incoming_id_mapping_.insert(p, mapped_id);
pointer_id_mapping_[mapped_id] = pointer_id_mapping_.insert(mapped_id,
PointerAttributes(p, is_active_buttons, hovering); PointerAttributes(p, is_active_buttons, hovering));
return mapped_id; return mapped_id;
} }
bool PointerEventFactory::Remove(const PointerId mapped_id) { bool PointerEventFactory::Remove(const PointerId mapped_id) {
// Do not remove mouse pointer id as it should always be there. // Do not remove mouse pointer id as it should always be there.
if (mapped_id == kMouseId || if (mapped_id == kMouseId || !pointer_id_mapping_.Contains(mapped_id))
pointer_id_mapping_.find(mapped_id) == pointer_id_mapping_.end())
return false; return false;
IncomingId p = pointer_id_mapping_.at(mapped_id).incoming_id; IncomingId p = pointer_id_mapping_.at(mapped_id).incoming_id;
...@@ -509,8 +510,8 @@ Vector<PointerId> PointerEventFactory::GetPointerIdsOfNonHoveringPointers() ...@@ -509,8 +510,8 @@ Vector<PointerId> PointerEventFactory::GetPointerIdsOfNonHoveringPointers()
for (auto iter = pointer_id_mapping_.begin(); for (auto iter = pointer_id_mapping_.begin();
iter != pointer_id_mapping_.end(); ++iter) { iter != pointer_id_mapping_.end(); ++iter) {
PointerId mapped_id = iter->first; PointerId mapped_id = static_cast<PointerId>(iter->key);
if (!iter->second.hovering) if (!iter->value.hovering)
mapped_ids.push_back(mapped_id); mapped_ids.push_back(mapped_id);
} }
...@@ -520,16 +521,15 @@ Vector<PointerId> PointerEventFactory::GetPointerIdsOfNonHoveringPointers() ...@@ -520,16 +521,15 @@ Vector<PointerId> PointerEventFactory::GetPointerIdsOfNonHoveringPointers()
} }
bool PointerEventFactory::IsPrimary(PointerId mapped_id) const { bool PointerEventFactory::IsPrimary(PointerId mapped_id) const {
const auto& pointer_iterator = pointer_id_mapping_.find(mapped_id); if (!pointer_id_mapping_.Contains(mapped_id))
if (pointer_iterator == pointer_id_mapping_.end())
return false; return false;
IncomingId p = pointer_iterator->second.incoming_id; IncomingId p = pointer_id_mapping_.at(mapped_id).incoming_id;
return primary_id_[p.PointerTypeInt()] == mapped_id; return primary_id_[p.PointerTypeInt()] == mapped_id;
} }
bool PointerEventFactory::IsActive(const PointerId pointer_id) const { bool PointerEventFactory::IsActive(const PointerId pointer_id) const {
return pointer_id_mapping_.find(pointer_id) != pointer_id_mapping_.end(); return pointer_id_mapping_.Contains(pointer_id);
} }
bool PointerEventFactory::IsPrimary( bool PointerEventFactory::IsPrimary(
...@@ -550,9 +550,8 @@ bool PointerEventFactory::IsPrimary( ...@@ -550,9 +550,8 @@ bool PointerEventFactory::IsPrimary(
bool PointerEventFactory::IsActiveButtonsState( bool PointerEventFactory::IsActiveButtonsState(
const PointerId pointer_id) const { const PointerId pointer_id) const {
const auto& pointer_iterator = pointer_id_mapping_.find(pointer_id); return pointer_id_mapping_.Contains(pointer_id) &&
return pointer_iterator != pointer_id_mapping_.end() && pointer_id_mapping_.at(pointer_id).is_active_buttons;
pointer_iterator->second.is_active_buttons;
} }
WebPointerProperties::PointerType PointerEventFactory::GetPointerType( WebPointerProperties::PointerType PointerEventFactory::GetPointerType(
......
...@@ -91,6 +91,13 @@ class CORE_EXPORT PointerEventFactory { ...@@ -91,6 +91,13 @@ class CORE_EXPORT PointerEventFactory {
const WebPointerProperties& event) const; const WebPointerProperties& event) const;
private: private:
// We use int64_t to cover the whole range for PointerId with no
// deleted hash value.
template <typename T>
using PointerIdKeyMap = HashMap<int64_t,
T,
WTF::IntHash<int64_t>,
WTF::UnsignedWithZeroKeyHashTraits<int64_t>>;
typedef struct IncomingId : public std::pair<int, int> { typedef struct IncomingId : public std::pair<int, int> {
IncomingId() = default; IncomingId() = default;
IncomingId(WebPointerProperties::PointerType pointer_type, int raw_id) IncomingId(WebPointerProperties::PointerType pointer_type, int raw_id)
...@@ -145,10 +152,7 @@ class CORE_EXPORT PointerEventFactory { ...@@ -145,10 +152,7 @@ class CORE_EXPORT PointerEventFactory {
WTF::PairHashTraits<WTF::UnsignedWithZeroKeyHashTraits<int>, WTF::PairHashTraits<WTF::UnsignedWithZeroKeyHashTraits<int>,
WTF::UnsignedWithZeroKeyHashTraits<int>>> WTF::UnsignedWithZeroKeyHashTraits<int>>>
pointer_incoming_id_mapping_; pointer_incoming_id_mapping_;
// We are using unordered_map instead of WTF::HashMap to accommodate PointerIdKeyMap<PointerAttributes> pointer_id_mapping_;
// for all the possible keys in the given rage as WTF::HashTraits have at
// least one unsupported key like 0 or max value in their range.
std::unordered_map<PointerId, PointerAttributes> pointer_id_mapping_;
int primary_id_[static_cast<int>( int primary_id_[static_cast<int>(
WebPointerProperties::PointerType::kLastEntry) + WebPointerProperties::PointerType::kLastEntry) +
1]; 1];
...@@ -156,7 +160,7 @@ class CORE_EXPORT PointerEventFactory { ...@@ -156,7 +160,7 @@ class CORE_EXPORT PointerEventFactory {
WebPointerProperties::PointerType::kLastEntry) + WebPointerProperties::PointerType::kLastEntry) +
1]; 1];
std::unordered_map<PointerId, FloatPoint> pointer_id_last_position_mapping_; PointerIdKeyMap<FloatPoint> pointer_id_last_position_mapping_;
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment