File: RemoteProgressBasedTimelineRegistry.cpp

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (184 lines) | stat: -rw-r--r-- 8,375 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "config.h"
#import "RemoteProgressBasedTimelineRegistry.h"

#if ENABLE(THREADED_ANIMATIONS)

#import "RemoteScrollingTree.h"
#import <WebCore/ScrollingTreeScrollingNode.h>
#import <wtf/TZoneMallocInlines.h>

namespace WebKit {

WTF_MAKE_TZONE_ALLOCATED_IMPL(RemoteProgressBasedTimelineRegistry);

void RemoteProgressBasedTimelineRegistry::update(const RemoteScrollingTree& scrollingTree, WebCore::ProcessIdentifier processIdentifier, const WebCore::AcceleratedTimelinesUpdate& timelinesUpdate)
{
    auto& processTimelines = m_timelines.ensure(processIdentifier, [] {
        return UncheckedKeyHashMap<WebCore::ScrollingNodeID, HashSet<Ref<RemoteProgressBasedTimeline>>>();
    }).iterator->value;

    using IterationCallback = const Function<void(const TimelineID&, WebCore::ProgressResolutionData)>;
    auto forEachProgressBasedTimelineRepresentation = [&](const HashSet<Ref<WebCore::AcceleratedTimeline>>& timelineRepresentations, NOESCAPE IterationCallback& function) {
        if (timelineRepresentations.isEmpty())
            return;
        for (auto& timelineRepresentation : timelineRepresentations) {
            if (!timelineRepresentation->isProgressBased())
                continue;
            ASSERT(timelineRepresentation->progressResolutionData());
            TimelineID timelineID { timelineRepresentation->identifier(), processIdentifier };
            function(timelineID, *timelineRepresentation->progressResolutionData());
        }
    };

    forEachProgressBasedTimelineRepresentation(timelinesUpdate.created, [&](auto timelineID, auto resolutionData) {
        auto& sourceTimelines = processTimelines.ensure(resolutionData.source, [] {
            return HashSet<Ref<RemoteProgressBasedTimeline>>();
        }).iterator->value;
        // There should not be a pre-existing timeline for this identifier since we're creating it.
        ASSERT([&] {
            for (auto& existingTimeline : sourceTimelines) {
                if (existingTimeline->identifier() == timelineID)
                    return false;
            }
            return true;
        }());
        sourceTimelines.add(RemoteProgressBasedTimeline::create(timelineID, resolutionData));
    });

    forEachProgressBasedTimelineRepresentation(timelinesUpdate.modified, [&](auto timelineID, auto resolutionData) {
        // FIXME: we should make it so that when we call this function we're
        // guaranteed to have a matching source node and turn this into a Ref.
        RefPtr sourceNode = dynamicDowncast<WebCore::ScrollingTreeScrollingNode>(Ref { scrollingTree }->nodeForID(resolutionData.source));

        auto sourceIterator = processTimelines.find(resolutionData.source);
        if (sourceIterator != processTimelines.end()) {
            auto& existingTimelines = sourceIterator->value;
            for (auto& existingTimeline : existingTimelines) {
                if (existingTimeline->identifier() == timelineID) {
                    existingTimeline->setResolutionData(sourceNode.get(), resolutionData);
                    return;
                }
            }
        }

        // If we didn't find the timeline within the existing timelines for this source,
        // it may previously have been associated with another source.
        for (auto& [source, existingTimelines] : processTimelines) {
            // We've already looked at the exisiting source.
            if (source == resolutionData.source)
                continue;
            auto matchingTimelines = existingTimelines.takeIf([&](const auto& existingTimeline) {
                return existingTimeline->identifier() == timelineID;
            });
            if (matchingTimelines.isEmpty())
                continue;
            ASSERT(matchingTimelines.size() == 1);
            auto& existingTimeline = matchingTimelines[0];
            existingTimeline->setResolutionData(sourceNode.get(), resolutionData);
            // Move it to the right source.
            processTimelines.ensure(existingTimeline->source(), [] {
                return HashSet<Ref<RemoteProgressBasedTimeline>>();
            }).iterator->value.add(existingTimeline.releaseNonNull());
            return;
        }

        // If we reach this point, we've been told to update a timeline that did not exist.
        ASSERT_NOT_REACHED();
    });

    HashSet<WebCore::ScrollingNodeID> emptySources;
    for (auto& destroyedTimelineIdentifier : timelinesUpdate.destroyed) {
        TimelineID destroyedTimelineID { destroyedTimelineIdentifier, processIdentifier };
        for (auto& [source, existingTimelines] : processTimelines) {
            existingTimelines.removeIf([destroyedTimelineID](auto& existingTimeline) {
                return existingTimeline->identifier() == destroyedTimelineID;
            });
            if (existingTimelines.isEmpty())
                emptySources.add(source);
        }
    }

    for (auto& emptySource : emptySources)
        processTimelines.remove(emptySource);

    if (processTimelines.isEmpty())
        m_timelines.remove(processIdentifier);
}

RemoteProgressBasedTimeline* RemoteProgressBasedTimelineRegistry::get(const TimelineID& timelineID) const
{
    auto it = m_timelines.find(timelineID.processIdentifier());
    if (it == m_timelines.end())
        return nullptr;

    for (auto& sourceTimelines : it->value.values()) {
        for (auto& timeline : sourceTimelines) {
            if (timeline->identifier() == timelineID)
                return timeline.ptr();
        }
    }

    return nullptr;
}

bool RemoteProgressBasedTimelineRegistry::hasTimelineForNode(const WebCore::ScrollingTreeScrollingNode& node) const
{
    auto scrollingNodeID = node.scrollingNodeID();
    auto it = m_timelines.find(scrollingNodeID.processIdentifier());
    return it != m_timelines.end() && it->value.contains(scrollingNodeID);
}

void RemoteProgressBasedTimelineRegistry::updateTimelinesForNode(const WebCore::ScrollingTreeScrollingNode& node)
{
    auto processIterator = m_timelines.find(node.scrollingNodeID().processIdentifier());
    if (processIterator == m_timelines.end())
        return;

    auto& processTimelines = processIterator->value;
    auto sourceIterator = processTimelines.find(node.scrollingNodeID());
    if (sourceIterator != processTimelines.end()) {
        for (auto& timeline : sourceIterator->value)
            timeline->updateCurrentTime(node);
    }
}

HashSet<Ref<RemoteProgressBasedTimeline>> RemoteProgressBasedTimelineRegistry::timelinesForScrollingNodeIDForTesting(WebCore::ScrollingNodeID scrollingNodeID) const
{
    auto processIterator = m_timelines.find(scrollingNodeID.processIdentifier());
    if (processIterator == m_timelines.end())
        return { };
    auto& processTimelines = processIterator->value;
    auto sourceIterator = processTimelines.find(scrollingNodeID);
    if (sourceIterator == processTimelines.end())
        return { };
    return sourceIterator->value;
}

} // namespace WebKit

#endif // ENABLE(THREADED_ANIMATIONS)