2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "PlatformCALayerRemote.h"
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>
44 using namespace WebCore;
48 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
50 RefPtr<PlatformCALayerRemote> layer;
52 if (layerType == LayerTypeTiledBackingLayer || layerType == LayerTypePageTiledBackingLayer)
53 layer = adoptRef(new PlatformCALayerRemoteTiledBacking(layerType, owner, context));
55 layer = adoptRef(new PlatformCALayerRemote(layerType, owner, context));
57 context->layerWasCreated(layer.get(), layerType);
59 return layer.release();
62 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(PlatformLayer *platformLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
64 RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemoteCustom(static_cast<PlatformLayer*>(platformLayer), owner, context));
66 context->layerWasCreated(layer.get(), LayerTypeCustom);
68 return layer.release();
71 PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemote::create(const PlatformCALayerRemote& other, WebCore::PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
73 RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemote(other, owner, context));
75 context->layerWasCreated(layer.get(), other.layerType());
77 return layer.release();
80 PlatformCALayerRemote::PlatformCALayerRemote(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
81 : PlatformCALayer(layerType, owner)
82 , m_superlayer(nullptr)
83 , m_maskLayer(nullptr)
84 , m_acceleratesDrawing(false)
88 m_properties.contentsScale = owner->platformCALayerDeviceScaleFactor();
91 PlatformCALayerRemote::PlatformCALayerRemote(const PlatformCALayerRemote& other, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
92 : PlatformCALayer(other.layerType(), owner)
93 , m_properties(other.m_properties)
94 , m_superlayer(nullptr)
95 , m_maskLayer(nullptr)
96 , m_acceleratesDrawing(other.acceleratesDrawing())
101 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::clone(PlatformCALayerClient* client) const
103 RefPtr<PlatformCALayerRemote> clone = PlatformCALayerRemote::create(*this, client, m_context);
105 clone->m_properties.notePropertiesChanged(static_cast<RemoteLayerTreeTransaction::LayerChange>(m_properties.everChangedProperties & ~RemoteLayerTreeTransaction::BackingStoreChanged));
107 return clone.release();
110 PlatformCALayerRemote::~PlatformCALayerRemote()
112 for (const auto& layer : m_children)
113 toPlatformCALayerRemote(layer.get())->m_superlayer = nullptr;
116 m_context->layerWillBeDestroyed(this);
119 void PlatformCALayerRemote::recursiveBuildTransaction(RemoteLayerTreeTransaction& transaction)
121 ASSERT(!m_properties.backingStore || owner());
123 if (m_properties.backingStore && (!owner() || !owner()->platformCALayerDrawsContent())) {
124 m_properties.backingStore = nullptr;
125 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
128 if (m_properties.backingStore && m_properties.backingStore->display())
129 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
131 if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
132 if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
133 m_properties.children.clear();
134 for (const auto& layer : m_children)
135 m_properties.children.append(layer->layerID());
138 if (m_layerType == LayerTypeCustom) {
139 RemoteLayerTreePropertyApplier::applyProperties(platformLayer(), nullptr, m_properties, RemoteLayerTreePropertyApplier::RelatedLayerMap());
144 transaction.layerPropertiesChanged(this, m_properties);
147 for (size_t i = 0; i < m_children.size(); ++i) {
148 PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
149 ASSERT(child->superlayer() == this);
150 child->recursiveBuildTransaction(transaction);
154 m_maskLayer->recursiveBuildTransaction(transaction);
157 void PlatformCALayerRemote::didCommit()
159 m_properties.addedAnimations.clear();
160 m_properties.keyPathsOfAnimationsToRemove.clear();
161 m_properties.resetChangedProperties();
164 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
167 m_owner->platformCALayerAnimationStarted(beginTime);
170 void PlatformCALayerRemote::ensureBackingStore()
174 if (!m_properties.backingStore)
175 m_properties.backingStore = std::make_unique<RemoteLayerBackingStore>(m_context);
177 updateBackingStore();
180 void PlatformCALayerRemote::updateBackingStore()
182 if (!m_properties.backingStore)
185 m_properties.backingStore->ensureBackingStore(this, expandedIntSize(m_properties.bounds.size()), m_properties.contentsScale, m_acceleratesDrawing, m_properties.opaque);
188 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
190 ensureBackingStore();
193 m_properties.backingStore->setNeedsDisplay();
197 // FIXME: Need to map this through contentsRect/etc.
198 m_properties.backingStore->setNeedsDisplay(enclosingIntRect(*rect));
201 void PlatformCALayerRemote::setContentsChanged()
205 PlatformCALayer* PlatformCALayerRemote::superlayer() const
210 void PlatformCALayerRemote::removeFromSuperlayer()
215 m_superlayer->removeSublayer(this);
218 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
220 size_t childIndex = m_children.find(layer);
221 if (childIndex != notFound)
222 m_children.remove(childIndex);
223 layer->m_superlayer = nullptr;
224 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
227 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
229 removeAllSublayers();
232 for (const auto& layer : list) {
233 layer->removeFromSuperlayer();
234 toPlatformCALayerRemote(layer.get())->m_superlayer = this;
237 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
240 void PlatformCALayerRemote::removeAllSublayers()
242 PlatformCALayerList layersToRemove = m_children;
243 for (const auto& layer : layersToRemove)
244 layer->removeFromSuperlayer();
245 ASSERT(m_children.isEmpty());
246 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
249 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
251 RefPtr<PlatformCALayer> layerProtector(layer);
253 layer->removeFromSuperlayer();
254 m_children.append(layer);
255 toPlatformCALayerRemote(layer)->m_superlayer = this;
256 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
259 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
261 RefPtr<PlatformCALayer> layerProtector(layer);
263 layer->removeFromSuperlayer();
264 m_children.insert(index, layer);
265 toPlatformCALayerRemote(layer)->m_superlayer = this;
266 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
269 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
271 ASSERT(reference->superlayer() == this);
272 RefPtr<PlatformCALayer> layerProtector(layer);
274 layer->removeFromSuperlayer();
275 size_t referenceIndex = m_children.find(reference);
276 if (referenceIndex != notFound) {
277 m_children[referenceIndex]->removeFromSuperlayer();
278 m_children.insert(referenceIndex, layer);
279 toPlatformCALayerRemote(layer)->m_superlayer = this;
282 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
285 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
287 PlatformCALayerList layersToMove = toPlatformCALayerRemote(source)->m_children;
289 if (const PlatformCALayerList* customLayers = source->customSublayers()) {
290 for (const auto& layer : *customLayers) {
291 size_t layerIndex = layersToMove.find(layer);
292 if (layerIndex != notFound)
293 layersToMove.remove(layerIndex);
297 setSublayers(layersToMove);
300 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
302 m_properties.addedAnimations.set(key, toPlatformCAAnimationRemote(animation)->properties());
303 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
306 m_context->willStartAnimationOnLayer(this);
309 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
311 m_properties.addedAnimations.remove(key);
312 m_properties.keyPathsOfAnimationsToRemove.add(key);
313 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
316 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
322 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
325 m_maskLayer = toPlatformCALayerRemote(layer);
326 m_properties.maskLayerID = m_maskLayer->layerID();
328 m_maskLayer = nullptr;
329 m_properties.maskLayerID = 0;
332 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
335 bool PlatformCALayerRemote::isOpaque() const
337 return m_properties.opaque;
340 void PlatformCALayerRemote::setOpaque(bool value)
342 m_properties.opaque = value;
343 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
345 updateBackingStore();
348 FloatRect PlatformCALayerRemote::bounds() const
350 return m_properties.bounds;
353 void PlatformCALayerRemote::setBounds(const FloatRect& value)
355 if (value == m_properties.bounds)
358 m_properties.bounds = value;
359 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BoundsChanged);
361 if (requiresCustomAppearanceUpdateOnBoundsChange())
362 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
364 updateBackingStore();
367 FloatPoint3D PlatformCALayerRemote::position() const
369 return m_properties.position;
372 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
374 if (value == m_properties.position)
377 m_properties.position = value;
378 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
381 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
383 return m_properties.anchorPoint;
386 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
388 if (value == m_properties.anchorPoint)
391 m_properties.anchorPoint = value;
392 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
395 TransformationMatrix PlatformCALayerRemote::transform() const
397 return m_properties.transform ? *m_properties.transform : TransformationMatrix();
400 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
402 m_properties.transform = std::make_unique<TransformationMatrix>(value);
403 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
406 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
408 return m_properties.sublayerTransform ? *m_properties.sublayerTransform : TransformationMatrix();
411 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
413 m_properties.sublayerTransform = std::make_unique<TransformationMatrix>(value);
414 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
417 void PlatformCALayerRemote::setHidden(bool value)
419 m_properties.hidden = value;
420 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
423 void PlatformCALayerRemote::setGeometryFlipped(bool value)
425 m_properties.geometryFlipped = value;
426 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
429 bool PlatformCALayerRemote::isDoubleSided() const
431 return m_properties.doubleSided;
434 void PlatformCALayerRemote::setDoubleSided(bool value)
436 m_properties.doubleSided = value;
437 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
440 bool PlatformCALayerRemote::masksToBounds() const
442 return m_properties.masksToBounds;
445 void PlatformCALayerRemote::setMasksToBounds(bool value)
447 if (value == m_properties.masksToBounds)
450 m_properties.masksToBounds = value;
451 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
454 bool PlatformCALayerRemote::acceleratesDrawing() const
456 return m_acceleratesDrawing;
459 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
461 m_acceleratesDrawing = acceleratesDrawing;
462 updateBackingStore();
465 CFTypeRef PlatformCALayerRemote::contents() const
470 void PlatformCALayerRemote::setContents(CFTypeRef value)
474 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
476 m_properties.contentsRect = value;
477 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
480 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
482 m_properties.minificationFilter = value;
483 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
486 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
488 m_properties.magnificationFilter = value;
489 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
492 Color PlatformCALayerRemote::backgroundColor() const
494 return m_properties.backgroundColor;
497 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
499 m_properties.backgroundColor = value;
500 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
503 void PlatformCALayerRemote::setBorderWidth(float value)
505 if (value == m_properties.borderWidth)
508 m_properties.borderWidth = value;
509 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
512 void PlatformCALayerRemote::setBorderColor(const Color& value)
514 if (value == m_properties.borderColor)
517 m_properties.borderColor = value;
518 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
521 float PlatformCALayerRemote::opacity() const
523 return m_properties.opacity;
526 void PlatformCALayerRemote::setOpacity(float value)
528 m_properties.opacity = value;
529 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
532 #if ENABLE(CSS_FILTERS)
533 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
535 m_properties.filters = std::make_unique<FilterOperations>(filters);
536 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
539 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
541 ASSERT_NOT_REACHED();
544 #if ENABLE(CSS_COMPOSITING)
545 void PlatformCALayerRemote::setBlendMode(BlendMode blendMode)
547 m_properties.blendMode = blendMode;
548 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BlendModeChanged);
552 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
554 return PlatformCALayerMac::filtersCanBeComposited(filters);
558 void PlatformCALayerRemote::setName(const String& value)
560 m_properties.name = value;
561 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
564 void PlatformCALayerRemote::setSpeed(float value)
566 m_properties.speed = value;
567 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
570 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
572 m_properties.timeOffset = value;
573 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
576 float PlatformCALayerRemote::contentsScale() const
578 return m_properties.contentsScale;
581 void PlatformCALayerRemote::setContentsScale(float value)
583 m_properties.contentsScale = value;
584 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
586 updateBackingStore();
589 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
591 m_properties.edgeAntialiasingMask = value;
592 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
595 bool PlatformCALayerRemote::requiresCustomAppearanceUpdateOnBoundsChange() const
597 return m_properties.customAppearance == GraphicsLayer::ScrollingShadow;
600 GraphicsLayer::CustomAppearance PlatformCALayerRemote::customAppearance() const
602 return m_properties.customAppearance;
605 void PlatformCALayerRemote::updateCustomAppearance(GraphicsLayer::CustomAppearance customAppearance)
607 m_properties.customAppearance = customAppearance;
608 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
611 GraphicsLayer::CustomBehavior PlatformCALayerRemote::customBehavior() const
613 return m_properties.customBehavior;
616 void PlatformCALayerRemote::updateCustomBehavior(GraphicsLayer::CustomBehavior customBehavior)
618 m_properties.customBehavior = customBehavior;
619 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomBehaviorChanged);
622 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
624 return PlatformCALayerRemote::create(layerType, client, m_context);
627 void PlatformCALayerRemote::enumerateRectsBeingDrawn(CGContextRef context, void (^block)(CGRect))
629 m_properties.backingStore->enumerateRectsBeingDrawn(context, block);
632 uint32_t PlatformCALayerRemote::hostingContextID()
634 ASSERT_NOT_REACHED();
638 } // namespace WebKit