Skip to content

Commit

Permalink
Bug 1819802 part 1: When getting cached a11y character bounds, return…
Browse files Browse the repository at this point in the history
… a single rect rather than an array of rects. r=morgan

When hit testing, we retrieve the bounds for every character in a text leaf in a separate call.
Because we need to convert from an array of flat ints to rects, this previously meant we built the entire array for every character.
For a large text leaf, building this rect array is relatively expensive.
We only need a single rect at a time.
Therefore, RemoteAccessibleBase now returns the rect for a single requested character instead of building an array of rects.

Differential Revision: https://phabricator.services.mozilla.com/D171419
  • Loading branch information
jcsteh committed Mar 3, 2023
1 parent ed1ebe7 commit 958055f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
4 changes: 4 additions & 0 deletions accessible/base/CacheConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ static constexpr RelationData kRelationTypeAtoms[] = {
RelationType::FLOWS_FROM},
};

// The count of numbers needed to serialize an nsRect. This is used when
// flattening character rects into an array of ints.
constexpr int32_t kNumbersInRect = 4;

} // namespace a11y
} // namespace mozilla

Expand Down
10 changes: 3 additions & 7 deletions accessible/base/TextLeafRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1668,13 +1668,9 @@ LayoutDeviceIntRect TextLeafPoint::CharBounds() {
}

RemoteAccessible* remote = mAcc->AsRemote();
if (Maybe<nsTArray<nsRect>> charBounds = remote->GetCachedCharData()) {
if (mOffset < static_cast<int32_t>(charBounds->Length())) {
return remote->BoundsWithOffset(Some(charBounds->ElementAt(mOffset)));
}
// It is valid for a client to call this with an offset 1 after the last
// character because of the insertion point at the end of text boxes.
MOZ_ASSERT(mOffset == static_cast<int32_t>(charBounds->Length()));
nsRect charBounds = remote->GetCachedCharRect(mOffset);
if (!charBounds.IsEmpty()) {
return remote->BoundsWithOffset(Some(charBounds));
}

return LayoutDeviceIntRect();
Expand Down
18 changes: 10 additions & 8 deletions accessible/ipc/RemoteAccessibleBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,25 +1064,27 @@ RemoteAccessibleBase<Derived>::GetCachedTextLines() {
}

template <class Derived>
Maybe<nsTArray<nsRect>> RemoteAccessibleBase<Derived>::GetCachedCharData() {
nsRect RemoteAccessibleBase<Derived>::GetCachedCharRect(int32_t aOffset) {
MOZ_ASSERT(IsText());
if (!mCachedFields) {
return Nothing();
return nsRect();
}

if (Maybe<const nsTArray<int32_t>&> maybeCharData =
mCachedFields->GetAttribute<nsTArray<int32_t>>(
nsGkAtoms::characterData)) {
const nsTArray<int32_t>& charData = *maybeCharData;
nsTArray<nsRect> rects;
for (int i = 0; i < static_cast<int32_t>(charData.Length()); i += 4) {
nsRect r(charData[i], charData[i + 1], charData[i + 2], charData[i + 3]);
rects.AppendElement(r);
const int32_t index = aOffset * kNumbersInRect;
if (index < static_cast<int32_t>(charData.Length())) {
return nsRect(charData[index], charData[index + 1], charData[index + 2],
charData[index + 3]);
}
return Some(std::move(rects));
// It is valid for a client to call this with an offset 1 after the last
// character because of the insertion point at the end of text boxes.
MOZ_ASSERT(index == static_cast<int32_t>(charData.Length()));
}

return Nothing();
return nsRect();
}

template <class Derived>
Expand Down
2 changes: 1 addition & 1 deletion accessible/ipc/RemoteAccessibleBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class RemoteAccessibleBase : public Accessible, public HyperTextAccessibleBase {

uint32_t GetCachedTextLength();
Maybe<const nsTArray<int32_t>&> GetCachedTextLines();
Maybe<nsTArray<nsRect>> GetCachedCharData();
nsRect GetCachedCharRect(int32_t aOffset);
RefPtr<const AccAttributes> GetCachedTextAttributes();
RefPtr<const AccAttributes> GetCachedARIAAttributes() const;

Expand Down

0 comments on commit 958055f

Please sign in to comment.