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>
43 #if USE(TEXTURE_MAPPER)
44 #include "texmap/TextureMapperPlatformLayer.h"
53 typedef CALayer PlatformLayer;
55 typedef struct _CACFLayer PlatformLayer;
57 #if USE(TEXTURE_MAPPER)
59 class TextureMapperPlatformLayer;
60 typedef TextureMapperPlatformLayer PlatformLayer;
64 class QGraphicsObject;
67 typedef QGraphicsObject PlatformLayer;
70 #elif PLATFORM(CHROMIUM)
73 typedef LayerChromium PlatformLayer;
75 #elif PLATFORM(ANDROID)
78 typedef LayerAndroid PlatformLayer;
79 typedef void* NativeLayer;
82 typedef void* PlatformLayer;
85 enum LayerTreeAsTextBehaviorFlags {
86 LayerTreeAsTextBehaviorNormal = 0,
87 LayerTreeAsTextDebug = 1 << 0, // Dump extra debugging info like layer addresses.
89 typedef unsigned LayerTreeAsTextBehavior;
94 class GraphicsContext;
99 // Base class for animation values (also used for transitions). Here to
100 // represent values for properties being animated via the GraphicsLayer,
101 // without pulling in style-related data from outside of the platform directory.
102 class AnimationValue {
103 WTF_MAKE_NONCOPYABLE(AnimationValue); WTF_MAKE_FAST_ALLOCATED;
105 AnimationValue(float keyTime, PassRefPtr<TimingFunction> timingFunction = 0)
109 m_timingFunction = timingFunction;
112 virtual ~AnimationValue() { }
114 float keyTime() const { return m_keyTime; }
115 const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
119 RefPtr<TimingFunction> m_timingFunction;
122 // Used to store one float value of an animation.
123 class FloatAnimationValue : public AnimationValue {
125 FloatAnimationValue(float keyTime, float value, PassRefPtr<TimingFunction> timingFunction = 0)
126 : AnimationValue(keyTime, timingFunction)
131 float value() const { return m_value; }
137 // Used to store one transform value in a keyframe list.
138 class TransformAnimationValue : public AnimationValue {
140 TransformAnimationValue(float keyTime, const TransformOperations* value = 0, PassRefPtr<TimingFunction> timingFunction = 0)
141 : AnimationValue(keyTime, timingFunction)
144 m_value = adoptPtr(new TransformOperations(*value));
147 const TransformOperations* value() const { return m_value.get(); }
150 OwnPtr<TransformOperations> m_value;
153 // Used to store a series of values in a keyframe list. Values will all be of the same type,
154 // which can be inferred from the property.
155 class KeyframeValueList {
156 WTF_MAKE_NONCOPYABLE(KeyframeValueList); WTF_MAKE_FAST_ALLOCATED;
159 KeyframeValueList(AnimatedPropertyID property)
160 : m_property(property)
166 deleteAllValues(m_values);
169 AnimatedPropertyID property() const { return m_property; }
171 size_t size() const { return m_values.size(); }
172 const AnimationValue* at(size_t i) const { return m_values.at(i); }
174 // Insert, sorted by keyTime. Takes ownership of the pointer.
175 void insert(const AnimationValue*);
178 Vector<const AnimationValue*> m_values;
179 AnimatedPropertyID m_property;
184 // GraphicsLayer is an abstraction for a rendering surface with backing store,
185 // which may have associated transformation and animations.
187 class GraphicsLayer {
188 WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED;
190 static PassOwnPtr<GraphicsLayer> create(GraphicsLayerClient*);
192 virtual ~GraphicsLayer();
194 GraphicsLayerClient* client() const { return m_client; }
196 // Layer name. Only used to identify layers in debug output
197 const String& name() const { return m_name; }
198 virtual void setName(const String& name) { m_name = name; }
200 GraphicsLayer* parent() const { return m_parent; };
201 void setParent(GraphicsLayer* layer) { m_parent = layer; } // Internal use only.
203 // Returns true if the layer has the given layer as an ancestor (excluding self).
204 bool hasAncestor(GraphicsLayer*) const;
206 const Vector<GraphicsLayer*>& children() const { return m_children; }
207 // Returns true if the child list changed.
208 virtual bool setChildren(const Vector<GraphicsLayer*>&);
210 // Add child layers. If the child is already parented, it will be removed from its old parent.
211 virtual void addChild(GraphicsLayer*);
212 virtual void addChildAtIndex(GraphicsLayer*, int index);
213 virtual void addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling);
214 virtual void addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling);
215 virtual bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild);
217 void removeAllChildren();
218 virtual void removeFromParent();
220 GraphicsLayer* maskLayer() const { return m_maskLayer; }
221 virtual void setMaskLayer(GraphicsLayer* layer) { m_maskLayer = layer; }
223 // The given layer will replicate this layer and its children; the replica renders behind this layer.
224 virtual void setReplicatedByLayer(GraphicsLayer*);
225 // Whether this layer is being replicated by another layer.
226 bool isReplicated() const { return m_replicaLayer; }
227 // The layer that replicates this layer (if any).
228 GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
230 const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
231 void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
233 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
234 IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
235 void setOffsetFromRenderer(const IntSize& offset) { m_offsetFromRenderer = offset; }
237 // The position of the layer (the location of its top-left corner in its parent)
238 const FloatPoint& position() const { return m_position; }
239 virtual void setPosition(const FloatPoint& p) { m_position = p; }
241 // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
242 // affects the origin of the transforms.
243 const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
244 virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; }
246 // The bounds of the layer
247 const FloatSize& size() const { return m_size; }
248 virtual void setSize(const FloatSize& size) { m_size = size; }
250 const TransformationMatrix& transform() const { return m_transform; }
251 virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
253 const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
254 virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
256 bool preserves3D() const { return m_preserves3D; }
257 virtual void setPreserves3D(bool b) { m_preserves3D = b; }
259 bool masksToBounds() const { return m_masksToBounds; }
260 virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
262 bool drawsContent() const { return m_drawsContent; }
263 virtual void setDrawsContent(bool b) { m_drawsContent = b; }
265 bool acceleratesDrawing() const { return m_acceleratesDrawing; }
266 virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
268 // The color used to paint the layer backgrounds
269 const Color& backgroundColor() const { return m_backgroundColor; }
270 virtual void setBackgroundColor(const Color&);
271 virtual void clearBackgroundColor();
272 bool backgroundColorSet() const { return m_backgroundColorSet; }
274 // opaque means that we know the layer contents have no alpha
275 bool contentsOpaque() const { return m_contentsOpaque; }
276 virtual void setContentsOpaque(bool b) { m_contentsOpaque = b; }
278 bool backfaceVisibility() const { return m_backfaceVisibility; }
279 virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
281 float opacity() const { return m_opacity; }
282 virtual void setOpacity(float opacity) { m_opacity = opacity; }
284 // Some GraphicsLayers paint only the foreground or the background content
285 GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
286 void setPaintingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; }
288 virtual void setNeedsDisplay() = 0;
289 // mark the given rect (in layer coords) as needing dispay. Never goes deep.
290 virtual void setNeedsDisplayInRect(const FloatRect&) = 0;
292 virtual void setContentsNeedsDisplay() { };
294 // Set that the position/size of the contents (image or video).
295 IntRect contentsRect() const { return m_contentsRect; }
296 virtual void setContentsRect(const IntRect& r) { m_contentsRect = r; }
298 // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
299 static String animationNameForTransition(AnimatedPropertyID);
301 // Return true if the animation is handled by the compositing system. If this returns
302 // false, the animation will be run by AnimationController.
303 // These methods handle both transitions and keyframe animations.
304 virtual bool addAnimation(const KeyframeValueList&, const IntSize& /*boxSize*/, const Animation*, const String& /*animationName*/, double /*timeOffset*/) { return false; }
305 virtual void pauseAnimation(const String& /*animationName*/, double /*timeOffset*/) { }
306 virtual void removeAnimation(const String& /*animationName*/) { }
308 virtual void suspendAnimations(double time);
309 virtual void resumeAnimations();
312 virtual void setContentsToImage(Image*) { }
313 virtual void setContentsToMedia(PlatformLayer*) { } // video or plug-in
314 virtual void setContentsBackgroundColor(const Color&) { }
315 virtual void setContentsToCanvas(PlatformLayer*) { }
316 virtual bool hasContentsLayer() const { return false; }
318 // Callback from the underlying graphics system to draw layer contents.
319 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
320 // Callback from the underlying graphics system when the layer has been displayed
321 virtual void layerDidDisplay(PlatformLayer*) { }
323 // For hosting this GraphicsLayer in a native layer hierarchy.
324 virtual PlatformLayer* platformLayer() const { return 0; }
326 // Change the scale at which the contents are rendered. Note that contentsScale may not return
327 // the same value passed to setContentsScale(), because of clamping and hysteresis.
328 virtual float contentsScale() const { return 1; }
329 virtual void setContentsScale(float) { }
331 void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
333 int repaintCount() const { return m_repaintCount; }
334 int incrementRepaintCount() { return ++m_repaintCount; }
336 enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
338 // Flippedness of the contents of this layer. Does not affect sublayer geometry.
339 virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
340 CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
342 bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
343 bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
345 void updateDebugIndicators();
347 virtual void setDebugBackgroundColor(const Color&) { }
348 virtual void setDebugBorder(const Color&, float /*borderWidth*/) { }
349 // z-position is the z-equivalent of position(). It's only used for debugging purposes.
350 virtual float zPosition() const { return m_zPosition; }
351 virtual void setZPosition(float);
353 virtual void distributeOpacity(float);
354 virtual float accumulatedOpacity() const;
356 // Some compositing systems may do internal batching to synchronize compositing updates
357 // with updates drawn into the window. These methods flush internal batched state on this layer
358 // and descendant layers, and this layer only.
359 virtual void syncCompositingState() { }
360 virtual void syncCompositingStateForThisLayerOnly() { }
362 // Return a string with a human readable form of the layer tree, If debug is true
363 // pointers for the layers and timing data will be included in the returned string.
364 String layerTreeAsText(LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
366 bool usingTiledLayer() const { return m_usingTiledLayer; }
370 typedef Vector<TransformOperation::OperationType> TransformOperationList;
371 // Given a list of TransformAnimationValues, return an array of transform operations.
372 // On return, if hasBigRotation is true, functions contain rotations of >= 180 degrees
373 static void fetchTransformOperationList(const KeyframeValueList&, TransformOperationList&, bool& isValid, bool& hasBigRotation);
375 virtual void setOpacityInternal(float) { }
377 // The layer being replicated.
378 GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
379 virtual void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
381 GraphicsLayer(GraphicsLayerClient*);
383 void dumpProperties(TextStream&, int indent, LayerTreeAsTextBehavior) const;
385 GraphicsLayerClient* m_client;
388 // Offset from the owning renderer
389 IntSize m_offsetFromRenderer;
391 // Position is relative to the parent GraphicsLayer
392 FloatPoint m_position;
393 FloatPoint3D m_anchorPoint;
395 TransformationMatrix m_transform;
396 TransformationMatrix m_childrenTransform;
398 Color m_backgroundColor;
402 bool m_backgroundColorSet : 1;
403 bool m_contentsOpaque : 1;
404 bool m_preserves3D: 1;
405 bool m_backfaceVisibility : 1;
406 bool m_usingTiledLayer : 1;
407 bool m_masksToBounds : 1;
408 bool m_drawsContent : 1;
409 bool m_acceleratesDrawing : 1;
411 GraphicsLayerPaintingPhase m_paintingPhase;
412 CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
414 Vector<GraphicsLayer*> m_children;
415 GraphicsLayer* m_parent;
417 GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
419 GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now.
420 // The replica is not parented; this is the primary reference to it.
421 GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer.
422 FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica.
424 IntRect m_contentsRect;
430 } // namespace WebCore
433 // Outside the WebCore namespace for ease of invocation from gdb.
434 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
437 #endif // USE(ACCELERATED_COMPOSITING)
439 #endif // GraphicsLayer_h