[iOS][WebKit2] Mark layer contents as being opaque if they are
[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, m_properties.opaque);
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     updateBackingStore();
309 }
310
311 FloatRect PlatformCALayerRemote::bounds() const
312 {
313     return FloatRect(FloatPoint(), m_properties.size);
314 }
315
316 void PlatformCALayerRemote::setBounds(const FloatRect& value)
317 {
318     if (value.size() == m_properties.size)
319         return;
320
321     m_properties.size = value.size();
322     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SizeChanged);
323     
324     if (requiresCustomAppearanceUpdateOnBoundsChange())
325         m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
326
327     updateBackingStore();
328 }
329
330 FloatPoint3D PlatformCALayerRemote::position() const
331 {
332     return m_properties.position;
333 }
334
335 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
336 {
337     if (value == m_properties.position)
338         return;
339
340     m_properties.position = value;
341     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
342 }
343
344 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
345 {
346     return m_properties.anchorPoint;
347 }
348
349 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
350 {
351     if (value == m_properties.anchorPoint)
352         return;
353
354     m_properties.anchorPoint = value;
355     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
356 }
357
358 TransformationMatrix PlatformCALayerRemote::transform() const
359 {
360     return m_properties.transform ? *m_properties.transform : TransformationMatrix();
361 }
362
363 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
364 {
365     m_properties.transform = std::make_unique<TransformationMatrix>(value);
366     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
367 }
368
369 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
370 {
371     return m_properties.sublayerTransform ? *m_properties.sublayerTransform : TransformationMatrix();
372 }
373
374 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
375 {
376     m_properties.sublayerTransform = std::make_unique<TransformationMatrix>(value);
377     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
378 }
379
380 void PlatformCALayerRemote::setHidden(bool value)
381 {
382     m_properties.hidden = value;
383     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
384 }
385
386 void PlatformCALayerRemote::setGeometryFlipped(bool value)
387 {
388     m_properties.geometryFlipped = value;
389     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
390 }
391
392 bool PlatformCALayerRemote::isDoubleSided() const
393 {
394     return m_properties.doubleSided;
395 }
396
397 void PlatformCALayerRemote::setDoubleSided(bool value)
398 {
399     m_properties.doubleSided = value;
400     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
401 }
402
403 bool PlatformCALayerRemote::masksToBounds() const
404 {
405     return m_properties.masksToBounds;
406 }
407
408 void PlatformCALayerRemote::setMasksToBounds(bool value)
409 {
410     if (value == m_properties.masksToBounds)
411         return;
412
413     m_properties.masksToBounds = value;
414     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
415 }
416
417 bool PlatformCALayerRemote::acceleratesDrawing() const
418 {
419     return m_acceleratesDrawing;
420 }
421
422 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
423 {
424     m_acceleratesDrawing = acceleratesDrawing;
425     updateBackingStore();
426 }
427
428 CFTypeRef PlatformCALayerRemote::contents() const
429 {
430     return nullptr;
431 }
432
433 void PlatformCALayerRemote::setContents(CFTypeRef value)
434 {
435 }
436
437 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
438 {
439     m_properties.contentsRect = value;
440     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
441 }
442
443 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
444 {
445     m_properties.minificationFilter = value;
446     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
447 }
448
449 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
450 {
451     m_properties.magnificationFilter = value;
452     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
453 }
454
455 Color PlatformCALayerRemote::backgroundColor() const
456 {
457     return m_properties.backgroundColor;
458 }
459
460 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
461 {
462     m_properties.backgroundColor = value;
463     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
464 }
465
466 void PlatformCALayerRemote::setBorderWidth(float value)
467 {
468     if (value == m_properties.borderWidth)
469         return;
470
471     m_properties.borderWidth = value;
472     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
473 }
474
475 void PlatformCALayerRemote::setBorderColor(const Color& value)
476 {
477     if (value == m_properties.borderColor)
478         return;
479
480     m_properties.borderColor = value;
481     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
482 }
483
484 float PlatformCALayerRemote::opacity() const
485 {
486     return m_properties.opacity;
487 }
488
489 void PlatformCALayerRemote::setOpacity(float value)
490 {
491     m_properties.opacity = value;
492     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
493 }
494
495 #if ENABLE(CSS_FILTERS)
496 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
497 {
498     m_properties.filters = std::make_unique<FilterOperations>(filters);
499     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
500 }
501
502 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
503 {
504     ASSERT_NOT_REACHED();
505 }
506
507 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
508 {
509     return PlatformCALayerMac::filtersCanBeComposited(filters);
510 }
511 #endif
512
513 void PlatformCALayerRemote::setName(const String& value)
514 {
515     m_properties.name = value;
516     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
517 }
518
519 void PlatformCALayerRemote::setSpeed(float value)
520 {
521     m_properties.speed = value;
522     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
523 }
524
525 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
526 {
527     m_properties.timeOffset = value;
528     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
529 }
530
531 float PlatformCALayerRemote::contentsScale() const
532 {
533     return m_properties.contentsScale;
534 }
535
536 void PlatformCALayerRemote::setContentsScale(float value)
537 {
538     m_properties.contentsScale = value;
539     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
540
541     updateBackingStore();
542 }
543
544 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
545 {
546     m_properties.edgeAntialiasingMask = value;
547     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
548 }
549
550 bool PlatformCALayerRemote::requiresCustomAppearanceUpdateOnBoundsChange() const
551 {
552     return m_properties.customAppearance == GraphicsLayer::ScrollingShadow;
553 }
554
555 GraphicsLayer::CustomAppearance PlatformCALayerRemote::customAppearance() const
556 {
557     return m_properties.customAppearance;
558 }
559
560 void PlatformCALayerRemote::updateCustomAppearance(GraphicsLayer::CustomAppearance customAppearance)
561 {
562     m_properties.customAppearance = customAppearance;
563     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
564 }
565
566 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
567 {
568     return PlatformCALayerRemote::create(layerType, client, m_context);
569 }
570
571 void PlatformCALayerRemote::enumerateRectsBeingDrawn(CGContextRef context, void (^block)(CGRect))
572 {
573     m_properties.backingStore->enumerateRectsBeingDrawn(context, block);
574 }
575
576 uint32_t PlatformCALayerRemote::hostingContextID()
577 {
578     ASSERT_NOT_REACHED();
579     return 0;
580 }