Remote Layer Tree: Apply layer changes to LayerTypeCustom layers to the custom layer...
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / mac / PlatformCALayerRemote.cpp
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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if USE(ACCELERATED_COMPOSITING)
29
30 #import "PlatformCALayerRemote.h"
31
32 #import "PlatformCALayerRemoteCustom.h"
33 #import "PlatformCALayerRemoteTiledBacking.h"
34 #import "RemoteLayerBackingStore.h"
35 #import "RemoteLayerTreeContext.h"
36 #import "RemoteLayerTreePropertyApplier.h"
37 #import <WebCore/AnimationUtilities.h>
38 #import <WebCore/GraphicsContext.h>
39 #import <WebCore/GraphicsLayerCA.h>
40 #import <WebCore/LengthFunctions.h>
41 #import <WebCore/PlatformCAFilters.h>
42 #import <WebCore/PlatformCALayerMac.h>
43 #import <WebCore/TiledBacking.h>
44 #import <wtf/CurrentTime.h>
45 #import <wtf/RetainPtr.h>
46
47 using namespace WebCore;
48 using namespace WebKit;
49
50 static RemoteLayerTreeTransaction::LayerID generateLayerID()
51 {
52     static RemoteLayerTreeTransaction::LayerID layerID;
53     return ++layerID;
54 }
55
56 static PlatformCALayerRemote* toPlatformCALayerRemote(PlatformCALayer* layer)
57 {
58     ASSERT_WITH_SECURITY_IMPLICATION(!layer || layer->isRemote());
59     return static_cast<PlatformCALayerRemote*>(layer);
60 }
61
62 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::create(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
63 {
64     RefPtr<PlatformCALayerRemote> layer;
65
66     if (layerType == LayerTypeTiledBackingLayer ||  layerType == LayerTypePageTiledBackingLayer)
67         layer = adoptRef(new PlatformCALayerRemoteTiledBacking(layerType, owner, context));
68     else
69         layer = adoptRef(new PlatformCALayerRemote(layerType, owner, context));
70
71     context->layerWasCreated(layer.get(), layerType);
72
73     return layer.release();
74 }
75
76 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::create(PlatformLayer *platformLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
77 {
78     RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemoteCustom(static_cast<PlatformLayer*>(platformLayer), owner, context));
79
80     context->layerWasCreated(layer.get(), LayerTypeCustom);
81
82     return layer.release();
83 }
84
85 PlatformCALayerRemote::PlatformCALayerRemote(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
86     : PlatformCALayer(layerType, owner)
87     , m_layerID(generateLayerID())
88     , m_superlayer(nullptr)
89     , m_acceleratesDrawing(false)
90     , m_context(context)
91 {
92     // FIXME: match all default values from CA.
93     setContentsScale(1);
94 }
95
96 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::clone(PlatformCALayerClient* owner) const
97 {
98     return nullptr;
99 }
100
101 PlatformCALayerRemote::~PlatformCALayerRemote()
102 {
103     for (const auto& layer : m_children)
104         toPlatformCALayerRemote(layer.get())->m_superlayer = nullptr;
105     m_context->layerWillBeDestroyed(this);
106 }
107
108 void PlatformCALayerRemote::recursiveBuildTransaction(RemoteLayerTreeTransaction& transaction)
109 {
110     if (m_properties.backingStore.display())
111         m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
112
113     if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
114         if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
115             m_properties.children.clear();
116             for (auto layer : m_children)
117                 m_properties.children.append(toPlatformCALayerRemote(layer.get())->layerID());
118         }
119
120         if (m_layerType == LayerTypeCustom) {
121             RemoteLayerTreePropertyApplier::applyPropertiesToLayer(platformLayer(), m_properties, RemoteLayerTreePropertyApplier::RelatedLayerMap());
122             m_properties.changedProperties = RemoteLayerTreeTransaction::NoChange;
123             return;
124         }
125
126         transaction.layerPropertiesChanged(this, m_properties);
127         m_properties.changedProperties = RemoteLayerTreeTransaction::NoChange;
128     }
129
130     for (size_t i = 0; i < m_children.size(); ++i) {
131         PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
132         ASSERT(child->superlayer() == this);
133         child->recursiveBuildTransaction(transaction);
134     }
135 }
136
137 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
138 {
139 }
140
141 void PlatformCALayerRemote::ensureBackingStore()
142 {
143     m_properties.backingStore.ensureBackingStore(this, expandedIntSize(m_properties.size), m_properties.contentsScale, m_acceleratesDrawing);
144 }
145
146 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
147 {
148     ensureBackingStore();
149
150     if (!rect) {
151         m_properties.backingStore.setNeedsDisplay();
152         return;
153     }
154
155     // FIXME: Need to map this through contentsRect/etc.
156     m_properties.backingStore.setNeedsDisplay(enclosingIntRect(*rect));
157 }
158
159 void PlatformCALayerRemote::setContentsChanged()
160 {
161 }
162
163 PlatformCALayer* PlatformCALayerRemote::superlayer() const
164 {
165     return m_superlayer;
166 }
167
168 void PlatformCALayerRemote::removeFromSuperlayer()
169 {
170     if (!m_superlayer)
171         return;
172
173     m_superlayer->removeSublayer(this);
174 }
175
176 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
177 {
178     size_t childIndex = m_children.find(layer);
179     if (childIndex != notFound)
180         m_children.remove(childIndex);
181     layer->m_superlayer = nullptr;
182     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
183 }
184
185 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
186 {
187     removeAllSublayers();
188     m_children = list;
189
190     for (const auto& layer : list) {
191         layer->removeFromSuperlayer();
192         toPlatformCALayerRemote(layer.get())->m_superlayer = this;
193     }
194
195     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
196 }
197
198 void PlatformCALayerRemote::removeAllSublayers()
199 {
200     PlatformCALayerList layersToRemove = m_children;
201     for (const auto& layer : layersToRemove)
202         layer->removeFromSuperlayer();
203     ASSERT(m_children.isEmpty());
204     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
205 }
206
207 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
208 {
209     RefPtr<PlatformCALayer> layerProtector(layer);
210
211     layer->removeFromSuperlayer();
212     m_children.append(layer);
213     toPlatformCALayerRemote(layer)->m_superlayer = this;
214     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
215 }
216
217 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
218 {
219     RefPtr<PlatformCALayer> layerProtector(layer);
220
221     layer->removeFromSuperlayer();
222     m_children.insert(index, layer);
223     toPlatformCALayerRemote(layer)->m_superlayer = this;
224     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
225 }
226
227 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
228 {
229     ASSERT(reference->superlayer() == this);
230     RefPtr<PlatformCALayer> layerProtector(layer);
231
232     layer->removeFromSuperlayer();
233     size_t referenceIndex = m_children.find(reference);
234     if (referenceIndex != notFound) {
235         m_children[referenceIndex]->removeFromSuperlayer();
236         m_children.remove(referenceIndex);
237         m_children.insert(referenceIndex, layer);
238     }
239
240     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
241 }
242
243 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
244 {
245     setSublayers(toPlatformCALayerRemote(source)->m_children);
246 }
247
248 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
249 {
250     ASSERT_NOT_REACHED();
251 }
252
253 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
254 {
255     ASSERT_NOT_REACHED();
256 }
257
258 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
259 {
260     ASSERT_NOT_REACHED();
261
262     return nullptr;
263 }
264
265 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
266 {
267     m_properties.maskLayer = toPlatformCALayerRemote(layer)->layerID();
268     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
269 }
270
271 bool PlatformCALayerRemote::isOpaque() const
272 {
273     return m_properties.opaque;
274 }
275
276 void PlatformCALayerRemote::setOpaque(bool value)
277 {
278     m_properties.opaque = value;
279     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
280 }
281
282 FloatRect PlatformCALayerRemote::bounds() const
283 {
284     return FloatRect(FloatPoint(), m_properties.size);
285 }
286
287 void PlatformCALayerRemote::setBounds(const FloatRect& value)
288 {
289     m_properties.size = value.size();
290     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SizeChanged);
291
292     ensureBackingStore();
293 }
294
295 FloatPoint3D PlatformCALayerRemote::position() const
296 {
297     return m_properties.position;
298 }
299
300 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
301 {
302     m_properties.position = value;
303     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
304 }
305
306 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
307 {
308     return m_properties.anchorPoint;
309 }
310
311 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
312 {
313     m_properties.anchorPoint = value;
314     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
315 }
316
317 TransformationMatrix PlatformCALayerRemote::transform() const
318 {
319     return m_properties.transform;
320 }
321
322 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
323 {
324     m_properties.transform = value;
325     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
326 }
327
328 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
329 {
330     return m_properties.sublayerTransform;
331 }
332
333 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
334 {
335     m_properties.sublayerTransform = value;
336     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
337 }
338
339 void PlatformCALayerRemote::setHidden(bool value)
340 {
341     m_properties.hidden = value;
342     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
343 }
344
345 void PlatformCALayerRemote::setGeometryFlipped(bool value)
346 {
347     m_properties.geometryFlipped = value;
348     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
349 }
350
351 bool PlatformCALayerRemote::isDoubleSided() const
352 {
353     return m_properties.doubleSided;
354 }
355
356 void PlatformCALayerRemote::setDoubleSided(bool value)
357 {
358     m_properties.doubleSided = value;
359     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
360 }
361
362 bool PlatformCALayerRemote::masksToBounds() const
363 {
364     return m_properties.masksToBounds;
365 }
366
367 void PlatformCALayerRemote::setMasksToBounds(bool value)
368 {
369     m_properties.masksToBounds = value;
370     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
371 }
372
373 bool PlatformCALayerRemote::acceleratesDrawing() const
374 {
375     return m_acceleratesDrawing;
376 }
377
378 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
379 {
380     m_acceleratesDrawing = acceleratesDrawing;
381     ensureBackingStore();
382 }
383
384 CFTypeRef PlatformCALayerRemote::contents() const
385 {
386     return nullptr;
387 }
388
389 void PlatformCALayerRemote::setContents(CFTypeRef value)
390 {
391 }
392
393 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
394 {
395     m_properties.contentsRect = value;
396     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
397 }
398
399 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
400 {
401     m_properties.minificationFilter = value;
402     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
403 }
404
405 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
406 {
407     m_properties.magnificationFilter = value;
408     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
409 }
410
411 Color PlatformCALayerRemote::backgroundColor() const
412 {
413     return m_properties.backgroundColor;
414 }
415
416 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
417 {
418     m_properties.backgroundColor = value;
419     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
420 }
421
422 void PlatformCALayerRemote::setBorderWidth(float value)
423 {
424     m_properties.borderWidth = value;
425     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
426 }
427
428 void PlatformCALayerRemote::setBorderColor(const Color& value)
429 {
430     m_properties.borderColor = value;
431     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
432 }
433
434 float PlatformCALayerRemote::opacity() const
435 {
436     return m_properties.opacity;
437 }
438
439 void PlatformCALayerRemote::setOpacity(float value)
440 {
441     m_properties.opacity = value;
442     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
443 }
444
445 #if ENABLE(CSS_FILTERS)
446 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
447 {
448     m_properties.filters = filters;
449     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
450 }
451
452 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
453 {
454     ASSERT_NOT_REACHED();
455 }
456
457 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
458 {
459     return PlatformCALayerMac::filtersCanBeComposited(filters);
460 }
461 #endif
462
463 void PlatformCALayerRemote::setName(const String& value)
464 {
465     m_properties.name = value;
466     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
467 }
468
469 void PlatformCALayerRemote::setSpeed(float value)
470 {
471     m_properties.speed = value;
472     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
473 }
474
475 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
476 {
477     m_properties.timeOffset = value;
478     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
479 }
480
481 float PlatformCALayerRemote::contentsScale() const
482 {
483     return m_properties.contentsScale;
484 }
485
486 void PlatformCALayerRemote::setContentsScale(float value)
487 {
488     m_properties.contentsScale = value;
489     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
490
491     ensureBackingStore();
492 }
493
494 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
495 {
496     m_properties.edgeAntialiasingMask = value;
497     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
498 }
499
500 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
501 {
502     return PlatformCALayerRemote::create(layerType, client, m_context);
503 }
504
505 uint32_t PlatformCALayerRemote::hostingContextID()
506 {
507     ASSERT_NOT_REACHED();
508     return 0;
509 }
510
511 #endif // USE(ACCELERATED_COMPOSITING)