| 1 | /* |
| 2 | * Copyright (C) 2018 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 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
| 29 | |
| 30 | #include <wtf/HashMap.h> |
| 31 | #include <wtf/HashSet.h> |
| 32 | #include <wtf/IsoMalloc.h> |
| 33 | #include <wtf/OptionSet.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
| 38 | class RenderView; |
| 39 | #endif |
| 40 | |
| 41 | namespace Display { |
| 42 | class Box; |
| 43 | } |
| 44 | |
| 45 | namespace Layout { |
| 46 | |
| 47 | enum class StyleDiff; |
| 48 | class Box; |
| 49 | class Container; |
| 50 | class FormattingContext; |
| 51 | class FormattingState; |
| 52 | |
| 53 | // LayoutState is the entry point for layout. It takes the initial containing block which acts as the root of the layout context. |
| 54 | // LayoutState::layout() generates the display tree for the root container's subtree (it does not run layout on the root though). |
| 55 | // Note, while the initial containing block is entry point for the initial layout, it does not necessarily need to be the entry point of any |
| 56 | // subsequent layouts (subtree layout). A non-initial, subtree layout could be initiated on multiple formatting contexts. |
| 57 | // Each formatting context has an entry point for layout, which potenitally means multiple entry points per layout frame. |
| 58 | // LayoutState also holds the formatting states. They cache formatting context specific data to enable performant incremental layouts. |
| 59 | class LayoutState { |
| 60 | WTF_MAKE_ISO_ALLOCATED(LayoutState); |
| 61 | public: |
| 62 | LayoutState(const Container& initialContainingBlock); |
| 63 | |
| 64 | void updateLayout(); |
| 65 | void styleChanged(const Box&, StyleDiff); |
| 66 | void setInQuirksMode(bool inQuirksMode) { m_inQuirksMode = inQuirksMode; } |
| 67 | |
| 68 | enum class UpdateType { |
| 69 | Overflow = 1 << 0, |
| 70 | Position = 1 << 1, |
| 71 | Size = 1 << 2, |
| 72 | All = Overflow | Position | Size |
| 73 | }; |
| 74 | void markNeedsUpdate(const Box&, OptionSet<UpdateType>); |
| 75 | bool needsUpdate(const Box&) const; |
| 76 | |
| 77 | FormattingState& formattingStateForBox(const Box&) const; |
| 78 | FormattingState& establishedFormattingState(const Box& formattingRoot) const; |
| 79 | bool hasFormattingState(const Box& formattingRoot) const { return m_formattingStates.contains(&formattingRoot); } |
| 80 | FormattingState& createFormattingStateForFormattingRootIfNeeded(const Box& formattingRoot); |
| 81 | |
| 82 | std::unique_ptr<FormattingContext> createFormattingContext(const Box& formattingContextRoot); |
| 83 | #ifndef NDEBUG |
| 84 | void registerFormattingContext(const FormattingContext&); |
| 85 | void deregisterFormattingContext(const FormattingContext& formattingContext) { m_formattingContextList.remove(&formattingContext); } |
| 86 | #endif |
| 87 | |
| 88 | Display::Box& displayBoxForLayoutBox(const Box& layoutBox) const; |
| 89 | bool hasDisplayBox(const Box& layoutBox) const { return m_layoutToDisplayBox.contains(&layoutBox); } |
| 90 | |
| 91 | bool inQuirksMode() const { return m_inQuirksMode; } |
| 92 | // For testing purposes only |
| 93 | void verifyAndOutputMismatchingLayoutTree(const RenderView&) const; |
| 94 | |
| 95 | private: |
| 96 | const Container& initialContainingBlock() const { return *m_initialContainingBlock; } |
| 97 | void layoutFormattingContextSubtree(const Box&); |
| 98 | |
| 99 | WeakPtr<const Container> m_initialContainingBlock; |
| 100 | HashSet<const Container*> m_formattingContextRootListForLayout; |
| 101 | HashMap<const Box*, std::unique_ptr<FormattingState>> m_formattingStates; |
| 102 | #ifndef NDEBUG |
| 103 | HashSet<const FormattingContext*> m_formattingContextList; |
| 104 | #endif |
| 105 | mutable HashMap<const Box*, std::unique_ptr<Display::Box>> m_layoutToDisplayBox; |
| 106 | bool m_inQuirksMode { false }; |
| 107 | }; |
| 108 | |
| 109 | #ifndef NDEBUG |
| 110 | inline void LayoutState::registerFormattingContext(const FormattingContext& formattingContext) |
| 111 | { |
| 112 | // Multiple formatting contexts of the same root within a layout frame indicates defective layout logic. |
| 113 | ASSERT(!m_formattingContextList.contains(&formattingContext)); |
| 114 | m_formattingContextList.add(&formattingContext); |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | |