a8cf2078a653e59a3ba2f154ba65628d8a53c364
[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 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 #import "PlatformCALayerRemote.h"
28
29 #import "PlatformCALayerRemoteCustom.h"
30 #import "PlatformCALayerRemoteTiledBacking.h"
31 #import "RemoteLayerBackingStore.h"
32 #import "RemoteLayerTreeContext.h"
33 #import "RemoteLayerTreePropertyApplier.h"
34 #import <WebCore/AnimationUtilities.h>
35 #import <WebCore/GraphicsContext.h>
36 #import <WebCore/GraphicsLayerCA.h>
37 #import <WebCore/LengthFunctions.h>
38 #import <WebCore/PlatformCAFilters.h>
39 #import <WebCore/PlatformCALayerMac.h>
40 #import <WebCore/TiledBacking.h>
41 #import <wtf/CurrentTime.h>
42 #import <wtf/RetainPtr.h>
43
44 using namespace WebCore;
45 using namespace WebKit;
46
47 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
48 {
49     RefPtr<PlatformCALayerRemote> layer;
50
51     if (layerType == LayerTypeTiledBackingLayer ||  layerType == LayerTypePageTiledBackingLayer)
52         layer = adoptRef(new PlatformCALayerRemoteTiledBacking(layerType, owner, context));
53     else
54         layer = adoptRef(new PlatformCALayerRemote(layerType, owner, context));
55
56     context->layerWasCreated(layer.get(), layerType);
57
58     return layer.release();
59 }
60
61 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(PlatformLayer *platformLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
62 {
63     RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemoteCustom(static_cast<PlatformLayer*>(platformLayer), owner, context));
64
65     context->layerWasCreated(layer.get(), LayerTypeCustom);
66
67     return layer.release();
68 }
69
70 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(const PlatformCALayerRemote& other, WebCore::PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
71 {
72     RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemote(other, owner, context));
73
74     context->layerWasCreated(layer.get(), LayerTypeCustom);
75
76     return layer.release();
77 }
78
79 PlatformCALayerRemote::PlatformCALayerRemote(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
80     : PlatformCALayer(layerType, owner)
81     , m_superlayer(nullptr)
82     , m_maskLayer(nullptr)
83     , m_acceleratesDrawing(false)
84     , m_context(context)
85 {
86 }
87
88 PlatformCALayerRemote::PlatformCALayerRemote(const PlatformCALayerRemote& other, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
89     : PlatformCALayer(other.layerType(), owner)
90     , m_properties(other.m_properties)
91     , m_superlayer(nullptr)
92     , m_maskLayer(nullptr)
93     , m_acceleratesDrawing(other.acceleratesDrawing())
94     , m_context(context)
95 {
96 }
97
98 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::clone(PlatformCALayerClient* client) const
99 {
100     RefPtr<PlatformCALayerRemote> clone = PlatformCALayerRemote::create(*this, client, m_context);
101
102     clone->m_properties.notePropertiesChanged(static_cast<RemoteLayerTreeTransaction::LayerChange>(m_properties.everChangedProperties & ~RemoteLayerTreeTransaction::BackingStoreChanged));
103
104     return clone.release();
105 }
106
107 PlatformCALayerRemote::~PlatformCALayerRemote()
108 {
109     for (const auto& layer : m_children)
110         toPlatformCALayerRemote(layer.get())->m_superlayer = nullptr;
111     m_context->layerWillBeDestroyed(this);
112 }
113
114 void PlatformCALayerRemote::recursiveBuildTransaction(RemoteLayerTreeTransaction& transaction)
115 {
116     if (m_properties.backingStore && m_properties.backingStore->display())
117         m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
118
119     if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
120         if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
121             m_properties.children.clear();
122             for (const auto& layer : m_children)
123                 m_properties.children.append(layer->layerID());
124         }
125
126         if (m_layerType == LayerTypeCustom) {
127             RemoteLayerTreePropertyApplier::applyProperties(platformLayer(), m_properties, RemoteLayerTreePropertyApplier::RelatedLayerMap());
128             m_properties.changedProperties = RemoteLayerTreeTransaction::NoChange;
129             return;
130         }
131
132         transaction.layerPropertiesChanged(this, m_properties);
133         m_properties.changedProperties = RemoteLayerTreeTransaction::NoChange;
134     }
135
136     for (size_t i = 0; i < m_children.size(); ++i) {
137         PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
138         ASSERT(child->superlayer() == this);
139         child->recursiveBuildTransaction(transaction);
140     }
141
142     if (m_maskLayer)
143         m_maskLayer->recursiveBuildTransaction(transaction);
144 }
145
146 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
147 {
148 }
149
150 void PlatformCALayerRemote::ensureBackingStore()
151 {
152     if (!m_properties.backingStore)
153         m_properties.backingStore = std::make_unique<RemoteLayerBackingStore>();
154
155     updateBackingStore();
156 }
157
158 void PlatformCALayerRemote::updateBackingStore()
159 {
160     if (!m_properties.backingStore)
161         return;
162
163     m_properties.backingStore->ensureBackingStore(this, expandedIntSize(m_properties.size), m_properties.contentsScale, m_acceleratesDrawing);
164 }
165
166 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
167 {
168     ensureBackingStore();
169
170     if (!rect) {
171         m_properties.backingStore->setNeedsDisplay();
172         return;
173     }
174
175     // FIXME: Need to map this through contentsRect/etc.
176     m_properties.backingStore->setNeedsDisplay(enclosingIntRect(*rect));
177 }
178
179 void PlatformCALayerRemote::setContentsChanged()
180 {
181 }
182
183 PlatformCALayer* PlatformCALayerRemote::superlayer() const
184 {
185     return m_superlayer;
186 }
187
188 void PlatformCALayerRemote::removeFromSuperlayer()
189 {
190     if (!m_superlayer)
191         return;
192
193     m_superlayer->removeSublayer(this);
194 }
195
196 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
197 {
198     size_t childIndex = m_children.find(layer);
199     if (childIndex != notFound)
200         m_children.remove(childIndex);
201     layer->m_superlayer = nullptr;
202     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
203 }
204
205 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
206 {
207     removeAllSublayers();
208     m_children = list;
209
210     for (const auto& layer : list) {
211         layer->removeFromSuperlayer();
212         toPlatformCALayerRemote(layer.get())->m_superlayer = this;
213     }
214
215     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
216 }
217
218 void PlatformCALayerRemote::removeAllSublayers()
219 {
220     PlatformCALayerList layersToRemove = m_children;
221     for (const auto& layer : layersToRemove)
222         layer->removeFromSuperlayer();
223     ASSERT(m_children.isEmpty());
224     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
225 }
226
227 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
228 {
229     RefPtr<PlatformCALayer> layerProtector(layer);
230
231     layer->removeFromSuperlayer();
232     m_children.append(layer);
233     toPlatformCALayerRemote(layer)->m_superlayer = this;
234     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
235 }
236
237 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
238 {
239     RefPtr<PlatformCALayer> layerProtector(layer);
240
241     layer->removeFromSuperlayer();
242     m_children.insert(index, layer);
243     toPlatformCALayerRemote(layer)->m_superlayer = this;
244     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
245 }
246
247 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
248 {
249     ASSERT(reference->superlayer() == this);
250     RefPtr<PlatformCALayer> layerProtector(layer);
251
252     layer->removeFromSuperlayer();
253     size_t referenceIndex = m_children.find(reference);
254     if (referenceIndex != notFound) {
255         m_children[referenceIndex]->removeFromSuperlayer();
256         m_children.insert(referenceIndex, layer);
257         toPlatformCALayerRemote(layer)->m_superlayer = this;
258     }
259
260     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
261 }
262
263 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
264 {
265     setSublayers(toPlatformCALayerRemote(source)->m_children);
266 }
267
268 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
269 {
270     ASSERT_NOT_REACHED();
271 }
272
273 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
274 {
275     ASSERT_NOT_REACHED();
276 }
277
278 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
279 {
280     ASSERT_NOT_REACHED();
281
282     return nullptr;
283 }
284
285 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
286 {
287     if (layer) {
288         m_maskLayer = toPlatformCALayerRemote(layer);
289         m_properties.maskLayerID = m_maskLayer->layerID();
290     } else {
291         m_maskLayer = nullptr;
292         m_properties.maskLayerID = 0;
293     }
294
295     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
296 }
297
298 bool PlatformCALayerRemote::isOpaque() const
299 {
300     return m_properties.opaque;
301 }
302
303 void PlatformCALayerRemote::setOpaque(bool value)
304 {
305     m_properties.opaque = value;
306     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
307 }
308
309 FloatRect PlatformCALayerRemote::bounds() const
310 {
311     return FloatRect(FloatPoint(), m_properties.size);
312 }
313
314 void PlatformCALayerRemote::setBounds(const FloatRect& value)
315 {
316     if (value.size() == m_properties.size)
317         return;
318
319     m_properties.size = value.size();
320     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SizeChanged);
321     
322     if (requiresCustomAppearanceUpdateOnBoundsChange())
323         m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
324
325     updateBackingStore();
326 }
327
328 FloatPoint3D PlatformCALayerRemote::position() const
329 {
330     return m_properties.position;
331 }
332
333 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
334 {
335     if (value == m_properties.position)
336         return;
337
338     m_properties.position = value;
339     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
340 }
341
342 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
343 {
344     return m_properties.anchorPoint;
345 }
346
347 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
348 {
349     if (value == m_properties.anchorPoint)
350         return;
351
352     m_properties.anchorPoint = value;
353     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
354 }
355
356 TransformationMatrix PlatformCALayerRemote::transform() const
357 {
358     return m_properties.transform ? *m_properties.transform : TransformationMatrix();
359 }
360
361 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
362 {
363     m_properties.transform = std::make_unique<TransformationMatrix>(value);
364     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
365 }
366
367 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
368 {
369     return m_properties.sublayerTransform ? *m_properties.sublayerTransform : TransformationMatrix();
370 }
371
372 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
373 {
374     m_properties.sublayerTransform = std::make_unique<TransformationMatrix>(value);
375     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
376 }
377
378 void PlatformCALayerRemote::setHidden(bool value)
379 {
380     m_properties.hidden = value;
381     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
382 }
383
384 void PlatformCALayerRemote::setGeometryFlipped(bool value)
385 {
386     m_properties.geometryFlipped = value;
387     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
388 }
389
390 bool PlatformCALayerRemote::isDoubleSided() const
391 {
392     return m_properties.doubleSided;
393 }
394
395 void PlatformCALayerRemote::setDoubleSided(bool value)
396 {
397     m_properties.doubleSided = value;
398     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
399 }
400
401 bool PlatformCALayerRemote::masksToBounds() const
402 {
403     return m_properties.masksToBounds;
404 }
405
406 void PlatformCALayerRemote::setMasksToBounds(bool value)
407 {
408     if (value == m_properties.masksToBounds)
409         return;
410
411     m_properties.masksToBounds = value;
412     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
413 }
414
415 bool PlatformCALayerRemote::acceleratesDrawing() const
416 {
417     return m_acceleratesDrawing;
418 }
419
420 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
421 {
422     m_acceleratesDrawing = acceleratesDrawing;
423     updateBackingStore();
424 }
425
426 CFTypeRef PlatformCALayerRemote::contents() const
427 {
428     return nullptr;
429 }
430
431 void PlatformCALayerRemote::setContents(CFTypeRef value)
432 {
433 }
434
435 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
436 {
437     m_properties.contentsRect = value;
438     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
439 }
440
441 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
442 {
443     m_properties.minificationFilter = value;
444     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
445 }
446
447 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
448 {
449     m_properties.magnificationFilter = value;
450     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
451 }
452
453 Color PlatformCALayerRemote::backgroundColor() const
454 {
455     return m_properties.backgroundColor;
456 }
457
458 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
459 {
460     m_properties.backgroundColor = value;
461     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
462 }
463
464 void PlatformCALayerRemote::setBorderWidth(float value)
465 {
466     if (value == m_properties.borderWidth)
467         return;
468
469     m_properties.borderWidth = value;
470     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
471 }
472
473 void PlatformCALayerRemote::setBorderColor(const Color& value)
474 {
475     if (value == m_properties.borderColor)
476         return;
477
478     m_properties.borderColor = value;
479     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
480 }
481
482 float PlatformCALayerRemote::opacity() const
483 {
484     return m_properties.opacity;
485 }
486
487 void PlatformCALayerRemote::setOpacity(float value)
488 {
489     m_properties.opacity = value;
490     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
491 }
492
493 #if ENABLE(CSS_FILTERS)
494 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
495 {
496     m_properties.filters = std::make_unique<FilterOperations>(filters);
497     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
498 }
499
500 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
501 {
502     ASSERT_NOT_REACHED();
503 }
504
505 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
506 {
507     return PlatformCALayerMac::filtersCanBeComposited(filters);
508 }
509 #endif
510
511 void PlatformCALayerRemote::setName(const String& value)
512 {
513     m_properties.name = value;
514     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
515 }
516
517 void PlatformCALayerRemote::setSpeed(float value)
518 {
519     m_properties.speed = value;
520     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
521 }
522
523 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
524 {
525     m_properties.timeOffset = value;
526     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
527 }
528
529 float PlatformCALayerRemote::contentsScale() const
530 {
531     return m_properties.contentsScale;
532 }
533
534 void PlatformCALayerRemote::setContentsScale(float value)
535 {
536     m_properties.contentsScale = value;
537     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
538
539     updateBackingStore();
540 }
541
542 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
543 {
544     m_properties.edgeAntialiasingMask = value;
545     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
546 }
547
548 bool PlatformCALayerRemote::requiresCustomAppearanceUpdateOnBoundsChange() const
549 {
550     return m_properties.customAppearance == GraphicsLayer::ScrollingShadow;
551 }
552
553 GraphicsLayer::CustomAppearance PlatformCALayerRemote::customAppearance() const
554 {
555     return m_properties.customAppearance;
556 }
557
558 void PlatformCALayerRemote::updateCustomAppearance(GraphicsLayer::CustomAppearance customAppearance)
559 {
560     m_properties.customAppearance = customAppearance;
561     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
562 }
563
564 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
565 {
566     return PlatformCALayerRemote::create(layerType, client, m_context);
567 }
568
569 void PlatformCALayerRemote::enumerateRectsBeingDrawn(CGContextRef context, void (^block)(CGRect))
570 {
571     m_properties.backingStore->enumerateRectsBeingDrawn(context, block);
572 }
573
574 uint32_t PlatformCALayerRemote::hostingContextID()
575 {
576     ASSERT_NOT_REACHED();
577     return 0;
578 }