Skip to content

Commit efaa47c

Browse files
committed
[GFC] Move PlacedGridItem construction from ImplicitGrid to GridFormattingContext.
https://bugs.webkit.org/show_bug.cgi?id=299533 rdar://problem/161337846 Reviewed by Brent Fulgham. Currently, PlacedGridItems are created via placedGridItems() on ImplicitGrid by constructing them out of the set of UnplacedGridItems which it has mapped to different areas in the grid. This has worked well enough for now, but as we prepare to size the grid items, we need to be able to store additional data on these objects in order to accomplish this. In particular, we need to store and be able to use the various sizing properties (width, min-width, max-width, etc.) for each dimension (inline vs block). However, we do not have enough information at the ImplicitGrid level in order to map these properties to the appropriate dimension. Instead, we should have GridFormattingContext start creating these objects. Not only will this allow us to correctly create PlacedGridItems with the data that we need, but it also allows ImplicitGrid to return some simple structures that describe the ImplicitGrid rather than a more complex object. * Source/WebCore/layout/formattingContexts/grid/GridLayout.cpp: (WebCore::Layout::GridLayout::placeGridItems): (WebCore::Layout::GridLayout::layout): Previously we were getting the PlacedGridItems as a result of the grid item placement portion (placeGridItems()) of the layout algorithm. Now we get the grid areas for each item which were resolved from that process. We can then ask the GridFormattingContext to create the set of PlacedGridItems from this data. * Source/WebCore/layout/formattingContexts/grid/ImplicitGrid.cpp: (WebCore::Layout::ImplicitGrid::gridAreas const): Instead of returning PlacedGridItems we return the set of grid areas contained within the ImplicitGrid. This is just a map of each item that has been placed in the grid along with the area (set of lines) that it has been placed in. The caller can then take the UnplacedGridItem and its grid area and do whatever it needs to which includes constructing a PlacedGridItem if they need it. Canonical link: https://commits.webkit.org/300547@main
1 parent 9bea292 commit efaa47c

File tree

10 files changed

+80
-19
lines changed

10 files changed

+80
-19
lines changed

Source/WebCore/WebCore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,7 @@
43444344
A5DEBDA416FB908700836FE0 /* WebKitPlaybackTargetAvailabilityEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = A5DEBDA016FB908700836FE0 /* WebKitPlaybackTargetAvailabilityEvent.h */; };
43454345
A5F36D3B18F758720054C024 /* PageDebugger.h in Headers */ = {isa = PBXBuildFile; fileRef = A5F36D3918F758720054C024 /* PageDebugger.h */; settings = {ATTRIBUTES = (Private, ); }; };
43464346
A5FA575E2E85BFE500EF058F /* LayoutIntegrationGridCoverage.h in Headers */ = {isa = PBXBuildFile; fileRef = A5FA575C2E85BFDE00EF058F /* LayoutIntegrationGridCoverage.h */; settings = {ATTRIBUTES = (Private, ); }; };
4347+
A5FA57662E85F40D00EF058F /* GridAreaLines.h in Headers */ = {isa = PBXBuildFile; fileRef = A5FA57652E85F40600EF058F /* GridAreaLines.h */; settings = {ATTRIBUTES = (Private, ); }; };
43474348
A6D169641346B4C1000EB770 /* ShadowRoot.h in Headers */ = {isa = PBXBuildFile; fileRef = A6D169631346B4C1000EB770 /* ShadowRoot.h */; settings = {ATTRIBUTES = (Private, ); }; };
43484349
A6D5A99D1629D70000297330 /* ScrollingTreeScrollingNodeDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = A6D5A99B1629D70000297330 /* ScrollingTreeScrollingNodeDelegate.h */; settings = {ATTRIBUTES = (Private, ); }; };
43494350
A70B77D62A9EE40B003D8566 /* Quaternion.h in Headers */ = {isa = PBXBuildFile; fileRef = A70B77D52A9EE40B003D8566 /* Quaternion.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -16883,6 +16884,7 @@
1688316884
A5F6E16C132ED46E008EDAE3 /* Autocapitalize.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Autocapitalize.cpp; sourceTree = "<group>"; };
1688416885
A5FA575C2E85BFDE00EF058F /* LayoutIntegrationGridCoverage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LayoutIntegrationGridCoverage.h; sourceTree = "<group>"; };
1688516886
A5FA575D2E85BFDE00EF058F /* LayoutIntegrationGridCoverage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LayoutIntegrationGridCoverage.cpp; sourceTree = "<group>"; };
16887+
A5FA57652E85F40600EF058F /* GridAreaLines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GridAreaLines.h; sourceTree = "<group>"; };
1688616888
A5FD1D922ADDF9CC00F90FE5 /* AncestorSubgridIterator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AncestorSubgridIterator.cpp; sourceTree = "<group>"; };
1688716889
A5FD1D932ADDF9CC00F90FE5 /* AncestorSubgridIterator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AncestorSubgridIterator.h; sourceTree = "<group>"; };
1688816890
A6D169611346B49B000EB770 /* ShadowRoot.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ShadowRoot.cpp; sourceTree = "<group>"; };
@@ -32520,6 +32522,7 @@
3252032522
A585CC412E566A7800A69390 /* grid */ = {
3252132523
isa = PBXGroup;
3252232524
children = (
32525+
A5FA57652E85F40600EF058F /* GridAreaLines.h */,
3252332526
A585CC3E2E566A7800A69390 /* GridFormattingContext.cpp */,
3252432527
A585CC3D2E566A7800A69390 /* GridFormattingContext.h */,
3252532528
A585CC402E566A7800A69390 /* GridLayout.cpp */,
@@ -42959,6 +42962,7 @@
4295942962
77A17A7B12F2890B004E02F6 /* GraphicsTypesGL.h in Headers */,
4296042963
E112F4721E3A861600D6CDFD /* Grid.h in Headers */,
4296142964
CD3E251C18046B0600E27F56 /* GridArea.h in Headers */,
42965+
A5FA57662E85F40D00EF058F /* GridAreaLines.h in Headers */,
4296242966
A585CC442E566A7800A69390 /* GridFormattingContext.h in Headers */,
4296342967
A585CC452E566A7800A69390 /* GridLayout.h in Headers */,
4296442968
A54CAA142C409C4500D6A999 /* GridLayoutState.h in Headers */,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2025 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#pragma once
27+
28+
namespace WebCore {
29+
namespace Layout {
30+
// https://drafts.csswg.org/css-grid-1/#grid-area
31+
struct GridAreaLines {
32+
size_t columnStartLine;
33+
size_t columnEndLine;
34+
size_t rowStartLine;
35+
size_t rowEndLine;
36+
};
37+
}
38+
}

Source/WebCore/layout/formattingContexts/grid/GridFormattingContext.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "GridLayout.h"
3030
#include "LayoutChildIterator.h"
31+
#include "PlacedGridItem.h"
3132
#include "RenderStyleInlines.h"
3233
#include "StylePrimitiveNumeric.h"
3334
#include "UnplacedGridItem.h"
@@ -112,5 +113,15 @@ void GridFormattingContext::layout(GridLayoutConstraints layoutConstraints)
112113
GridLayout { *this }.layout(layoutConstraints, unplacedGridItems);
113114
}
114115

116+
GridFormattingContext::PlacedGridItems GridFormattingContext::constructPlacedGridItems(const GridAreas& gridAreas) const
117+
{
118+
PlacedGridItems placedGridItems;
119+
placedGridItems.reserveInitialCapacity(gridAreas.size());
120+
for (auto [ unplacedGridItem, gridAreaLines ] : gridAreas)
121+
placedGridItems.constructAndAppend(unplacedGridItem, gridAreaLines);
122+
123+
return placedGridItems;
124+
}
125+
115126
} // namespace Layout
116127
} // namespace WebCore

Source/WebCore/layout/formattingContexts/grid/GridFormattingContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ namespace WebCore {
3333
namespace Layout {
3434

3535
class ElementBox;
36+
class PlacedGridItem;
3637

3738
class UnplacedGridItem;
39+
40+
struct GridAreaLines;
3841
struct UnplacedGridItems;
3942

4043
class GridFormattingContext : public CanMakeCheckedPtr<GridFormattingContext> {
@@ -51,6 +54,10 @@ class GridFormattingContext : public CanMakeCheckedPtr<GridFormattingContext> {
5154

5255
void layout(GridLayoutConstraints);
5356

57+
using PlacedGridItems = Vector<PlacedGridItem>;
58+
using GridAreas = HashMap<UnplacedGridItem, GridAreaLines>;
59+
PlacedGridItems constructPlacedGridItems(const GridAreas&) const;
60+
5461
const ElementBox& root() const { return m_gridBox; }
5562

5663
private:

Source/WebCore/layout/formattingContexts/grid/GridLayout.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ auto GridLayout::placeGridItems(const UnplacedGridItems& unplacedGridItems, cons
4848
const Vector<Style::GridTrackSize>& gridTemplateRowsTrackSizes)
4949
{
5050
struct Result {
51-
PlacedGridItems placedGridItems;
51+
using GridAreas = HashMap<UnplacedGridItem, GridAreaLines>;
52+
GridAreas gridAreas;
5253
size_t implicitGridColumnsCount;
5354
size_t implicitGridRowsCount;
5455
};
@@ -63,7 +64,7 @@ auto GridLayout::placeGridItems(const UnplacedGridItems& unplacedGridItems, cons
6364
ASSERT(implicitGrid.columnsCount() == gridTemplateColumnsTrackSizes.size() && implicitGrid.rowsCount() == gridTemplateRowsTrackSizes.size(),
6465
"Since we currently only support placing items which are explicitly placed and fit within the explicit grid, the size of the implicit grid should match the passed in sizes.");
6566

66-
return Result { implicitGrid.placedGridItems(), implicitGrid.columnsCount(), implicitGrid.rowsCount() };
67+
return Result { implicitGrid.gridAreas(), implicitGrid.columnsCount(), implicitGrid.rowsCount() };
6768
}
6869

6970
// https://drafts.csswg.org/css-grid-1/#layout-algorithm
@@ -74,7 +75,8 @@ void GridLayout::layout(GridFormattingContext::GridLayoutConstraints, const Unpl
7475
auto& gridTemplateRowsTrackSizes = gridContainerStyle->gridTemplateRows().sizes;
7576

7677
// 1. Run the Grid Item Placement Algorithm to resolve the placement of all grid items in the grid.
77-
auto [ placedGridItems, implicitGridColumnsCount, implicitGridRowsCount ] = placeGridItems(unplacedGridItems, gridTemplateColumnsTrackSizes, gridTemplateRowsTrackSizes);
78+
auto [ gridAreas, implicitGridColumnsCount, implicitGridRowsCount ] = placeGridItems(unplacedGridItems, gridTemplateColumnsTrackSizes, gridTemplateRowsTrackSizes);
79+
auto placedGridItems = formattingContext().constructPlacedGridItems(gridAreas);
7880

7981
auto columnTrackSizingFunctionsList = trackSizingFunctions(implicitGridColumnsCount, gridTemplateColumnsTrackSizes);
8082
auto rowTrackSizingFunctionsList = trackSizingFunctions(implicitGridRowsCount, gridTemplateRowsTrackSizes);

Source/WebCore/layout/formattingContexts/grid/GridLayout.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class GridLayout {
6868
};
6969
static UsedTrackSizes performGridSizingAlgorithm(const PlacedGridItems&, const TrackSizingFunctionsList& columnTrackSizingFunctionsList, const TrackSizingFunctionsList& rowTrackSizingFunctionsList);
7070

71+
const GridFormattingContext& formattingContext() const { return m_gridFormattingContext.get(); }
72+
7173
const ElementBox& gridContainer() const;
7274
const RenderStyle& gridContainerStyle() const;
7375

Source/WebCore/layout/formattingContexts/grid/ImplicitGrid.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,23 @@ void ImplicitGrid::insertUnplacedGridItem(const UnplacedGridItem& unplacedGridIt
100100

101101
}
102102

103-
PlacedGridItems ImplicitGrid::placedGridItems() const
103+
ImplicitGrid::GridAreas ImplicitGrid::gridAreas() const
104104
{
105-
HashSet<UnplacedGridItem> processedUnplacedGridItems;
106-
PlacedGridItems placedGridItems;
105+
GridAreas gridAreas;
106+
gridAreas.reserveInitialCapacity(rowsCount() * columnsCount());
107107

108108
for (size_t rowIndex = 0; rowIndex < m_gridMatrix.size(); ++rowIndex) {
109109
for (size_t columnIndex = 0; columnIndex < m_gridMatrix[rowIndex].size(); ++columnIndex) {
110110

111111
const auto& gridCell = m_gridMatrix[rowIndex][columnIndex];
112112
for (const auto& unplacedGridItem : gridCell) {
113-
if (processedUnplacedGridItems.contains(unplacedGridItem))
114-
continue;
115-
processedUnplacedGridItems.add(unplacedGridItem);
116-
placedGridItems.append({ unplacedGridItem, { columnIndex, columnIndex + 1, rowIndex, rowIndex + 1 } });
113+
gridAreas.ensure(unplacedGridItem, [&]() {
114+
return GridAreaLines { columnIndex, columnIndex + 1, rowIndex, rowIndex + 1 };
115+
});
117116
}
118117
}
119118
}
120-
return placedGridItems;
119+
return gridAreas;
121120
}
122121

123122
} // namespace Layout

Source/WebCore/layout/formattingContexts/grid/ImplicitGrid.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace Layout {
3434
class PlacedGridItem;
3535
class UnplacedGridItem;
3636

37+
struct GridAreaLines;
38+
3739
using PlacedGridItems = Vector<PlacedGridItem>;
3840
using GridCell = Vector<UnplacedGridItem, 1>;
3941

@@ -47,7 +49,8 @@ class ImplicitGrid {
4749

4850
void insertUnplacedGridItem(const UnplacedGridItem&);
4951

50-
PlacedGridItems placedGridItems() const;
52+
using GridAreas = HashMap<UnplacedGridItem, GridAreaLines>;
53+
GridAreas gridAreas() const;
5154

5255
private:
5356
using GridMatrix = Vector<Vector<GridCell>>;

Source/WebCore/layout/formattingContexts/grid/PlacedGridItem.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include "GridAreaLines.h"
2829
#include "LayoutElementBox.h"
2930
#include <wtf/HashTraits.h>
3031

@@ -35,13 +36,6 @@ class UnplacedGridItem;
3536

3637
class PlacedGridItem {
3738
public:
38-
// https://drafts.csswg.org/css-grid-1/#grid-area
39-
struct GridAreaLines {
40-
size_t columnStartLine;
41-
size_t columnEndLine;
42-
size_t rowStartLine;
43-
size_t rowEndLine;
44-
};
4539

4640
PlacedGridItem(const UnplacedGridItem&, GridAreaLines);
4741

Source/WebCore/layout/formattingContexts/grid/UnplacedGridItem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ template<> struct HashTraits<WebCore::Layout::UnplacedGridItem> : SimpleClassHas
8484
static constexpr bool hasIsEmptyValueFunction = true;
8585

8686
static bool isEmptyValue(const WebCore::Layout::UnplacedGridItem& unplacedGridItem) { return unplacedGridItem.isHashTableEmptyValue(); }
87+
static WebCore::Layout::UnplacedGridItem emptyValue() { return WebCore::Layout::UnplacedGridItem { HashTableEmptyValueType::HashTableEmptyValue }; }
8788
};
8889

8990
template<> struct DefaultHash<WebCore::Layout::UnplacedGridItem> {

0 commit comments

Comments
 (0)