Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / layout / generic / nsGridContainerFrame.h
blobcb3eef68c31ccd2073d7f72994edd296f45fe98d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for CSS "display: grid | inline-grid" */
9 #ifndef nsGridContainerFrame_h___
10 #define nsGridContainerFrame_h___
12 #include "mozilla/CSSOrderAwareFrameIterator.h"
13 #include "mozilla/MathAlgorithms.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/HashTable.h"
16 #include "nsAtomHashKeys.h"
17 #include "nsContainerFrame.h"
18 #include "nsILineIterator.h"
20 namespace mozilla {
21 class PresShell;
22 namespace dom {
23 class Grid;
25 } // namespace mozilla
27 /**
28 * Factory function.
29 * @return a newly allocated nsGridContainerFrame (infallible)
31 nsContainerFrame* NS_NewGridContainerFrame(mozilla::PresShell* aPresShell,
32 mozilla::ComputedStyle* aStyle);
34 namespace mozilla {
36 /**
37 * The number of implicit / explicit tracks and their sizes.
39 struct ComputedGridTrackInfo {
40 ComputedGridTrackInfo(
41 uint32_t aNumLeadingImplicitTracks, uint32_t aNumExplicitTracks,
42 uint32_t aStartFragmentTrack, uint32_t aEndFragmentTrack,
43 nsTArray<nscoord>&& aPositions, nsTArray<nscoord>&& aSizes,
44 nsTArray<uint32_t>&& aStates, nsTArray<bool>&& aRemovedRepeatTracks,
45 uint32_t aRepeatFirstTrack,
46 nsTArray<nsTArray<StyleCustomIdent>>&& aResolvedLineNames,
47 bool aIsSubgrid, bool aIsMasonry)
48 : mNumLeadingImplicitTracks(aNumLeadingImplicitTracks),
49 mNumExplicitTracks(aNumExplicitTracks),
50 mStartFragmentTrack(aStartFragmentTrack),
51 mEndFragmentTrack(aEndFragmentTrack),
52 mPositions(std::move(aPositions)),
53 mSizes(std::move(aSizes)),
54 mStates(std::move(aStates)),
55 mRemovedRepeatTracks(std::move(aRemovedRepeatTracks)),
56 mResolvedLineNames(std::move(aResolvedLineNames)),
57 mRepeatFirstTrack(aRepeatFirstTrack),
58 mIsSubgrid(aIsSubgrid),
59 mIsMasonry(aIsMasonry) {}
60 uint32_t mNumLeadingImplicitTracks;
61 uint32_t mNumExplicitTracks;
62 uint32_t mStartFragmentTrack;
63 uint32_t mEndFragmentTrack;
64 nsTArray<nscoord> mPositions;
65 nsTArray<nscoord> mSizes;
66 nsTArray<uint32_t> mStates;
67 // Indicates if a track has been collapsed. This will be populated for each
68 // track in the repeat(auto-fit) and repeat(auto-fill), even if there are no
69 // collapsed tracks.
70 nsTArray<bool> mRemovedRepeatTracks;
71 // Contains lists of all line name lists, including the name lists inside
72 // repeats. When a repeat(auto) track exists, the internal track names will
73 // appear once each in this array.
74 nsTArray<nsTArray<StyleCustomIdent>> mResolvedLineNames;
75 uint32_t mRepeatFirstTrack;
76 bool mIsSubgrid;
77 bool mIsMasonry;
80 struct ComputedGridLineInfo {
81 explicit ComputedGridLineInfo(
82 nsTArray<nsTArray<RefPtr<nsAtom>>>&& aNames,
83 const nsTArray<RefPtr<nsAtom>>& aNamesBefore,
84 const nsTArray<RefPtr<nsAtom>>& aNamesAfter,
85 nsTArray<RefPtr<nsAtom>>&& aNamesFollowingRepeat)
86 : mNames(std::move(aNames)),
87 mNamesBefore(aNamesBefore.Clone()),
88 mNamesAfter(aNamesAfter.Clone()),
89 mNamesFollowingRepeat(std::move(aNamesFollowingRepeat)) {}
90 nsTArray<nsTArray<RefPtr<nsAtom>>> mNames;
91 nsTArray<RefPtr<nsAtom>> mNamesBefore;
92 nsTArray<RefPtr<nsAtom>> mNamesAfter;
93 nsTArray<RefPtr<nsAtom>> mNamesFollowingRepeat;
95 } // namespace mozilla
97 class nsGridContainerFrame final : public nsContainerFrame,
98 public nsILineIterator {
99 public:
100 NS_DECL_FRAMEARENA_HELPERS(nsGridContainerFrame)
101 NS_DECL_QUERYFRAME
102 using ComputedGridTrackInfo = mozilla::ComputedGridTrackInfo;
103 using ComputedGridLineInfo = mozilla::ComputedGridLineInfo;
104 using LogicalAxis = mozilla::LogicalAxis;
105 using BaselineSharingGroup = mozilla::BaselineSharingGroup;
106 using NamedArea = mozilla::StyleNamedArea;
108 template <typename T>
109 using PerBaseline = mozilla::EnumeratedArray<BaselineSharingGroup, T, 2>;
111 template <typename T>
112 using PerLogicalAxis = mozilla::EnumeratedArray<LogicalAxis, T, 2>;
114 // nsIFrame overrides
115 void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
116 const ReflowInput& aReflowInput,
117 nsReflowStatus& aStatus) override;
118 void Init(nsIContent* aContent, nsContainerFrame* aParent,
119 nsIFrame* aPrevInFlow) override;
120 void DidSetComputedStyle(ComputedStyle* aOldStyle) override;
121 nscoord GetMinISize(gfxContext* aRenderingContext) override;
122 nscoord GetPrefISize(gfxContext* aRenderingContext) override;
123 void MarkIntrinsicISizesDirty() override;
125 void BuildDisplayList(nsDisplayListBuilder* aBuilder,
126 const nsDisplayListSet& aLists) override;
128 Maybe<nscoord> GetNaturalBaselineBOffset(
129 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup,
130 BaselineExportContext) const override {
131 if (StyleDisplay()->IsContainLayout() ||
132 HasAnyStateBits(NS_STATE_GRID_SYNTHESIZE_BASELINE)) {
133 return Nothing{};
135 return mozilla::Some(GetBBaseline(aBaselineGroup));
138 #ifdef DEBUG_FRAME_DUMP
139 nsresult GetFrameName(nsAString& aResult) const override;
140 void ExtraContainerFrameInfo(nsACString& aTo) const override;
141 #endif
143 // nsContainerFrame overrides
144 bool DrainSelfOverflowList() override;
145 void AppendFrames(ChildListID aListID, nsFrameList&& aFrameList) override;
146 void InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
147 const nsLineList::iterator* aPrevFrameLine,
148 nsFrameList&& aFrameList) override;
149 void RemoveFrame(DestroyContext&, ChildListID, nsIFrame*) override;
150 mozilla::StyleAlignFlags CSSAlignmentForAbsPosChild(
151 const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const override;
153 #ifdef DEBUG
154 void SetInitialChildList(ChildListID aListID,
155 nsFrameList&& aChildList) override;
156 #endif
159 * Return the containing block for aChild which MUST be an abs.pos. child
160 * of a grid container and that container must have been reflowed.
162 static const nsRect& GridItemCB(nsIFrame* aChild);
164 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridItemContainingBlockRect, nsRect)
167 * These properties are created by a call to
168 * nsGridContainerFrame::GetGridFrameWithComputedInfo, typically from
169 * Element::GetGridFragments.
171 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridColTrackInfo, ComputedGridTrackInfo)
172 const ComputedGridTrackInfo* GetComputedTemplateColumns() {
173 const ComputedGridTrackInfo* info = GetProperty(GridColTrackInfo());
174 MOZ_ASSERT(info, "Property generation wasn't requested.");
175 return info;
178 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridRowTrackInfo, ComputedGridTrackInfo)
179 const ComputedGridTrackInfo* GetComputedTemplateRows() {
180 const ComputedGridTrackInfo* info = GetProperty(GridRowTrackInfo());
181 MOZ_ASSERT(info, "Property generation wasn't requested.");
182 return info;
185 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridColumnLineInfo, ComputedGridLineInfo)
186 const ComputedGridLineInfo* GetComputedTemplateColumnLines() {
187 const ComputedGridLineInfo* info = GetProperty(GridColumnLineInfo());
188 MOZ_ASSERT(info, "Property generation wasn't requested.");
189 return info;
192 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridRowLineInfo, ComputedGridLineInfo)
193 const ComputedGridLineInfo* GetComputedTemplateRowLines() {
194 const ComputedGridLineInfo* info = GetProperty(GridRowLineInfo());
195 MOZ_ASSERT(info, "Property generation wasn't requested.");
196 return info;
200 * This property is set by the creation of a dom::Grid object, and cleared
201 * during GC unlink. Since the Grid object manages the lifecycle, the property
202 * itself is set without a destructor. The property is also cleared whenever
203 * new grid computed info is generated during reflow, ensuring that we aren't
204 * holding a stale dom::Grid object.
206 NS_DECLARE_FRAME_PROPERTY_WITHOUT_DTOR(GridFragmentInfo, mozilla::dom::Grid)
207 mozilla::dom::Grid* GetGridFragmentInfo() {
208 return GetProperty(GridFragmentInfo());
211 using ImplicitNamedAreas =
212 mozilla::HashMap<mozilla::AtomHashKey, NamedArea, mozilla::AtomHashKey>;
213 NS_DECLARE_FRAME_PROPERTY_DELETABLE(ImplicitNamedAreasProperty,
214 ImplicitNamedAreas)
215 ImplicitNamedAreas* GetImplicitNamedAreas() const {
216 return GetProperty(ImplicitNamedAreasProperty());
219 using ExplicitNamedAreas = mozilla::StyleOwnedSlice<NamedArea>;
220 NS_DECLARE_FRAME_PROPERTY_DELETABLE(ExplicitNamedAreasProperty,
221 ExplicitNamedAreas)
222 ExplicitNamedAreas* GetExplicitNamedAreas() const {
223 return GetProperty(ExplicitNamedAreasProperty());
226 using nsContainerFrame::IsMasonry;
228 /** Return true if this frame has masonry layout in any axis. */
229 bool IsMasonry() const {
230 return HasAnyStateBits(NS_STATE_GRID_IS_ROW_MASONRY |
231 NS_STATE_GRID_IS_COL_MASONRY);
234 /** Return true if this frame is subgridded in its aAxis. */
235 bool IsSubgrid(LogicalAxis aAxis) const {
236 return HasAnyStateBits(aAxis == mozilla::LogicalAxis::Block
237 ? NS_STATE_GRID_IS_ROW_SUBGRID
238 : NS_STATE_GRID_IS_COL_SUBGRID);
240 bool IsColSubgrid() const { return IsSubgrid(mozilla::LogicalAxis::Inline); }
241 bool IsRowSubgrid() const { return IsSubgrid(mozilla::LogicalAxis::Block); }
242 /** Return true if this frame is subgridded in any axis. */
243 bool IsSubgrid() const {
244 return HasAnyStateBits(NS_STATE_GRID_IS_ROW_SUBGRID |
245 NS_STATE_GRID_IS_COL_SUBGRID);
248 /** Return true if this frame has an item that is subgridded in our aAxis. */
249 bool HasSubgridItems(LogicalAxis aAxis) const {
250 return HasAnyStateBits(aAxis == mozilla::LogicalAxis::Block
251 ? NS_STATE_GRID_HAS_ROW_SUBGRID_ITEM
252 : NS_STATE_GRID_HAS_COL_SUBGRID_ITEM);
254 /** Return true if this frame has any subgrid items. */
255 bool HasSubgridItems() const {
256 return HasAnyStateBits(NS_STATE_GRID_HAS_ROW_SUBGRID_ITEM |
257 NS_STATE_GRID_HAS_COL_SUBGRID_ITEM);
260 * Return true if the grid item aChild should stretch in its aAxis (i.e. aAxis
261 * is in the aChild's writing-mode).
263 * Note: this method does *not* consider the grid item's aspect-ratio and
264 * natural size in the axis when the self-alignment value is 'normal' per
265 * https://drafts.csswg.org/css-grid/#grid-item-sizing
267 bool GridItemShouldStretch(const nsIFrame* aChild, LogicalAxis aAxis) const;
270 * Returns true if aFrame forms an independent formatting context and hence
271 * should be inhibited from being a subgrid (i.e. if the used value of
272 * 'grid-template-{rows,columns}:subgrid' should be 'none').
273 * https://drafts.csswg.org/css-grid-2/#subgrid-listing
275 * (Note this only makes sense to call if aFrame is itself either a grid
276 * container frame or a wrapper frame for a grid container frame, e.g. a
277 * scroll container frame for a scrollable grid. Having said that, this is
278 * technically safe to call on any non-null frame.)
280 static bool ShouldInhibitSubgridDueToIFC(const nsIFrame* aFrame);
283 * Return a container grid frame for the supplied frame, if available.
284 * @return nullptr if aFrame has no grid container.
286 static nsGridContainerFrame* GetGridContainerFrame(nsIFrame* aFrame);
289 * Return a container grid frame, and ensure it has computed grid info
290 * @return nullptr if aFrame has no grid container, or frame was destroyed
291 * @note this might destroy layout/style data since it may flush layout
293 MOZ_CAN_RUN_SCRIPT_BOUNDARY
294 static nsGridContainerFrame* GetGridFrameWithComputedInfo(nsIFrame* aFrame);
296 struct Subgrid;
297 struct UsedTrackSizes;
298 struct TrackSize;
299 struct GridItemInfo;
300 struct GridReflowInput;
301 struct FindItemInGridOrderResult {
302 // The first(last) item in (reverse) grid order.
303 const GridItemInfo* mItem;
304 // Does the above item span the first(last) track?
305 bool mIsInEdgeTrack;
308 /** Return our parent grid container; |this| MUST be a subgrid. */
309 nsGridContainerFrame* ParentGridContainerForSubgrid() const;
311 // https://drafts.csswg.org/css-sizing/#constraints
312 enum class SizingConstraint {
313 MinContent, // sizing under min-content constraint
314 MaxContent, // sizing under max-content constraint
315 NoConstraint // no constraint, used during Reflow
318 protected:
319 typedef mozilla::LogicalPoint LogicalPoint;
320 typedef mozilla::LogicalRect LogicalRect;
321 typedef mozilla::LogicalSize LogicalSize;
322 typedef mozilla::WritingMode WritingMode;
323 struct Grid;
324 struct GridArea;
325 class LineNameMap;
326 struct LineRange;
327 struct SharedGridData;
328 struct SubgridFallbackTrackSizingFunctions;
329 struct TrackSizingFunctions;
330 struct Tracks;
331 struct TranslatedLineRange;
332 friend nsContainerFrame* NS_NewGridContainerFrame(
333 mozilla::PresShell* aPresShell, ComputedStyle* aStyle);
334 explicit nsGridContainerFrame(ComputedStyle* aStyle,
335 nsPresContext* aPresContext)
336 : nsContainerFrame(aStyle, aPresContext, kClassID),
337 mCachedMinISize(NS_INTRINSIC_ISIZE_UNKNOWN),
338 mCachedPrefISize(NS_INTRINSIC_ISIZE_UNKNOWN) {
339 for (auto& perAxisBaseline : mBaseline) {
340 for (auto& baseline : perAxisBaseline) {
341 baseline = NS_INTRINSIC_ISIZE_UNKNOWN;
347 * XXX temporary - move the ImplicitNamedAreas stuff to the style system.
348 * The implicit area names that come from x-start .. x-end lines in
349 * grid-template-columns / grid-template-rows are stored in this frame
350 * property when needed, as a ImplicitNamedAreas* value.
352 void InitImplicitNamedAreas(const nsStylePosition* aStyle);
354 using LineNameList =
355 const mozilla::StyleOwnedSlice<mozilla::StyleCustomIdent>;
356 void AddImplicitNamedAreas(mozilla::Span<LineNameList>);
357 using StyleLineNameListValue =
358 const mozilla::StyleGenericLineNameListValue<mozilla::StyleInteger>;
359 void AddImplicitNamedAreas(mozilla::Span<StyleLineNameListValue>);
362 * Reflow and place our children.
363 * @return the consumed size of all of this grid container's continuations
364 * so far including this frame
366 nscoord ReflowChildren(GridReflowInput& aState,
367 const LogicalRect& aContentArea,
368 const nsSize& aContainerSize,
369 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus);
372 * Helper for GetMinISize / GetPrefISize.
374 nscoord IntrinsicISize(gfxContext* aRenderingContext,
375 mozilla::IntrinsicISizeType aConstraint);
377 nscoord GetBBaseline(BaselineSharingGroup aBaselineGroup) const {
378 return mBaseline[mozilla::LogicalAxis::Block][aBaselineGroup];
380 nscoord GetIBaseline(BaselineSharingGroup aBaselineGroup) const {
381 return mBaseline[mozilla::LogicalAxis::Inline][aBaselineGroup];
385 * Calculate this grid container's baselines.
386 * @param aBaselineSet which baseline(s) to derive from a baseline-group or
387 * items; a baseline not included is synthesized from the border-box instead.
388 * @param aFragmentStartTrack is the first track in this fragment in the same
389 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
390 * @param aFirstExcludedTrack should be the first track in the next fragment
391 * or one beyond the final track in the last fragment, in aMajor's axis.
392 * Pass the number of tracks if that's not the axis we're fragmenting in.
394 enum BaselineSet : uint32_t {
395 eNone = 0x0,
396 eFirst = 0x1,
397 eLast = 0x2,
398 eBoth = eFirst | eLast,
400 void CalculateBaselines(BaselineSet aBaselineSet,
401 mozilla::CSSOrderAwareFrameIterator* aIter,
402 const nsTArray<GridItemInfo>* aGridItems,
403 const Tracks& aTracks, uint32_t aFragmentStartTrack,
404 uint32_t aFirstExcludedTrack, WritingMode aWM,
405 const nsSize& aCBPhysicalSize,
406 nscoord aCBBorderPaddingStart,
407 nscoord aCBBorderPaddingStartEnd, nscoord aCBSize);
410 * Synthesize a Grid container baseline for aGroup.
412 nscoord SynthesizeBaseline(const FindItemInGridOrderResult& aGridOrderItem,
413 LogicalAxis aAxis, BaselineSharingGroup aGroup,
414 const nsSize& aCBPhysicalSize, nscoord aCBSize,
415 WritingMode aCBWM);
417 * Find the first item in Grid Order in this fragment.
418 * https://drafts.csswg.org/css-grid/#grid-order
419 * @param aFragmentStartTrack is the first track in this fragment in the same
420 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
422 static FindItemInGridOrderResult FindFirstItemInGridOrder(
423 mozilla::CSSOrderAwareFrameIterator& aIter,
424 const nsTArray<GridItemInfo>& aGridItems, LineRange GridArea::*aMajor,
425 LineRange GridArea::*aMinor, uint32_t aFragmentStartTrack);
427 * Find the last item in Grid Order in this fragment.
428 * @param aFragmentStartTrack is the first track in this fragment in the same
429 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
430 * @param aFirstExcludedTrack should be the first track in the next fragment
431 * or one beyond the final track in the last fragment, in aMajor's axis.
432 * Pass the number of tracks if that's not the axis we're fragmenting in.
434 static FindItemInGridOrderResult FindLastItemInGridOrder(
435 mozilla::ReverseCSSOrderAwareFrameIterator& aIter,
436 const nsTArray<GridItemInfo>& aGridItems, LineRange GridArea::*aMajor,
437 LineRange GridArea::*aMinor, uint32_t aFragmentStartTrack,
438 uint32_t aFirstExcludedTrack);
441 * Update our NS_STATE_GRID_IS_COL/ROW_SUBGRID bits and related subgrid state
442 * on our entire continuation chain based on the current style.
443 * This is needed because grid-template-columns/rows style changes only
444 * trigger a reflow so we need to update this dynamically.
446 void UpdateSubgridFrameState();
449 * Return the NS_STATE_GRID_IS_COL/ROW_SUBGRID and
450 * NS_STATE_GRID_IS_ROW/COL_MASONRY bits we ought to have.
452 nsFrameState ComputeSelfSubgridMasonryBits() const;
454 private:
455 // Helpers for ReflowChildren
456 struct Fragmentainer {
458 * The distance from the first grid container fragment's block-axis content
459 * edge to the fragmentainer end.
461 nscoord mToFragmentainerEnd;
463 * True if the current fragment is at the start of the fragmentainer.
465 bool mIsTopOfPage;
467 * Is there a Class C break opportunity at the start content edge?
469 bool mCanBreakAtStart;
471 * Is there a Class C break opportunity at the end content edge?
473 bool mCanBreakAtEnd;
475 * Is the grid container's block-size unconstrained?
477 bool mIsAutoBSize;
480 mozilla::Maybe<nsGridContainerFrame::Fragmentainer> GetNearestFragmentainer(
481 const GridReflowInput& aState) const;
483 // @return the consumed size of all continuations so far including this frame
484 nscoord ReflowInFragmentainer(GridReflowInput& aState,
485 const LogicalRect& aContentArea,
486 ReflowOutput& aDesiredSize,
487 nsReflowStatus& aStatus,
488 Fragmentainer& aFragmentainer,
489 const nsSize& aContainerSize);
491 // Helper for ReflowInFragmentainer
492 // @return the consumed size of all continuations so far including this frame
493 nscoord ReflowRowsInFragmentainer(
494 GridReflowInput& aState, const LogicalRect& aContentArea,
495 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus,
496 Fragmentainer& aFragmentainer, const nsSize& aContainerSize,
497 const nsTArray<const GridItemInfo*>& aItems, uint32_t aStartRow,
498 uint32_t aEndRow, nscoord aBSize, nscoord aAvailableSize);
500 // Helper for ReflowChildren / ReflowInFragmentainer
501 void ReflowInFlowChild(nsIFrame* aChild, const GridItemInfo* aGridItemInfo,
502 nsSize aContainerSize,
503 const mozilla::Maybe<nscoord>& aStretchBSize,
504 const Fragmentainer* aFragmentainer,
505 const GridReflowInput& aState,
506 const LogicalRect& aContentArea,
507 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus);
510 * Places and reflows items when we have masonry layout.
511 * It handles unconstrained reflow and also fragmentation when the row axis
512 * is the masonry axis. ReflowInFragmentainer handles the case when we're
513 * fragmenting and our row axis is a grid axis and it handles masonry layout
514 * in the column axis in that case.
515 * @return the intrinsic size in the masonry axis
517 nscoord MasonryLayout(GridReflowInput& aState,
518 const LogicalRect& aContentArea,
519 SizingConstraint aConstraint,
520 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus,
521 Fragmentainer* aFragmentainer,
522 const nsSize& aContainerSize);
524 // Return the stored UsedTrackSizes, if any.
525 UsedTrackSizes* GetUsedTrackSizes() const;
527 // Store the given TrackSizes in aAxis on a UsedTrackSizes frame property.
528 void StoreUsedTrackSizes(LogicalAxis aAxis,
529 const nsTArray<TrackSize>& aSizes);
531 // The internal implementation for AddImplicitNamedAreas().
532 void AddImplicitNamedAreasInternal(LineNameList& aNameList,
533 ImplicitNamedAreas*& aAreas);
536 * Cached values to optimize GetMinISize/GetPrefISize.
538 nscoord mCachedMinISize;
539 nscoord mCachedPrefISize;
541 // Our baselines, one per BaselineSharingGroup per axis.
542 PerLogicalAxis<PerBaseline<nscoord>> mBaseline;
544 public:
545 // A cached result for a grid item's block-axis measuring reflow. This
546 // cache prevents us from doing exponential reflows in cases of deeply
547 // nested grid frames.
549 // We store the cached value in the grid item's frame property table.
551 // We cache the following as a "key"
552 // - The size of the grid area in the item's inline axis
553 // - The item's block axis baseline padding
554 // ...and we cache the following as the "value",
555 // - The item's border-box BSize
556 class CachedBAxisMeasurement {
557 public:
558 NS_DECLARE_FRAME_PROPERTY_SMALL_VALUE(Prop, CachedBAxisMeasurement)
559 CachedBAxisMeasurement(const nsIFrame* aFrame, const LogicalSize& aCBSize,
560 const nscoord aBSize)
561 : mKey(aFrame, aCBSize), mBSize(aBSize) {}
563 CachedBAxisMeasurement() = default;
565 bool IsValidFor(const nsIFrame* aFrame, const LogicalSize& aCBSize) const {
566 if (aFrame->IsSubtreeDirty()) {
567 return false;
570 if (!CanCacheMeasurement(aFrame, aCBSize)) {
571 return false;
574 return mKey == Key(aFrame, aCBSize);
577 static bool CanCacheMeasurement(const nsIFrame* aFrame,
578 const LogicalSize& aCBSize) {
579 return Key::CanHash(aFrame, aCBSize);
582 nscoord BSize() const { return mBSize; }
584 void Update(const nsIFrame* aFrame, const LogicalSize& aCBSize,
585 const nscoord aBSize) {
586 MOZ_ASSERT(CanCacheMeasurement(aFrame, aCBSize));
587 mKey.mHashKey = Key::GenerateHash(aFrame, aCBSize);
588 mBSize = aBSize;
591 private:
592 struct Key {
593 // mHashKey is generated by combining these 2 variables together
594 // 1. The containing block size in the item's inline axis used
595 // for measuring reflow
596 // 2. The item's baseline padding property
597 uint32_t mHashKey;
599 Key() = default;
601 Key(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
602 MOZ_ASSERT(CanHash(aFrame, aCBSize));
603 mHashKey = GenerateHash(aFrame, aCBSize);
606 void UpdateHash(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
607 MOZ_ASSERT(CanHash(aFrame, aCBSize));
608 mHashKey = GenerateHash(aFrame, aCBSize);
611 static uint32_t GenerateHash(const nsIFrame* aFrame,
612 const LogicalSize& aCBSize) {
613 MOZ_ASSERT(CanHash(aFrame, aCBSize));
615 nscoord gridAreaISize = aCBSize.ISize(aFrame->GetWritingMode());
616 nscoord bBaselinePaddingProperty =
617 abs(aFrame->GetProperty(nsIFrame::BBaselinePadProperty()));
619 uint_fast8_t bitsNeededForISize = mozilla::FloorLog2(gridAreaISize) + 1;
621 return (gridAreaISize << (32 - bitsNeededForISize)) |
622 bBaselinePaddingProperty;
625 static bool CanHash(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
626 uint_fast8_t bitsNeededForISize =
627 mozilla::FloorLog2(aCBSize.ISize(aFrame->GetWritingMode())) + 1;
629 uint_fast8_t bitsNeededForBBaselinePadding =
630 mozilla::FloorLog2(
631 abs(aFrame->GetProperty(nsIFrame::BBaselinePadProperty()))) +
634 return bitsNeededForISize + bitsNeededForBBaselinePadding <= 32;
637 bool operator==(const Key& aOther) const {
638 return mHashKey == aOther.mHashKey;
642 Key mKey;
643 nscoord mBSize;
646 bool CanProvideLineIterator() const final { return true; }
647 nsILineIterator* GetLineIterator() final { return this; }
648 int32_t GetNumLines() const final;
649 bool IsLineIteratorFlowRTL() final;
650 mozilla::Result<LineInfo, nsresult> GetLine(int32_t aLineNumber) final;
651 int32_t FindLineContaining(nsIFrame* aFrame, int32_t aStartLine = 0) final;
652 NS_IMETHOD FindFrameAt(int32_t aLineNumber, nsPoint aPos,
653 nsIFrame** aFrameFound, bool* aPosIsBeforeFirstFrame,
654 bool* aPosIsAfterLastFrame) final;
655 NS_IMETHOD CheckLineOrder(int32_t aLine, bool* aIsReordered,
656 nsIFrame** aFirstVisual,
657 nsIFrame** aLastVisual) final;
660 #endif /* nsGridContainerFrame_h___ */