This source file includes following definitions.
- GetScrollDamage
- GetPaintBounds
- HasPendingUpdate
- ClearPendingUpdate
- PopPendingUpdate
- InvalidateRect
- ScrollRect
- ScrollPaintRect
- ShouldInvalidateScrollRect
- InvalidateScrollRect
- CombinePaintRects
#include "content/renderer/paint_aggregator.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
namespace content {
static const float kMaxRedundantPaintToScrollArea = 0.8f;
static const size_t kMaxPaintRects = 5;
static const float kMaxPaintRectsAreaRatio = 0.7f;
PaintAggregator::PendingUpdate::PendingUpdate() {}
PaintAggregator::PendingUpdate::~PendingUpdate() {}
gfx::Rect PaintAggregator::PendingUpdate::GetScrollDamage() const {
DCHECK(!(scroll_delta.x() && scroll_delta.y()));
gfx::Rect damaged_rect;
if (scroll_delta.x()) {
int dx = scroll_delta.x();
damaged_rect.set_y(scroll_rect.y());
damaged_rect.set_height(scroll_rect.height());
if (dx > 0) {
damaged_rect.set_x(scroll_rect.x());
damaged_rect.set_width(dx);
} else {
damaged_rect.set_x(scroll_rect.right() + dx);
damaged_rect.set_width(-dx);
}
} else {
int dy = scroll_delta.y();
damaged_rect.set_x(scroll_rect.x());
damaged_rect.set_width(scroll_rect.width());
if (dy > 0) {
damaged_rect.set_y(scroll_rect.y());
damaged_rect.set_height(dy);
} else {
damaged_rect.set_y(scroll_rect.bottom() + dy);
damaged_rect.set_height(-dy);
}
}
return gfx::IntersectRects(scroll_rect, damaged_rect);
}
gfx::Rect PaintAggregator::PendingUpdate::GetPaintBounds() const {
gfx::Rect bounds;
for (size_t i = 0; i < paint_rects.size(); ++i)
bounds.Union(paint_rects[i]);
return bounds;
}
bool PaintAggregator::HasPendingUpdate() const {
return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
}
void PaintAggregator::ClearPendingUpdate() {
update_ = PendingUpdate();
}
void PaintAggregator::PopPendingUpdate(PendingUpdate* update) {
if (update_.scroll_rect.IsEmpty() && update_.paint_rects.size() > 1) {
int paint_area = 0;
gfx::Rect union_rect;
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
paint_area += update_.paint_rects[i].size().GetArea();
union_rect.Union(update_.paint_rects[i]);
}
int union_area = union_rect.size().GetArea();
if (float(paint_area) / float(union_area) > kMaxPaintRectsAreaRatio)
CombinePaintRects();
}
*update = update_;
ClearPendingUpdate();
}
void PaintAggregator::InvalidateRect(const gfx::Rect& rect) {
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
const gfx::Rect& existing_rect = update_.paint_rects[i];
if (existing_rect.Contains(rect))
return;
if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
gfx::Rect combined_rect = gfx::UnionRects(existing_rect, rect);
update_.paint_rects.erase(update_.paint_rects.begin() + i);
InvalidateRect(combined_rect);
return;
}
}
update_.paint_rects.push_back(rect);
if (!update_.scroll_rect.IsEmpty()) {
if (ShouldInvalidateScrollRect(rect)) {
InvalidateScrollRect();
} else if (update_.scroll_rect.Contains(rect)) {
update_.paint_rects[update_.paint_rects.size() - 1] =
gfx::SubtractRects(rect, update_.GetScrollDamage());
if (update_.paint_rects[update_.paint_rects.size() - 1].IsEmpty())
update_.paint_rects.erase(update_.paint_rects.end() - 1);
}
}
if (update_.paint_rects.size() > kMaxPaintRects)
CombinePaintRects();
HISTOGRAM_COUNTS_100("MPArch.RW_IntermediatePaintRectCount",
update_.paint_rects.size());
}
void PaintAggregator::ScrollRect(const gfx::Vector2d& delta,
const gfx::Rect& clip_rect) {
if (delta.x() != 0 && delta.y() != 0) {
InvalidateRect(clip_rect);
return;
}
if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
InvalidateRect(clip_rect);
return;
}
if ((delta.x() && update_.scroll_delta.y()) ||
(delta.y() && update_.scroll_delta.x())) {
InvalidateRect(clip_rect);
return;
}
update_.scroll_rect = clip_rect;
update_.scroll_delta += delta;
if (update_.scroll_delta.IsZero()) {
update_.scroll_rect = gfx::Rect();
return;
}
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
if (update_.scroll_rect.Contains(update_.paint_rects[i])) {
update_.paint_rects[i] = ScrollPaintRect(update_.paint_rects[i], delta);
if (update_.paint_rects[i].IsEmpty()) {
update_.paint_rects.erase(update_.paint_rects.begin() + i);
i--;
}
} else if (update_.scroll_rect.Intersects(update_.paint_rects[i])) {
InvalidateScrollRect();
return;
}
}
if (ShouldInvalidateScrollRect(gfx::Rect()))
InvalidateScrollRect();
}
gfx::Rect PaintAggregator::ScrollPaintRect(const gfx::Rect& paint_rect,
const gfx::Vector2d& delta) const {
gfx::Rect result = paint_rect + delta;
result.Intersect(update_.scroll_rect);
result.Subtract(update_.GetScrollDamage());
return result;
}
bool PaintAggregator::ShouldInvalidateScrollRect(const gfx::Rect& rect) const {
if (!rect.IsEmpty()) {
if (!update_.scroll_rect.Intersects(rect))
return false;
if (!update_.scroll_rect.Contains(rect))
return true;
}
int paint_area = rect.size().GetArea();
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
const gfx::Rect& existing_rect = update_.paint_rects[i];
if (update_.scroll_rect.Contains(existing_rect))
paint_area += existing_rect.size().GetArea();
}
int scroll_area = update_.scroll_rect.size().GetArea();
if (float(paint_area) / float(scroll_area) > kMaxRedundantPaintToScrollArea)
return true;
return false;
}
void PaintAggregator::InvalidateScrollRect() {
gfx::Rect scroll_rect = update_.scroll_rect;
update_.scroll_rect = gfx::Rect();
update_.scroll_delta = gfx::Vector2d();
InvalidateRect(scroll_rect);
}
void PaintAggregator::CombinePaintRects() {
if (update_.scroll_rect.IsEmpty()) {
gfx::Rect bounds = update_.GetPaintBounds();
update_.paint_rects.clear();
update_.paint_rects.push_back(bounds);
} else {
gfx::Rect inner, outer;
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
const gfx::Rect& existing_rect = update_.paint_rects[i];
if (update_.scroll_rect.Contains(existing_rect)) {
inner.Union(existing_rect);
} else {
outer.Union(existing_rect);
}
}
update_.paint_rects.clear();
update_.paint_rects.push_back(inner);
update_.paint_rects.push_back(outer);
}
}
}