tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / GraphicsLayer.h
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 PLATFORM(MAC)
44 #ifdef __OBJC__
45 @class CALayer;
46 #else
47 class CALayer;
48 #endif
49 typedef CALayer PlatformLayer;
50 #elif PLATFORM(WIN)
51 typedef struct _CACFLayer PlatformLayer;
52 #elif PLATFORM(QT)
53 #if USE(TEXTURE_MAPPER)
54 namespace WebCore {
55 class TextureMapperPlatformLayer;
56 typedef TextureMapperPlatformLayer PlatformLayer;
57 };
58 #else
59 QT_BEGIN_NAMESPACE
60 class QGraphicsObject;
61 QT_END_NAMESPACE
62 namespace WebCore {
63 typedef QGraphicsObject PlatformLayer;
64 }
65 #endif
66 #elif PLATFORM(CHROMIUM)
67 namespace WebCore {
68 class LayerChromium;
69 typedef LayerChromium PlatformLayer;
70 }
71 #elif PLATFORM(EFL)
72 #if USE(TIZEN_TEXTURE_MAPPER)
73 namespace WebCore {
74 class TextureMapperPlatformLayer;
75 typedef TextureMapperPlatformLayer PlatformLayer;
76 };
77 #else
78 namespace WebCore {
79 class EflLayer;
80 typedef EflLayer PlatformLayer;
81 }
82 #endif
83 #else
84 typedef void* PlatformLayer;
85 #endif
86
87 enum LayerTreeAsTextBehaviorFlags {
88     LayerTreeAsTextBehaviorNormal = 0,
89     LayerTreeAsTextDebug = 1 << 0, // Dump extra debugging info like layer addresses.
90 };
91 typedef unsigned LayerTreeAsTextBehavior;
92
93 namespace WebCore {
94
95 class FloatPoint3D;
96 class GraphicsContext;
97 class Image;
98 class TextStream;
99 class TimingFunction;
100
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 {
105 public:
106     AnimationValue(float keyTime, PassRefPtr<TimingFunction> timingFunction = 0)
107         : m_keyTime(keyTime)
108     {
109         if (timingFunction)
110             m_timingFunction = timingFunction;
111     }
112     
113     virtual ~AnimationValue() { }
114
115     float keyTime() const { return m_keyTime; }
116     const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
117     virtual AnimationValue* clone() const = 0;
118
119 private:
120     float m_keyTime;
121     RefPtr<TimingFunction> m_timingFunction;
122 };
123
124 // Used to store one float value of an animation.
125 class FloatAnimationValue : public AnimationValue {
126 public:
127     FloatAnimationValue(float keyTime, float value, PassRefPtr<TimingFunction> timingFunction = 0)
128         : AnimationValue(keyTime, timingFunction)
129         , m_value(value)
130     {
131     }
132     virtual AnimationValue* clone() const { return new FloatAnimationValue(*this); }
133
134     float value() const { return m_value; }
135
136 private:
137     float m_value;
138 };
139
140 // Used to store one transform value in a keyframe list.
141 class TransformAnimationValue : public AnimationValue {
142 public:
143     TransformAnimationValue(float keyTime, const TransformOperations* value = 0, PassRefPtr<TimingFunction> timingFunction = 0)
144         : AnimationValue(keyTime, timingFunction)
145     {
146         if (value)
147             m_value = *value;
148     }
149     virtual AnimationValue* clone() const { return new TransformAnimationValue(*this); }
150
151     const TransformOperations* value() const { return &m_value; }
152
153 private:
154     TransformOperations m_value;
155 };
156
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 {
160 public:
161
162     KeyframeValueList(AnimatedPropertyID property)
163         : m_property(property)
164     {
165     }
166
167     KeyframeValueList(const KeyframeValueList& other)
168         : m_property(other.property())
169     {
170         for (size_t i = 0; i < other.m_values.size(); ++i)
171             m_values.append(other.m_values[i]->clone());
172     }
173
174     ~KeyframeValueList()
175     {
176         deleteAllValues(m_values);
177     }
178     
179     AnimatedPropertyID property() const { return m_property; }
180
181     size_t size() const { return m_values.size(); }
182     const AnimationValue* at(size_t i) const { return m_values.at(i); }
183     
184     // Insert, sorted by keyTime. Takes ownership of the pointer.
185     void insert(const AnimationValue*);
186     
187 protected:
188     Vector<const AnimationValue*> m_values;
189     AnimatedPropertyID m_property;
190 };
191
192
193
194 // GraphicsLayer is an abstraction for a rendering surface with backing store,
195 // which may have associated transformation and animations.
196
197 class GraphicsLayer {
198     WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED;
199 public:
200     static PassOwnPtr<GraphicsLayer> create(GraphicsLayerClient*);
201     
202     virtual ~GraphicsLayer();
203
204     GraphicsLayerClient* client() const { return m_client; }
205
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; }
209
210     GraphicsLayer* parent() const { return m_parent; };
211     void setParent(GraphicsLayer*); // Internal use only.
212     
213     // Returns true if the layer has the given layer as an ancestor (excluding self).
214     bool hasAncestor(GraphicsLayer*) const;
215     
216     const Vector<GraphicsLayer*>& children() const { return m_children; }
217     // Returns true if the child list changed.
218     virtual bool setChildren(const Vector<GraphicsLayer*>&);
219
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);
226
227     void removeAllChildren();
228     virtual void removeFromParent();
229
230     GraphicsLayer* maskLayer() const { return m_maskLayer; }
231     virtual void setMaskLayer(GraphicsLayer* layer) { m_maskLayer = layer; }
232     
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; }
239
240     const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
241     void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
242
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; }
246
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; }
250     
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; }
255
256     // The size of the layer.
257     const FloatSize& size() const { return m_size; }
258     virtual void setSize(const FloatSize& size) { m_size = size; }
259
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; }
263
264     const TransformationMatrix& transform() const { return m_transform; }
265     virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
266
267     const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
268     virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
269
270     bool preserves3D() const { return m_preserves3D; }
271     virtual void setPreserves3D(bool b) { m_preserves3D = b; }
272     
273     bool masksToBounds() const { return m_masksToBounds; }
274     virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
275     
276     bool drawsContent() const { return m_drawsContent; }
277     virtual void setDrawsContent(bool b) { m_drawsContent = b; }
278
279     bool contentsAreVisible() const { return m_contentsVisible; }
280     virtual void setContentsVisible(bool b) { m_contentsVisible = b; }
281
282     bool acceleratesDrawing() const { return m_acceleratesDrawing; }
283     virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
284
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; }
290
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; }
294
295     bool backfaceVisibility() const { return m_backfaceVisibility; }
296     virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
297
298     float opacity() const { return m_opacity; }
299     virtual void setOpacity(float opacity) { m_opacity = opacity; }
300
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; }
304
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;
308     
309     virtual void setContentsNeedsDisplay() { };
310
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; }
314     
315     // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
316     static String animationNameForTransition(AnimatedPropertyID);
317     
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*/) { }
324
325     virtual void suspendAnimations(double time);
326     virtual void resumeAnimations();
327     
328     // Layer contents
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; }
334
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*) { }
339     
340     // For hosting this GraphicsLayer in a native layer hierarchy.
341     virtual PlatformLayer* platformLayer() const { return 0; }
342     
343     void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
344
345     int repaintCount() const { return m_repaintCount; }
346     int incrementRepaintCount() { return ++m_repaintCount; }
347
348     enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
349
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; }
353
354     bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
355     bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
356     
357     void updateDebugIndicators();
358     
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);
364
365     virtual void distributeOpacity(float);
366     virtual float accumulatedOpacity() const;
367
368     virtual void setMaintainsPixelAlignment(bool maintainsAlignment) { m_maintainsPixelAlignment = maintainsAlignment; }
369     virtual bool maintainsPixelAlignment() const { return m_maintainsPixelAlignment; }
370     
371     void setAppliesPageScale(bool appliesScale = true) { m_appliesPageScale = appliesScale; }
372     bool appliesPageScale() const { return m_appliesPageScale; }
373
374     float pageScaleFactor() const { return m_client ? m_client->pageScaleFactor() : 1; }
375     float deviceScaleFactor() const { return m_client ? m_client->deviceScaleFactor() : 1; }
376
377     virtual void deviceOrPageScaleFactorChanged() { }
378     void noteDeviceOrPageScaleFactorChangedIncludingDescendants();
379
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() { }
385     
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;
389
390     bool usingTiledLayer() const { return m_usingTiledLayer; }
391
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);
397 #endif
398
399 protected:
400
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);
405
406     virtual void setOpacityInternal(float) { }
407
408     // The layer being replicated.
409     GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
410     virtual void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
411
412     GraphicsLayer(GraphicsLayerClient*);
413
414     void dumpProperties(TextStream&, int indent, LayerTreeAsTextBehavior) const;
415
416     GraphicsLayerClient* m_client;
417     String m_name;
418     
419     // Offset from the owning renderer
420     IntSize m_offsetFromRenderer;
421     
422     // Position is relative to the parent GraphicsLayer
423     FloatPoint m_position;
424     FloatPoint3D m_anchorPoint;
425     FloatSize m_size;
426     FloatPoint m_boundsOrigin;
427
428     TransformationMatrix m_transform;
429     TransformationMatrix m_childrenTransform;
430
431     Color m_backgroundColor;
432     float m_opacity;
433     float m_zPosition;
434
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.
446
447     GraphicsLayerPaintingPhase m_paintingPhase;
448     CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
449
450     Vector<GraphicsLayer*> m_children;
451     GraphicsLayer* m_parent;
452
453     GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
454
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.
459
460     IntRect m_contentsRect;
461
462     int m_repaintCount;
463
464 #if PLATFORM(QT) || PLATFORM(EFL)
465     static GraphicsLayer::GraphicsLayerFactory* s_graphicsLayerFactory;
466 #endif
467 };
468
469
470 } // namespace WebCore
471
472 #ifndef NDEBUG
473 // Outside the WebCore namespace for ease of invocation from gdb.
474 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
475 #endif
476
477 #endif // USE(ACCELERATED_COMPOSITING)
478
479 #endif // GraphicsLayer_h
480