Remote Layer Tree: Apply layer changes to LayerTypeCustom layers to the custom layer...
[WebKit-https.git] / Source / WebKit2 / UIProcess / mac / RemoteLayerTreeHost.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 "RemoteLayerTreeHost.h"
28
29 #import "RemoteLayerTreeHostMessages.h"
30 #import "RemoteLayerTreePropertyApplier.h"
31 #import "RemoteLayerTreeTransaction.h"
32 #import "ShareableBitmap.h"
33 #import "WebPageProxy.h"
34 #import "WebProcessProxy.h"
35 #import <WebCore/PlatformLayer.h>
36 #import <WebKitSystemInterface.h>
37
38 #import <QuartzCore/QuartzCore.h>
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 RemoteLayerTreeHost::RemoteLayerTreeHost(WebPageProxy* webPageProxy)
45     : m_webPageProxy(webPageProxy)
46     , m_rootLayer(nullptr)
47 {
48     m_webPageProxy->process()->addMessageReceiver(Messages::RemoteLayerTreeHost::messageReceiverName(), m_webPageProxy->pageID(), this);
49 }
50
51 RemoteLayerTreeHost::~RemoteLayerTreeHost()
52 {
53     m_webPageProxy->process()->removeMessageReceiver(Messages::RemoteLayerTreeHost::messageReceiverName(), m_webPageProxy->pageID());
54 }
55
56 void RemoteLayerTreeHost::commit(const RemoteLayerTreeTransaction& transaction)
57 {
58 #if !defined(NDEBUG)
59     transaction.dump();
60 #endif
61
62     for (auto createdLayer : transaction.createdLayers())
63         createLayer(createdLayer);
64
65     CALayer *rootLayer = getLayer(transaction.rootLayerID());
66     if (m_rootLayer != rootLayer) {
67         m_rootLayer = rootLayer;
68         m_webPageProxy->setAcceleratedCompositingRootLayer(m_rootLayer);
69     }
70
71     for (auto changedLayer : transaction.changedLayers()) {
72         RemoteLayerTreeTransaction::LayerID layerID = changedLayer.key;
73         const auto& properties = changedLayer.value;
74
75         CALayer *layer = getLayer(layerID);
76         ASSERT(layer);
77
78         RemoteLayerTreePropertyApplier::RelatedLayerMap relatedLayers;
79         if (properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
80             for (auto child : properties.children)
81                 relatedLayers.set(child, getLayer(child));
82         }
83
84         if (properties.changedProperties & RemoteLayerTreeTransaction::MaskLayerChanged)
85             relatedLayers.set(properties.maskLayer, getLayer(properties.maskLayer));
86
87         RemoteLayerTreePropertyApplier::applyPropertiesToLayer(layer, properties, relatedLayers);
88     }
89
90     for (auto destroyedLayer : transaction.destroyedLayers())
91         m_layers.remove(destroyedLayer);
92 }
93
94 CALayer *RemoteLayerTreeHost::getLayer(RemoteLayerTreeTransaction::LayerID layerID)
95 {
96     return m_layers.get(layerID).get();
97 }
98
99 CALayer *RemoteLayerTreeHost::createLayer(RemoteLayerTreeTransaction::LayerCreationProperties properties)
100 {
101     RetainPtr<CALayer>& layer = m_layers.add(properties.layerID, nullptr).iterator->value;
102
103     ASSERT(!layer);
104
105     switch (properties.type) {
106     case PlatformCALayer::LayerTypeLayer:
107     case PlatformCALayer::LayerTypeWebLayer:
108     case PlatformCALayer::LayerTypeRootLayer:
109     case PlatformCALayer::LayerTypeSimpleLayer:
110     case PlatformCALayer::LayerTypeTiledBackingLayer:
111     case PlatformCALayer::LayerTypePageTiledBackingLayer:
112     case PlatformCALayer::LayerTypeTiledBackingTileLayer:
113         layer = adoptNS([[CALayer alloc] init]);
114         break;
115     case PlatformCALayer::LayerTypeTransformLayer:
116         layer = adoptNS([[CATransformLayer alloc] init]);
117         break;
118     case PlatformCALayer::LayerTypeCustom:
119         layer = WKMakeRenderLayer(properties.hostingContextID);
120         break;
121     default:
122         ASSERT_NOT_REACHED();
123     }
124
125     RemoteLayerTreePropertyApplier::disableActionsForLayer(layer.get());
126
127     return layer.get();
128 }
129
130 } // namespace WebKit