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 42 #if PLATFORM(MAC) 43 #ifdef __OBJC__ 44 @class WebLayer; 45 @class CALayer; 46 typedef CALayer PlatformLayer; 47 typedef CALayer* NativeLayer; 48 #else 49 typedef void* PlatformLayer; 50 typedef void* NativeLayer; 51 #endif 52 #else 53 typedef void* PlatformLayer; 54 typedef void* NativeLayer; 55 #endif 56 57 namespace WebCore { 58 59 class FloatPoint3D; 60 class GraphicsContext; 61 class Image; 62 class TextStream; 63 class TimingFunction; 64 65 // Base class for animation values (also used for transitions). Here to 66 // represent values for properties being animated via the GraphicsLayer, 67 // without pulling in style-related data from outside of the platform directory. 68 class AnimationValue : public Noncopyable { 69 public: 70 AnimationValue(float keyTime, const TimingFunction* timingFunction = 0) m_keyTime(keyTime)71 : m_keyTime(keyTime) 72 , m_timingFunction(0) 73 { 74 if (timingFunction) 75 m_timingFunction.set(new TimingFunction(*timingFunction)); 76 } 77 ~AnimationValue()78 virtual ~AnimationValue() { } 79 keyTime()80 float keyTime() const { return m_keyTime; } timingFunction()81 const TimingFunction* timingFunction() const { return m_timingFunction.get(); } 82 83 private: 84 float m_keyTime; 85 OwnPtr<TimingFunction> m_timingFunction; 86 }; 87 88 // Used to store one float value of an animation. 89 class FloatAnimationValue : public AnimationValue { 90 public: 91 FloatAnimationValue(float keyTime, float value, const TimingFunction* timingFunction = 0) AnimationValue(keyTime,timingFunction)92 : AnimationValue(keyTime, timingFunction) 93 , m_value(value) 94 { 95 } 96 value()97 float value() const { return m_value; } 98 99 private: 100 float m_value; 101 }; 102 103 // Used to store one transform value in a keyframe list. 104 class TransformAnimationValue : public AnimationValue { 105 public: 106 TransformAnimationValue(float keyTime, const TransformOperations* value = 0, const TimingFunction* timingFunction = 0) AnimationValue(keyTime,timingFunction)107 : AnimationValue(keyTime, timingFunction) 108 { 109 if (value) 110 m_value.set(new TransformOperations(*value)); 111 } 112 value()113 const TransformOperations* value() const { return m_value.get(); } 114 115 private: 116 OwnPtr<TransformOperations> m_value; 117 }; 118 119 // Used to store a series of values in a keyframe list. Values will all be of the same type, 120 // which can be inferred from the property. 121 class KeyframeValueList : public Noncopyable { 122 public: 123 KeyframeValueList(AnimatedPropertyID property)124 KeyframeValueList(AnimatedPropertyID property) 125 : m_property(property) 126 { 127 } 128 ~KeyframeValueList()129 ~KeyframeValueList() 130 { 131 deleteAllValues(m_values); 132 } 133 property()134 AnimatedPropertyID property() const { return m_property; } 135 size()136 size_t size() const { return m_values.size(); } at(size_t i)137 const AnimationValue* at(size_t i) const { return m_values.at(i); } 138 139 // Insert, sorted by keyTime. Takes ownership of the pointer. 140 void insert(const AnimationValue*); 141 142 protected: 143 Vector<const AnimationValue*> m_values; 144 AnimatedPropertyID m_property; 145 }; 146 147 148 149 // GraphicsLayer is an abstraction for a rendering surface with backing store, 150 // which may have associated transformation and animations. 151 152 class GraphicsLayer { 153 public: 154 155 static GraphicsLayer* createGraphicsLayer(GraphicsLayerClient*); 156 157 virtual ~GraphicsLayer(); 158 client()159 GraphicsLayerClient* client() const { return m_client; } 160 161 // Layer name. Only used to identify layers in debug output name()162 const String& name() const { return m_name; } setName(const String & name)163 virtual void setName(const String& name) { m_name = name; } 164 165 // For hosting this GraphicsLayer in a native layer hierarchy. nativeLayer()166 virtual NativeLayer nativeLayer() const { return 0; } 167 parent()168 GraphicsLayer* parent() const { return m_parent; }; setParent(GraphicsLayer * layer)169 void setParent(GraphicsLayer* layer) { m_parent = layer; } // Internal use only. 170 children()171 const Vector<GraphicsLayer*>& children() const { return m_children; } 172 173 // Add child layers. If the child is already parented, it will be removed from its old parent. 174 virtual void addChild(GraphicsLayer*); 175 virtual void addChildAtIndex(GraphicsLayer*, int index); 176 virtual void addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling); 177 virtual void addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling); 178 virtual bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild); 179 180 void removeAllChildren(); 181 virtual void removeFromParent(); 182 183 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative). offsetFromRenderer()184 IntSize offsetFromRenderer() const { return m_offsetFromRenderer; } setOffsetFromRenderer(const IntSize & offset)185 void setOffsetFromRenderer(const IntSize& offset) { m_offsetFromRenderer = offset; } 186 187 // The position of the layer (the location of its top-left corner in its parent) position()188 const FloatPoint& position() const { return m_position; } setPosition(const FloatPoint & p)189 virtual void setPosition(const FloatPoint& p) { m_position = p; } 190 191 // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point 192 // affects the origin of the transforms. anchorPoint()193 const FloatPoint3D& anchorPoint() const { return m_anchorPoint; } setAnchorPoint(const FloatPoint3D & p)194 virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; } 195 196 // The bounds of the layer size()197 const FloatSize& size() const { return m_size; } setSize(const FloatSize & size)198 virtual void setSize(const FloatSize& size) { m_size = size; } 199 transform()200 const TransformationMatrix& transform() const { return m_transform; } setTransform(const TransformationMatrix & t)201 virtual void setTransform(const TransformationMatrix& t) { m_transform = t; } 202 childrenTransform()203 const TransformationMatrix& childrenTransform() const { return m_childrenTransform; } setChildrenTransform(const TransformationMatrix & t)204 virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; } 205 preserves3D()206 bool preserves3D() const { return m_preserves3D; } setPreserves3D(bool b)207 virtual void setPreserves3D(bool b) { m_preserves3D = b; } 208 masksToBounds()209 bool masksToBounds() const { return m_masksToBounds; } setMasksToBounds(bool b)210 virtual void setMasksToBounds(bool b) { m_masksToBounds = b; } 211 drawsContent()212 bool drawsContent() const { return m_drawsContent; } setDrawsContent(bool b)213 virtual void setDrawsContent(bool b) { m_drawsContent = b; } 214 215 // The color used to paint the layer backgrounds backgroundColor()216 const Color& backgroundColor() const { return m_backgroundColor; } 217 virtual void setBackgroundColor(const Color&); 218 virtual void clearBackgroundColor(); backgroundColorSet()219 bool backgroundColorSet() const { return m_backgroundColorSet; } 220 221 // opaque means that we know the layer contents have no alpha contentsOpaque()222 bool contentsOpaque() const { return m_contentsOpaque; } setContentsOpaque(bool b)223 virtual void setContentsOpaque(bool b) { m_contentsOpaque = b; } 224 backfaceVisibility()225 bool backfaceVisibility() const { return m_backfaceVisibility; } setBackfaceVisibility(bool b)226 virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; } 227 opacity()228 float opacity() const { return m_opacity; } setOpacity(float opacity)229 virtual void setOpacity(float opacity) { m_opacity = opacity; } 230 231 // Some GraphicsLayers paint only the foreground or the background content drawingPhase()232 GraphicsLayerPaintingPhase drawingPhase() const { return m_paintingPhase; } setDrawingPhase(GraphicsLayerPaintingPhase phase)233 void setDrawingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; } 234 235 virtual void setNeedsDisplay() = 0; 236 // mark the given rect (in layer coords) as needing dispay. Never goes deep. 237 virtual void setNeedsDisplayInRect(const FloatRect&) = 0; 238 239 // Set that the position/size of the contents (image or video). contentsRect()240 IntRect contentsRect() const { return m_contentsRect; } setContentsRect(const IntRect & r)241 virtual void setContentsRect(const IntRect& r) { m_contentsRect = r; } 242 243 // Return true if the animation is handled by the compositing system. If this returns 244 // false, the animation will be run by AnimationController. addAnimation(const KeyframeValueList &,const IntSize &,const Animation *,const String &,double)245 virtual bool addAnimation(const KeyframeValueList&, const IntSize& /*boxSize*/, const Animation*, const String& /*keyframesName*/, double /*beginTime*/) { return false; } removeAnimationsForProperty(AnimatedPropertyID)246 virtual void removeAnimationsForProperty(AnimatedPropertyID) { } removeAnimationsForKeyframes(const String &)247 virtual void removeAnimationsForKeyframes(const String& /* keyframesName */) { } pauseAnimation(const String &)248 virtual void pauseAnimation(const String& /* keyframesName */) { } 249 250 virtual void suspendAnimations(double time); 251 virtual void resumeAnimations(); 252 253 // Layer contents setContentsToImage(Image *)254 virtual void setContentsToImage(Image*) { } setContentsToVideo(PlatformLayer *)255 virtual void setContentsToVideo(PlatformLayer*) { } setContentsBackgroundColor(const Color &)256 virtual void setContentsBackgroundColor(const Color&) { } 257 258 // Callback from the underlying graphics system to draw layer contents. 259 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip); 260 platformLayer()261 virtual PlatformLayer* platformLayer() const { return 0; } 262 263 void dumpLayer(TextStream&, int indent = 0) const; 264 265 #ifndef NDEBUG repaintCount()266 int repaintCount() const { return m_repaintCount; } incrementRepaintCount()267 int incrementRepaintCount() { return ++m_repaintCount; } 268 #endif 269 270 // Report whether the underlying compositing system uses a top-down 271 // or a bottom-up coordinate system. 272 enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp }; 273 static CompositingCoordinatesOrientation compositingCoordinatesOrientation(); 274 275 // Set the geometry orientation (top-down, or bottom-up) for this layer, which also controls sublayer geometry. setGeometryOrientation(CompositingCoordinatesOrientation orientation)276 virtual void setGeometryOrientation(CompositingCoordinatesOrientation orientation) { m_geometryOrientation = orientation; } geometryOrientation()277 CompositingCoordinatesOrientation geometryOrientation() const { return m_geometryOrientation; } 278 279 // Flippedness of the contents of this layer. Does not affect sublayer geometry. setContentsOrientation(CompositingCoordinatesOrientation orientation)280 virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; } contentsOrientation()281 CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; } 282 283 #ifndef NDEBUG 284 static bool showDebugBorders(); 285 static bool showRepaintCounter(); 286 287 void updateDebugIndicators(); 288 setDebugBackgroundColor(const Color &)289 virtual void setDebugBackgroundColor(const Color&) { } setDebugBorder(const Color &,float)290 virtual void setDebugBorder(const Color&, float /*borderWidth*/) { } 291 // z-position is the z-equivalent of position(). It's only used for debugging purposes. zPosition()292 virtual float zPosition() const { return m_zPosition; } 293 virtual void setZPosition(float); 294 #endif 295 296 virtual void distributeOpacity(float); 297 virtual float accumulatedOpacity() const; 298 299 // Some compositing systems may do internal batching to synchronize compositing updates 300 // with updates drawn into the window. This is a signal to flush any internal batched state. syncCompositingState()301 virtual void syncCompositingState() { } 302 303 protected: 304 305 typedef Vector<TransformOperation::OperationType> TransformOperationList; 306 // Given a list of TransformAnimationValues, return an array of transform operations. 307 // On return, if hasBigRotation is true, functions contain rotations of >= 180 degrees 308 static void fetchTransformOperationList(const KeyframeValueList&, TransformOperationList&, bool& isValid, bool& hasBigRotation); 309 setOpacityInternal(float)310 virtual void setOpacityInternal(float) { } 311 312 GraphicsLayer(GraphicsLayerClient*); 313 314 void dumpProperties(TextStream&, int indent) const; 315 316 GraphicsLayerClient* m_client; 317 String m_name; 318 319 // Offset from the owning renderer 320 IntSize m_offsetFromRenderer; 321 322 // Position is relative to the parent GraphicsLayer 323 FloatPoint m_position; 324 FloatPoint3D m_anchorPoint; 325 FloatSize m_size; 326 TransformationMatrix m_transform; 327 TransformationMatrix m_childrenTransform; 328 329 Color m_backgroundColor; 330 float m_opacity; 331 #ifndef NDEBUG 332 float m_zPosition; 333 #endif 334 335 bool m_backgroundColorSet : 1; 336 bool m_contentsOpaque : 1; 337 bool m_preserves3D: 1; 338 bool m_backfaceVisibility : 1; 339 bool m_usingTiledLayer : 1; 340 bool m_masksToBounds : 1; 341 bool m_drawsContent : 1; 342 343 GraphicsLayerPaintingPhase m_paintingPhase; 344 CompositingCoordinatesOrientation m_geometryOrientation; // affects geometry of layer positions 345 CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents 346 347 Vector<GraphicsLayer*> m_children; 348 GraphicsLayer* m_parent; 349 350 IntRect m_contentsRect; 351 352 #ifndef NDEBUG 353 int m_repaintCount; 354 #endif 355 }; 356 357 358 } // namespace WebCore 359 360 #endif // USE(ACCELERATED_COMPOSITING) 361 362 #endif // GraphicsLayer_h 363 364