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 if (m_properties.backingStore && (!owner() || !owner()->platformCALayerDrawsContent())) {
122 m_properties.backingStore = nullptr;
123 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
126 if (m_properties.backingStore && m_properties.backingStore->display())
127 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
129 if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
130 if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
131 m_properties.children.clear();
132 for (const auto& layer : m_children)
133 m_properties.children.append(layer->layerID());
136 if (m_layerType == LayerTypeCustom) {
137 RemoteLayerTreePropertyApplier::applyProperties(platformLayer(), nullptr, m_properties, RemoteLayerTreePropertyApplier::RelatedLayerMap());
142 transaction.layerPropertiesChanged(this, m_properties);
145 for (size_t i = 0; i < m_children.size(); ++i) {
146 PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
147 ASSERT(child->superlayer() == this);
148 child->recursiveBuildTransaction(transaction);
152 m_maskLayer->recursiveBuildTransaction(transaction);
155 void PlatformCALayerRemote::didCommit()
157 m_properties.addedAnimations.clear();
158 m_properties.keyPathsOfAnimationsToRemove.clear();
159 m_properties.resetChangedProperties();
162 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
165 m_owner->platformCALayerAnimationStarted(beginTime);
168 void PlatformCALayerRemote::ensureBackingStore()
170 if (!m_properties.backingStore)
171 m_properties.backingStore = std::make_unique<RemoteLayerBackingStore>(m_context);
173 updateBackingStore();
176 void PlatformCALayerRemote::updateBackingStore()
178 if (!m_properties.backingStore)
181 m_properties.backingStore->ensureBackingStore(this, expandedIntSize(m_properties.bounds.size()), m_properties.contentsScale, m_acceleratesDrawing, m_properties.opaque);
184 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
186 ensureBackingStore();
189 m_properties.backingStore->setNeedsDisplay();
193 // FIXME: Need to map this through contentsRect/etc.
194 m_properties.backingStore->setNeedsDisplay(enclosingIntRect(*rect));
197 void PlatformCALayerRemote::setContentsChanged()
201 PlatformCALayer* PlatformCALayerRemote::superlayer() const
206 void PlatformCALayerRemote::removeFromSuperlayer()
211 m_superlayer->removeSublayer(this);
214 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
216 size_t childIndex = m_children.find(layer);
217 if (childIndex != notFound)
218 m_children.remove(childIndex);
219 layer->m_superlayer = nullptr;
220 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
223 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
225 removeAllSublayers();
228 for (const auto& layer : list) {
229 layer->removeFromSuperlayer();
230 toPlatformCALayerRemote(layer.get())->m_superlayer = this;
233 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
236 void PlatformCALayerRemote::removeAllSublayers()
238 PlatformCALayerList layersToRemove = m_children;
239 for (const auto& layer : layersToRemove)
240 layer->removeFromSuperlayer();
241 ASSERT(m_children.isEmpty());
242 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
245 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
247 RefPtr<PlatformCALayer> layerProtector(layer);
249 layer->removeFromSuperlayer();
250 m_children.append(layer);
251 toPlatformCALayerRemote(layer)->m_superlayer = this;
252 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
255 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
257 RefPtr<PlatformCALayer> layerProtector(layer);
259 layer->removeFromSuperlayer();
260 m_children.insert(index, layer);
261 toPlatformCALayerRemote(layer)->m_superlayer = this;
262 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
265 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
267 ASSERT(reference->superlayer() == this);
268 RefPtr<PlatformCALayer> layerProtector(layer);
270 layer->removeFromSuperlayer();
271 size_t referenceIndex = m_children.find(reference);
272 if (referenceIndex != notFound) {
273 m_children[referenceIndex]->removeFromSuperlayer();
274 m_children.insert(referenceIndex, layer);
275 toPlatformCALayerRemote(layer)->m_superlayer = this;
278 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
281 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
283 PlatformCALayerList layersToMove = toPlatformCALayerRemote(source)->m_children;
285 if (const PlatformCALayerList* customLayers = source->customSublayers()) {
286 for (const auto& layer : *customLayers) {
287 size_t layerIndex = layersToMove.find(layer);
288 if (layerIndex != notFound)
289 layersToMove.remove(layerIndex);
293 setSublayers(layersToMove);
296 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
298 m_properties.addedAnimations.set(key, toPlatformCAAnimationRemote(animation)->properties());
299 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
302 m_context->willStartAnimationOnLayer(this);
305 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
307 m_properties.addedAnimations.remove(key);
308 m_properties.keyPathsOfAnimationsToRemove.add(key);
309 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnimationsChanged);
312 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
318 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
321 m_maskLayer = toPlatformCALayerRemote(layer);
322 m_properties.maskLayerID = m_maskLayer->layerID();
324 m_maskLayer = nullptr;
325 m_properties.maskLayerID = 0;
328 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
331 bool PlatformCALayerRemote::isOpaque() const
333 return m_properties.opaque;
336 void PlatformCALayerRemote::setOpaque(bool value)
338 m_properties.opaque = value;
339 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
341 updateBackingStore();
344 FloatRect PlatformCALayerRemote::bounds() const
346 return m_properties.bounds;
349 void PlatformCALayerRemote::setBounds(const FloatRect& value)
351 if (value == m_properties.bounds)
354 m_properties.bounds = value;
355 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BoundsChanged);
357 if (requiresCustomAppearanceUpdateOnBoundsChange())
358 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
360 updateBackingStore();
363 FloatPoint3D PlatformCALayerRemote::position() const
365 return m_properties.position;
368 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
370 if (value == m_properties.position)
373 m_properties.position = value;
374 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
377 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
379 return m_properties.anchorPoint;
382 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
384 if (value == m_properties.anchorPoint)
387 m_properties.anchorPoint = value;
388 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
391 TransformationMatrix PlatformCALayerRemote::transform() const
393 return m_properties.transform ? *m_properties.transform : TransformationMatrix();
396 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
398 m_properties.transform = std::make_unique<TransformationMatrix>(value);
399 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
402 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
404 return m_properties.sublayerTransform ? *m_properties.sublayerTransform : TransformationMatrix();
407 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
409 m_properties.sublayerTransform = std::make_unique<TransformationMatrix>(value);
410 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
413 void PlatformCALayerRemote::setHidden(bool value)
415 m_properties.hidden = value;
416 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
419 void PlatformCALayerRemote::setGeometryFlipped(bool value)
421 m_properties.geometryFlipped = value;
422 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
425 bool PlatformCALayerRemote::isDoubleSided() const
427 return m_properties.doubleSided;
430 void PlatformCALayerRemote::setDoubleSided(bool value)
432 m_properties.doubleSided = value;
433 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
436 bool PlatformCALayerRemote::masksToBounds() const
438 return m_properties.masksToBounds;
441 void PlatformCALayerRemote::setMasksToBounds(bool value)
443 if (value == m_properties.masksToBounds)
446 m_properties.masksToBounds = value;
447 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
450 bool PlatformCALayerRemote::acceleratesDrawing() const
452 return m_acceleratesDrawing;
455 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
457 m_acceleratesDrawing = acceleratesDrawing;
458 updateBackingStore();
461 CFTypeRef PlatformCALayerRemote::contents() const
466 void PlatformCALayerRemote::setContents(CFTypeRef value)
470 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
472 m_properties.contentsRect = value;
473 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
476 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
478 m_properties.minificationFilter = value;
479 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
482 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
484 m_properties.magnificationFilter = value;
485 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
488 Color PlatformCALayerRemote::backgroundColor() const
490 return m_properties.backgroundColor;
493 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
495 m_properties.backgroundColor = value;
496 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
499 void PlatformCALayerRemote::setBorderWidth(float value)
501 if (value == m_properties.borderWidth)
504 m_properties.borderWidth = value;
505 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
508 void PlatformCALayerRemote::setBorderColor(const Color& value)
510 if (value == m_properties.borderColor)
513 m_properties.borderColor = value;
514 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
517 float PlatformCALayerRemote::opacity() const
519 return m_properties.opacity;
522 void PlatformCALayerRemote::setOpacity(float value)
524 m_properties.opacity = value;
525 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
528 #if ENABLE(CSS_FILTERS)
529 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
531 m_properties.filters = std::make_unique<FilterOperations>(filters);
532 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
535 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
537 ASSERT_NOT_REACHED();
540 #if ENABLE(CSS_COMPOSITING)
541 void PlatformCALayerRemote::setBlendMode(BlendMode blendMode)
543 m_properties.blendMode = blendMode;
544 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BlendModeChanged);
548 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
550 return PlatformCALayerMac::filtersCanBeComposited(filters);
554 void PlatformCALayerRemote::setName(const String& value)
556 m_properties.name = value;
557 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
560 void PlatformCALayerRemote::setSpeed(float value)
562 m_properties.speed = value;
563 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
566 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
568 m_properties.timeOffset = value;
569 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
572 float PlatformCALayerRemote::contentsScale() const
574 return m_properties.contentsScale;
577 void PlatformCALayerRemote::setContentsScale(float value)
579 m_properties.contentsScale = value;
580 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
582 updateBackingStore();
585 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
587 m_properties.edgeAntialiasingMask = value;
588 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
591 bool PlatformCALayerRemote::requiresCustomAppearanceUpdateOnBoundsChange() const
593 return m_properties.customAppearance == GraphicsLayer::ScrollingShadow;
596 GraphicsLayer::CustomAppearance PlatformCALayerRemote::customAppearance() const
598 return m_properties.customAppearance;
601 void PlatformCALayerRemote::updateCustomAppearance(GraphicsLayer::CustomAppearance customAppearance)
603 m_properties.customAppearance = customAppearance;
604 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomAppearanceChanged);
607 GraphicsLayer::CustomBehavior PlatformCALayerRemote::customBehavior() const
609 return m_properties.customBehavior;
612 void PlatformCALayerRemote::updateCustomBehavior(GraphicsLayer::CustomBehavior customBehavior)
614 m_properties.customBehavior = customBehavior;
615 m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::CustomBehaviorChanged);
618 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
620 return PlatformCALayerRemote::create(layerType, client, m_context);
623 void PlatformCALayerRemote::enumerateRectsBeingDrawn(CGContextRef context, void (^block)(CGRect))
625 m_properties.backingStore->enumerateRectsBeingDrawn(context, block);
628 uint32_t PlatformCALayerRemote::hostingContextID()
630 ASSERT_NOT_REACHED();
634 } // namespace WebKit