]> git.saurik.com Git - iphone-api.git/blob - WebCore/GraphicsLayer.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / 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 "TransformationMatrix.h"
38 #include "TransformOperations.h"
39 #include <wtf/OwnPtr.h>
40
41 #if PLATFORM(MAC)
42 #ifdef __OBJC__
43 @class WebLayer;
44 @class CALayer;
45 typedef WebLayer PlatformLayer;
46 typedef CALayer* NativeLayer;
47 #else
48 typedef void* PlatformLayer;
49 typedef void* NativeLayer;
50 #endif
51 #else
52 typedef void* PlatformLayer;
53 typedef void* NativeLayer;
54 #endif
55
56 #if PLATFORM(IPHONE_SIMULATOR) || !defined(NDEBUG)
57 #define SUPPORT_DEBUG_INDICATORS 1
58 #endif
59
60 namespace WebCore {
61
62 class FloatPoint3D;
63 class GraphicsContext;
64 class Image;
65 class TextStream;
66 class TimingFunction;
67
68 // GraphicsLayer is an abstraction for a rendering surface with backing store,
69 // which may have associated transformation and animations.
70
71 class GraphicsLayer {
72 public:
73 // Used to store one float value of a keyframe animation.
74 class FloatValue {
75 public:
76 FloatValue(float key, float value, const TimingFunction* timingFunction = 0)
77 : m_key(key), m_value(value), m_timingFunction(0)
78 {
79 if (timingFunction)
80 m_timingFunction.set(new TimingFunction(*timingFunction));
81 }
82
83 FloatValue(const FloatValue& other)
84 : m_key(other.key()), m_value(other.value()), m_timingFunction(0)
85 {
86 if (other.timingFunction())
87 m_timingFunction.set(new TimingFunction(*other.timingFunction()));
88 }
89
90 const FloatValue& operator=(const FloatValue& other)
91 {
92 if (&other != this)
93 set(other.key(), other.value(), other.timingFunction());
94 return *this;
95 }
96
97 void set(float key, float value, const TimingFunction*);
98
99 float key() const { return m_key; }
100 float value() const { return m_value; }
101 const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
102
103 private:
104 float m_key;
105 float m_value;
106 OwnPtr<TimingFunction> m_timingFunction;
107 };
108
109
110 class FloatValueList {
111 public:
112 void insert(float key, float value, const TimingFunction* timingFunction);
113
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; }
117
118 private:
119 Vector<FloatValue> m_values;
120 };
121
122 // Used to store one transform in a keyframe list.
123 class TransformValue {
124 public:
125 TransformValue(float key = NAN, const TransformOperations* value = 0, const TimingFunction* timingFunction = 0)
126 : m_key(key)
127 {
128 if (value)
129 m_value.set(new TransformOperations(*value));
130 if (timingFunction)
131 m_timingFunction.set(new TimingFunction(*timingFunction));
132 }
133
134 TransformValue(const TransformValue& other)
135 : m_key(other.key())
136 {
137 if (other.value())
138 m_value.set(new TransformOperations(*other.value()));
139 if (other.timingFunction())
140 m_timingFunction.set(new TimingFunction(*other.timingFunction()));
141 }
142
143 const TransformValue& operator=(const TransformValue& other)
144 {
145 if (&other != this)
146 set(other.key(), other.value(), other.timingFunction());
147 return *this;
148 }
149
150 void set(float key, const TransformOperations* value, const TimingFunction* timingFunction);
151
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(); }
155
156 private:
157 float m_key;
158 OwnPtr<TransformOperations> m_value;
159 OwnPtr<TimingFunction> m_timingFunction;
160 };
161
162 // Used to store a series of transforms in a keyframe list.
163 class TransformValueList {
164 public:
165 typedef Vector<TransformOperation::OperationType> FunctionList;
166
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; }
170
171 void insert(float key, const TransformOperations* value, const TimingFunction* timingFunction);
172
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;
176 private:
177 Vector<TransformValue> m_values;
178 };
179
180 static GraphicsLayer* createGraphicsLayer(GraphicsLayerClient*);
181
182 virtual ~GraphicsLayer();
183
184 GraphicsLayerClient* client() const { return m_client; }
185
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; }
189
190 // For hosting this GraphicsLayer in a native layer hierarchy.
191 virtual NativeLayer nativeLayer() const { return 0; }
192
193 GraphicsLayer* parent() const { return m_parent; };
194 void setParent(GraphicsLayer* layer) { m_parent = layer; } // Internal use only.
195
196 const Vector<GraphicsLayer*>& children() const { return m_children; }
197
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);
204
205 void removeAllChildren();
206 virtual void removeFromParent();
207
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; }
211
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; }
215
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; }
220
221 // The bounds of the layer
222 const FloatSize& size() const { return m_size; }
223 virtual void setSize(const FloatSize& size) { m_size = size; }
224
225 const TransformationMatrix& transform() const { return m_transform; }
226 virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
227
228 const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
229 virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
230
231 bool preserves3D() const { return m_preserves3D; }
232 virtual void setPreserves3D(bool b) { m_preserves3D = b; }
233
234 bool masksToBounds() const { return m_masksToBounds; }
235 virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
236
237 bool drawsContent() const { return m_drawsContent; }
238 virtual void setDrawsContent(bool b) { m_drawsContent = b; }
239
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; }
245
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; }
249
250 bool backfaceVisibility() const { return m_backfaceVisibility; }
251 virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
252
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);
256
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; }
260
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;
264
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;
267
268 void removeFinishedAnimations(const String& name, int index, bool reset);
269 void removeFinishedTransitions(AnimatedPropertyID);
270 void removeAllAnimations();
271
272 virtual void suspendAnimations();
273 virtual void resumeAnimations();
274
275 // Layer contents
276 virtual void setContentsToImage(Image*) { }
277 virtual void setContentsToVideo(PlatformLayer*) { }
278 virtual void setContentsBackgroundColor(const Color&) { }
279 virtual void clearContents() { }
280
281 virtual void updateContentsRect() { }
282
283 // Callback from the underlying graphics system to draw layer contents.
284 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
285
286 virtual PlatformLayer* platformLayer() const { return 0; }
287
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);
292
293 void dumpLayer(TextStream&, int indent = 0) const;
294
295 #ifdef SUPPORT_DEBUG_INDICATORS
296 int repaintCount() const { return m_repaintCount; }
297 int incrementRepaintCount() { return ++m_repaintCount; }
298 #endif
299
300 // Platform behaviors
301 static bool graphicsContextsFlipped();
302
303 #ifdef SUPPORT_DEBUG_INDICATORS
304 static bool showDebugBorders();
305 static bool showRepaintCounter();
306
307 void updateDebugIndicators();
308
309 virtual void setDebugBackgroundColor(const Color&) { }
310 virtual void setDebugBorder(const Color&, float /*borderWidth*/) { }
311 #endif
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);
315
316 static String propertyIdToString(AnimatedPropertyID);
317
318 protected:
319 GraphicsLayer(GraphicsLayerClient*);
320
321 void dumpProperties(TextStream&, int indent) const;
322
323 // returns -1 if not found
324 int findAnimationEntry(AnimatedPropertyID, short index) const;
325 void addAnimationEntry(AnimatedPropertyID, short index, bool isTransition, const Animation*);
326
327 virtual void removeAnimation(int /*index*/, bool /*reset*/) {}
328 void removeAllAnimationsForProperty(AnimatedPropertyID);
329
330 float clampedContentsScaleForScale(float scale) const;
331
332 GraphicsLayerClient* m_client;
333 String m_name;
334
335 // Offset from the owning renderer
336 IntSize m_offsetFromRenderer;
337
338 // Position is relative to the parent GraphicsLayer
339 FloatPoint m_position;
340 FloatPoint3D m_anchorPoint;
341 FloatSize m_size;
342 TransformationMatrix m_transform;
343 TransformationMatrix m_childrenTransform;
344
345 Color m_backgroundColor;
346 float m_opacity;
347 float m_zPosition;
348
349 float m_contentsScale;
350
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;
358
359 GraphicsLayerPaintingPhase m_paintingPhase;
360
361 Vector<GraphicsLayer*> m_children;
362 GraphicsLayer* m_parent;
363
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 {
368 public:
369 AnimationEntry(const Animation* animation, AnimatedPropertyID property, short index, bool isTransition)
370 : m_animation(const_cast<Animation*>(animation))
371 , m_property(property)
372 , m_index(index)
373 , m_isCurrent(true)
374 , m_isTransition(isTransition)
375 {
376 }
377
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; }
384
385 bool matches(AnimatedPropertyID property, short index) const
386 {
387 return m_property == property && m_index == index;
388 }
389
390 void reset(const Animation* animation, bool isTransition)
391 {
392 m_animation = const_cast<Animation*>(animation);
393 m_isTransition = isTransition;
394 m_isCurrent = true;
395 }
396
397 private:
398 RefPtr<Animation> m_animation;
399 AnimatedPropertyID m_property : 14;
400 short m_index : 16;
401 bool m_isCurrent : 1;
402 bool m_isTransition : 1;
403 };
404
405 Vector<AnimationEntry> m_animations; // running animations/transitions
406 #ifdef SUPPORT_DEBUG_INDICATORS
407 int m_repaintCount;
408 #endif
409 };
410
411
412 } // namespace WebCore
413
414 #endif // USE(ACCELERATED_COMPOSITING)
415
416 #endif // GraphicsLayer_h
417