Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / layout / generic / nsGridContainerFrame.h
blob4fe47f3d7bd65906847fd339bdad69799d5b8a95
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,
110 BaselineSharingGroup(2), T>;
112 template <typename T>
113 using PerLogicalAxis =
114 mozilla::EnumeratedArray<LogicalAxis, LogicalAxis(2), T>;
116 // nsIFrame overrides
117 void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
118 const ReflowInput& aReflowInput,
119 nsReflowStatus& aStatus) override;
120 void Init(nsIContent* aContent, nsContainerFrame* aParent,
121 nsIFrame* aPrevInFlow) override;
122 void DidSetComputedStyle(ComputedStyle* aOldStyle) override;
123 nscoord GetMinISize(gfxContext* aRenderingContext) override;
124 nscoord GetPrefISize(gfxContext* aRenderingContext) override;
125 void MarkIntrinsicISizesDirty() override;
127 void BuildDisplayList(nsDisplayListBuilder* aBuilder,
128 const nsDisplayListSet& aLists) override;
130 Maybe<nscoord> GetNaturalBaselineBOffset(
131 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup,
132 BaselineExportContext) const override {
133 if (StyleDisplay()->IsContainLayout() ||
134 HasAnyStateBits(NS_STATE_GRID_SYNTHESIZE_BASELINE)) {
135 return Nothing{};
137 return mozilla::Some(GetBBaseline(aBaselineGroup));
140 #ifdef DEBUG_FRAME_DUMP
141 nsresult GetFrameName(nsAString& aResult) const override;
142 void ExtraContainerFrameInfo(nsACString& aTo) const override;
143 #endif
145 // nsContainerFrame overrides
146 bool DrainSelfOverflowList() override;
147 void AppendFrames(ChildListID aListID, nsFrameList&& aFrameList) override;
148 void InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
149 const nsLineList::iterator* aPrevFrameLine,
150 nsFrameList&& aFrameList) override;
151 void RemoveFrame(DestroyContext&, ChildListID, nsIFrame*) override;
152 mozilla::StyleAlignFlags CSSAlignmentForAbsPosChild(
153 const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const override;
155 #ifdef DEBUG
156 void SetInitialChildList(ChildListID aListID,
157 nsFrameList&& aChildList) override;
158 #endif
161 * Return the containing block for aChild which MUST be an abs.pos. child
162 * of a grid container and that container must have been reflowed.
164 static const nsRect& GridItemCB(nsIFrame* aChild);
166 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridItemContainingBlockRect, nsRect)
169 * These properties are created by a call to
170 * nsGridContainerFrame::GetGridFrameWithComputedInfo, typically from
171 * Element::GetGridFragments.
173 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridColTrackInfo, ComputedGridTrackInfo)
174 const ComputedGridTrackInfo* GetComputedTemplateColumns() {
175 const ComputedGridTrackInfo* info = GetProperty(GridColTrackInfo());
176 MOZ_ASSERT(info, "Property generation wasn't requested.");
177 return info;
180 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridRowTrackInfo, ComputedGridTrackInfo)
181 const ComputedGridTrackInfo* GetComputedTemplateRows() {
182 const ComputedGridTrackInfo* info = GetProperty(GridRowTrackInfo());
183 MOZ_ASSERT(info, "Property generation wasn't requested.");
184 return info;
187 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridColumnLineInfo, ComputedGridLineInfo)
188 const ComputedGridLineInfo* GetComputedTemplateColumnLines() {
189 const ComputedGridLineInfo* info = GetProperty(GridColumnLineInfo());
190 MOZ_ASSERT(info, "Property generation wasn't requested.");
191 return info;
194 NS_DECLARE_FRAME_PROPERTY_DELETABLE(GridRowLineInfo, ComputedGridLineInfo)
195 const ComputedGridLineInfo* GetComputedTemplateRowLines() {
196 const ComputedGridLineInfo* info = GetProperty(GridRowLineInfo());
197 MOZ_ASSERT(info, "Property generation wasn't requested.");
198 return info;
202 * This property is set by the creation of a dom::Grid object, and cleared
203 * during GC unlink. Since the Grid object manages the lifecycle, the property
204 * itself is set without a destructor. The property is also cleared whenever
205 * new grid computed info is generated during reflow, ensuring that we aren't
206 * holding a stale dom::Grid object.
208 NS_DECLARE_FRAME_PROPERTY_WITHOUT_DTOR(GridFragmentInfo, mozilla::dom::Grid)
209 mozilla::dom::Grid* GetGridFragmentInfo() {
210 return GetProperty(GridFragmentInfo());
213 using ImplicitNamedAreas =
214 mozilla::HashMap<mozilla::AtomHashKey, NamedArea, mozilla::AtomHashKey>;
215 NS_DECLARE_FRAME_PROPERTY_DELETABLE(ImplicitNamedAreasProperty,
216 ImplicitNamedAreas)
217 ImplicitNamedAreas* GetImplicitNamedAreas() const {
218 return GetProperty(ImplicitNamedAreasProperty());
221 using ExplicitNamedAreas = mozilla::StyleOwnedSlice<NamedArea>;
222 NS_DECLARE_FRAME_PROPERTY_DELETABLE(ExplicitNamedAreasProperty,
223 ExplicitNamedAreas)
224 ExplicitNamedAreas* GetExplicitNamedAreas() const {
225 return GetProperty(ExplicitNamedAreasProperty());
228 using nsContainerFrame::IsMasonry;
230 /** Return true if this frame has masonry layout in any axis. */
231 bool IsMasonry() const {
232 return HasAnyStateBits(NS_STATE_GRID_IS_ROW_MASONRY |
233 NS_STATE_GRID_IS_COL_MASONRY);
236 /** Return true if this frame is subgridded in its aAxis. */
237 bool IsSubgrid(LogicalAxis aAxis) const {
238 return HasAnyStateBits(aAxis == mozilla::eLogicalAxisBlock
239 ? NS_STATE_GRID_IS_ROW_SUBGRID
240 : NS_STATE_GRID_IS_COL_SUBGRID);
242 bool IsColSubgrid() const { return IsSubgrid(mozilla::eLogicalAxisInline); }
243 bool IsRowSubgrid() const { return IsSubgrid(mozilla::eLogicalAxisBlock); }
244 /** Return true if this frame is subgridded in any axis. */
245 bool IsSubgrid() const {
246 return HasAnyStateBits(NS_STATE_GRID_IS_ROW_SUBGRID |
247 NS_STATE_GRID_IS_COL_SUBGRID);
250 /** Return true if this frame has an item that is subgridded in our aAxis. */
251 bool HasSubgridItems(LogicalAxis aAxis) const {
252 return HasAnyStateBits(aAxis == mozilla::eLogicalAxisBlock
253 ? NS_STATE_GRID_HAS_ROW_SUBGRID_ITEM
254 : NS_STATE_GRID_HAS_COL_SUBGRID_ITEM);
256 /** Return true if this frame has any subgrid items. */
257 bool HasSubgridItems() const {
258 return HasAnyStateBits(NS_STATE_GRID_HAS_ROW_SUBGRID_ITEM |
259 NS_STATE_GRID_HAS_COL_SUBGRID_ITEM);
262 * Return true if the grid item aChild should stretch in its aAxis (i.e. aAxis
263 * is in the aChild's writing-mode).
265 * Note: this method does *not* consider the grid item's aspect-ratio and
266 * natural size in the axis when the self-alignment value is 'normal' per
267 * https://drafts.csswg.org/css-grid/#grid-item-sizing
269 bool GridItemShouldStretch(const nsIFrame* aChild, LogicalAxis aAxis) const;
272 * Returns true if aFrame forms an independent formatting context and hence
273 * should be inhibited from being a subgrid (i.e. if the used value of
274 * 'grid-template-{rows,columns}:subgrid' should be 'none').
275 * https://drafts.csswg.org/css-grid-2/#subgrid-listing
277 * (Note this only makes sense to call if aFrame is itself either a grid
278 * container frame or a wrapper frame for a grid container frame, e.g. a
279 * scroll container frame for a scrollable grid. Having said that, this is
280 * technically safe to call on any non-null frame.)
282 static bool ShouldInhibitSubgridDueToIFC(const nsIFrame* aFrame);
285 * Return a container grid frame for the supplied frame, if available.
286 * @return nullptr if aFrame has no grid container.
288 static nsGridContainerFrame* GetGridContainerFrame(nsIFrame* aFrame);
291 * Return a container grid frame, and ensure it has computed grid info
292 * @return nullptr if aFrame has no grid container, or frame was destroyed
293 * @note this might destroy layout/style data since it may flush layout
295 MOZ_CAN_RUN_SCRIPT_BOUNDARY
296 static nsGridContainerFrame* GetGridFrameWithComputedInfo(nsIFrame* aFrame);
298 struct Subgrid;
299 struct UsedTrackSizes;
300 struct TrackSize;
301 struct GridItemInfo;
302 struct GridReflowInput;
303 struct FindItemInGridOrderResult {
304 // The first(last) item in (reverse) grid order.
305 const GridItemInfo* mItem;
306 // Does the above item span the first(last) track?
307 bool mIsInEdgeTrack;
310 /** Return our parent grid container; |this| MUST be a subgrid. */
311 nsGridContainerFrame* ParentGridContainerForSubgrid() const;
313 // https://drafts.csswg.org/css-sizing/#constraints
314 enum class SizingConstraint {
315 MinContent, // sizing under min-content constraint
316 MaxContent, // sizing under max-content constraint
317 NoConstraint // no constraint, used during Reflow
320 protected:
321 typedef mozilla::LogicalPoint LogicalPoint;
322 typedef mozilla::LogicalRect LogicalRect;
323 typedef mozilla::LogicalSize LogicalSize;
324 typedef mozilla::WritingMode WritingMode;
325 struct Grid;
326 struct GridArea;
327 class LineNameMap;
328 struct LineRange;
329 struct SharedGridData;
330 struct SubgridFallbackTrackSizingFunctions;
331 struct TrackSizingFunctions;
332 struct Tracks;
333 struct TranslatedLineRange;
334 friend nsContainerFrame* NS_NewGridContainerFrame(
335 mozilla::PresShell* aPresShell, ComputedStyle* aStyle);
336 explicit nsGridContainerFrame(ComputedStyle* aStyle,
337 nsPresContext* aPresContext)
338 : nsContainerFrame(aStyle, aPresContext, kClassID),
339 mCachedMinISize(NS_INTRINSIC_ISIZE_UNKNOWN),
340 mCachedPrefISize(NS_INTRINSIC_ISIZE_UNKNOWN) {
341 for (auto& perAxisBaseline : mBaseline) {
342 for (auto& baseline : perAxisBaseline) {
343 baseline = NS_INTRINSIC_ISIZE_UNKNOWN;
349 * XXX temporary - move the ImplicitNamedAreas stuff to the style system.
350 * The implicit area names that come from x-start .. x-end lines in
351 * grid-template-columns / grid-template-rows are stored in this frame
352 * property when needed, as a ImplicitNamedAreas* value.
354 void InitImplicitNamedAreas(const nsStylePosition* aStyle);
356 using LineNameList =
357 const mozilla::StyleOwnedSlice<mozilla::StyleCustomIdent>;
358 void AddImplicitNamedAreas(mozilla::Span<LineNameList>);
359 using StyleLineNameListValue =
360 const mozilla::StyleGenericLineNameListValue<mozilla::StyleInteger>;
361 void AddImplicitNamedAreas(mozilla::Span<StyleLineNameListValue>);
364 * Reflow and place our children.
365 * @return the consumed size of all of this grid container's continuations
366 * so far including this frame
368 nscoord ReflowChildren(GridReflowInput& aState,
369 const LogicalRect& aContentArea,
370 const nsSize& aContainerSize,
371 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus);
374 * Helper for GetMinISize / GetPrefISize.
376 nscoord IntrinsicISize(gfxContext* aRenderingContext,
377 mozilla::IntrinsicISizeType aConstraint);
379 nscoord GetBBaseline(BaselineSharingGroup aBaselineGroup) const {
380 return mBaseline[mozilla::eLogicalAxisBlock][aBaselineGroup];
382 nscoord GetIBaseline(BaselineSharingGroup aBaselineGroup) const {
383 return mBaseline[mozilla::eLogicalAxisInline][aBaselineGroup];
387 * Calculate this grid container's baselines.
388 * @param aBaselineSet which baseline(s) to derive from a baseline-group or
389 * items; a baseline not included is synthesized from the border-box instead.
390 * @param aFragmentStartTrack is the first track in this fragment in the same
391 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
392 * @param aFirstExcludedTrack should be the first track in the next fragment
393 * or one beyond the final track in the last fragment, in aMajor's axis.
394 * Pass the number of tracks if that's not the axis we're fragmenting in.
396 enum BaselineSet : uint32_t {
397 eNone = 0x0,
398 eFirst = 0x1,
399 eLast = 0x2,
400 eBoth = eFirst | eLast,
402 void CalculateBaselines(BaselineSet aBaselineSet,
403 mozilla::CSSOrderAwareFrameIterator* aIter,
404 const nsTArray<GridItemInfo>* aGridItems,
405 const Tracks& aTracks, uint32_t aFragmentStartTrack,
406 uint32_t aFirstExcludedTrack, WritingMode aWM,
407 const nsSize& aCBPhysicalSize,
408 nscoord aCBBorderPaddingStart,
409 nscoord aCBBorderPaddingStartEnd, nscoord aCBSize);
412 * Synthesize a Grid container baseline for aGroup.
414 nscoord SynthesizeBaseline(const FindItemInGridOrderResult& aItem,
415 LogicalAxis aAxis, BaselineSharingGroup aGroup,
416 const nsSize& aCBPhysicalSize, nscoord aCBSize,
417 WritingMode aCBWM);
419 * Find the first item in Grid Order in this fragment.
420 * https://drafts.csswg.org/css-grid/#grid-order
421 * @param aFragmentStartTrack is the first track in this fragment in the same
422 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
424 static FindItemInGridOrderResult FindFirstItemInGridOrder(
425 mozilla::CSSOrderAwareFrameIterator& aIter,
426 const nsTArray<GridItemInfo>& aGridItems, LineRange GridArea::*aMajor,
427 LineRange GridArea::*aMinor, uint32_t aFragmentStartTrack);
429 * Find the last item in Grid Order in this fragment.
430 * @param aFragmentStartTrack is the first track in this fragment in the same
431 * axis as aMajor. Pass zero if that's not the axis we're fragmenting in.
432 * @param aFirstExcludedTrack should be the first track in the next fragment
433 * or one beyond the final track in the last fragment, in aMajor's axis.
434 * Pass the number of tracks if that's not the axis we're fragmenting in.
436 static FindItemInGridOrderResult FindLastItemInGridOrder(
437 mozilla::ReverseCSSOrderAwareFrameIterator& aIter,
438 const nsTArray<GridItemInfo>& aGridItems, LineRange GridArea::*aMajor,
439 LineRange GridArea::*aMinor, uint32_t aFragmentStartTrack,
440 uint32_t aFirstExcludedTrack);
443 * Update our NS_STATE_GRID_IS_COL/ROW_SUBGRID bits and related subgrid state
444 * on our entire continuation chain based on the current style.
445 * This is needed because grid-template-columns/rows style changes only
446 * trigger a reflow so we need to update this dynamically.
448 void UpdateSubgridFrameState();
451 * Return the NS_STATE_GRID_IS_COL/ROW_SUBGRID and
452 * NS_STATE_GRID_IS_ROW/COL_MASONRY bits we ought to have.
454 nsFrameState ComputeSelfSubgridMasonryBits() const;
456 private:
457 // Helpers for ReflowChildren
458 struct Fragmentainer {
460 * The distance from the first grid container fragment's block-axis content
461 * edge to the fragmentainer end.
463 nscoord mToFragmentainerEnd;
465 * True if the current fragment is at the start of the fragmentainer.
467 bool mIsTopOfPage;
469 * Is there a Class C break opportunity at the start content edge?
471 bool mCanBreakAtStart;
473 * Is there a Class C break opportunity at the end content edge?
475 bool mCanBreakAtEnd;
477 * Is the grid container's block-size unconstrained?
479 bool mIsAutoBSize;
482 mozilla::Maybe<nsGridContainerFrame::Fragmentainer> GetNearestFragmentainer(
483 const GridReflowInput& aState) const;
485 // @return the consumed size of all continuations so far including this frame
486 nscoord ReflowInFragmentainer(GridReflowInput& aState,
487 const LogicalRect& aContentArea,
488 ReflowOutput& aDesiredSize,
489 nsReflowStatus& aStatus,
490 Fragmentainer& aFragmentainer,
491 const nsSize& aContainerSize);
493 // Helper for ReflowInFragmentainer
494 // @return the consumed size of all continuations so far including this frame
495 nscoord ReflowRowsInFragmentainer(
496 GridReflowInput& aState, const LogicalRect& aContentArea,
497 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus,
498 Fragmentainer& aFragmentainer, const nsSize& aContainerSize,
499 const nsTArray<const GridItemInfo*>& aItems, uint32_t aStartRow,
500 uint32_t aEndRow, nscoord aBSize, nscoord aAvailableSize);
502 // Helper for ReflowChildren / ReflowInFragmentainer
503 void ReflowInFlowChild(nsIFrame* aChild, const GridItemInfo* aGridItemInfo,
504 nsSize aContainerSize,
505 const mozilla::Maybe<nscoord>& aStretchBSize,
506 const Fragmentainer* aFragmentainer,
507 const GridReflowInput& aState,
508 const LogicalRect& aContentArea,
509 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus);
512 * Places and reflows items when we have masonry layout.
513 * It handles unconstrained reflow and also fragmentation when the row axis
514 * is the masonry axis. ReflowInFragmentainer handles the case when we're
515 * fragmenting and our row axis is a grid axis and it handles masonry layout
516 * in the column axis in that case.
517 * @return the intrinsic size in the masonry axis
519 nscoord MasonryLayout(GridReflowInput& aState,
520 const LogicalRect& aContentArea,
521 SizingConstraint aConstraint,
522 ReflowOutput& aDesiredSize, nsReflowStatus& aStatus,
523 Fragmentainer* aFragmentainer,
524 const nsSize& aContainerSize);
526 // Return the stored UsedTrackSizes, if any.
527 UsedTrackSizes* GetUsedTrackSizes() const;
529 // Store the given TrackSizes in aAxis on a UsedTrackSizes frame property.
530 void StoreUsedTrackSizes(LogicalAxis aAxis,
531 const nsTArray<TrackSize>& aSizes);
533 // The internal implementation for AddImplicitNamedAreas().
534 void AddImplicitNamedAreasInternal(LineNameList& aNameList,
535 ImplicitNamedAreas*& aAreas);
538 * Cached values to optimize GetMinISize/GetPrefISize.
540 nscoord mCachedMinISize;
541 nscoord mCachedPrefISize;
543 // Our baselines, one per BaselineSharingGroup per axis.
544 PerLogicalAxis<PerBaseline<nscoord>> mBaseline;
546 public:
547 // A cached result for a grid item's block-axis measuring reflow. This
548 // cache prevents us from doing exponential reflows in cases of deeply
549 // nested grid frames.
551 // We store the cached value in the grid item's frame property table.
553 // We cache the following as a "key"
554 // - The size of the grid area in the item's inline axis
555 // - The item's block axis baseline padding
556 // ...and we cache the following as the "value",
557 // - The item's border-box BSize
558 class CachedBAxisMeasurement {
559 public:
560 NS_DECLARE_FRAME_PROPERTY_SMALL_VALUE(Prop, CachedBAxisMeasurement)
561 CachedBAxisMeasurement(const nsIFrame* aFrame, const LogicalSize& aCBSize,
562 const nscoord aBSize)
563 : mKey(aFrame, aCBSize), mBSize(aBSize) {}
565 CachedBAxisMeasurement() = default;
567 bool IsValidFor(const nsIFrame* aFrame, const LogicalSize& aCBSize) const {
568 if (aFrame->IsSubtreeDirty()) {
569 return false;
572 if (!CanCacheMeasurement(aFrame, aCBSize)) {
573 return false;
576 return mKey == Key(aFrame, aCBSize);
579 static bool CanCacheMeasurement(const nsIFrame* aFrame,
580 const LogicalSize& aCBSize) {
581 return Key::CanHash(aFrame, aCBSize);
584 nscoord BSize() const { return mBSize; }
586 void Update(const nsIFrame* aFrame, const LogicalSize& aCBSize,
587 const nscoord aBSize) {
588 MOZ_ASSERT(CanCacheMeasurement(aFrame, aCBSize));
589 mKey.mHashKey = Key::GenerateHash(aFrame, aCBSize);
590 mBSize = aBSize;
593 private:
594 struct Key {
595 // mHashKey is generated by combining these 2 variables together
596 // 1. The containing block size in the item's inline axis used
597 // for measuring reflow
598 // 2. The item's baseline padding property
599 uint32_t mHashKey;
601 Key() = default;
603 Key(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
604 MOZ_ASSERT(CanHash(aFrame, aCBSize));
605 mHashKey = GenerateHash(aFrame, aCBSize);
608 void UpdateHash(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
609 MOZ_ASSERT(CanHash(aFrame, aCBSize));
610 mHashKey = GenerateHash(aFrame, aCBSize);
613 static uint32_t GenerateHash(const nsIFrame* aFrame,
614 const LogicalSize& aCBSize) {
615 MOZ_ASSERT(CanHash(aFrame, aCBSize));
617 nscoord gridAreaISize = aCBSize.ISize(aFrame->GetWritingMode());
618 nscoord bBaselinePaddingProperty =
619 abs(aFrame->GetProperty(nsIFrame::BBaselinePadProperty()));
621 uint_fast8_t bitsNeededForISize = mozilla::FloorLog2(gridAreaISize) + 1;
623 return (gridAreaISize << (32 - bitsNeededForISize)) |
624 bBaselinePaddingProperty;
627 static bool CanHash(const nsIFrame* aFrame, const LogicalSize& aCBSize) {
628 uint_fast8_t bitsNeededForISize =
629 mozilla::FloorLog2(aCBSize.ISize(aFrame->GetWritingMode())) + 1;
631 uint_fast8_t bitsNeededForBBaselinePadding =
632 mozilla::FloorLog2(
633 abs(aFrame->GetProperty(nsIFrame::BBaselinePadProperty()))) +
636 return bitsNeededForISize + bitsNeededForBBaselinePadding <= 32;
639 bool operator==(const Key& aOther) const {
640 return mHashKey == aOther.mHashKey;
644 Key mKey;
645 nscoord mBSize;
648 bool CanProvideLineIterator() const final { return true; }
649 nsILineIterator* GetLineIterator() final { return this; }
650 int32_t GetNumLines() const final;
651 bool IsLineIteratorFlowRTL() final;
652 mozilla::Result<LineInfo, nsresult> GetLine(int32_t aLineNumber) final;
653 int32_t FindLineContaining(nsIFrame* aFrame, int32_t aStartLine = 0) final;
654 NS_IMETHOD FindFrameAt(int32_t aLineNumber, nsPoint aPos,
655 nsIFrame** aFrameFound, bool* aPosIsBeforeFirstFrame,
656 bool* aPosIsAfterLastFrame) final;
657 NS_IMETHOD CheckLineOrder(int32_t aLine, bool* aIsReordered,
658 nsIFrame** aFirstVisual,
659 nsIFrame** aLastVisual) final;
662 #endif /* nsGridContainerFrame_h___ */