| 1 | /* |
| 2 | * Copyright (C) 2014-2017 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 "ScrollingStateTree.h" |
| 31 | #include "ScrollingTree.h" |
| 32 | #include <wtf/Condition.h> |
| 33 | #include <wtf/RefPtr.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | class AsyncScrollingCoordinator; |
| 38 | |
| 39 | // The ThreadedScrollingTree class lives almost exclusively on the scrolling thread and manages the |
| 40 | // hierarchy of scrollable regions on the page. It's also responsible for dispatching events |
| 41 | // to the correct scrolling tree nodes or dispatching events back to the ScrollingCoordinator |
| 42 | // object on the main thread if they can't be handled on the scrolling thread for various reasons. |
| 43 | class ThreadedScrollingTree : public ScrollingTree { |
| 44 | public: |
| 45 | virtual ~ThreadedScrollingTree(); |
| 46 | |
| 47 | void commitTreeState(std::unique_ptr<ScrollingStateTree>) override; |
| 48 | |
| 49 | ScrollingEventResult handleWheelEvent(const PlatformWheelEvent&) override; |
| 50 | |
| 51 | // Can be called from any thread. Will try to handle the wheel event on the scrolling thread. |
| 52 | // Returns true if the wheel event can be handled on the scrolling thread and false if the |
| 53 | // event must be sent again to the WebCore event handler. |
| 54 | ScrollingEventResult tryToHandleWheelEvent(const PlatformWheelEvent&) override; |
| 55 | |
| 56 | void invalidate() override; |
| 57 | |
| 58 | void incrementPendingCommitCount(); |
| 59 | void decrementPendingCommitCount(); |
| 60 | |
| 61 | protected: |
| 62 | explicit ThreadedScrollingTree(AsyncScrollingCoordinator&); |
| 63 | |
| 64 | void scrollingTreeNodeDidScroll(ScrollingTreeScrollingNode&, ScrollingLayerPositionAction = ScrollingLayerPositionAction::Sync) override; |
| 65 | #if PLATFORM(MAC) |
| 66 | void handleWheelEventPhase(PlatformWheelEventPhase) override; |
| 67 | void setActiveScrollSnapIndices(ScrollingNodeID, unsigned horizontalIndex, unsigned verticalIndex) override; |
| 68 | void deferTestsForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) override; |
| 69 | void removeTestDeferralForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) override; |
| 70 | #endif |
| 71 | |
| 72 | #if PLATFORM(COCOA) |
| 73 | void currentSnapPointIndicesDidChange(ScrollingNodeID, unsigned horizontal, unsigned vertical) override; |
| 74 | #endif |
| 75 | |
| 76 | void reportExposedUnfilledArea(MonotonicTime, unsigned unfilledArea) override; |
| 77 | void reportSynchronousScrollingReasonsChanged(MonotonicTime, SynchronousScrollingReasons) override; |
| 78 | |
| 79 | private: |
| 80 | bool isThreadedScrollingTree() const override { return true; } |
| 81 | void applyLayerPositions() override; |
| 82 | |
| 83 | RefPtr<AsyncScrollingCoordinator> m_scrollingCoordinator; |
| 84 | |
| 85 | void waitForPendingCommits(); |
| 86 | |
| 87 | Lock m_pendingCommitCountMutex; |
| 88 | unsigned m_pendingCommitCount { 0 }; |
| 89 | Condition m_commitCondition; |
| 90 | }; |
| 91 | |
| 92 | } // namespace WebCore |
| 93 | |
| 94 | SPECIALIZE_TYPE_TRAITS_SCROLLING_TREE(WebCore::ThreadedScrollingTree, isThreadedScrollingTree()) |
| 95 | |
| 96 | #endif // ENABLE(ASYNC_SCROLLING) |
| 97 | |