OSDN Git Service

HW accelate button focus rings
[android-x86/external-webkit.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 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)
106         : m_keyTime(keyTime)
107     {
108         if (timingFunction)
109             m_timingFunction = timingFunction;
110     }
111     
112     virtual ~AnimationValue() { }
113
114     float keyTime() const { return m_keyTime; }
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)
126         : AnimationValue(keyTime, timingFunction)
127         , m_value(value)
128     {
129     }
130
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)
141         : AnimationValue(keyTime, timingFunction)
142     {
143         if (value)
144             m_value = adoptPtr(new TransformOperations(*value));
145     }
146
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
159     KeyframeValueList(AnimatedPropertyID property)
160         : m_property(property)
161     {
162     }
163     
164     ~KeyframeValueList()
165     {
166         deleteAllValues(m_values);
167     }
168     
169     AnimatedPropertyID property() const { return m_property; }
170
171     size_t size() const { return m_values.size(); }
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
194     GraphicsLayerClient* client() const { return m_client; }
195
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; }
199
200     GraphicsLayer* parent() const { return m_parent; };
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     
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
220     GraphicsLayer* maskLayer() const { return m_maskLayer; }
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.
226     bool isReplicated() const { return m_replicaLayer; }
227     // The layer that replicates this layer (if any).
228     GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
229
230     const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
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).
234     IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
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)
238     const FloatPoint& position() const { return m_position; }
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.
243     const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
244     virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; }
245
246     // The bounds of the layer
247     const FloatSize& size() const { return m_size; }
248     virtual void setSize(const FloatSize& size) { m_size = size; }
249
250     const TransformationMatrix& transform() const { return m_transform; }
251     virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
252
253     const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
254     virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
255
256     bool preserves3D() const { return m_preserves3D; }
257     virtual void setPreserves3D(bool b) { m_preserves3D = b; }
258     
259     bool masksToBounds() const { return m_masksToBounds; }
260     virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
261     
262     bool drawsContent() const { return m_drawsContent; }
263     virtual void setDrawsContent(bool b) { m_drawsContent = b; }
264
265     bool acceleratesDrawing() const { return m_acceleratesDrawing; }
266     virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
267
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; }
273
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; }
277
278     bool backfaceVisibility() const { return m_backfaceVisibility; }
279     virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
280
281     float opacity() const { return m_opacity; }
282     virtual void setOpacity(float opacity) { m_opacity = opacity; }
283
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; }
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     
292     virtual void setContentsNeedsDisplay() { };
293
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; }
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.
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*/) { }
307
308     virtual void suspendAnimations(double time);
309     virtual void resumeAnimations();
310     
311     // Layer contents
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; }
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
321     virtual void layerDidDisplay(PlatformLayer*) { }
322     
323     // For hosting this GraphicsLayer in a native layer hierarchy.
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.
328     virtual float contentsScale() const { return 1; }
329     virtual void setContentsScale(float) { }
330
331     void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
332
333     int repaintCount() const { return m_repaintCount; }
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.
339     virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
340     CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
341
342     bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
343     bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
344     
345     void updateDebugIndicators();
346     
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);
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.
359     virtual void syncCompositingState() { }
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
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
375     virtual void setOpacityInternal(float) { }
376     
377     // The layer being replicated.
378     GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
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