2 * Copyright (C) 2009 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 COMPUTER, 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.
26 #ifndef GraphicsLayer_h
27 #define GraphicsLayer_h
29 #if USE(ACCELERATED_COMPOSITING)
31 #include "Animation.h"
33 #include "FloatPoint.h"
34 #include "FloatPoint3D.h"
35 #include "FloatSize.h"
36 #include "GraphicsLayerClient.h"
38 #include "TransformationMatrix.h"
39 #include "TransformOperations.h"
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassOwnPtr.h>
49 typedef CALayer PlatformLayer;
51 typedef struct _CACFLayer PlatformLayer;
53 #if USE(TEXTURE_MAPPER)
55 class TextureMapperPlatformLayer;
56 typedef TextureMapperPlatformLayer PlatformLayer;
60 class QGraphicsObject;
63 typedef QGraphicsObject PlatformLayer;
66 #elif PLATFORM(CHROMIUM)
69 typedef LayerChromium PlatformLayer;
72 #if USE(TIZEN_TEXTURE_MAPPER)
74 class TextureMapperPlatformLayer;
75 typedef TextureMapperPlatformLayer PlatformLayer;
80 typedef EflLayer PlatformLayer;
84 typedef void* PlatformLayer;
87 enum LayerTreeAsTextBehaviorFlags {
88 LayerTreeAsTextBehaviorNormal = 0,
89 LayerTreeAsTextDebug = 1 << 0, // Dump extra debugging info like layer addresses.
91 typedef unsigned LayerTreeAsTextBehavior;
96 class GraphicsContext;
101 // Base class for animation values (also used for transitions). Here to
102 // represent values for properties being animated via the GraphicsLayer,
103 // without pulling in style-related data from outside of the platform directory.
104 class AnimationValue {
106 AnimationValue(float keyTime, PassRefPtr<TimingFunction> timingFunction = 0)
110 m_timingFunction = timingFunction;
113 virtual ~AnimationValue() { }
115 float keyTime() const { return m_keyTime; }
116 const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
117 virtual AnimationValue* clone() const = 0;
121 RefPtr<TimingFunction> m_timingFunction;
124 // Used to store one float value of an animation.
125 class FloatAnimationValue : public AnimationValue {
127 FloatAnimationValue(float keyTime, float value, PassRefPtr<TimingFunction> timingFunction = 0)
128 : AnimationValue(keyTime, timingFunction)
132 virtual AnimationValue* clone() const { return new FloatAnimationValue(*this); }
134 float value() const { return m_value; }
140 // Used to store one transform value in a keyframe list.
141 class TransformAnimationValue : public AnimationValue {
143 TransformAnimationValue(float keyTime, const TransformOperations* value = 0, PassRefPtr<TimingFunction> timingFunction = 0)
144 : AnimationValue(keyTime, timingFunction)
149 virtual AnimationValue* clone() const { return new TransformAnimationValue(*this); }
151 const TransformOperations* value() const { return &m_value; }
154 TransformOperations m_value;
157 // Used to store a series of values in a keyframe list. Values will all be of the same type,
158 // which can be inferred from the property.
159 class KeyframeValueList {
162 KeyframeValueList(AnimatedPropertyID property)
163 : m_property(property)
167 KeyframeValueList(const KeyframeValueList& other)
168 : m_property(other.property())
170 for (size_t i = 0; i < other.m_values.size(); ++i)
171 m_values.append(other.m_values[i]->clone());
176 deleteAllValues(m_values);
179 AnimatedPropertyID property() const { return m_property; }
181 size_t size() const { return m_values.size(); }
182 const AnimationValue* at(size_t i) const { return m_values.at(i); }
184 // Insert, sorted by keyTime. Takes ownership of the pointer.
185 void insert(const AnimationValue*);
188 Vector<const AnimationValue*> m_values;
189 AnimatedPropertyID m_property;
194 // GraphicsLayer is an abstraction for a rendering surface with backing store,
195 // which may have associated transformation and animations.
197 class GraphicsLayer {
198 WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED;
200 static PassOwnPtr<GraphicsLayer> create(GraphicsLayerClient*);
202 virtual ~GraphicsLayer();
204 GraphicsLayerClient* client() const { return m_client; }
206 // Layer name. Only used to identify layers in debug output
207 const String& name() const { return m_name; }
208 virtual void setName(const String& name) { m_name = name; }
210 GraphicsLayer* parent() const { return m_parent; };
211 void setParent(GraphicsLayer*); // Internal use only.
213 // Returns true if the layer has the given layer as an ancestor (excluding self).
214 bool hasAncestor(GraphicsLayer*) const;
216 const Vector<GraphicsLayer*>& children() const { return m_children; }
217 // Returns true if the child list changed.
218 virtual bool setChildren(const Vector<GraphicsLayer*>&);
220 // Add child layers. If the child is already parented, it will be removed from its old parent.
221 virtual void addChild(GraphicsLayer*);
222 virtual void addChildAtIndex(GraphicsLayer*, int index);
223 virtual void addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling);
224 virtual void addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling);
225 virtual bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild);
227 void removeAllChildren();
228 virtual void removeFromParent();
230 GraphicsLayer* maskLayer() const { return m_maskLayer; }
231 virtual void setMaskLayer(GraphicsLayer* layer) { m_maskLayer = layer; }
233 // The given layer will replicate this layer and its children; the replica renders behind this layer.
234 virtual void setReplicatedByLayer(GraphicsLayer*);
235 // Whether this layer is being replicated by another layer.
236 bool isReplicated() const { return m_replicaLayer; }
237 // The layer that replicates this layer (if any).
238 GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
240 const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
241 void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
243 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
244 IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
245 void setOffsetFromRenderer(const IntSize& offset) { m_offsetFromRenderer = offset; }
247 // The position of the layer (the location of its top-left corner in its parent)
248 const FloatPoint& position() const { return m_position; }
249 virtual void setPosition(const FloatPoint& p) { m_position = p; }
251 // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
252 // affects the origin of the transforms.
253 const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
254 virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; }
256 // The size of the layer.
257 const FloatSize& size() const { return m_size; }
258 virtual void setSize(const FloatSize& size) { m_size = size; }
260 // The boundOrigin affects the offset at which content is rendered, and sublayers are positioned.
261 const FloatPoint& boundsOrigin() const { return m_boundsOrigin; }
262 virtual void setBoundsOrigin(const FloatPoint& origin) { m_boundsOrigin = origin; }
264 const TransformationMatrix& transform() const { return m_transform; }
265 virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
267 const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
268 virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
270 bool preserves3D() const { return m_preserves3D; }
271 virtual void setPreserves3D(bool b) { m_preserves3D = b; }
273 bool masksToBounds() const { return m_masksToBounds; }
274 virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
276 bool drawsContent() const { return m_drawsContent; }
277 virtual void setDrawsContent(bool b) { m_drawsContent = b; }
279 bool contentsAreVisible() const { return m_contentsVisible; }
280 virtual void setContentsVisible(bool b) { m_contentsVisible = b; }
282 bool acceleratesDrawing() const { return m_acceleratesDrawing; }
283 virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
285 // The color used to paint the layer backgrounds
286 const Color& backgroundColor() const { return m_backgroundColor; }
287 virtual void setBackgroundColor(const Color&);
288 virtual void clearBackgroundColor();
289 bool backgroundColorSet() const { return m_backgroundColorSet; }
291 // opaque means that we know the layer contents have no alpha
292 bool contentsOpaque() const { return m_contentsOpaque; }
293 virtual void setContentsOpaque(bool b) { m_contentsOpaque = b; }
295 bool backfaceVisibility() const { return m_backfaceVisibility; }
296 virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
298 float opacity() const { return m_opacity; }
299 virtual void setOpacity(float opacity) { m_opacity = opacity; }
301 // Some GraphicsLayers paint only the foreground or the background content
302 GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
303 void setPaintingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; }
305 virtual void setNeedsDisplay() = 0;
306 // mark the given rect (in layer coords) as needing dispay. Never goes deep.
307 virtual void setNeedsDisplayInRect(const FloatRect&) = 0;
309 virtual void setContentsNeedsDisplay() { };
311 // Set that the position/size of the contents (image or video).
312 IntRect contentsRect() const { return m_contentsRect; }
313 virtual void setContentsRect(const IntRect& r) { m_contentsRect = r; }
315 // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
316 static String animationNameForTransition(AnimatedPropertyID);
318 // Return true if the animation is handled by the compositing system. If this returns
319 // false, the animation will be run by AnimationController.
320 // These methods handle both transitions and keyframe animations.
321 virtual bool addAnimation(const KeyframeValueList&, const IntSize& /*boxSize*/, const Animation*, const String& /*animationName*/, double /*timeOffset*/) { return false; }
322 virtual void pauseAnimation(const String& /*animationName*/, double /*timeOffset*/) { }
323 virtual void removeAnimation(const String& /*animationName*/) { }
325 virtual void suspendAnimations(double time);
326 virtual void resumeAnimations();
329 virtual void setContentsToImage(Image*) { }
330 virtual void setContentsToMedia(PlatformLayer*) { } // video or plug-in
331 virtual void setContentsToBackgroundColor(const Color&) { }
332 virtual void setContentsToCanvas(PlatformLayer*) { }
333 virtual bool hasContentsLayer() const { return false; }
335 // Callback from the underlying graphics system to draw layer contents.
336 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
337 // Callback from the underlying graphics system when the layer has been displayed
338 virtual void layerDidDisplay(PlatformLayer*) { }
340 // For hosting this GraphicsLayer in a native layer hierarchy.
341 virtual PlatformLayer* platformLayer() const { return 0; }
343 void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
345 int repaintCount() const { return m_repaintCount; }
346 int incrementRepaintCount() { return ++m_repaintCount; }
348 enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
350 // Flippedness of the contents of this layer. Does not affect sublayer geometry.
351 virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
352 CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
354 bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
355 bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
357 void updateDebugIndicators();
359 virtual void setDebugBackgroundColor(const Color&) { }
360 virtual void setDebugBorder(const Color&, float /*borderWidth*/) { }
361 // z-position is the z-equivalent of position(). It's only used for debugging purposes.
362 virtual float zPosition() const { return m_zPosition; }
363 virtual void setZPosition(float);
365 virtual void distributeOpacity(float);
366 virtual float accumulatedOpacity() const;
368 virtual void setMaintainsPixelAlignment(bool maintainsAlignment) { m_maintainsPixelAlignment = maintainsAlignment; }
369 virtual bool maintainsPixelAlignment() const { return m_maintainsPixelAlignment; }
371 void setAppliesPageScale(bool appliesScale = true) { m_appliesPageScale = appliesScale; }
372 bool appliesPageScale() const { return m_appliesPageScale; }
374 float pageScaleFactor() const { return m_client ? m_client->pageScaleFactor() : 1; }
375 float deviceScaleFactor() const { return m_client ? m_client->deviceScaleFactor() : 1; }
377 virtual void deviceOrPageScaleFactorChanged() { }
378 void noteDeviceOrPageScaleFactorChangedIncludingDescendants();
380 // Some compositing systems may do internal batching to synchronize compositing updates
381 // with updates drawn into the window. These methods flush internal batched state on this layer
382 // and descendant layers, and this layer only.
383 virtual void syncCompositingState(const FloatRect& /* clipRect */) { }
384 virtual void syncCompositingStateForThisLayerOnly() { }
386 // Return a string with a human readable form of the layer tree, If debug is true
387 // pointers for the layers and timing data will be included in the returned string.
388 String layerTreeAsText(LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
390 bool usingTiledLayer() const { return m_usingTiledLayer; }
392 #if PLATFORM(QT) || PLATFORM(EFL)
393 // This allows several alternative GraphicsLayer implementations in the same port,
394 // e.g. if a different GraphicsLayer implementation is needed in WebKit1 vs. WebKit2.
395 typedef PassOwnPtr<GraphicsLayer> GraphicsLayerFactory(GraphicsLayerClient*);
396 static void setGraphicsLayerFactory(GraphicsLayerFactory);
401 typedef Vector<TransformOperation::OperationType> TransformOperationList;
402 // Given a list of TransformAnimationValues, return an array of transform operations.
403 // On return, if hasBigRotation is true, functions contain rotations of >= 180 degrees
404 static void fetchTransformOperationList(const KeyframeValueList&, TransformOperationList&, bool& isValid, bool& hasBigRotation);
406 virtual void setOpacityInternal(float) { }
408 // The layer being replicated.
409 GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
410 virtual void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
412 GraphicsLayer(GraphicsLayerClient*);
414 void dumpProperties(TextStream&, int indent, LayerTreeAsTextBehavior) const;
416 GraphicsLayerClient* m_client;
419 // Offset from the owning renderer
420 IntSize m_offsetFromRenderer;
422 // Position is relative to the parent GraphicsLayer
423 FloatPoint m_position;
424 FloatPoint3D m_anchorPoint;
426 FloatPoint m_boundsOrigin;
428 TransformationMatrix m_transform;
429 TransformationMatrix m_childrenTransform;
431 Color m_backgroundColor;
435 bool m_backgroundColorSet : 1;
436 bool m_contentsOpaque : 1;
437 bool m_preserves3D: 1;
438 bool m_backfaceVisibility : 1;
439 bool m_usingTiledLayer : 1;
440 bool m_masksToBounds : 1;
441 bool m_drawsContent : 1;
442 bool m_contentsVisible : 1;
443 bool m_acceleratesDrawing : 1;
444 bool m_maintainsPixelAlignment : 1;
445 bool m_appliesPageScale : 1; // Set for the layer which has the page scale applied to it.
447 GraphicsLayerPaintingPhase m_paintingPhase;
448 CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
450 Vector<GraphicsLayer*> m_children;
451 GraphicsLayer* m_parent;
453 GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
455 GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now.
456 // The replica is not parented; this is the primary reference to it.
457 GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer.
458 FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica.
460 IntRect m_contentsRect;
464 #if PLATFORM(QT) || PLATFORM(EFL)
465 static GraphicsLayer::GraphicsLayerFactory* s_graphicsLayerFactory;
470 } // namespace WebCore
473 // Outside the WebCore namespace for ease of invocation from gdb.
474 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
477 #endif // USE(ACCELERATED_COMPOSITING)
479 #endif // GraphicsLayer_h