| 1 | /* |
| 2 | * Copyright (C) 2012-2015 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(ASYNC_SCROLLING) |
| 29 | |
| 30 | #include "PlatformWheelEvent.h" |
| 31 | #include "Region.h" |
| 32 | #include "ScrollingCoordinator.h" |
| 33 | #include "TouchAction.h" |
| 34 | #include "WheelEventTestTrigger.h" |
| 35 | #include <wtf/HashMap.h> |
| 36 | #include <wtf/Lock.h> |
| 37 | #include <wtf/ThreadSafeRefCounted.h> |
| 38 | #include <wtf/TypeCasts.h> |
| 39 | |
| 40 | namespace WebCore { |
| 41 | |
| 42 | class IntPoint; |
| 43 | class ScrollingStateTree; |
| 44 | class ScrollingStateNode; |
| 45 | class ScrollingTreeFrameScrollingNode; |
| 46 | class ScrollingTreeNode; |
| 47 | class ScrollingTreeScrollingNode; |
| 48 | |
| 49 | class ScrollingTree : public ThreadSafeRefCounted<ScrollingTree> { |
| 50 | public: |
| 51 | WEBCORE_EXPORT ScrollingTree(); |
| 52 | WEBCORE_EXPORT virtual ~ScrollingTree(); |
| 53 | |
| 54 | virtual bool isThreadedScrollingTree() const { return false; } |
| 55 | virtual bool isRemoteScrollingTree() const { return false; } |
| 56 | virtual bool isScrollingTreeIOS() const { return false; } |
| 57 | |
| 58 | // This implies that we'll do hit-testing in the scrolling tree. |
| 59 | bool asyncFrameOrOverflowScrollingEnabled() const { return m_asyncFrameOrOverflowScrollingEnabled; } |
| 60 | void setAsyncFrameOrOverflowScrollingEnabled(bool); |
| 61 | |
| 62 | virtual ScrollingEventResult tryToHandleWheelEvent(const PlatformWheelEvent&) = 0; |
| 63 | WEBCORE_EXPORT bool shouldHandleWheelEventSynchronously(const PlatformWheelEvent&); |
| 64 | |
| 65 | void setMainFrameIsRubberBanding(bool); |
| 66 | bool isRubberBandInProgress(); |
| 67 | void setMainFrameIsScrollSnapping(bool); |
| 68 | bool isScrollSnapInProgress(); |
| 69 | |
| 70 | virtual void invalidate() { } |
| 71 | WEBCORE_EXPORT virtual void commitTreeState(std::unique_ptr<ScrollingStateTree>); |
| 72 | |
| 73 | WEBCORE_EXPORT virtual void applyLayerPositions(); |
| 74 | |
| 75 | virtual Ref<ScrollingTreeNode> createScrollingTreeNode(ScrollingNodeType, ScrollingNodeID) = 0; |
| 76 | |
| 77 | WEBCORE_EXPORT ScrollingTreeNode* nodeForID(ScrollingNodeID) const; |
| 78 | |
| 79 | // Called after a scrolling tree node has handled a scroll and updated its layers. |
| 80 | // Updates FrameView/RenderLayer scrolling state and GraphicsLayers. |
| 81 | virtual void scrollingTreeNodeDidScroll(ScrollingTreeScrollingNode&, ScrollingLayerPositionAction = ScrollingLayerPositionAction::Sync) = 0; |
| 82 | |
| 83 | // Called for requested scroll position updates. |
| 84 | virtual void scrollingTreeNodeRequestsScroll(ScrollingNodeID, const FloatPoint& /*scrollPosition*/, bool /*representsProgrammaticScroll*/) { } |
| 85 | |
| 86 | // Delegated scrolling/zooming has caused the viewport to change, so update viewport-constrained layers |
| 87 | // (but don't cause scroll events to be fired). |
| 88 | WEBCORE_EXPORT virtual void mainFrameViewportChangedViaDelegatedScrolling(const FloatPoint& scrollPosition, const WebCore::FloatRect& layoutViewport, double scale); |
| 89 | |
| 90 | void notifyRelatedNodesAfterScrollPositionChange(ScrollingTreeScrollingNode& changedNode); |
| 91 | |
| 92 | virtual void reportSynchronousScrollingReasonsChanged(MonotonicTime, SynchronousScrollingReasons) { } |
| 93 | virtual void reportExposedUnfilledArea(MonotonicTime, unsigned /* unfilledArea */) { } |
| 94 | |
| 95 | #if PLATFORM(IOS_FAMILY) |
| 96 | virtual void scrollingTreeNodeWillStartPanGesture() { } |
| 97 | virtual void scrollingTreeNodeWillStartScroll() { } |
| 98 | virtual void scrollingTreeNodeDidEndScroll() { } |
| 99 | #endif |
| 100 | |
| 101 | WEBCORE_EXPORT TrackingType eventTrackingTypeForPoint(const AtomicString& eventName, IntPoint); |
| 102 | #if ENABLE(POINTER_EVENTS) |
| 103 | WEBCORE_EXPORT Optional<TouchActionData> touchActionDataAtPoint(IntPoint) const; |
| 104 | #endif |
| 105 | |
| 106 | #if PLATFORM(MAC) |
| 107 | virtual void handleWheelEventPhase(PlatformWheelEventPhase) = 0; |
| 108 | virtual void setActiveScrollSnapIndices(ScrollingNodeID, unsigned /*horizontalIndex*/, unsigned /*verticalIndex*/) { } |
| 109 | virtual void deferTestsForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) { } |
| 110 | virtual void removeTestDeferralForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) { } |
| 111 | #endif |
| 112 | |
| 113 | #if PLATFORM(COCOA) |
| 114 | WEBCORE_EXPORT virtual void currentSnapPointIndicesDidChange(ScrollingNodeID, unsigned horizontal, unsigned vertical) = 0; |
| 115 | #endif |
| 116 | |
| 117 | void setMainFramePinState(bool pinnedToTheLeft, bool pinnedToTheRight, bool pinnedToTheTop, bool pinnedToTheBottom); |
| 118 | |
| 119 | // Can be called from any thread. Will update what edges allow rubber-banding. |
| 120 | WEBCORE_EXPORT void setCanRubberBandState(bool canRubberBandAtLeft, bool canRubberBandAtRight, bool canRubberBandAtTop, bool canRubberBandAtBottom); |
| 121 | |
| 122 | bool isHandlingProgrammaticScroll() const { return m_isHandlingProgrammaticScroll; } |
| 123 | void setIsHandlingProgrammaticScroll(bool isHandlingProgrammaticScroll) { m_isHandlingProgrammaticScroll = isHandlingProgrammaticScroll; } |
| 124 | |
| 125 | void setScrollPinningBehavior(ScrollPinningBehavior); |
| 126 | WEBCORE_EXPORT ScrollPinningBehavior scrollPinningBehavior(); |
| 127 | |
| 128 | WEBCORE_EXPORT bool willWheelEventStartSwipeGesture(const PlatformWheelEvent&); |
| 129 | |
| 130 | WEBCORE_EXPORT void setScrollingPerformanceLoggingEnabled(bool flag); |
| 131 | bool scrollingPerformanceLoggingEnabled(); |
| 132 | |
| 133 | ScrollingTreeFrameScrollingNode* rootNode() const { return m_rootNode.get(); } |
| 134 | |
| 135 | ScrollingNodeID latchedNode(); |
| 136 | void setLatchedNode(ScrollingNodeID); |
| 137 | void clearLatchedNode(); |
| 138 | |
| 139 | bool hasLatchedNode() const { return m_treeState.latchedNodeID; } |
| 140 | void setOrClearLatchedNode(const PlatformWheelEvent&, ScrollingNodeID); |
| 141 | |
| 142 | bool hasFixedOrSticky() const { return !!m_fixedOrStickyNodeCount; } |
| 143 | void fixedOrStickyNodeAdded() { ++m_fixedOrStickyNodeCount; } |
| 144 | void fixedOrStickyNodeRemoved() |
| 145 | { |
| 146 | ASSERT(m_fixedOrStickyNodeCount); |
| 147 | --m_fixedOrStickyNodeCount; |
| 148 | } |
| 149 | |
| 150 | // A map of overflow scrolling nodes to positioned nodes which need to be updated |
| 151 | // when the scroller changes, but are not descendants. |
| 152 | using RelatedNodesMap = HashMap<ScrollingNodeID, Vector<ScrollingNodeID>>; |
| 153 | RelatedNodesMap& overflowRelatedNodes() { return m_overflowRelatedNodesMap; } |
| 154 | |
| 155 | HashSet<ScrollingNodeID>& positionedNodesWithRelatedOverflow() { return m_positionedNodesWithRelatedOverflow; } |
| 156 | |
| 157 | WEBCORE_EXPORT String scrollingTreeAsText(ScrollingStateTreeAsTextBehavior = ScrollingStateTreeAsTextBehaviorNormal); |
| 158 | |
| 159 | protected: |
| 160 | void setMainFrameScrollPosition(FloatPoint); |
| 161 | |
| 162 | WEBCORE_EXPORT virtual ScrollingEventResult handleWheelEvent(const PlatformWheelEvent&); |
| 163 | |
| 164 | private: |
| 165 | using OrphanScrollingNodeMap = HashMap<ScrollingNodeID, RefPtr<ScrollingTreeNode>>; |
| 166 | void updateTreeFromStateNode(const ScrollingStateNode*, OrphanScrollingNodeMap&, HashSet<ScrollingNodeID>& unvisitedNodes); |
| 167 | |
| 168 | void applyLayerPositionsRecursive(ScrollingTreeNode&, FloatRect layoutViewport, FloatSize cumulativeDelta); |
| 169 | |
| 170 | void notifyRelatedNodesRecursive(ScrollingTreeScrollingNode& changedNode, ScrollingTreeNode& currNode, const FloatRect& layoutViewport, FloatSize cumulativeDelta); |
| 171 | |
| 172 | Lock m_treeMutex; // Protects the scrolling tree. |
| 173 | |
| 174 | RefPtr<ScrollingTreeFrameScrollingNode> m_rootNode; |
| 175 | |
| 176 | using ScrollingTreeNodeMap = HashMap<ScrollingNodeID, ScrollingTreeNode*>; |
| 177 | ScrollingTreeNodeMap m_nodeMap; |
| 178 | |
| 179 | RelatedNodesMap m_overflowRelatedNodesMap; |
| 180 | HashSet<ScrollingNodeID> m_positionedNodesWithRelatedOverflow; |
| 181 | |
| 182 | struct TreeState { |
| 183 | ScrollingNodeID latchedNodeID { 0 }; |
| 184 | EventTrackingRegions eventTrackingRegions; |
| 185 | FloatPoint mainFrameScrollPosition; |
| 186 | bool mainFrameIsRubberBanding { false }; |
| 187 | bool mainFrameIsScrollSnapping { false }; |
| 188 | }; |
| 189 | |
| 190 | Lock m_treeStateMutex; |
| 191 | TreeState m_treeState; |
| 192 | |
| 193 | struct SwipeState { |
| 194 | ScrollPinningBehavior scrollPinningBehavior { DoNotPin }; |
| 195 | bool rubberBandsAtLeft { true }; |
| 196 | bool rubberBandsAtRight { true }; |
| 197 | bool rubberBandsAtTop { true }; |
| 198 | bool rubberBandsAtBottom { true }; |
| 199 | bool mainFramePinnedToTheLeft { true }; |
| 200 | bool mainFramePinnedToTheRight { true }; |
| 201 | bool mainFramePinnedToTheTop { true }; |
| 202 | bool mainFramePinnedToTheBottom { true }; |
| 203 | }; |
| 204 | |
| 205 | Lock m_swipeStateMutex; |
| 206 | SwipeState m_swipeState; |
| 207 | |
| 208 | unsigned m_fixedOrStickyNodeCount { 0 }; |
| 209 | bool m_isHandlingProgrammaticScroll { false }; |
| 210 | bool m_scrollingPerformanceLoggingEnabled { false }; |
| 211 | bool m_asyncFrameOrOverflowScrollingEnabled { false }; |
| 212 | }; |
| 213 | |
| 214 | } // namespace WebCore |
| 215 | |
| 216 | #define SPECIALIZE_TYPE_TRAITS_SCROLLING_TREE(ToValueTypeName, predicate) \ |
| 217 | SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \ |
| 218 | static bool isType(const WebCore::ScrollingTree& tree) { return tree.predicate; } \ |
| 219 | SPECIALIZE_TYPE_TRAITS_END() |
| 220 | #endif // ENABLE(ASYNC_SCROLLING) |
| 221 | |