Remote Layer Tree: Apply layer changes to LayerTypeCustom layers to the custom layer...
[WebKit-https.git] / Source / WebKit2 / Shared / mac / RemoteLayerTreePropertyApplier.mm
1 /*
2  * Copyright (C) 2013 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 "RemoteLayerTreePropertyApplier.h"
28
29 #import "PlatformCALayerRemote.h"
30 #import <WebCore/PlatformCAFilters.h>
31
32 using namespace WebCore;
33
34 namespace WebKit {
35
36 static RetainPtr<CGColorRef> cgColorFromColor(Color color)
37 {
38     CGFloat components[4];
39     color.getRGBA(components[0], components[1], components[2], components[3]);
40
41     RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
42     return adoptCF(CGColorCreate(colorSpace.get(), components));
43 }
44
45 static NSString *toCAFilterType(PlatformCALayer::FilterType type)
46 {
47     switch (type) {
48     case PlatformCALayer::Linear:
49         return kCAFilterLinear;
50     case PlatformCALayer::Nearest:
51         return kCAFilterNearest;
52     case PlatformCALayer::Trilinear:
53         return kCAFilterTrilinear;
54     };
55     
56     ASSERT_NOT_REACHED();
57     return 0;
58 }
59
60 void RemoteLayerTreePropertyApplier::applyPropertiesToLayer(CALayer *layer, RemoteLayerTreeTransaction::LayerProperties properties, RelatedLayerMap relatedLayers)
61 {
62     if (properties.changedProperties & RemoteLayerTreeTransaction::NameChanged)
63         layer.name = properties.name;
64
65     if (properties.changedProperties & RemoteLayerTreeTransaction::PositionChanged) {
66         layer.position = CGPointMake(properties.position.x(), properties.position.y());
67         layer.zPosition = properties.position.z();
68     }
69
70     if (properties.changedProperties & RemoteLayerTreeTransaction::AnchorPointChanged) {
71         layer.anchorPoint = CGPointMake(properties.anchorPoint.x(), properties.anchorPoint.y());
72         layer.anchorPointZ = properties.anchorPoint.z();
73     }
74
75     if (properties.changedProperties & RemoteLayerTreeTransaction::SizeChanged)
76         layer.bounds = FloatRect(FloatPoint(), properties.size);
77
78     if (properties.changedProperties & RemoteLayerTreeTransaction::BackgroundColorChanged)
79         layer.backgroundColor = cgColorFromColor(properties.backgroundColor).get();
80
81     if (properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
82         RetainPtr<NSMutableArray> children = adoptNS([[NSMutableArray alloc] initWithCapacity:properties.children.size()]);
83         for (auto child : properties.children)
84             [children addObject:relatedLayers.get(child)];
85         layer.sublayers = children.get();
86     }
87
88     if (properties.changedProperties & RemoteLayerTreeTransaction::BorderColorChanged)
89         layer.borderColor = cgColorFromColor(properties.borderColor).get();
90
91     if (properties.changedProperties & RemoteLayerTreeTransaction::BorderWidthChanged)
92         layer.borderWidth = properties.borderWidth;
93
94     if (properties.changedProperties & RemoteLayerTreeTransaction::OpacityChanged)
95         layer.opacity = properties.opacity;
96
97     if (properties.changedProperties & RemoteLayerTreeTransaction::TransformChanged)
98         layer.transform = properties.transform;
99
100     if (properties.changedProperties & RemoteLayerTreeTransaction::SublayerTransformChanged)
101         layer.sublayerTransform = properties.sublayerTransform;
102
103     if (properties.changedProperties & RemoteLayerTreeTransaction::HiddenChanged)
104         layer.hidden = properties.hidden;
105
106     if (properties.changedProperties & RemoteLayerTreeTransaction::GeometryFlippedChanged)
107         layer.geometryFlipped = properties.geometryFlipped;
108
109     if (properties.changedProperties & RemoteLayerTreeTransaction::DoubleSidedChanged)
110         layer.doubleSided = properties.doubleSided;
111
112     if (properties.changedProperties & RemoteLayerTreeTransaction::MasksToBoundsChanged)
113         layer.masksToBounds = properties.masksToBounds;
114
115     if (properties.changedProperties & RemoteLayerTreeTransaction::OpaqueChanged)
116         layer.opaque = properties.opaque;
117
118     if (properties.changedProperties & RemoteLayerTreeTransaction::MaskLayerChanged) {
119         CALayer *maskLayer = relatedLayers.get(properties.maskLayer);
120         ASSERT(!maskLayer.superlayer);
121         if (!maskLayer.superlayer)
122             layer.mask = maskLayer;
123     }
124
125     if (properties.changedProperties & RemoteLayerTreeTransaction::ContentsRectChanged)
126         layer.contentsRect = properties.contentsRect;
127
128     if (properties.changedProperties & RemoteLayerTreeTransaction::ContentsScaleChanged)
129         layer.contentsScale = properties.contentsScale;
130
131     if (properties.changedProperties & RemoteLayerTreeTransaction::MinificationFilterChanged)
132         layer.minificationFilter = toCAFilterType(properties.minificationFilter);
133
134     if (properties.changedProperties & RemoteLayerTreeTransaction::MagnificationFilterChanged)
135         layer.magnificationFilter = toCAFilterType(properties.magnificationFilter);
136
137     if (properties.changedProperties & RemoteLayerTreeTransaction::SpeedChanged)
138         layer.speed = properties.speed;
139
140     if (properties.changedProperties & RemoteLayerTreeTransaction::TimeOffsetChanged)
141         layer.timeOffset = properties.timeOffset;
142
143     if (properties.changedProperties & RemoteLayerTreeTransaction::BackingStoreChanged) {
144         if (properties.backingStore.acceleratesDrawing())
145             layer.contents = (id)properties.backingStore.surface().get();
146         else
147             layer.contents = (id)properties.backingStore.image().get();
148     }
149
150     if (properties.changedProperties & RemoteLayerTreeTransaction::FiltersChanged)
151         PlatformCAFilters::setFiltersOnLayer(layer, properties.filters);
152
153     if (properties.changedProperties & RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged)
154         layer.edgeAntialiasingMask = properties.edgeAntialiasingMask;
155 }
156
157 void RemoteLayerTreePropertyApplier::disableActionsForLayer(CALayer *layer)
158 {
159     NSNull *nullValue = [NSNull null];
160     layer.style = @{
161         @"actions" : @{
162             @"anchorPoint" : nullValue,
163             @"anchorPointZ" : nullValue,
164             @"bounds" : nullValue,
165             @"contents" : nullValue,
166             @"contentsRect" : nullValue,
167             @"opacity" : nullValue,
168             @"position" : nullValue,
169             @"shadowColor" : nullValue,
170             @"sublayerTransform" : nullValue,
171             @"sublayers" : nullValue,
172             @"transform" : nullValue,
173             @"zPosition" : nullValue
174         }
175     };
176 }
177
178 } // namespace WebKit