0

Add gfx::UnionRects() variant that takes a span of rects

Do this for gfx::Rect and gfx::RectF. Convert some existing code to use
the newly added helpers.

Change-Id: Ibe11b05199ddb8414f735777773d92a042fcd25a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5854921
Reviewed-by: Keren Zhu <kerenzhu@chromium.org>
Reviewed-by: Robert Flack <flackr@chromium.org>
Commit-Queue: Robert Flack <flackr@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Auto-Submit: Lei Zhang <thestig@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Reviewed-by: Akihiro Ota <akihiroota@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1354815}
This commit is contained in:
Lei Zhang
2024-09-12 21:07:40 +00:00
committed by Chromium LUCI CQ
parent f85e7708f7
commit 16c5032993
9 changed files with 29 additions and 24 deletions
ash/accessibility/ui
cc/base
components
ui_devtools
viz
service
pdf/pdfium
ui/gfx/geometry

@ -39,10 +39,7 @@ void AccessibilityHighlightLayer::Set(const std::vector<gfx::Rect>& rects,
// Calculate the bounds of all the rects together, that represents
// the bounds of the full layer.
gfx::Rect bounds;
for (const gfx::Rect rect : rects_) {
bounds.Union(rect);
}
gfx::Rect bounds = gfx::UnionRects(rects_);
int inset = kLayerMargin;
bounds.Inset(-inset);

@ -39,15 +39,13 @@ void InvalidationRegion::FinalizePendingRects() {
if (pending_rects_.empty())
return;
gfx::Rect pending_bounds = gfx::UnionRects(pending_rects_);
if (region_.GetRegionComplexity() + pending_rects_.size() >
kMaxInvalidationRectCount) {
gfx::Rect pending_bounds = region_.bounds();
for (auto& rect : pending_rects_)
pending_bounds.Union(rect);
pending_bounds.Union(region_.bounds());
region_ = pending_bounds;
} else {
for (auto& rect : pending_rects_)
region_.Union(rect);
region_.Union(pending_bounds);
}
pending_rects_.clear();

@ -49,9 +49,7 @@ void AppendLayerPropertiesMatchedStyle(
const ui::Layer::ShapeRects* alpha_shape_bounds = layer->alpha_shape();
if (alpha_shape_bounds && alpha_shape_bounds->size()) {
gfx::Rect bounding_box;
for (auto& shape_bound : *alpha_shape_bounds)
bounding_box.Union(shape_bound);
gfx::Rect bounding_box = gfx::UnionRects(*alpha_shape_bounds);
ret->emplace_back("alpha-shape-bounding-box", bounding_box.ToString());
}

@ -309,10 +309,8 @@ bool HasOccludingDamageRect(
// surface_damage_rect_list[0] is the one on the very top.
// surface_damage_rect_list[overlay_damage_index] is the damage rect of
// this overlay surface.
gfx::Rect occluding_damage_rect;
for (size_t i = 0; i < overlay_damage_index; ++i) {
occluding_damage_rect.Union(surface_damage_rect_list[i]);
}
gfx::Rect occluding_damage_rect = gfx::UnionRects(
base::make_span(surface_damage_rect_list).first(overlay_damage_index));
occluding_damage_rect.Intersect(quad_rect_in_target_space);
return !occluding_damage_rect.IsEmpty();

@ -2101,15 +2101,13 @@ bool PDFiumEngine::SelectFindResult(bool forward) {
selection_.push_back(find_results_[current_find_index_.value()]);
// If the result is not in view, scroll to it.
gfx::Rect bounding_rect;
gfx::Rect visible_rect = GetVisibleRect();
// Use zoom of 1.0 since `visible_rect` is without zoom.
const std::vector<gfx::Rect>& rects =
find_results_[current_find_index_.value()].GetScreenRects(
gfx::Point(), 1.0, layout_.options().default_page_orientation());
for (const auto& rect : rects)
bounding_rect.Union(rect);
const gfx::Rect bounding_rect = gfx::UnionRects(rects);
if (!visible_rect.Contains(bounding_rect)) {
gfx::Point center = bounding_rect.CenterPoint();
// Make the page centered.
@ -2152,12 +2150,9 @@ std::vector<gfx::Rect> PDFiumEngine::GetAllScreenRectsUnion(
std::vector<gfx::Rect> rect_vector;
rect_vector.reserve(rect_range.size());
for (const auto& range : rect_range) {
gfx::Rect result_rect;
const std::vector<gfx::Rect>& rects = range.GetScreenRects(
point, current_zoom_, layout_.options().default_page_orientation());
for (const auto& rect : rects)
result_rect.Union(rect);
rect_vector.push_back(result_rect);
rect_vector.push_back(gfx::UnionRects(rects));
}
return rect_vector;
}

@ -344,6 +344,14 @@ Rect UnionRects(const Rect& a, const Rect& b) {
return result;
}
Rect UnionRects(base::span<const Rect> rects) {
Rect result;
for (const Rect& rect : rects) {
result.Union(rect);
}
return result;
}
Rect UnionRectsEvenIfEmpty(const Rect& a, const Rect& b) {
Rect result = a;
result.UnionEvenIfEmpty(b);

@ -16,7 +16,7 @@
#include <iosfwd>
#include <string>
#include "base/check.h"
#include "base/containers/span.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_conversions.h"
#include "build/build_config.h"
@ -292,6 +292,7 @@ inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
GEOMETRY_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
GEOMETRY_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
GEOMETRY_EXPORT Rect UnionRects(base::span<const Rect> rects);
GEOMETRY_EXPORT Rect UnionRectsEvenIfEmpty(const Rect& a, const Rect& b);
GEOMETRY_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);

@ -285,6 +285,14 @@ RectF UnionRects(const RectF& a, const RectF& b) {
return result;
}
RectF UnionRects(base::span<const RectF> rects) {
RectF result;
for (const RectF& rect : rects) {
result.Union(rect);
}
return result;
}
RectF UnionRectsEvenIfEmpty(const RectF& a, const RectF& b) {
RectF result = a;
result.UnionEvenIfEmpty(b);

@ -8,6 +8,7 @@
#include <iosfwd>
#include <string>
#include "base/containers/span.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/outsets_f.h"
@ -272,6 +273,7 @@ inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
GEOMETRY_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
GEOMETRY_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
GEOMETRY_EXPORT RectF UnionRects(base::span<const RectF> rects);
GEOMETRY_EXPORT RectF UnionRectsEvenIfEmpty(const RectF& a, const RectF& b);
GEOMETRY_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);