TiledBackingStore.cpp [plain text]
#include "config.h"
#include "TiledBackingStore.h"
#if USE(COORDINATED_GRAPHICS)
#include "GraphicsContext.h"
#include "TiledBackingStoreClient.h"
#include <wtf/CheckedArithmetic.h>
namespace WebCore {
static const int defaultTileDimension = 512;
static IntPoint innerBottomRight(const IntRect& rect)
{
return IntPoint(rect.maxX() - 1, rect.maxY() - 1);
}
TiledBackingStore::TiledBackingStore(TiledBackingStoreClient* client, float contentsScale)
: m_client(client)
, m_tileSize(defaultTileDimension, defaultTileDimension)
, m_coverAreaMultiplier(2.0f)
, m_contentsScale(contentsScale)
, m_supportsAlpha(false)
, m_pendingTileCreation(false)
{
}
TiledBackingStore::~TiledBackingStore()
{
}
void TiledBackingStore::setTrajectoryVector(const FloatPoint& trajectoryVector)
{
m_pendingTrajectoryVector = trajectoryVector;
m_pendingTrajectoryVector.normalize();
}
void TiledBackingStore::createTilesIfNeeded(const IntRect& unscaledVisibleRect, const IntRect& contentsRect)
{
IntRect scaledContentsRect = mapFromContents(contentsRect);
IntRect visibleRect = mapFromContents(unscaledVisibleRect);
bool didChange = m_trajectoryVector != m_pendingTrajectoryVector || m_visibleRect != visibleRect || m_rect != scaledContentsRect;
if (didChange || m_pendingTileCreation)
createTiles(visibleRect, scaledContentsRect);
}
void TiledBackingStore::invalidate(const IntRect& contentsDirtyRect)
{
IntRect dirtyRect(mapFromContents(contentsDirtyRect));
IntRect keepRectFitToTileSize = tileRectForCoordinate(tileCoordinateForPoint(m_keepRect.location()));
keepRectFitToTileSize.unite(tileRectForCoordinate(tileCoordinateForPoint(innerBottomRight(m_keepRect))));
IntRect coveredDirtyRect = intersection(dirtyRect, keepRectFitToTileSize);
Tile::Coordinate topLeft = tileCoordinateForPoint(coveredDirtyRect.location());
Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(coveredDirtyRect));
for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
Tile* currentTile = m_tiles.get(Tile::Coordinate(xCoordinate, yCoordinate));
if (!currentTile)
continue;
currentTile->invalidate(dirtyRect);
}
}
}
void TiledBackingStore::updateTileBuffers()
{
bool updated = false;
for (auto& tile : m_tiles.values()) {
if (!tile->isDirty())
continue;
updated |= tile->updateBackBuffer();
}
if (updated)
m_client->didUpdateTileBuffers();
}
double TiledBackingStore::tileDistance(const IntRect& viewport, const Tile::Coordinate& tileCoordinate) const
{
if (viewport.intersects(tileRectForCoordinate(tileCoordinate)))
return 0;
IntPoint viewCenter = viewport.location() + IntSize(viewport.width() / 2, viewport.height() / 2);
Tile::Coordinate centerCoordinate = tileCoordinateForPoint(viewCenter);
return std::max(abs(centerCoordinate.y() - tileCoordinate.y()), abs(centerCoordinate.x() - tileCoordinate.x()));
}
float TiledBackingStore::coverageRatio(const WebCore::IntRect& dirtyRect) const
{
float rectArea = dirtyRect.width() * dirtyRect.height();
float coverArea = 0.0f;
Tile::Coordinate topLeft = tileCoordinateForPoint(dirtyRect.location());
Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(dirtyRect));
for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
Tile::Coordinate currentCoordinate(xCoordinate, yCoordinate);
Tile* currentTile = m_tiles.get(currentCoordinate);
if (currentTile && currentTile->isReadyToPaint()) {
IntRect coverRect = intersection(dirtyRect, currentTile->rect());
coverArea += coverRect.width() * coverRect.height();
}
}
}
return coverArea / rectArea;
}
bool TiledBackingStore::visibleAreaIsCovered() const
{
return coverageRatio(intersection(m_visibleRect, m_rect)) == 1.0f;
}
void TiledBackingStore::createTiles(const IntRect& visibleRect, const IntRect& scaledContentsRect)
{
const IntRect previousRect = m_rect;
m_rect = scaledContentsRect;
m_trajectoryVector = m_pendingTrajectoryVector;
m_visibleRect = visibleRect;
if (m_rect.isEmpty()) {
setCoverRect(IntRect());
setKeepRect(IntRect());
return;
}
IntRect coverRect;
IntRect keepRect;
computeCoverAndKeepRect(m_visibleRect, coverRect, keepRect);
setCoverRect(coverRect);
setKeepRect(keepRect);
if (coverRect.isEmpty())
return;
bool didResizeTiles = false;
if (previousRect != m_rect)
didResizeTiles = resizeEdgeTiles();
double shortestDistance = std::numeric_limits<double>::infinity();
Vector<Tile::Coordinate> tilesToCreate;
unsigned requiredTileCount = 0;
Tile::Coordinate topLeft = tileCoordinateForPoint(coverRect.location());
Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(coverRect));
for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
Tile::Coordinate currentCoordinate(xCoordinate, yCoordinate);
if (m_tiles.contains(currentCoordinate))
continue;
++requiredTileCount;
double distance = tileDistance(m_visibleRect, currentCoordinate);
if (distance > shortestDistance)
continue;
if (distance < shortestDistance) {
tilesToCreate.clear();
shortestDistance = distance;
}
tilesToCreate.append(currentCoordinate);
}
}
unsigned tilesToCreateCount = tilesToCreate.size();
for (unsigned n = 0; n < tilesToCreateCount; ++n) {
Tile::Coordinate coordinate = tilesToCreate[n];
m_tiles.add(coordinate, std::make_unique<Tile>(*this, coordinate));
}
requiredTileCount -= tilesToCreateCount;
if (tilesToCreateCount || didResizeTiles)
updateTileBuffers();
m_pendingTileCreation = requiredTileCount;
if (m_pendingTileCreation)
m_client->tiledBackingStoreHasPendingTileCreation();
}
void TiledBackingStore::adjustForContentsRect(IntRect& rect) const
{
IntRect bounds = m_rect;
IntSize candidateSize = rect.size();
rect.intersect(bounds);
if (rect.size() == candidateSize)
return;
if (rect.isEmpty())
return;
int pixelsCovered = 0;
if (!WTF::safeMultiply(candidateSize.width(), candidateSize.height(), pixelsCovered))
pixelsCovered = std::numeric_limits<int>::max();
if (rect.width() < candidateSize.width())
rect.inflateY(((pixelsCovered / rect.width()) - rect.height()) / 2);
if (rect.height() < candidateSize.height())
rect.inflateX(((pixelsCovered / rect.height()) - rect.width()) / 2);
rect.intersect(bounds);
}
void TiledBackingStore::computeCoverAndKeepRect(const IntRect& visibleRect, IntRect& coverRect, IntRect& keepRect) const
{
coverRect = visibleRect;
keepRect = visibleRect;
if (m_coverAreaMultiplier > 1) {
coverRect.inflateX(visibleRect.width() * (m_coverAreaMultiplier - 1) / 2);
coverRect.inflateY(visibleRect.height() * (m_coverAreaMultiplier - 1) / 2);
keepRect = coverRect;
if (m_trajectoryVector != FloatPoint::zero()) {
float trajectoryVectorMultiplier = (m_coverAreaMultiplier - 1) / 2;
coverRect = visibleRect;
coverRect.move(coverRect.width() * m_trajectoryVector.x() * trajectoryVectorMultiplier, coverRect.height() * m_trajectoryVector.y() * trajectoryVectorMultiplier);
coverRect.unite(visibleRect);
}
ASSERT(keepRect.contains(coverRect));
}
adjustForContentsRect(coverRect);
keepRect.unite(coverRect);
keepRect.inflateX(m_tileSize.width() / 2);
keepRect.inflateY(m_tileSize.height() / 2);
keepRect.intersect(m_rect);
ASSERT(coverRect.isEmpty() || keepRect.contains(coverRect));
}
bool TiledBackingStore::resizeEdgeTiles()
{
bool wasResized = false;
Vector<Tile::Coordinate> tilesToRemove;
for (auto& tile : m_tiles.values()) {
Tile::Coordinate tileCoordinate = tile->coordinate();
IntRect tileRect = tile->rect();
IntRect expectedTileRect = tileRectForCoordinate(tileCoordinate);
if (expectedTileRect.isEmpty())
tilesToRemove.append(tileCoordinate);
else if (expectedTileRect != tileRect) {
tile->resize(expectedTileRect.size());
wasResized = true;
}
}
for (auto& coordinateToRemove : tilesToRemove)
m_tiles.remove(coordinateToRemove);
return wasResized;
}
void TiledBackingStore::setKeepRect(const IntRect& keepRect)
{
FloatRect keepRectF = keepRect;
Vector<Tile::Coordinate> toRemove;
for (auto& tile : m_tiles.values()) {
Tile::Coordinate coordinate = tile->coordinate();
FloatRect tileRect = tile->rect();
if (!tileRect.intersects(keepRectF))
toRemove.append(coordinate);
}
for (auto& coordinateToRemove : toRemove)
m_tiles.remove(coordinateToRemove);
m_keepRect = keepRect;
}
void TiledBackingStore::removeAllNonVisibleTiles(const IntRect& unscaledVisibleRect, const IntRect& contentsRect)
{
IntRect boundedVisibleRect = mapFromContents(intersection(unscaledVisibleRect, contentsRect));
setKeepRect(boundedVisibleRect);
}
IntRect TiledBackingStore::mapToContents(const IntRect& rect) const
{
return enclosingIntRect(FloatRect(rect.x() / m_contentsScale,
rect.y() / m_contentsScale,
rect.width() / m_contentsScale,
rect.height() / m_contentsScale));
}
IntRect TiledBackingStore::mapFromContents(const IntRect& rect) const
{
return enclosingIntRect(FloatRect(rect.x() * m_contentsScale,
rect.y() * m_contentsScale,
rect.width() * m_contentsScale,
rect.height() * m_contentsScale));
}
IntRect TiledBackingStore::tileRectForCoordinate(const Tile::Coordinate& coordinate) const
{
IntRect rect(coordinate.x() * m_tileSize.width(),
coordinate.y() * m_tileSize.height(),
m_tileSize.width(),
m_tileSize.height());
rect.intersect(m_rect);
return rect;
}
Tile::Coordinate TiledBackingStore::tileCoordinateForPoint(const IntPoint& point) const
{
int x = point.x() / m_tileSize.width();
int y = point.y() / m_tileSize.height();
return Tile::Coordinate(std::max(x, 0), std::max(y, 0));
}
void TiledBackingStore::setSupportsAlpha(bool a)
{
if (a == m_supportsAlpha)
return;
m_supportsAlpha = a;
invalidate(m_rect);
}
}
#endif