2 Copyright (C) 2010-2012 Nokia Corporation and/or its subsidiary(-ies)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "TiledBackingStore.h"
23 #if USE(TILED_BACKING_STORE)
25 #include "GraphicsContext.h"
26 #include "TiledBackingStoreClient.h"
30 static const int defaultTileDimension = 512;
32 static IntPoint innerBottomRight(const IntRect& rect)
34 // Actually, the rect does not contain rect.maxX(). Refer to IntRect::contain.
35 return IntPoint(rect.maxX() - 1, rect.maxY() - 1);
38 TiledBackingStore::TiledBackingStore(TiledBackingStoreClient* client, std::unique_ptr<TiledBackingStoreBackend> backend)
40 , m_backend(WTF::move(backend))
41 , m_tileSize(defaultTileDimension, defaultTileDimension)
42 , m_coverAreaMultiplier(2.0f)
43 , m_contentsScale(1.f)
44 , m_supportsAlpha(false)
45 , m_pendingTileCreation(false)
49 TiledBackingStore::~TiledBackingStore()
53 void TiledBackingStore::setTileSize(const IntSize& size)
59 void TiledBackingStore::setTrajectoryVector(const FloatPoint& trajectoryVector)
61 m_pendingTrajectoryVector = trajectoryVector;
62 m_pendingTrajectoryVector.normalize();
65 void TiledBackingStore::coverWithTilesIfNeeded()
67 IntRect visibleRect = this->visibleRect();
68 IntRect rect = mapFromContents(m_client->tiledBackingStoreContentsRect());
70 bool didChange = m_trajectoryVector != m_pendingTrajectoryVector || m_visibleRect != visibleRect || m_rect != rect;
71 if (didChange || m_pendingTileCreation)
75 void TiledBackingStore::invalidate(const IntRect& contentsDirtyRect)
77 IntRect dirtyRect(mapFromContents(contentsDirtyRect));
78 IntRect keepRectFitToTileSize = tileRectForCoordinate(tileCoordinateForPoint(m_keepRect.location()));
79 keepRectFitToTileSize.unite(tileRectForCoordinate(tileCoordinateForPoint(innerBottomRight(m_keepRect))));
81 // Only iterate on the part of the rect that we know we might have tiles.
82 IntRect coveredDirtyRect = intersection(dirtyRect, keepRectFitToTileSize);
83 Tile::Coordinate topLeft = tileCoordinateForPoint(coveredDirtyRect.location());
84 Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(coveredDirtyRect));
86 for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
87 for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
88 RefPtr<Tile> currentTile = tileAt(Tile::Coordinate(xCoordinate, yCoordinate));
91 // Pass the full rect to each tile as coveredDirtyRect might not
92 // contain them completely and we don't want partial tile redraws.
93 currentTile->invalidate(dirtyRect);
98 void TiledBackingStore::updateTileBuffers()
100 m_client->tiledBackingStorePaintBegin();
102 Vector<IntRect> paintedArea;
103 Vector<RefPtr<Tile> > dirtyTiles;
104 for (auto& tile : m_tiles.values()) {
105 if (!tile->isDirty())
107 dirtyTiles.append(tile);
110 if (dirtyTiles.isEmpty()) {
111 m_client->tiledBackingStorePaintEnd(paintedArea);
115 // FIXME: In single threaded case, tile back buffers could be updated asynchronously
116 // one by one and then swapped to front in one go. This would minimize the time spent
117 // blocking on tile updates.
118 unsigned size = dirtyTiles.size();
119 for (unsigned n = 0; n < size; ++n) {
120 Vector<IntRect> paintedRects = dirtyTiles[n]->updateBackBuffer();
121 paintedArea.appendVector(paintedRects);
122 dirtyTiles[n]->swapBackBufferToFront();
125 m_client->tiledBackingStorePaintEnd(paintedArea);
128 IntRect TiledBackingStore::visibleRect() const
130 return mapFromContents(m_client->tiledBackingStoreVisibleRect());
133 void TiledBackingStore::setContentsScale(float scale)
135 if (scale == m_contentsScale)
138 m_contentsScale = scale;
140 coverWithTilesIfNeeded();
143 double TiledBackingStore::tileDistance(const IntRect& viewport, const Tile::Coordinate& tileCoordinate) const
145 if (viewport.intersects(tileRectForCoordinate(tileCoordinate)))
148 IntPoint viewCenter = viewport.location() + IntSize(viewport.width() / 2, viewport.height() / 2);
149 Tile::Coordinate centerCoordinate = tileCoordinateForPoint(viewCenter);
151 return std::max(abs(centerCoordinate.y() - tileCoordinate.y()), abs(centerCoordinate.x() - tileCoordinate.x()));
154 // Returns a ratio between 0.0f and 1.0f of the surface of contentsRect covered by rendered tiles.
155 float TiledBackingStore::coverageRatio(const WebCore::IntRect& contentsRect) const
157 IntRect dirtyRect = mapFromContents(contentsRect);
158 float rectArea = dirtyRect.width() * dirtyRect.height();
159 float coverArea = 0.0f;
161 Tile::Coordinate topLeft = tileCoordinateForPoint(dirtyRect.location());
162 Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(dirtyRect));
164 for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
165 for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
166 Tile::Coordinate currentCoordinate(xCoordinate, yCoordinate);
167 RefPtr<Tile> currentTile = tileAt(currentCoordinate);
168 if (currentTile && currentTile->isReadyToPaint()) {
169 IntRect coverRect = intersection(dirtyRect, currentTile->rect());
170 coverArea += coverRect.width() * coverRect.height();
174 return coverArea / rectArea;
177 bool TiledBackingStore::visibleAreaIsCovered() const
179 IntRect boundedVisibleContentsRect = intersection(m_client->tiledBackingStoreVisibleRect(), m_client->tiledBackingStoreContentsRect());
180 return coverageRatio(boundedVisibleContentsRect) == 1.0f;
183 void TiledBackingStore::createTiles()
185 // Update our backing store geometry.
186 const IntRect previousRect = m_rect;
187 m_rect = mapFromContents(m_client->tiledBackingStoreContentsRect());
188 m_trajectoryVector = m_pendingTrajectoryVector;
189 m_visibleRect = visibleRect();
191 if (m_rect.isEmpty()) {
192 setCoverRect(IntRect());
193 setKeepRect(IntRect());
197 /* We must compute cover and keep rects using the visibleRect, instead of the rect intersecting the visibleRect with m_rect,
198 * because TBS can be used as a backing store of GraphicsLayer and the visible rect usually does not intersect with m_rect.
199 * In the below case, the intersecting rect is an empty.
204 * | +-------|-----------------------+
205 * | | HERE | cover or keep |
206 * +---------------+ rect |
213 * +-------------------------------+
215 * We must create or keep the tiles in the HERE region.
220 computeCoverAndKeepRect(m_visibleRect, coverRect, keepRect);
222 setCoverRect(coverRect);
223 setKeepRect(keepRect);
225 if (coverRect.isEmpty())
228 // Resize tiles at the edge in case the contents size has changed, but only do so
229 // after having dropped tiles outside the keep rect.
230 bool didResizeTiles = false;
231 if (previousRect != m_rect)
232 didResizeTiles = resizeEdgeTiles();
234 // Search for the tile position closest to the viewport center that does not yet contain a tile.
235 // Which position is considered the closest depends on the tileDistance function.
236 double shortestDistance = std::numeric_limits<double>::infinity();
237 Vector<Tile::Coordinate> tilesToCreate;
238 unsigned requiredTileCount = 0;
240 // Cover areas (in tiles) with minimum distance from the visible rect. If the visible rect is
241 // not covered already it will be covered first in one go, due to the distance being 0 for tiles
242 // inside the visible rect.
243 Tile::Coordinate topLeft = tileCoordinateForPoint(coverRect.location());
244 Tile::Coordinate bottomRight = tileCoordinateForPoint(innerBottomRight(coverRect));
245 for (int yCoordinate = topLeft.y(); yCoordinate <= bottomRight.y(); ++yCoordinate) {
246 for (int xCoordinate = topLeft.x(); xCoordinate <= bottomRight.x(); ++xCoordinate) {
247 Tile::Coordinate currentCoordinate(xCoordinate, yCoordinate);
248 if (tileAt(currentCoordinate))
251 double distance = tileDistance(m_visibleRect, currentCoordinate);
252 if (distance > shortestDistance)
254 if (distance < shortestDistance) {
255 tilesToCreate.clear();
256 shortestDistance = distance;
258 tilesToCreate.append(currentCoordinate);
262 // Now construct the tile(s) within the shortest distance.
263 unsigned tilesToCreateCount = tilesToCreate.size();
264 for (unsigned n = 0; n < tilesToCreateCount; ++n) {
265 Tile::Coordinate coordinate = tilesToCreate[n];
266 setTile(coordinate, m_backend->createTile(this, coordinate));
268 requiredTileCount -= tilesToCreateCount;
270 // Paint the content of the newly created tiles or resized tiles.
271 if (tilesToCreateCount || didResizeTiles)
274 // Re-call createTiles on a timer to cover the visible area with the newest shortest distance.
275 m_pendingTileCreation = requiredTileCount;
276 if (m_pendingTileCreation)
277 m_client->tiledBackingStoreHasPendingTileCreation();
280 void TiledBackingStore::adjustForContentsRect(IntRect& rect) const
282 IntRect bounds = m_rect;
283 IntSize candidateSize = rect.size();
285 rect.intersect(bounds);
287 if (rect.size() == candidateSize)
291 * In the following case, there is no intersection of the contents rect and the cover rect.
292 * Thus the latter should not be inflated.
298 * +-------------------------------+
304 * +-------------------------------+
309 // Try to create a cover rect of the same size as the candidate, but within content bounds.
310 int pixelsCovered = candidateSize.width() * candidateSize.height();
312 if (rect.width() < candidateSize.width())
313 rect.inflateY(((pixelsCovered / rect.width()) - rect.height()) / 2);
314 if (rect.height() < candidateSize.height())
315 rect.inflateX(((pixelsCovered / rect.height()) - rect.width()) / 2);
317 rect.intersect(bounds);
320 void TiledBackingStore::computeCoverAndKeepRect(const IntRect& visibleRect, IntRect& coverRect, IntRect& keepRect) const
322 coverRect = visibleRect;
323 keepRect = visibleRect;
325 // If we cover more that the actual viewport we can be smart about which tiles we choose to render.
326 if (m_coverAreaMultiplier > 1) {
327 // The initial cover area covers equally in each direction, according to the coverAreaMultiplier.
328 coverRect.inflateX(visibleRect.width() * (m_coverAreaMultiplier - 1) / 2);
329 coverRect.inflateY(visibleRect.height() * (m_coverAreaMultiplier - 1) / 2);
330 keepRect = coverRect;
332 if (m_trajectoryVector != FloatPoint::zero()) {
333 // A null trajectory vector (no motion) means that tiles for the coverArea will be created.
334 // A non-null trajectory vector will shrink the covered rect to visibleRect plus its expansion from its
335 // center toward the cover area edges in the direction of the given vector.
337 // E.g. if visibleRect == (10,10)5x5 and coverAreaMultiplier == 3.0:
338 // a (0,0) trajectory vector will create tiles intersecting (5,5)15x15,
339 // a (1,0) trajectory vector will create tiles intersecting (10,10)10x5,
340 // and a (1,1) trajectory vector will create tiles intersecting (10,10)10x10.
342 // Multiply the vector by the distance to the edge of the cover area.
343 float trajectoryVectorMultiplier = (m_coverAreaMultiplier - 1) / 2;
345 // Unite the visible rect with a "ghost" of the visible rect moved in the direction of the trajectory vector.
346 coverRect = visibleRect;
347 coverRect.move(coverRect.width() * m_trajectoryVector.x() * trajectoryVectorMultiplier,
348 coverRect.height() * m_trajectoryVector.y() * trajectoryVectorMultiplier);
350 coverRect.unite(visibleRect);
352 ASSERT(keepRect.contains(coverRect));
355 adjustForContentsRect(coverRect);
357 // The keep rect is an inflated version of the cover rect, inflated in tile dimensions.
358 keepRect.unite(coverRect);
359 keepRect.inflateX(m_tileSize.width() / 2);
360 keepRect.inflateY(m_tileSize.height() / 2);
361 keepRect.intersect(m_rect);
363 ASSERT(coverRect.isEmpty() || keepRect.contains(coverRect));
366 bool TiledBackingStore::resizeEdgeTiles()
368 bool wasResized = false;
369 Vector<Tile::Coordinate> tilesToRemove;
370 for (auto& tile : m_tiles.values()) {
371 Tile::Coordinate tileCoordinate = tile->coordinate();
372 IntRect tileRect = tile->rect();
373 IntRect expectedTileRect = tileRectForCoordinate(tileCoordinate);
374 if (expectedTileRect.isEmpty())
375 tilesToRemove.append(tileCoordinate);
376 else if (expectedTileRect != tileRect) {
377 tile->resize(expectedTileRect.size());
381 unsigned removeCount = tilesToRemove.size();
382 for (unsigned n = 0; n < removeCount; ++n)
383 removeTile(tilesToRemove[n]);
387 void TiledBackingStore::setKeepRect(const IntRect& keepRect)
389 // Drop tiles outside the new keepRect.
391 FloatRect keepRectF = keepRect;
393 Vector<Tile::Coordinate> toRemove;
394 for (auto& tile : m_tiles.values()) {
395 Tile::Coordinate coordinate = tile->coordinate();
396 FloatRect tileRect = tile->rect();
397 if (!tileRect.intersects(keepRectF))
398 toRemove.append(coordinate);
400 unsigned removeCount = toRemove.size();
401 for (unsigned n = 0; n < removeCount; ++n)
402 removeTile(toRemove[n]);
404 m_keepRect = keepRect;
407 void TiledBackingStore::removeAllNonVisibleTiles()
409 IntRect boundedVisibleRect = mapFromContents(intersection(m_client->tiledBackingStoreVisibleRect(), m_client->tiledBackingStoreContentsRect()));
410 setKeepRect(boundedVisibleRect);
413 PassRefPtr<Tile> TiledBackingStore::tileAt(const Tile::Coordinate& coordinate) const
415 return m_tiles.get(coordinate);
418 void TiledBackingStore::setTile(const Tile::Coordinate& coordinate, PassRefPtr<Tile> tile)
420 m_tiles.set(coordinate, tile);
423 void TiledBackingStore::removeTile(const Tile::Coordinate& coordinate)
425 m_tiles.remove(coordinate);
428 IntRect TiledBackingStore::mapToContents(const IntRect& rect) const
430 return enclosingIntRect(FloatRect(rect.x() / m_contentsScale,
431 rect.y() / m_contentsScale,
432 rect.width() / m_contentsScale,
433 rect.height() / m_contentsScale));
436 IntRect TiledBackingStore::mapFromContents(const IntRect& rect) const
438 return enclosingIntRect(FloatRect(rect.x() * m_contentsScale,
439 rect.y() * m_contentsScale,
440 rect.width() * m_contentsScale,
441 rect.height() * m_contentsScale));
444 IntRect TiledBackingStore::tileRectForCoordinate(const Tile::Coordinate& coordinate) const
446 IntRect rect(coordinate.x() * m_tileSize.width(),
447 coordinate.y() * m_tileSize.height(),
449 m_tileSize.height());
451 rect.intersect(m_rect);
455 Tile::Coordinate TiledBackingStore::tileCoordinateForPoint(const IntPoint& point) const
457 int x = point.x() / m_tileSize.width();
458 int y = point.y() / m_tileSize.height();
459 return Tile::Coordinate(std::max(x, 0), std::max(y, 0));
462 void TiledBackingStore::setSupportsAlpha(bool a)
464 if (a == m_supportsAlpha)