This source file includes following definitions.
- calculateArea
- subtractIntersection
- sharesEdge
- calculateScrollDamage
- calculatePaintBounds
- hasPendingUpdate
- clearPendingUpdate
- popPendingUpdate
- invalidateRect
- scrollRect
- scrollPaintRect
- shouldInvalidateScrollRect
- invalidateScrollRect
- combinePaintRects
#include "config.h"
#include "PaintAggregator.h"
#include "public/platform/Platform.h"
using namespace WebCore;
namespace blink {
static const float maxRedundantPaintToScrollArea = 0.8f;
static const size_t maxPaintRects = 5;
static const float maxPaintRectsAreaRatio = 0.7f;
static int calculateArea(const IntRect& rect)
{
return rect.size().width() * rect.size().height();
}
static IntRect subtractIntersection(const IntRect& a, const IntRect& b)
{
if (!a.intersects(b))
return a;
if (b.contains(a))
return IntRect();
int rx = a.x();
int ry = a.y();
int rr = a.maxX();
int rb = a.maxY();
if (b.y() <= a.y() && b.maxY() >= a.maxY()) {
if (b.x() <= a.x())
rx = b.maxX();
else
rr = b.x();
} else if (b.x() <= a.x() && b.maxX() >= a.maxX()) {
if (b.y() <= a.y())
ry = b.maxY();
else
rb = b.y();
}
return IntRect(rx, ry, rr - rx, rb - ry);
}
static bool sharesEdge(const IntRect& a, const IntRect& b)
{
return (a.y() == b.y() && a.height() == b.height() && (a.x() == b.maxX() || a.maxX() == b.x()))
|| (a.x() == b.x() && a.width() == b.width() && (a.y() == b.maxY() || a.maxY() == b.y()));
}
PaintAggregator::PendingUpdate::PendingUpdate()
{
}
PaintAggregator::PendingUpdate::~PendingUpdate()
{
}
IntRect PaintAggregator::PendingUpdate::calculateScrollDamage() const
{
ASSERT(!(scrollDelta.x() && scrollDelta.y()));
IntRect damagedRect;
if (scrollDelta.x()) {
int dx = scrollDelta.x();
damagedRect.setY(scrollRect.y());
damagedRect.setHeight(scrollRect.height());
if (dx > 0) {
damagedRect.setX(scrollRect.x());
damagedRect.setWidth(dx);
} else {
damagedRect.setX(scrollRect.maxX() + dx);
damagedRect.setWidth(-dx);
}
} else {
int dy = scrollDelta.y();
damagedRect.setX(scrollRect.x());
damagedRect.setWidth(scrollRect.width());
if (dy > 0) {
damagedRect.setY(scrollRect.y());
damagedRect.setHeight(dy);
} else {
damagedRect.setY(scrollRect.maxY() + dy);
damagedRect.setHeight(-dy);
}
}
return intersection(scrollRect, damagedRect);
}
IntRect PaintAggregator::PendingUpdate::calculatePaintBounds() const
{
IntRect bounds;
for (size_t i = 0; i < paintRects.size(); ++i)
bounds.unite(paintRects[i]);
return bounds;
}
bool PaintAggregator::hasPendingUpdate() const
{
return !m_update.scrollRect.isEmpty() || !m_update.paintRects.isEmpty();
}
void PaintAggregator::clearPendingUpdate()
{
m_update = PendingUpdate();
}
void PaintAggregator::popPendingUpdate(PendingUpdate* update)
{
if (m_update.scrollRect.isEmpty() && m_update.paintRects.size() > 1) {
int paintArea = 0;
IntRect unionRect;
for (size_t i = 0; i < m_update.paintRects.size(); ++i) {
paintArea += calculateArea(m_update.paintRects[i]);
unionRect.unite(m_update.paintRects[i]);
}
int unionArea = calculateArea(unionRect);
if (float(paintArea) / float(unionArea) > maxPaintRectsAreaRatio)
combinePaintRects();
}
*update = m_update;
clearPendingUpdate();
}
void PaintAggregator::invalidateRect(const IntRect& rect)
{
for (size_t i = 0; i < m_update.paintRects.size(); ++i) {
const IntRect& existingRect = m_update.paintRects[i];
if (existingRect.contains(rect))
return;
if (rect.intersects(existingRect) || sharesEdge(rect, existingRect)) {
IntRect combinedRect = unionRect(existingRect, rect);
m_update.paintRects.remove(i);
invalidateRect(combinedRect);
return;
}
}
m_update.paintRects.append(rect);
if (!m_update.scrollRect.isEmpty()) {
if (shouldInvalidateScrollRect(rect))
invalidateScrollRect();
else if (m_update.scrollRect.contains(rect)) {
m_update.paintRects[m_update.paintRects.size() - 1] =
subtractIntersection(rect, m_update.calculateScrollDamage());
if (m_update.paintRects[m_update.paintRects.size() - 1].isEmpty())
m_update.paintRects.remove(m_update.paintRects.size() - 1);
}
}
if (m_update.paintRects.size() > maxPaintRects)
combinePaintRects();
blink::Platform::current()->histogramCustomCounts("MPArch.RW_IntermediatePaintRectCount",
m_update.paintRects.size(), 1, 100, 50);
}
void PaintAggregator::scrollRect(int dx, int dy, const IntRect& clipRect)
{
if (dx && dy) {
invalidateRect(clipRect);
return;
}
if (!m_update.scrollRect.isEmpty() && m_update.scrollRect != clipRect) {
invalidateRect(clipRect);
return;
}
if ((dx && m_update.scrollDelta.y()) || (dy && m_update.scrollDelta.x())) {
invalidateRect(clipRect);
return;
}
m_update.scrollRect = clipRect;
m_update.scrollDelta.move(dx, dy);
if (m_update.scrollDelta == IntPoint()) {
m_update.scrollRect = IntRect();
return;
}
for (size_t i = 0; i < m_update.paintRects.size(); ++i) {
if (m_update.scrollRect.contains(m_update.paintRects[i])) {
m_update.paintRects[i] = scrollPaintRect(m_update.paintRects[i], dx, dy);
if (m_update.paintRects[i].isEmpty()) {
m_update.paintRects.remove(i);
i--;
}
} else if (m_update.scrollRect.intersects(m_update.paintRects[i])) {
invalidateScrollRect();
return;
}
}
if (shouldInvalidateScrollRect(IntRect()))
invalidateScrollRect();
}
IntRect PaintAggregator::scrollPaintRect(const IntRect& paintRect, int dx, int dy) const
{
IntRect result = paintRect;
result.move(dx, dy);
result = intersection(m_update.scrollRect, result);
return subtractIntersection(result, m_update.calculateScrollDamage());
}
bool PaintAggregator::shouldInvalidateScrollRect(const IntRect& rect) const
{
if (!rect.isEmpty()) {
if (!m_update.scrollRect.intersects(rect))
return false;
if (!m_update.scrollRect.contains(rect))
return true;
}
int paintArea = calculateArea(rect);
for (size_t i = 0; i < m_update.paintRects.size(); ++i) {
const IntRect& existingRect = m_update.paintRects[i];
if (m_update.scrollRect.contains(existingRect))
paintArea += calculateArea(existingRect);
}
int scrollArea = calculateArea(m_update.scrollRect);
if (float(paintArea) / float(scrollArea) > maxRedundantPaintToScrollArea)
return true;
return false;
}
void PaintAggregator::invalidateScrollRect()
{
IntRect scrollRect = m_update.scrollRect;
m_update.scrollRect = IntRect();
m_update.scrollDelta = IntPoint();
invalidateRect(scrollRect);
}
void PaintAggregator::combinePaintRects()
{
if (m_update.scrollRect.isEmpty()) {
IntRect bounds = m_update.calculatePaintBounds();
m_update.paintRects.clear();
m_update.paintRects.append(bounds);
} else {
IntRect inner, outer;
for (size_t i = 0; i < m_update.paintRects.size(); ++i) {
const IntRect& existingRect = m_update.paintRects[i];
if (m_update.scrollRect.contains(existingRect))
inner.unite(existingRect);
else
outer.unite(existingRect);
}
m_update.paintRects.clear();
m_update.paintRects.append(inner);
m_update.paintRects.append(outer);
}
}
}