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"
37 #include "TransformationMatrix.h"
38 #include "TransformOperations.h"
39 #include <wtf/OwnPtr.h>
45 typedef WebLayer PlatformLayer
;
46 typedef CALayer
* NativeLayer
;
48 typedef void* PlatformLayer
;
49 typedef void* NativeLayer
;
52 typedef void* PlatformLayer
;
53 typedef void* NativeLayer
;
56 #if PLATFORM(IPHONE_SIMULATOR) || !defined(NDEBUG)
57 #define SUPPORT_DEBUG_INDICATORS 1
63 class GraphicsContext
;
68 // GraphicsLayer is an abstraction for a rendering surface with backing store,
69 // which may have associated transformation and animations.
73 // Used to store one float value of a keyframe animation.
76 FloatValue(float key
, float value
, const TimingFunction
* timingFunction
= 0)
77 : m_key(key
), m_value(value
), m_timingFunction(0)
80 m_timingFunction
.set(new TimingFunction(*timingFunction
));
83 FloatValue(const FloatValue
& other
)
84 : m_key(other
.key()), m_value(other
.value()), m_timingFunction(0)
86 if (other
.timingFunction())
87 m_timingFunction
.set(new TimingFunction(*other
.timingFunction()));
90 const FloatValue
& operator=(const FloatValue
& other
)
93 set(other
.key(), other
.value(), other
.timingFunction());
97 void set(float key
, float value
, const TimingFunction
*);
99 float key() const { return m_key
; }
100 float value() const { return m_value
; }
101 const TimingFunction
* timingFunction() const { return m_timingFunction
.get(); }
106 OwnPtr
<TimingFunction
> m_timingFunction
;
110 class FloatValueList
{
112 void insert(float key
, float value
, const TimingFunction
* timingFunction
);
114 size_t size() const { return m_values
.size(); }
115 const FloatValue
& at(size_t i
) const { return m_values
.at(i
); }
116 const Vector
<FloatValue
>& values() const { return m_values
; }
119 Vector
<FloatValue
> m_values
;
122 // Used to store one transform in a keyframe list.
123 class TransformValue
{
125 TransformValue(float key
= NAN
, const TransformOperations
* value
= 0, const TimingFunction
* timingFunction
= 0)
129 m_value
.set(new TransformOperations(*value
));
131 m_timingFunction
.set(new TimingFunction(*timingFunction
));
134 TransformValue(const TransformValue
& other
)
138 m_value
.set(new TransformOperations(*other
.value()));
139 if (other
.timingFunction())
140 m_timingFunction
.set(new TimingFunction(*other
.timingFunction()));
143 const TransformValue
& operator=(const TransformValue
& other
)
146 set(other
.key(), other
.value(), other
.timingFunction());
150 void set(float key
, const TransformOperations
* value
, const TimingFunction
* timingFunction
);
152 float key() const { return m_key
; }
153 const TransformOperations
* value() const { return m_value
.get(); }
154 const TimingFunction
* timingFunction() const { return m_timingFunction
.get(); }
158 OwnPtr
<TransformOperations
> m_value
;
159 OwnPtr
<TimingFunction
> m_timingFunction
;
162 // Used to store a series of transforms in a keyframe list.
163 class TransformValueList
{
165 typedef Vector
<TransformOperation::OperationType
> FunctionList
;
167 size_t size() const { return m_values
.size(); }
168 const TransformValue
& at(size_t i
) const { return m_values
.at(i
); }
169 const Vector
<TransformValue
>& values() const { return m_values
; }
171 void insert(float key
, const TransformOperations
* value
, const TimingFunction
* timingFunction
);
173 // return a list of the required functions. List is empty if keyframes are not valid
174 // If return value is true, functions contain rotations of >= 180 degrees
175 void makeFunctionList(FunctionList
& list
, bool& isValid
, bool& hasBigRotation
) const;
177 Vector
<TransformValue
> m_values
;
180 static GraphicsLayer
* createGraphicsLayer(GraphicsLayerClient
*);
182 virtual ~GraphicsLayer();
184 GraphicsLayerClient
* client() const { return m_client
; }
186 // Layer name. Only used to identify layers in debug output
187 const String
& name() const { return m_name
; }
188 virtual void setName(const String
& name
) { m_name
= name
; }
190 // For hosting this GraphicsLayer in a native layer hierarchy.
191 virtual NativeLayer
nativeLayer() const { return 0; }
193 GraphicsLayer
* parent() const { return m_parent
; };
194 void setParent(GraphicsLayer
* layer
) { m_parent
= layer
; } // Internal use only.
196 const Vector
<GraphicsLayer
*>& children() const { return m_children
; }
198 // Add child layers. If the child is already parented, it will be removed from its old parent.
199 virtual void addChild(GraphicsLayer
*);
200 virtual void addChildAtIndex(GraphicsLayer
*, int index
);
201 virtual void addChildAbove(GraphicsLayer
* layer
, GraphicsLayer
* sibling
);
202 virtual void addChildBelow(GraphicsLayer
* layer
, GraphicsLayer
* sibling
);
203 virtual bool replaceChild(GraphicsLayer
* oldChild
, GraphicsLayer
* newChild
);
205 void removeAllChildren();
206 virtual void removeFromParent();
208 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
209 IntSize
offsetFromRenderer() const { return m_offsetFromRenderer
; }
210 void setOffsetFromRenderer(const IntSize
& offset
) { m_offsetFromRenderer
= offset
; }
212 // The position of the layer (the location of its top-left corner in its parent)
213 const FloatPoint
& position() const { return m_position
; }
214 virtual void setPosition(const FloatPoint
& p
) { m_position
= p
; }
216 // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
217 // affects the origin of the transforms.
218 const FloatPoint3D
& anchorPoint() const { return m_anchorPoint
; }
219 virtual void setAnchorPoint(const FloatPoint3D
& p
) { m_anchorPoint
= p
; }
221 // The bounds of the layer
222 const FloatSize
& size() const { return m_size
; }
223 virtual void setSize(const FloatSize
& size
) { m_size
= size
; }
225 const TransformationMatrix
& transform() const { return m_transform
; }
226 virtual void setTransform(const TransformationMatrix
& t
) { m_transform
= t
; }
228 const TransformationMatrix
& childrenTransform() const { return m_childrenTransform
; }
229 virtual void setChildrenTransform(const TransformationMatrix
& t
) { m_childrenTransform
= t
; }
231 bool preserves3D() const { return m_preserves3D
; }
232 virtual void setPreserves3D(bool b
) { m_preserves3D
= b
; }
234 bool masksToBounds() const { return m_masksToBounds
; }
235 virtual void setMasksToBounds(bool b
) { m_masksToBounds
= b
; }
237 bool drawsContent() const { return m_drawsContent
; }
238 virtual void setDrawsContent(bool b
) { m_drawsContent
= b
; }
240 // The color used to paint the layer backgrounds
241 const Color
& backgroundColor() const { return m_backgroundColor
; }
242 virtual void setBackgroundColor(const Color
&, const Animation
* = 0, double beginTime
= 0);
243 virtual void clearBackgroundColor();
244 bool backgroundColorSet() const { return m_backgroundColorSet
; }
246 // opaque means that we know the layer contents have no alpha
247 bool contentsOpaque() const { return m_contentsOpaque
; }
248 virtual void setContentsOpaque(bool b
) { m_contentsOpaque
= b
; }
250 bool backfaceVisibility() const { return m_backfaceVisibility
; }
251 virtual void setBackfaceVisibility(bool b
) { m_backfaceVisibility
= b
; }
253 float opacity() const { return m_opacity
; }
254 // return true if we started an animation
255 virtual bool setOpacity(float o
, const Animation
* = 0, double beginTime
= 0);
257 // Some GraphicsLayers paint only the foreground or the background content
258 GraphicsLayerPaintingPhase
drawingPhase() const { return m_paintingPhase
; }
259 void setDrawingPhase(GraphicsLayerPaintingPhase phase
) { m_paintingPhase
= phase
; }
261 virtual void setNeedsDisplay() = 0;
262 // mark the given rect (in layer coords) as needing dispay. Never goes deep.
263 virtual void setNeedsDisplayInRect(const FloatRect
&) = 0;
265 virtual bool animateTransform(const TransformValueList
&, const IntSize
&, const Animation
*, double beginTime
, bool isTransition
) = 0;
266 virtual bool animateFloat(AnimatedPropertyID
, const FloatValueList
&, const Animation
*, double beginTime
) = 0;
268 void removeFinishedAnimations(const String
& name
, int index
, bool reset
);
269 void removeFinishedTransitions(AnimatedPropertyID
);
270 void removeAllAnimations();
272 virtual void suspendAnimations();
273 virtual void resumeAnimations();
276 virtual void setContentsToImage(Image
*) { }
277 virtual void setContentsToVideo(PlatformLayer
*) { }
278 virtual void setContentsBackgroundColor(const Color
&) { }
279 virtual void clearContents() { }
281 virtual void updateContentsRect() { }
283 // Callback from the underlying graphics system to draw layer contents.
284 void paintGraphicsLayerContents(GraphicsContext
&, const IntRect
& clip
);
286 virtual PlatformLayer
* platformLayer() const { return 0; }
288 // Change the scale at which the contents are rendered. Note that contentsScale may not return
289 // the same value passed to setContentsScale(), because of clamping and hysteresis.
290 float contentsScale() const { return m_contentsScale
; }
291 virtual void setContentsScale(float);
293 void dumpLayer(TextStream
&, int indent
= 0) const;
295 #ifdef SUPPORT_DEBUG_INDICATORS
296 int repaintCount() const { return m_repaintCount
; }
297 int incrementRepaintCount() { return ++m_repaintCount
; }
300 // Platform behaviors
301 static bool graphicsContextsFlipped();
303 #ifdef SUPPORT_DEBUG_INDICATORS
304 static bool showDebugBorders();
305 static bool showRepaintCounter();
307 void updateDebugIndicators();
309 virtual void setDebugBackgroundColor(const Color
&) { }
310 virtual void setDebugBorder(const Color
&, float /*borderWidth*/) { }
312 // z-position is the z-equivalent of position(). It's only used for debugging purposes.
313 virtual float zPosition() const { return m_zPosition
; }
314 virtual void setZPosition(float);
316 static String
propertyIdToString(AnimatedPropertyID
);
319 GraphicsLayer(GraphicsLayerClient
*);
321 void dumpProperties(TextStream
&, int indent
) const;
323 // returns -1 if not found
324 int findAnimationEntry(AnimatedPropertyID
, short index
) const;
325 void addAnimationEntry(AnimatedPropertyID
, short index
, bool isTransition
, const Animation
*);
327 virtual void removeAnimation(int /*index*/, bool /*reset*/) {}
328 void removeAllAnimationsForProperty(AnimatedPropertyID
);
330 float clampedContentsScaleForScale(float scale
) const;
332 GraphicsLayerClient
* m_client
;
335 // Offset from the owning renderer
336 IntSize m_offsetFromRenderer
;
338 // Position is relative to the parent GraphicsLayer
339 FloatPoint m_position
;
340 FloatPoint3D m_anchorPoint
;
342 TransformationMatrix m_transform
;
343 TransformationMatrix m_childrenTransform
;
345 Color m_backgroundColor
;
349 float m_contentsScale
;
351 bool m_backgroundColorSet
: 1;
352 bool m_contentsOpaque
: 1;
353 bool m_preserves3D
: 1;
354 bool m_backfaceVisibility
: 1;
355 bool m_usingTiledLayer
: 1;
356 bool m_masksToBounds
: 1;
357 bool m_drawsContent
: 1;
359 GraphicsLayerPaintingPhase m_paintingPhase
;
361 Vector
<GraphicsLayer
*> m_children
;
362 GraphicsLayer
* m_parent
;
364 // AnimationEntry represents an animation of a property on this layer.
365 // For transform only, there may be more than one, in which case 'index'
366 // is an index into the list of transforms.
367 class AnimationEntry
{
369 AnimationEntry(const Animation
* animation
, AnimatedPropertyID property
, short index
, bool isTransition
)
370 : m_animation(const_cast<Animation
*>(animation
))
371 , m_property(property
)
374 , m_isTransition(isTransition
)
378 const Animation
* animation() const { return m_animation
.get(); }
379 AnimatedPropertyID
property() const { return m_property
; }
380 int index() const { return m_index
; }
381 bool isCurrent() const { return m_isCurrent
; }
382 void setIsCurrent(bool b
= true) { m_isCurrent
= b
; }
383 bool isTransition() const { return m_isTransition
; }
385 bool matches(AnimatedPropertyID property
, short index
) const
387 return m_property
== property
&& m_index
== index
;
390 void reset(const Animation
* animation
, bool isTransition
)
392 m_animation
= const_cast<Animation
*>(animation
);
393 m_isTransition
= isTransition
;
398 RefPtr
<Animation
> m_animation
;
399 AnimatedPropertyID m_property
: 14;
401 bool m_isCurrent
: 1;
402 bool m_isTransition
: 1;
405 Vector
<AnimationEntry
> m_animations
; // running animations/transitions
406 #ifdef SUPPORT_DEBUG_INDICATORS
412 } // namespace WebCore
414 #endif // USE(ACCELERATED_COMPOSITING)
416 #endif // GraphicsLayer_h