This source file includes following definitions.
- GetScrollDamage
- GetPaintBounds
- max_paint_rects_
- HasPendingUpdate
- ClearPendingUpdate
- GetPendingUpdate
- InvalidateRect
- ScrollRect
- ScrollPaintRect
- ShouldInvalidateScrollRect
- InvalidateScrollRect
- CombinePaintRects
#include "ppapi/utility/graphics/paint_aggregator.h"
#include <algorithm>
#include "ppapi/cpp/logging.h"
namespace pp {
PaintAggregator::PaintUpdate::PaintUpdate() : has_scroll(false) {}
PaintAggregator::PaintUpdate::~PaintUpdate() {}
PaintAggregator::InternalPaintUpdate::InternalPaintUpdate() {}
PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() {}
Rect PaintAggregator::InternalPaintUpdate::GetScrollDamage() const {
PP_DCHECK(!(scroll_delta.x() && scroll_delta.y()));
Rect damaged_rect;
if (scroll_delta.x()) {
int32_t 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 {
int32_t 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 scroll_rect.Intersect(damaged_rect);
}
Rect PaintAggregator::InternalPaintUpdate::GetPaintBounds() const {
Rect bounds;
for (size_t i = 0; i < paint_rects.size(); ++i)
bounds = bounds.Union(paint_rects[i]);
return bounds;
}
PaintAggregator::PaintAggregator()
: max_redundant_paint_to_scroll_area_(0.8f),
max_paint_rects_(10) {
}
bool PaintAggregator::HasPendingUpdate() const {
return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
}
void PaintAggregator::ClearPendingUpdate() {
update_ = InternalPaintUpdate();
}
PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() const {
PaintUpdate ret;
ret.scroll_delta = update_.scroll_delta;
ret.scroll_rect = update_.scroll_rect;
ret.has_scroll = ret.scroll_delta.x() != 0 || ret.scroll_delta.y() != 0;
ret.paint_rects.reserve(update_.paint_rects.size() + 1);
for (size_t i = 0; i < update_.paint_rects.size(); i++)
ret.paint_rects.push_back(update_.paint_rects[i]);
ret.paint_bounds = update_.GetPaintBounds();
if (ret.has_scroll) {
PP_Rect scroll_damage = update_.GetScrollDamage();
ret.paint_rects.push_back(scroll_damage);
ret.paint_bounds = ret.paint_bounds.Union(scroll_damage);
}
return ret;
}
void PaintAggregator::InvalidateRect(const Rect& rect) {
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
const Rect& existing_rect = update_.paint_rects[i];
if (existing_rect.Contains(rect))
return;
if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
Rect combined_rect = existing_rect.Union(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] =
rect.Subtract(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() > max_paint_rects_)
CombinePaintRects();
}
void PaintAggregator::ScrollRect(const Rect& clip_rect, const Point& amount) {
if (amount.x() != 0 && amount.y() != 0) {
InvalidateRect(clip_rect);
return;
}
if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
InvalidateRect(clip_rect);
return;
}
if ((amount.x() && update_.scroll_delta.y()) ||
(amount.y() && update_.scroll_delta.x())) {
InvalidateRect(clip_rect);
return;
}
update_.scroll_rect = clip_rect;
update_.scroll_delta += amount;
if (update_.scroll_delta == Point()) {
update_.scroll_rect = 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], amount);
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(Rect()))
InvalidateScrollRect();
}
Rect PaintAggregator::ScrollPaintRect(const Rect& paint_rect,
const Point& amount) const {
Rect result = paint_rect;
result.Offset(amount);
result = update_.scroll_rect.Intersect(result);
return result.Subtract(update_.GetScrollDamage());
}
bool PaintAggregator::ShouldInvalidateScrollRect(const 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 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) > max_redundant_paint_to_scroll_area_)
return true;
return false;
}
void PaintAggregator::InvalidateScrollRect() {
Rect scroll_rect = update_.scroll_rect;
update_.scroll_rect = Rect();
update_.scroll_delta = Point();
InvalidateRect(scroll_rect);
}
void PaintAggregator::CombinePaintRects() {
if (update_.scroll_rect.IsEmpty()) {
Rect bounds = update_.GetPaintBounds();
update_.paint_rects.clear();
update_.paint_rects.push_back(bounds);
} else {
Rect inner, outer;
for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
const Rect& existing_rect = update_.paint_rects[i];
if (update_.scroll_rect.Contains(existing_rect)) {
inner = inner.Union(existing_rect);
} else {
outer = outer.Union(existing_rect);
}
}
update_.paint_rects.clear();
update_.paint_rects.push_back(inner);
update_.paint_rects.push_back(outer);
}
}
}