Add animationDidEnd callbacks on GraphicsLayer
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / mac / RemoteLayerTreeContext.mm
1 /*
2  * Copyright (C) 2012 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 #import "config.h"
27 #import "RemoteLayerTreeContext.h"
28
29 #import "GenericCallback.h"
30 #import "GraphicsLayerCARemote.h"
31 #import "PlatformCALayerRemote.h"
32 #import "RemoteLayerBackingStoreCollection.h"
33 #import "RemoteLayerTreeTransaction.h"
34 #import "WebPage.h"
35 #import <WebCore/FrameView.h>
36 #import <WebCore/MainFrame.h>
37 #import <WebCore/Page.h>
38 #import <wtf/TemporaryChange.h>
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 RemoteLayerTreeContext::RemoteLayerTreeContext(WebPage& webPage)
45     : m_webPage(webPage)
46     , m_currentTransaction(nullptr)
47 {
48 }
49
50 RemoteLayerTreeContext::~RemoteLayerTreeContext()
51 {
52     for (auto& layer : m_liveLayers.values())
53         layer->clearContext();
54 }
55
56 void RemoteLayerTreeContext::layerWasCreated(PlatformCALayerRemote& layer, PlatformCALayer::LayerType type)
57 {
58     GraphicsLayer::PlatformLayerID layerID = layer.layerID();
59
60     RemoteLayerTreeTransaction::LayerCreationProperties creationProperties;
61     creationProperties.layerID = layerID;
62     creationProperties.type = type;
63
64     if (layer.isPlatformCALayerRemoteCustom())
65         creationProperties.hostingContextID = layer.hostingContextID();
66
67     m_createdLayers.append(creationProperties);
68     m_liveLayers.add(layerID, &layer);
69 }
70
71 void RemoteLayerTreeContext::layerWillBeDestroyed(PlatformCALayerRemote& layer)
72 {
73     ASSERT(layer.layerID());
74     GraphicsLayer::PlatformLayerID layerID = layer.layerID();
75
76     m_liveLayers.remove(layerID);
77
78     ASSERT(!m_destroyedLayers.contains(layerID));
79     m_destroyedLayers.append(layerID);
80     
81     m_layersWithAnimations.remove(layerID);
82 }
83
84 void RemoteLayerTreeContext::backingStoreWasCreated(RemoteLayerBackingStore& backingStore)
85 {
86     m_backingStoreCollection.backingStoreWasCreated(backingStore);
87 }
88
89 void RemoteLayerTreeContext::backingStoreWillBeDestroyed(RemoteLayerBackingStore& backingStore)
90 {
91     m_backingStoreCollection.backingStoreWillBeDestroyed(backingStore);
92 }
93
94 void RemoteLayerTreeContext::backingStoreWillBeDisplayed(RemoteLayerBackingStore& backingStore)
95 {
96     m_backingStoreCollection.backingStoreWillBeDisplayed(backingStore);
97 }
98
99 std::unique_ptr<GraphicsLayer> RemoteLayerTreeContext::createGraphicsLayer(GraphicsLayerClient& client)
100 {
101     return std::make_unique<GraphicsLayerCARemote>(client, *this);
102 }
103
104 void RemoteLayerTreeContext::buildTransaction(RemoteLayerTreeTransaction& transaction, PlatformCALayer& rootLayer)
105 {
106     PlatformCALayerRemote& rootLayerRemote = toPlatformCALayerRemote(rootLayer);
107     transaction.setRootLayerID(rootLayerRemote.layerID());
108
109     m_currentTransaction = &transaction;
110     rootLayerRemote.recursiveBuildTransaction(*this, transaction);
111     m_currentTransaction = nullptr;
112
113     transaction.setCreatedLayers(WTF::move(m_createdLayers));
114     transaction.setDestroyedLayerIDs(WTF::move(m_destroyedLayers));
115 }
116
117 void RemoteLayerTreeContext::layerPropertyChangedWhileBuildingTransaction(PlatformCALayerRemote& layer)
118 {
119     if (m_currentTransaction)
120         m_currentTransaction->layerPropertiesChanged(layer);
121 }
122
123 void RemoteLayerTreeContext::willStartAnimationOnLayer(PlatformCALayerRemote& layer)
124 {
125     m_layersWithAnimations.add(layer.layerID(), &layer);
126 }
127
128 void RemoteLayerTreeContext::animationDidStart(WebCore::GraphicsLayer::PlatformLayerID layerID, const String& key, double startTime)
129 {
130     auto it = m_layersWithAnimations.find(layerID);
131     if (it != m_layersWithAnimations.end())
132         it->value->animationStarted(key, startTime);
133 }
134
135 void RemoteLayerTreeContext::animationDidEnd(WebCore::GraphicsLayer::PlatformLayerID layerID, const String& key)
136 {
137     auto it = m_layersWithAnimations.find(layerID);
138     if (it != m_layersWithAnimations.end())
139         it->value->animationEnded(key);
140 }
141
142 } // namespace WebKit