Changeset 208973 in webkit


Ignore:
Timestamp:
Nov 24, 2016 7:08:11 AM (7 years ago)
Author:
svillar@igalia.com
Message:

[css-grid] Convert grid representation into a class
https://bugs.webkit.org/show_bug.cgi?id=165042

Reviewed by Manuel Rego Casasnovas.

So far grids are represented as Vectors of Vectors. There are a couple of issues associated
to that decision. First or all, the source code in RenderGrid assumes the existence of that
data structure, meaning that we cannot eventually change it without changing a lot of
code. Apart from the coupling there is another issue, RenderGrid is full of methods to
access and manipulate that data structure.

Instead, it'd be much better to have a Grid class encapsulating both the data structures and
the methods required to access/manipulate it. Note that follow-up patches will move even
more data and procedures into this new class from the RenderGrid code.

No new tests required as this is a refactoring.

  • rendering/RenderGrid.cpp:

(WebCore::RenderGrid::Grid::ensureGridSize): Moved from RenderGrid.
(WebCore::RenderGrid::Grid::insert): Ditto.
(WebCore::RenderGrid::Grid::clear): Ditto.
(WebCore::RenderGrid::GridIterator::GridIterator):
(WebCore::RenderGrid::gridColumnCount): Use Grid's methods.
(WebCore::RenderGrid::gridRowCount): Ditto.
(WebCore::RenderGrid::placeItemsOnGrid): Use Grid's methods to insert children.
(WebCore::RenderGrid::populateExplicitGridAndOrderIterator): Ditto.
(WebCore::RenderGrid::placeSpecifiedMajorAxisItemsOnGrid): Ditto.
(WebCore::RenderGrid::placeAutoMajorAxisItemOnGrid): Ditto.
(WebCore::RenderGrid::numTracks): Use Grid's methods.
(WebCore::RenderGrid::ensureGridSize): Deleted. Moved to Grid class.
(WebCore::RenderGrid::insertItemIntoGrid): Deleted. Moved to Grid class.

  • rendering/RenderGrid.h:
Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r208971 r208973  
     12016-11-23  Sergio Villar Senin  <svillar@igalia.com>
     2
     3        [css-grid] Convert grid representation into a class
     4        https://bugs.webkit.org/show_bug.cgi?id=165042
     5
     6        Reviewed by Manuel Rego Casasnovas.
     7
     8        So far grids are represented as Vectors of Vectors. There are a couple of issues associated
     9        to that decision. First or all, the source code in RenderGrid assumes the existence of that
     10        data structure, meaning that we cannot eventually change it without changing a lot of
     11        code. Apart from the coupling there is another issue, RenderGrid is full of methods to
     12        access and manipulate that data structure.
     13
     14        Instead, it'd be much better to have a Grid class encapsulating both the data structures and
     15        the methods required to access/manipulate it. Note that follow-up patches will move even
     16        more data and procedures into this new class from the RenderGrid code.
     17
     18        No new tests required as this is a refactoring.
     19
     20        * rendering/RenderGrid.cpp:
     21        (WebCore::RenderGrid::Grid::ensureGridSize): Moved from RenderGrid.
     22        (WebCore::RenderGrid::Grid::insert): Ditto.
     23        (WebCore::RenderGrid::Grid::clear): Ditto.
     24        (WebCore::RenderGrid::GridIterator::GridIterator):
     25        (WebCore::RenderGrid::gridColumnCount): Use Grid's methods.
     26        (WebCore::RenderGrid::gridRowCount): Ditto.
     27        (WebCore::RenderGrid::placeItemsOnGrid): Use Grid's methods to insert children.
     28        (WebCore::RenderGrid::populateExplicitGridAndOrderIterator): Ditto.
     29        (WebCore::RenderGrid::placeSpecifiedMajorAxisItemsOnGrid): Ditto.
     30        (WebCore::RenderGrid::placeAutoMajorAxisItemOnGrid): Ditto.
     31        (WebCore::RenderGrid::numTracks): Use Grid's methods.
     32        (WebCore::RenderGrid::ensureGridSize): Deleted. Moved to Grid class.
     33        (WebCore::RenderGrid::insertItemIntoGrid): Deleted. Moved to Grid class.
     34        * rendering/RenderGrid.h:
     35
    1362016-11-24  Antti Koivisto  <antti@apple.com>
    237
  • trunk/Source/WebCore/rendering/RenderGrid.cpp

    r208962 r208973  
    4747};
    4848
     49void RenderGrid::Grid::ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize)
     50{
     51    const size_t oldColumnSize = numColumns();
     52    const size_t oldRowSize = numRows();
     53    if (maximumRowSize > oldRowSize) {
     54        m_grid.grow(maximumRowSize);
     55        for (size_t row = oldRowSize; row < maximumRowSize; ++row)
     56            m_grid[row].grow(oldColumnSize);
     57    }
     58
     59    if (maximumColumnSize > oldColumnSize) {
     60        for (size_t row = 0; row < numRows(); ++row)
     61            m_grid[row].grow(maximumColumnSize);
     62    }
     63}
     64
     65void RenderGrid::Grid::insert(RenderBox& child, const GridArea& area)
     66{
     67    ASSERT(area.rows.isTranslatedDefinite() && area.columns.isTranslatedDefinite());
     68    ensureGridSize(area.rows.endLine(), area.columns.endLine());
     69
     70    for (const auto& row : area.rows) {
     71        for (const auto& column : area.columns)
     72            m_grid[row][column].append(&child);
     73    }
     74}
     75
     76void RenderGrid::Grid::clear()
     77{
     78    m_grid.resize(0);
     79}
     80
    4981class GridTrack {
    5082public:
     
    150182    // |direction| is the direction that is fixed to |fixedTrackIndex| so e.g
    151183    // GridIterator(m_grid, ForColumns, 1) will walk over the rows of the 2nd column.
    152     GridIterator(const Vector<Vector<Vector<RenderBox*, 1>>>& grid, GridTrackSizingDirection direction, unsigned fixedTrackIndex, unsigned varyingTrackIndex = 0)
    153         : m_grid(grid)
     184    GridIterator(const Grid& grid, GridTrackSizingDirection direction, unsigned fixedTrackIndex, unsigned varyingTrackIndex = 0)
     185        : m_grid(grid.m_grid)
    154186        , m_direction(direction)
    155187        , m_rowIndex((direction == ForColumns) ? varyingTrackIndex : fixedTrackIndex)
     
    228260
    229261private:
    230     const Vector<Vector<Vector<RenderBox*, 1>>>& m_grid;
     262    const GridAsMatrix& m_grid;
    231263    GridTrackSizingDirection m_direction;
    232264    unsigned m_rowIndex;
     
    388420{
    389421    ASSERT(!m_gridIsDirty);
    390     return m_grid.size() ? m_grid[0].size() : 0;
     422    return m_grid.numColumns();
    391423}
    392424
     
    394426{
    395427    ASSERT(!m_gridIsDirty);
    396     return m_grid.size();
     428    return m_grid.numRows();
    397429}
    398430
     
    15111543#endif
    15121544
    1513 void RenderGrid::ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize)
    1514 {
    1515     const unsigned oldRowCount = gridRowCount();
    1516     if (maximumRowSize > oldRowCount) {
    1517         m_grid.grow(maximumRowSize);
    1518         for (unsigned row = oldRowCount; row < gridRowCount(); ++row)
    1519             m_grid[row].grow(gridColumnCount());
    1520     }
    1521 
    1522     if (maximumColumnSize > gridColumnCount()) {
    1523         for (unsigned row = 0; row < gridRowCount(); ++row)
    1524             m_grid[row].grow(maximumColumnSize);
    1525     }
    1526 }
    1527 
    1528 void RenderGrid::insertItemIntoGrid(RenderBox& child, const GridArea& area)
    1529 {
    1530     ASSERT(area.rows.isTranslatedDefinite() && area.columns.isTranslatedDefinite());
    1531     ensureGridSize(area.rows.endLine(), area.columns.endLine());
    1532 
    1533     for (auto row : area.rows) {
    1534         for (auto column : area.columns)
    1535             m_grid[row][column].append(&child);
    1536     }
    1537 }
    1538 
    15391545unsigned RenderGrid::computeAutoRepeatTracksCount(GridTrackSizingDirection direction, SizingOperation sizingOperation) const
    15401546{
     
    16801686            continue;
    16811687        }
    1682         insertItemIntoGrid(*child, GridArea(area.rows, area.columns));
     1688        m_grid.insert(*child, { area.rows, area.columns });
    16831689    }
    16841690
     
    17441750    }
    17451751
    1746     m_grid.grow(maximumRowIndex + std::abs(m_smallestRowStart));
    1747     for (auto& column : m_grid)
    1748         column.grow(maximumColumnIndex + std::abs(m_smallestColumnStart));
     1752    m_grid.ensureGridSize(maximumRowIndex + std::abs(m_smallestRowStart), maximumColumnIndex + std::abs(m_smallestColumnStart));
    17491753}
    17501754
     
    17811785
    17821786        m_gridItemArea.set(autoGridItem, *emptyGridArea);
    1783         insertItemIntoGrid(*autoGridItem, *emptyGridArea);
     1787        m_grid.insert(*autoGridItem, *emptyGridArea);
    17841788
    17851789        if (!isGridAutoFlowDense)
     
    18541858
    18551859    m_gridItemArea.set(&gridItem, *emptyGridArea);
    1856     insertItemIntoGrid(gridItem, *emptyGridArea);
     1860    m_grid.insert(gridItem, *emptyGridArea);
    18571861    autoPlacementCursor.first = emptyGridArea->rows.startLine();
    18581862    autoPlacementCursor.second = emptyGridArea->columns.startLine();
     
    27192723    // not stored in m_grid).
    27202724    if (direction == ForRows)
    2721         return m_grid.size();
    2722 
    2723     return m_grid.size() ? m_grid[0].size() : GridPositionsResolver::explicitGridColumnCount(style(), m_autoRepeatColumns);
     2725        return m_grid.numRows();
     2726
     2727    return m_grid.numRows() ? m_grid.numColumns() : GridPositionsResolver::explicitGridColumnCount(style(), m_autoRepeatColumns);
    27242728}
    27252729
  • trunk/Source/WebCore/rendering/RenderGrid.h

    r208962 r208973  
    8989    void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&) const;
    9090
    91     void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
    92     void insertItemIntoGrid(RenderBox&, const GridArea&);
    93 
    9491    unsigned computeAutoRepeatTracksCount(GridTrackSizingDirection, SizingOperation) const;
    9592
     
    201198    GridTrackSizingDirection flowAwareDirectionForChild(const RenderBox&, GridTrackSizingDirection) const;
    202199
    203     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
     200    typedef Vector<RenderBox*, 1> GridCell;
     201    typedef Vector<Vector<GridCell>> GridAsMatrix;
     202    class Grid final {
     203    public:
     204        Grid() { }
     205
     206        unsigned numColumns() const { return m_grid.size() ? m_grid[0].size() : 0; }
     207        unsigned numRows() const { return m_grid.size(); }
     208
     209        void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
     210        void insert(RenderBox&, const GridArea&);
     211
     212        const GridCell& cell(unsigned row, unsigned column) const { return m_grid[row][column]; }
     213
     214        void shrinkToFit() { m_grid.shrinkToFit(); }
     215
     216        void clear();
     217
     218    private:
     219        friend class GridIterator;
     220        GridAsMatrix m_grid;
     221    };
     222    Grid m_grid;
     223
    204224    Vector<LayoutUnit> m_columnPositions;
    205225    Vector<LayoutUnit> m_rowPositions;
Note: See TracChangeset for help on using the changeset viewer.