TextureMapperTiledBackingStore.cpp [plain text]
#include "config.h"
#include "TextureMapperTiledBackingStore.h"
#include "ImageBuffer.h"
#include "ImageObserver.h"
#include "TextureMapper.h"
namespace WebCore {
class GraphicsLayer;
void TextureMapperTiledBackingStore::updateContentsFromImageIfNeeded(TextureMapper& textureMapper)
{
if (!m_image)
return;
updateContents(textureMapper, m_image.get(), m_image->size(), enclosingIntRect(m_image->rect()), BitmapTexture::UpdateCannotModifyOriginalImageData);
if (m_image->imageObserver())
m_image->imageObserver()->didDraw(m_image.get());
m_image = nullptr;
}
TransformationMatrix TextureMapperTiledBackingStore::adjustedTransformForRect(const FloatRect& targetRect)
{
return TransformationMatrix::rectToRect(rect(), targetRect);
}
void TextureMapperTiledBackingStore::paintToTextureMapper(TextureMapper& textureMapper, const FloatRect& targetRect, const TransformationMatrix& transform, float opacity)
{
updateContentsFromImageIfNeeded(textureMapper);
TransformationMatrix adjustedTransform = transform * adjustedTransformForRect(targetRect);
for (auto& tile : m_tiles)
tile.paint(textureMapper, adjustedTransform, opacity, calculateExposedTileEdges(rect(), tile.rect()));
}
void TextureMapperTiledBackingStore::drawBorder(TextureMapper& textureMapper, const Color& borderColor, float borderWidth, const FloatRect& targetRect, const TransformationMatrix& transform)
{
TransformationMatrix adjustedTransform = transform * adjustedTransformForRect(targetRect);
for (auto& tile : m_tiles)
textureMapper.drawBorder(borderColor, borderWidth, tile.rect(), adjustedTransform);
}
void TextureMapperTiledBackingStore::drawRepaintCounter(TextureMapper& textureMapper, int repaintCount, const Color& borderColor, const FloatRect& targetRect, const TransformationMatrix& transform)
{
TransformationMatrix adjustedTransform = transform * adjustedTransformForRect(targetRect);
for (auto& tile : m_tiles)
textureMapper.drawNumber(repaintCount, borderColor, tile.rect().location(), adjustedTransform);
}
void TextureMapperTiledBackingStore::updateContentsScale(float scale)
{
if (m_contentsScale == scale)
return;
m_isScaleDirty = true;
m_contentsScale = scale;
}
void TextureMapperTiledBackingStore::createOrDestroyTilesIfNeeded(const FloatSize& size, const IntSize& tileSize, bool hasAlpha)
{
if (size == m_size && !m_isScaleDirty)
return;
m_size = size;
m_isScaleDirty = false;
FloatSize scaledSize(m_size);
if (!m_image)
scaledSize.scale(m_contentsScale);
Vector<FloatRect> tileRectsToAdd;
Vector<int> tileIndicesToRemove;
static const size_t TileEraseThreshold = 6;
for (float y = 0; y < scaledSize.height(); y += tileSize.height()) {
for (float x = 0; x < scaledSize.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 (auto& rect : tileRectsToAdd) {
if (!tileIndicesToRemove.isEmpty()) {
TextureMapperTile& tile = m_tiles[tileIndicesToRemove.last()];
tileIndicesToRemove.removeLast();
tile.setRect(rect);
if (tile.texture())
tile.texture()->reset(enclosingIntRect(tile.rect()).size(), hasAlpha ? BitmapTexture::SupportsAlpha : 0);
continue;
}
m_tiles.append(TextureMapperTile(rect));
}
for (auto& index : tileIndicesToRemove) {
if (m_tiles.size() <= TileEraseThreshold)
break;
m_tiles.remove(index);
}
}
void TextureMapperTiledBackingStore::updateContents(TextureMapper& textureMapper, Image* image, const FloatSize& totalSize, const IntRect& dirtyRect, BitmapTexture::UpdateContentsFlag updateContentsFlag)
{
createOrDestroyTilesIfNeeded(totalSize, textureMapper.maxTextureSize(), !image->currentFrameKnownToBeOpaque());
for (auto& tile : m_tiles)
tile.updateContents(textureMapper, image, dirtyRect, updateContentsFlag);
}
void TextureMapperTiledBackingStore::updateContents(TextureMapper& textureMapper, GraphicsLayer* sourceLayer, const FloatSize& totalSize, const IntRect& dirtyRect, BitmapTexture::UpdateContentsFlag updateContentsFlag)
{
createOrDestroyTilesIfNeeded(totalSize, textureMapper.maxTextureSize(), true);
for (auto& tile : m_tiles)
tile.updateContents(textureMapper, sourceLayer, dirtyRect, updateContentsFlag, m_contentsScale);
}
RefPtr<BitmapTexture> TextureMapperTiledBackingStore::texture() const
{
for (const auto& tile : m_tiles) {
if (auto texture = tile.texture())
return texture;
}
return nullptr;
}
}