TextureMapperBackingStore.cpp [plain text]
#include "config.h"
#include "TextureMapperBackingStore.h"
#include "GraphicsLayer.h"
#include "ImageBuffer.h"
#include "TextureMapper.h"
namespace WebCore {
void TextureMapperTile::updateContents(TextureMapper* textureMapper, Image* image, const IntRect& dirtyRect)
{
IntRect targetRect = enclosingIntRect(m_rect);
targetRect.intersect(dirtyRect);
if (targetRect.isEmpty())
return;
IntPoint sourceOffset = targetRect.location();
sourceOffset.move(-dirtyRect.x(), -dirtyRect.y());
targetRect.move(-m_rect.x(), -m_rect.y());
if (!m_texture) {
m_texture = textureMapper->createTexture();
m_texture->reset(targetRect.size(), image->currentFrameHasAlpha() ? BitmapTexture::SupportsAlpha : 0);
}
m_texture->updateContents(image, targetRect, sourceOffset);
}
void TextureMapperTile::paint(TextureMapper* textureMapper, const TransformationMatrix& transform, float opacity, BitmapTexture* mask)
{
textureMapper->drawTexture(*texture().get(), rect(), transform, opacity, mask);
}
void TextureMapperTiledBackingStore::updateContentsFromImageIfNeeded(TextureMapper* textureMapper)
{
if (!m_image)
return;
updateContents(textureMapper, m_image.get());
m_image.clear();
}
void TextureMapperTiledBackingStore::paintToTextureMapper(TextureMapper* textureMapper, const FloatRect& targetRect, const TransformationMatrix& transform, float opacity, BitmapTexture* mask)
{
updateContentsFromImageIfNeeded(textureMapper);
TransformationMatrix adjustedTransform = transform;
adjustedTransform.multiply(TransformationMatrix::rectToRect(rect(), targetRect));
for (size_t i = 0; i < m_tiles.size(); ++i)
m_tiles[i].paint(textureMapper, adjustedTransform, opacity, mask);
}
void TextureMapperTiledBackingStore::createOrDestroyTilesIfNeeded(const FloatSize& size, const IntSize& tileSize, bool hasAlpha)
{
if (size == m_size)
return;
m_size = size;
Vector<FloatRect> tileRectsToAdd;
Vector<int> tileIndicesToRemove;
static const size_t TileEraseThreshold = 6;
for (float y = 0; y < m_size.height(); y += tileSize.height()) {
for (float x = 0; x < m_size.width(); x += tileSize.width()) {
FloatRect tileRect(x, y, tileSize.width(), tileSize.height());
tileRect.intersect(rect());
tileRectsToAdd.append(tileRect);
}
}
for (int i = m_tiles.size() - 1; i >= 0; --i) {
FloatRect oldTile = m_tiles[i].rect();
bool existsAlready = false;
for (int j = tileRectsToAdd.size() - 1; j >= 0; --j) {
FloatRect newTile = tileRectsToAdd[j];
if (oldTile != newTile)
continue;
existsAlready = true;
tileRectsToAdd.remove(j);
break;
}
if (!existsAlready)
tileIndicesToRemove.append(i);
}
for (size_t i = 0; i < tileRectsToAdd.size(); ++i) {
if (!tileIndicesToRemove.isEmpty()) {
TextureMapperTile& tile = m_tiles[tileIndicesToRemove.last()];
tileIndicesToRemove.removeLast();
tile.setRect(tileRectsToAdd[i]);
if (tile.texture())
tile.texture()->reset(enclosingIntRect(tile.rect()).size(), hasAlpha ? BitmapTexture::SupportsAlpha : 0);
continue;
}
m_tiles.append(TextureMapperTile(tileRectsToAdd[i]));
}
for (size_t i = 0; i < tileIndicesToRemove.size() && m_tiles.size() > TileEraseThreshold; ++i)
m_tiles.remove(tileIndicesToRemove[i]);
}
void TextureMapperTiledBackingStore::updateContents(TextureMapper* textureMapper, Image* image, const FloatSize& totalSize, const IntRect& dirtyRect)
{
createOrDestroyTilesIfNeeded(totalSize, textureMapper->maxTextureSize(), image->currentFrameHasAlpha());
for (size_t i = 0; i < m_tiles.size(); ++i)
m_tiles[i].updateContents(textureMapper, image, dirtyRect);
}
PassRefPtr<BitmapTexture> TextureMapperTiledBackingStore::texture() const
{
for (size_t i = 0; i < m_tiles.size(); ++i) {
RefPtr<BitmapTexture> texture = m_tiles[i].texture();
if (texture)
return texture;
}
return PassRefPtr<BitmapTexture>();
}
}