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)
122 if (m_properties.backingStore && (!owner() || !owner()->platformCALayerDrawsContent())) {
123 m_properties.backingStore = nullptr;
124 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
127 if (m_properties.backingStore && m_properties.backingStore->display())
128 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
130 if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
131 if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
132 m_properties.children.clear();
133 for (const auto& layer : m_children)
134 m_properties.children.append(layer->layerID());
137 if (m_layerType == LayerTypeCustom) {
138 RemoteLayerTreePropertyApplier::applyProperties(platformLayer(), nullptr, m_properties, RemoteLayerTreePropertyApplier::RelatedLayerMap());
143 transaction.layerPropertiesChanged(this, m_properties);
146 for (size_t i = 0; i < m_children.size(); ++i) {
147 PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
148 ASSERT(child->superlayer() == this);
149 child->recursiveBuildTransaction(transaction);
153 m_maskLayer->recursiveBuildTransaction(transaction);
156 void PlatformCALayerRemote::didCommit()
158 m_properties.addedAnimations.clear();
159 m_properties.keyPathsOfAnimationsToRemove.clear();
160 m_properties.resetChangedProperties();
163 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
166 m_owner->platformCALayerAnimationStarted(beginTime);
169 void PlatformCALayerRemote::ensureBackingStore()
171 if (!m_properties.backingStore)
172 m_properties.backingStore = std::make_unique<RemoteLayerBackingStore>(m_context);
174 updateBackingStore();
177 void PlatformCALayerRemote::updateBackingStore()
179 if (!m_properties.backingStore)
182 m_properties.backingStore->ensureBackingStore(this, expandedIntSize(m_properties.bounds.size()), m_properties.contentsScale, m_acceleratesDrawing, m_properties.opaque);
185 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
187 ensureBackingStore();
190 m_properties.backingStore->setNeedsDisplay();
194 // FIXME: Need to map this through contentsRect/etc.
195 m_properties.backingStore->setNeedsDisplay(enclosingIntRect(*rect));
198 void PlatformCALayerRemote::setContentsChanged()
202 PlatformCALayer* PlatformCALayerRemote::superlayer() const
207 void PlatformCALayerRemote::removeFromSuperlayer()
212 m_superlayer->removeSublayer(this);
215 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
217 size_t childIndex = m_children.find(layer);
218 if (childIndex != notFound)
219 m_children.remove(childIndex);
220 layer->m_superlayer = nullptr;
221 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
224 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
226 removeAllSublayers();
229 for (const auto& layer : list) {
230 layer->removeFromSuperlayer();
231 toPlatformCALayerRemote(layer.get())->m_superlayer = this;
234 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
237 void PlatformCALayerRemote::removeAllSublayers()
239 PlatformCALayerList layersToRemove = m_children;
240 for (const auto& layer : layersToRemove)
241 layer->removeFromSuperlayer();
242 ASSERT(m_children.isEmpty());
243 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
246 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
248 RefPtr<PlatformCALayer> layerProtector(layer);
250 layer->removeFromSuperlayer();
251 m_children.append(layer);
252 toPlatformCALayerRemote(layer)->m_superlayer = this;
253 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
256 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
258 RefPtr<PlatformCALayer> layerProtector(layer);
260 layer->removeFromSuperlayer();
261 m_children.insert(index, layer);
262 toPlatformCALayerRemote(layer)->m_superlayer = this;
263 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
266 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
268 ASSERT(reference->superlayer() == this);
269 RefPtr<PlatformCALayer> layerProtector(layer);
271 layer->removeFromSuperlayer();
272 size_t referenceIndex = m_children.find(reference);
273 if (referenceIndex != notFound) {
274 m_children[referenceIndex]->removeFromSuperlayer();
275 m_children.insert(referenceIndex, layer);
276 toPlatformCALayerRemote(layer)->m_superlayer = this;
279 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
282 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
284 PlatformCALayerList layersToMove = toPlatformCALayerRemote(source)->m_children;
286 if (const PlatformCALayerList* customLayers = source->customSublayers()) {
287 for (const auto& layer : *customLayers) {
288 size_t layerIndex = layersToMove.find(layer);
289 if (layerIndex != notFound)
290 layersToMove.remove(layerIndex);
294 setSublayers(layersToMove);
297 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
299 m_properties.addedAnimations.set(key, toPlatformCAAnimationRemote(animation)->properties());
300 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
303 m_context->willStartAnimationOnLayer(this);
306 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
308 m_properties.addedAnimations.remove(key);
309 m_properties.keyPathsOfAnimationsToRemove.add(key);
310 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
313 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
319 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
322 m_maskLayer = toPlatformCALayerRemote(layer);
323 m_properties.maskLayerID = m_maskLayer->layerID();
325 m_maskLayer = nullptr;
326 m_properties.maskLayerID = 0;
329 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
332 bool PlatformCALayerRemote::isOpaque() const
334 return m_properties.opaque;
337 void PlatformCALayerRemote::setOpaque(bool value)
339 m_properties.opaque = value;
340 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
342 updateBackingStore();
345 FloatRect PlatformCALayerRemote::bounds() const
347 return m_properties.bounds;
350 void PlatformCALayerRemote::setBounds(const FloatRect& value)
352 if (value == m_properties.bounds)
355 m_properties.bounds = value;
356 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BoundsChanged);
358 if (requiresCustomAppearanceUpdateOnBoundsChange())
359 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
361 updateBackingStore();
364 FloatPoint3D PlatformCALayerRemote::position() const
366 return m_properties.position;
369 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
371 if (value == m_properties.position)
374 m_properties.position = value;
375 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
378 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
380 return m_properties.anchorPoint;
383 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
385 if (value == m_properties.anchorPoint)
388 m_properties.anchorPoint = value;
389 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
392 TransformationMatrix PlatformCALayerRemote::transform() const
394 return m_properties.transform ? *m_properties.transform : TransformationMatrix();
397 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
399 m_properties.transform = std::make_unique<TransformationMatrix>(value);
400 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
403 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
405 return m_properties.sublayerTransform ? *m_properties.sublayerTransform : TransformationMatrix();
408 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
410 m_properties.sublayerTransform = std::make_unique<TransformationMatrix>(value);
411 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
414 void PlatformCALayerRemote::setHidden(bool value)
416 m_properties.hidden = value;
417 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
420 void PlatformCALayerRemote::setGeometryFlipped(bool value)
422 m_properties.geometryFlipped = value;
423 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
426 bool PlatformCALayerRemote::isDoubleSided() const
428 return m_properties.doubleSided;
431 void PlatformCALayerRemote::setDoubleSided(bool value)
433 m_properties.doubleSided = value;
434 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
437 bool PlatformCALayerRemote::masksToBounds() const
439 return m_properties.masksToBounds;
442 void PlatformCALayerRemote::setMasksToBounds(bool value)
444 if (value == m_properties.masksToBounds)
447 m_properties.masksToBounds = value;
448 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
451 bool PlatformCALayerRemote::acceleratesDrawing() const
453 return m_acceleratesDrawing;
456 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
458 m_acceleratesDrawing = acceleratesDrawing;
459 updateBackingStore();
462 CFTypeRef PlatformCALayerRemote::contents() const
467 void PlatformCALayerRemote::setContents(CFTypeRef value)
471 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
473 m_properties.contentsRect = value;
474 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
477 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
479 m_properties.minificationFilter = value;
480 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
483 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
485 m_properties.magnificationFilter = value;
486 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
489 Color PlatformCALayerRemote::backgroundColor() const
491 return m_properties.backgroundColor;
494 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
496 m_properties.backgroundColor = value;
497 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
500 void PlatformCALayerRemote::setBorderWidth(float value)
502 if (value == m_properties.borderWidth)
505 m_properties.borderWidth = value;
506 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
509 void PlatformCALayerRemote::setBorderColor(const Color& value)
511 if (value == m_properties.borderColor)
514 m_properties.borderColor = value;
515 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
518 float PlatformCALayerRemote::opacity() const
520 return m_properties.opacity;
523 void PlatformCALayerRemote::setOpacity(float value)
525 m_properties.opacity = value;
526 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
529 #if ENABLE(CSS_FILTERS)
530 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
532 m_properties.filters = std::make_unique<FilterOperations>(filters);
533 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
536 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
538 ASSERT_NOT_REACHED();
541 #if ENABLE(CSS_COMPOSITING)
542 void PlatformCALayerRemote::setBlendMode(BlendMode blendMode)
544 m_properties.blendMode = blendMode;
545 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BlendModeChanged);
549 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
551 return PlatformCALayerMac::filtersCanBeComposited(filters);
555 void PlatformCALayerRemote::setName(const String& value)
557 m_properties.name = value;
558 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
561 void PlatformCALayerRemote::setSpeed(float value)
563 m_properties.speed = value;
564 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
567 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
569 m_properties.timeOffset = value;
570 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
573 float PlatformCALayerRemote::contentsScale() const
575 return m_properties.contentsScale;
578 void PlatformCALayerRemote::setContentsScale(float value)
580 m_properties.contentsScale = value;
581 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
583 updateBackingStore();
586 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
588 m_properties.edgeAntialiasingMask = value;
589 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
592 bool PlatformCALayerRemote::requiresCustomAppearanceUpdateOnBoundsChange() const
594 return m_properties.customAppearance == GraphicsLayer::ScrollingShadow;
597 GraphicsLayer::CustomAppearance PlatformCALayerRemote::customAppearance() const
599 return m_properties.customAppearance;
602 void PlatformCALayerRemote::updateCustomAppearance(GraphicsLayer::CustomAppearance customAppearance)
604 m_properties.customAppearance = customAppearance;
605 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
608 GraphicsLayer::CustomBehavior PlatformCALayerRemote::customBehavior() const
610 return m_properties.customBehavior;
613 void PlatformCALayerRemote::updateCustomBehavior(GraphicsLayer::CustomBehavior customBehavior)
615 m_properties.customBehavior = customBehavior;
616 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomBehaviorChanged);
619 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
621 return PlatformCALayerRemote::create(layerType, client, m_context);
624 void PlatformCALayerRemote::enumerateRectsBeingDrawn(CGContextRef context, void (^block)(CGRect))
626 m_properties.backingStore->enumerateRectsBeingDrawn(context, block);
629 uint32_t PlatformCALayerRemote::hostingContextID()
631 ASSERT_NOT_REACHED();
635 } // namespace WebKit