• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #ifndef GraphicsLayer_h
27 #define GraphicsLayer_h
28 
29 #if USE(ACCELERATED_COMPOSITING)
30 
31 #include "Animation.h"
32 #include "Color.h"
33 #include "FloatPoint.h"
34 #include "FloatPoint3D.h"
35 #include "FloatSize.h"
36 #include "GraphicsLayerClient.h"
37 #include "IntRect.h"
38 #include "TransformationMatrix.h"
39 #include "TransformOperations.h"
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassOwnPtr.h>
42 
43 #if USE(TEXTURE_MAPPER)
44 #include "texmap/TextureMapperPlatformLayer.h"
45 #endif
46 
47 #if PLATFORM(MAC)
48 #ifdef __OBJC__
49 @class CALayer;
50 #else
51 class CALayer;
52 #endif
53 typedef CALayer PlatformLayer;
54 #elif PLATFORM(WIN)
55 typedef struct _CACFLayer PlatformLayer;
56 #elif PLATFORM(QT)
57 #if USE(TEXTURE_MAPPER)
58 namespace WebCore {
59 class TextureMapperPlatformLayer;
60 typedef TextureMapperPlatformLayer PlatformLayer;
61 };
62 #else
63 QT_BEGIN_NAMESPACE
64 class QGraphicsObject;
65 QT_END_NAMESPACE
66 namespace WebCore {
67 typedef QGraphicsObject PlatformLayer;
68 }
69 #endif
70 #elif PLATFORM(CHROMIUM)
71 namespace WebCore {
72 class LayerChromium;
73 typedef LayerChromium PlatformLayer;
74 }
75 #elif PLATFORM(ANDROID)
76 namespace WebCore {
77 class LayerAndroid;
78 typedef LayerAndroid PlatformLayer;
79 typedef void* NativeLayer;
80 }
81 #else
82 typedef void* PlatformLayer;
83 #endif
84 
85 enum LayerTreeAsTextBehaviorFlags {
86     LayerTreeAsTextBehaviorNormal = 0,
87     LayerTreeAsTextDebug = 1 << 0, // Dump extra debugging info like layer addresses.
88 };
89 typedef unsigned LayerTreeAsTextBehavior;
90 
91 namespace WebCore {
92 
93 class FloatPoint3D;
94 class GraphicsContext;
95 class Image;
96 class TextStream;
97 class TimingFunction;
98 
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;
104 public:
105     AnimationValue(float keyTime, PassRefPtr<TimingFunction> timingFunction = 0)
m_keyTime(keyTime)106         : m_keyTime(keyTime)
107     {
108         if (timingFunction)
109             m_timingFunction = timingFunction;
110     }
111 
~AnimationValue()112     virtual ~AnimationValue() { }
113 
keyTime()114     float keyTime() const { return m_keyTime; }
timingFunction()115     const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
116 
117 private:
118     float m_keyTime;
119     RefPtr<TimingFunction> m_timingFunction;
120 };
121 
122 // Used to store one float value of an animation.
123 class FloatAnimationValue : public AnimationValue {
124 public:
125     FloatAnimationValue(float keyTime, float value, PassRefPtr<TimingFunction> timingFunction = 0)
AnimationValue(keyTime,timingFunction)126         : AnimationValue(keyTime, timingFunction)
127         , m_value(value)
128     {
129     }
130 
value()131     float value() const { return m_value; }
132 
133 private:
134     float m_value;
135 };
136 
137 // Used to store one transform value in a keyframe list.
138 class TransformAnimationValue : public AnimationValue {
139 public:
140     TransformAnimationValue(float keyTime, const TransformOperations* value = 0, PassRefPtr<TimingFunction> timingFunction = 0)
AnimationValue(keyTime,timingFunction)141         : AnimationValue(keyTime, timingFunction)
142     {
143         if (value)
144             m_value = adoptPtr(new TransformOperations(*value));
145     }
146 
value()147     const TransformOperations* value() const { return m_value.get(); }
148 
149 private:
150     OwnPtr<TransformOperations> m_value;
151 };
152 
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;
157 public:
158 
KeyframeValueList(AnimatedPropertyID property)159     KeyframeValueList(AnimatedPropertyID property)
160         : m_property(property)
161     {
162     }
163 
~KeyframeValueList()164     ~KeyframeValueList()
165     {
166         deleteAllValues(m_values);
167     }
168 
property()169     AnimatedPropertyID property() const { return m_property; }
170 
size()171     size_t size() const { return m_values.size(); }
at(size_t i)172     const AnimationValue* at(size_t i) const { return m_values.at(i); }
173 
174     // Insert, sorted by keyTime. Takes ownership of the pointer.
175     void insert(const AnimationValue*);
176 
177 protected:
178     Vector<const AnimationValue*> m_values;
179     AnimatedPropertyID m_property;
180 };
181 
182 
183 
184 // GraphicsLayer is an abstraction for a rendering surface with backing store,
185 // which may have associated transformation and animations.
186 
187 class GraphicsLayer {
188     WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED;
189 public:
190     static PassOwnPtr<GraphicsLayer> create(GraphicsLayerClient*);
191 
192     virtual ~GraphicsLayer();
193 
client()194     GraphicsLayerClient* client() const { return m_client; }
195 
196     // Layer name. Only used to identify layers in debug output
name()197     const String& name() const { return m_name; }
setName(const String & name)198     virtual void setName(const String& name) { m_name = name; }
199 
parent()200     GraphicsLayer* parent() const { return m_parent; };
setParent(GraphicsLayer * layer)201     void setParent(GraphicsLayer* layer) { m_parent = layer; } // Internal use only.
202 
203     // Returns true if the layer has the given layer as an ancestor (excluding self).
204     bool hasAncestor(GraphicsLayer*) const;
205 
children()206     const Vector<GraphicsLayer*>& children() const { return m_children; }
207     // Returns true if the child list changed.
208     virtual bool setChildren(const Vector<GraphicsLayer*>&);
209 
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);
216 
217     void removeAllChildren();
218     virtual void removeFromParent();
219 
maskLayer()220     GraphicsLayer* maskLayer() const { return m_maskLayer; }
setMaskLayer(GraphicsLayer * layer)221     virtual void setMaskLayer(GraphicsLayer* layer) { m_maskLayer = layer; }
222 
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.
isReplicated()226     bool isReplicated() const { return m_replicaLayer; }
227     // The layer that replicates this layer (if any).
replicaLayer()228     GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
229 
replicatedLayerPosition()230     const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
setReplicatedLayerPosition(const FloatPoint & p)231     void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
232 
233     // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
offsetFromRenderer()234     IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
setOffsetFromRenderer(const IntSize & offset)235     void setOffsetFromRenderer(const IntSize& offset) { m_offsetFromRenderer = offset; }
236 
237     // The position of the layer (the location of its top-left corner in its parent)
position()238     const FloatPoint& position() const { return m_position; }
setPosition(const FloatPoint & p)239     virtual void setPosition(const FloatPoint& p) { m_position = p; }
240 
241     // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
242     // affects the origin of the transforms.
anchorPoint()243     const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
setAnchorPoint(const FloatPoint3D & p)244     virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; }
245 
246     // The bounds of the layer
size()247     const FloatSize& size() const { return m_size; }
setSize(const FloatSize & size)248     virtual void setSize(const FloatSize& size) { m_size = size; }
249 
transform()250     const TransformationMatrix& transform() const { return m_transform; }
setTransform(const TransformationMatrix & t)251     virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
252 
childrenTransform()253     const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
setChildrenTransform(const TransformationMatrix & t)254     virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
255 
preserves3D()256     bool preserves3D() const { return m_preserves3D; }
setPreserves3D(bool b)257     virtual void setPreserves3D(bool b) { m_preserves3D = b; }
258 
masksToBounds()259     bool masksToBounds() const { return m_masksToBounds; }
setMasksToBounds(bool b)260     virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
261 
drawsContent()262     bool drawsContent() const { return m_drawsContent; }
setDrawsContent(bool b)263     virtual void setDrawsContent(bool b) { m_drawsContent = b; }
264 
acceleratesDrawing()265     bool acceleratesDrawing() const { return m_acceleratesDrawing; }
setAcceleratesDrawing(bool b)266     virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
267 
268     // The color used to paint the layer backgrounds
backgroundColor()269     const Color& backgroundColor() const { return m_backgroundColor; }
270     virtual void setBackgroundColor(const Color&);
271     virtual void clearBackgroundColor();
backgroundColorSet()272     bool backgroundColorSet() const { return m_backgroundColorSet; }
273 
274     // opaque means that we know the layer contents have no alpha
contentsOpaque()275     bool contentsOpaque() const { return m_contentsOpaque; }
setContentsOpaque(bool b)276     virtual void setContentsOpaque(bool b) { m_contentsOpaque = b; }
277 
backfaceVisibility()278     bool backfaceVisibility() const { return m_backfaceVisibility; }
setBackfaceVisibility(bool b)279     virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
280 
opacity()281     float opacity() const { return m_opacity; }
setOpacity(float opacity)282     virtual void setOpacity(float opacity) { m_opacity = opacity; }
283 
284     // Some GraphicsLayers paint only the foreground or the background content
paintingPhase()285     GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
setPaintingPhase(GraphicsLayerPaintingPhase phase)286     void setPaintingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; }
287 
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;
291 
setContentsNeedsDisplay()292     virtual void setContentsNeedsDisplay() { };
293 
294     // Set that the position/size of the contents (image or video).
contentsRect()295     IntRect contentsRect() const { return m_contentsRect; }
setContentsRect(const IntRect & r)296     virtual void setContentsRect(const IntRect& r) { m_contentsRect = r; }
297 
298     // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
299     static String animationNameForTransition(AnimatedPropertyID);
300 
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.
addAnimation(const KeyframeValueList &,const IntSize &,const Animation *,const String &,double)304     virtual bool addAnimation(const KeyframeValueList&, const IntSize& /*boxSize*/, const Animation*, const String& /*animationName*/, double /*timeOffset*/)  { return false; }
pauseAnimation(const String &,double)305     virtual void pauseAnimation(const String& /*animationName*/, double /*timeOffset*/) { }
removeAnimation(const String &)306     virtual void removeAnimation(const String& /*animationName*/) { }
307 
308     virtual void suspendAnimations(double time);
309     virtual void resumeAnimations();
310 
311     // Layer contents
setContentsToImage(Image *)312     virtual void setContentsToImage(Image*) { }
setContentsToMedia(PlatformLayer *)313     virtual void setContentsToMedia(PlatformLayer*) { } // video or plug-in
setContentsBackgroundColor(const Color &)314     virtual void setContentsBackgroundColor(const Color&) { }
setContentsToCanvas(PlatformLayer *)315     virtual void setContentsToCanvas(PlatformLayer*) { }
hasContentsLayer()316     virtual bool hasContentsLayer() const { return false; }
317 
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
layerDidDisplay(PlatformLayer *)321     virtual void layerDidDisplay(PlatformLayer*) { }
322 
323     // For hosting this GraphicsLayer in a native layer hierarchy.
platformLayer()324     virtual PlatformLayer* platformLayer() const { return 0; }
325 
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.
contentsScale()328     virtual float contentsScale() const { return 1; }
setContentsScale(float)329     virtual void setContentsScale(float) { }
330 
331     void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
332 
repaintCount()333     int repaintCount() const { return m_repaintCount; }
incrementRepaintCount()334     int incrementRepaintCount() { return ++m_repaintCount; }
335 
336     enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
337 
338     // Flippedness of the contents of this layer. Does not affect sublayer geometry.
setContentsOrientation(CompositingCoordinatesOrientation orientation)339     virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
contentsOrientation()340     CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
341 
showDebugBorders()342     bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
showRepaintCounter()343     bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
344 
345     void updateDebugIndicators();
346 
setDebugBackgroundColor(const Color &)347     virtual void setDebugBackgroundColor(const Color&) { }
setDebugBorder(const Color &,float)348     virtual void setDebugBorder(const Color&, float /*borderWidth*/) { }
349     // z-position is the z-equivalent of position(). It's only used for debugging purposes.
zPosition()350     virtual float zPosition() const { return m_zPosition; }
351     virtual void setZPosition(float);
352 
353     virtual void distributeOpacity(float);
354     virtual float accumulatedOpacity() const;
355 
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.
syncCompositingState()359     virtual void syncCompositingState() { }
syncCompositingStateForThisLayerOnly()360     virtual void syncCompositingStateForThisLayerOnly() { }
361 
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;
365 
usingTiledLayer()366     bool usingTiledLayer() const { return m_usingTiledLayer; }
367 
368 protected:
369 
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);
374 
setOpacityInternal(float)375     virtual void setOpacityInternal(float) { }
376 
377     // The layer being replicated.
replicatedLayer()378     GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
setReplicatedLayer(GraphicsLayer * layer)379     virtual void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
380 
381     GraphicsLayer(GraphicsLayerClient*);
382 
383     void dumpProperties(TextStream&, int indent, LayerTreeAsTextBehavior) const;
384 
385     GraphicsLayerClient* m_client;
386     String m_name;
387 
388     // Offset from the owning renderer
389     IntSize m_offsetFromRenderer;
390 
391     // Position is relative to the parent GraphicsLayer
392     FloatPoint m_position;
393     FloatPoint3D m_anchorPoint;
394     FloatSize m_size;
395     TransformationMatrix m_transform;
396     TransformationMatrix m_childrenTransform;
397 
398     Color m_backgroundColor;
399     float m_opacity;
400     float m_zPosition;
401 
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;
410 
411     GraphicsLayerPaintingPhase m_paintingPhase;
412     CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
413 
414     Vector<GraphicsLayer*> m_children;
415     GraphicsLayer* m_parent;
416 
417     GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
418 
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.
423 
424     IntRect m_contentsRect;
425 
426     int m_repaintCount;
427 };
428 
429 
430 } // namespace WebCore
431 
432 #ifndef NDEBUG
433 // Outside the WebCore namespace for ease of invocation from gdb.
434 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
435 #endif
436 
437 #endif // USE(ACCELERATED_COMPOSITING)
438 
439 #endif // GraphicsLayer_h
440 
441