Commit message: None
CVE: None
Vulnerability description: None

Project: Chrome

LayerTilerChromium::LayerTilerChromium(LayerRendererChromium* layerRenderer, const IntSize& tileSize)
    : m_skipsDraw(false)
    , m_layerRenderer(layerRenderer)
{
    setTileSize(tileSize);
}

Project: Chrome

IntRect LayerTilerChromium::contentRectToLayerRect(const IntRect& contentRect) const
{
    IntPoint pos(contentRect.x() - m_layerPosition.x(), contentRect.y() - m_layerPosition.y());
    IntRect layerRect(pos, contentRect.size());

    if (pos.x() < 0 || pos.y() < 0)
        layerRect = IntRect(IntPoint(0, 0), IntSize(contentRect.width() + pos.x(), contentRect.height() + pos.y()));
    return layerRect;
}

Project: Chrome

void LayerTilerChromium::contentRectToTileIndices(const IntRect& contentRect, int &left, int &top, int &right, int &bottom) const
{
    const IntRect layerRect = contentRectToLayerRect(contentRect);

    left = layerRect.x() / m_tileSize.width();
    top = layerRect.y() / m_tileSize.height();
    right = (layerRect.maxX() - 1) / m_tileSize.width();
    bottom = (layerRect.maxY() - 1) / m_tileSize.height();
}

Project: Chrome

PassOwnPtr<LayerTilerChromium> LayerTilerChromium::create(LayerRendererChromium* layerRenderer, const IntSize& tileSize)
{
    if (!layerRenderer || tileSize.isEmpty())
        return 0;

    return adoptPtr(new LayerTilerChromium(layerRenderer, tileSize));
}

Project: Chrome

LayerTilerChromium::Tile* LayerTilerChromium::createTile(int i, int j)
{
    const int index = tileIndex(i, j);
    ASSERT(!m_tiles[index]);

    if (m_unusedTiles.size() > 0) {
        m_tiles[index] = m_unusedTiles.last().release();
        m_unusedTiles.removeLast();
    } else {
        GraphicsContext3D* context = layerRendererContext();
        TextureManager* manager = layerRenderer()->textureManager();
        OwnPtr<Tile> tile = adoptPtr(new Tile(LayerTexture::create(context, manager)));
        m_tiles[index] = tile.release();
    }

    m_tiles[index]->m_dirtyLayerRect = tileLayerRect(i, j);
    return m_tiles[index].get();
}

Project: Chrome

void LayerTilerChromium::draw(const IntRect& contentRect)
{
    if (m_skipsDraw || !m_tiles.size())
        return;

    GraphicsContext3D* context = layerRendererContext();
    const ContentLayerChromium::SharedValues* contentLayerValues = layerRenderer()->contentLayerSharedValues();
    layerRenderer()->useShader(contentLayerValues->contentShaderProgram());
    GLC(context, context->uniform1i(contentLayerValues->shaderSamplerLocation(), 0));

    int left, top, right, bottom;
    contentRectToTileIndices(contentRect, left, top, right, bottom);
    for (int j = top; j <= bottom; ++j) {
        for (int i = left; i <= right; ++i) {
            Tile* tile = m_tiles[tileIndex(i, j)].get();
            ASSERT(tile);

            tile->texture()->bindTexture();

            TransformationMatrix tileMatrix;
            IntRect tileRect = tileContentRect(i, j);
            tileMatrix.translate3d(tileRect.x() - contentRect.x() + tileRect.width() / 2.0, tileRect.y() - contentRect.y() + tileRect.height() / 2.0, 0);

            LayerChromium::drawTexturedQuad(context, layerRenderer()->projectionMatrix(), tileMatrix, m_tileSize.width(), m_tileSize.height(), 1, contentLayerValues->shaderMatrixLocation(), contentLayerValues->shaderAlphaLocation());

            tile->texture()->unreserve();
        }
    }
}

Project: Chrome

void LayerTilerChromium::growLayerToContain(const IntRect& contentRect)
{
    IntRect layerRect = contentRectToLayerRect(contentRect);
    IntSize layerSize = IntSize(layerRect.maxX(), layerRect.maxY());

    IntSize newSize = layerSize.expandedTo(m_layerSize);
    resizeLayer(newSize);
}

Project: Chrome

void LayerTilerChromium::invalidateEntireLayer()
{
    for (size_t i = 0; i < m_tiles.size(); ++i) {
        if (m_tiles[i])
            m_unusedTiles.append(m_tiles[i].release());
    }
    m_tiles.clear();

    m_layerSize = IntSize();
    m_layerTileSize = IntSize();
    m_lastUpdateLayerRect = IntRect();
}

Project: Chrome

void LayerTilerChromium::invalidateTiles(const IntRect& oldLayerRect, const IntRect& newLayerRect)
{
    if (!m_tiles.size())
        return;

    IntRect oldContentRect = layerRectToContentRect(oldLayerRect);
    int oldLeft, oldTop, oldRight, oldBottom;
    contentRectToTileIndices(oldContentRect, oldLeft, oldTop, oldRight, oldBottom);

    IntRect newContentRect = layerRectToContentRect(newLayerRect);
    int newLeft, newTop, newRight, newBottom;
    contentRectToTileIndices(newContentRect, newLeft, newTop, newRight, newBottom);

    for (int j = oldTop; j <= oldBottom; ++j) {
        for (int i = oldLeft; i <= oldRight; ++i) {
            if (i >= newLeft && i <= newRight && j >= newTop && j <= newBottom)
                continue;

            const int index = tileIndex(i, j);
            if (m_tiles[index])
                m_unusedTiles.append(m_tiles[index].release());
        }
    }
}

Project: Chrome

GraphicsContext3D* LayerTilerChromium::layerRendererContext() const
{
    ASSERT(layerRenderer());
    return layerRenderer()->context();
}

Project: Chrome

void LayerTilerChromium::setTileSize(const IntSize& size)
{
    if (m_tileSize == size)
        return;

    reset();

    m_tileSize = size;
    m_tilePixels = adoptArrayPtr(new uint8_t[m_tileSize.width() * m_tileSize.height() * 4]);
}

Project: Chrome

int LayerTilerChromium::tileIndex(int i, int j) const
{
    ASSERT(i >= 0 && j >= 0 && i < m_layerTileSize.width() && j < m_layerTileSize.height());
    return i + j * m_layerTileSize.width();
}

Project: Chrome

IntRect LayerTilerChromium::tileLayerRect(int i, int j) const
{
    IntPoint anchor(i * m_tileSize.width(), j * m_tileSize.height());
    IntRect tile(anchor, m_tileSize);
    return tile;
}

Project: Chrome

LayerTilerChromium::~LayerTilerChromium()
{
    reset();
}