tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / texmap / TextureMapperNode.h
1 /*
2  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Library General Public License for more details.
13
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB.  If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef TextureMapperNode_h
21 #define TextureMapperNode_h
22
23 #include "FloatRect.h"
24 #include "GraphicsContext.h"
25 #include "GraphicsLayer.h"
26 #include "Image.h"
27 #include "IntPointHash.h"
28 #include "TextureMapper.h"
29 #include "Timer.h"
30 #include "TransformOperations.h"
31 #include "TranslateTransformOperation.h"
32 #include "UnitBezier.h"
33 #include <wtf/CurrentTime.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/RefCounted.h>
36
37 namespace WebCore {
38
39 class TextureMapperPlatformLayer;
40 class TextureMapperNode;
41 class GraphicsLayerTextureMapper;
42 class TextureMapperSurfaceManager;
43
44 class TextureMapperPaintOptions {
45 public:
46     BitmapTexture* surface;
47     TextureMapper* textureMapper;
48     TextureMapperSurfaceManager* surfaceManager;
49
50     float opacity;
51     bool isSurface;
52     TextureMapperPaintOptions() : surface(0), textureMapper(0), opacity(1.0), isSurface(false) { }
53 };
54
55 class TextureMapperAnimation : public RefCounted<TextureMapperAnimation> {
56 public:
57     String name;
58     KeyframeValueList keyframes;
59     IntSize boxSize;
60     RefPtr<Animation> animation;
61     bool paused;
62     Vector<TransformOperation::OperationType> functionList;
63     bool listsMatch;
64     bool hasBigRotation;
65     double startTime;
66     TextureMapperAnimation(const KeyframeValueList&);
67     static PassRefPtr<TextureMapperAnimation> create(const KeyframeValueList& values) { return adoptRef(new TextureMapperAnimation(values)); }
68 };
69
70 class TextureMapperNode {
71
72 public:
73     // This set of flags help us defer which properties of the layer have been
74     // modified by the compositor, so we can know what to look for in the next flush.
75     enum ChangeMask {
76         NoChanges =                 0,
77
78         ParentChange =              (1L << 0),
79         ChildrenChange =            (1L << 1),
80         MaskLayerChange =           (1L << 2),
81         PositionChange =            (1L << 3),
82
83         AnchorPointChange =         (1L << 4),
84         SizeChange  =               (1L << 5),
85         TransformChange =           (1L << 6),
86         ContentChange =             (1L << 7),
87
88         ContentsOrientationChange = (1L << 9),
89         OpacityChange =             (1L << 10),
90         ContentsRectChange =        (1L << 11),
91
92         Preserves3DChange =         (1L << 12),
93         MasksToBoundsChange =       (1L << 13),
94         DrawsContentChange =        (1L << 14),
95         ContentsOpaqueChange =      (1L << 15),
96
97         BackfaceVisibilityChange =  (1L << 16),
98         ChildrenTransformChange =   (1L << 17),
99         DisplayChange =             (1L << 18),
100         BackgroundColorChange =     (1L << 19),
101
102         ReplicaLayerChange =        (1L << 20),
103         AnimationChange =           (1L << 21)
104     };
105
106     enum SyncOptions {
107         TraverseDescendants = 1,
108         ComputationsOnly = 2
109     };
110
111     enum TileOwnership {
112         OwnedTiles,
113         ExternallyManagedTiles
114     };
115
116     typedef HashMap<TextureMapperNode*, FloatRect> NodeRectMap;
117
118     // The compositor lets us special-case images and colors, so we try to do so.
119     enum ContentType { HTMLContentType, DirectImageContentType, ColorContentType, MediaContentType, Canvas3DContentType};
120     struct ContentData {
121         FloatRect needsDisplayRect;
122         bool needsDisplay;
123         Color backgroundColor;
124
125         ContentType contentType;
126         RefPtr<Image> image;
127         const TextureMapperPlatformLayer* media;
128 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
129         int pixmapID;
130 #endif
131         ContentData()
132             : needsDisplay(false)
133             , contentType(HTMLContentType)
134             , image(0)
135             , media(0)
136 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
137             , pixmapID(0)
138 #endif
139         {
140         }
141     };
142
143     TextureMapperNode()
144         : m_parent(0), m_effectTarget(0), m_opacity(1.0), m_surfaceManager(0), m_textureMapper(0) { }
145
146     virtual ~TextureMapperNode();
147
148     void syncCompositingState(GraphicsLayerTextureMapper*, int syncOptions = 0);
149     void syncCompositingState(GraphicsLayerTextureMapper*, TextureMapper*, int syncOptions = 0);
150     void syncAnimationsRecursively();
151     IntSize size() const { return IntSize(m_size.width(), m_size.height()); }
152     void setTransform(const TransformationMatrix&);
153     void setOpacity(float value) { m_opacity = value; }
154     void setTextureMapper(TextureMapper* texmap) { m_textureMapper = texmap; }
155     bool descendantsOrSelfHaveRunningAnimations() const;
156
157     void paint();
158
159 #if USE(TILED_BACKING_STORE)
160     void setTileOwnership(TileOwnership ownership) { m_state.tileOwnership = ownership; }
161     int createContentsTile(float scale);
162     void removeContentsTile(int id);
163     void setContentsTileBackBuffer(int id, const IntRect& sourceRect, const IntRect& targetRect, void* bits, BitmapTexture::PixelFormat);
164     void setTileBackBufferTextureForDirectlyCompositedImage(int id, const IntRect& sourceRect, const FloatRect& targetRect, BitmapTexture*);
165     void clearAllDirectlyCompositedImageTiles();
166     bool collectVisibleContentsRects(NodeRectMap&, const FloatRect&);
167     void purgeNodeTexturesRecursive();
168 #endif
169     void setID(int id) { m_id = id; }
170     int id() const { return m_id; }
171
172     const TextureMapperPlatformLayer* media() const { return m_currentContent.media; }
173
174 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
175     bool collectVisibleContentRects(NodeRectMap&, const FloatRect&);
176 #endif
177
178 private:
179     TextureMapperNode* rootLayer();
180     void computeAllTransforms();
181     void computeVisibleRect(const FloatRect& rootVisibleRect);
182     void computePerspectiveTransformIfNeeded();
183     void computeReplicaTransformIfNeeded();
184     void computeOverlapsIfNeeded();
185     void computeLocalTransformIfNeeded();
186     void computeTiles();
187     void swapContentsBuffers();
188     int countDescendantsWithContent() const;
189     FloatRect targetRectForTileRect(const FloatRect& totalTargetRect, const FloatRect& tileRect) const;
190     void invalidateViewport(const FloatRect&);
191     void notifyChange(ChangeMask);
192     void syncCompositingStateSelf(GraphicsLayerTextureMapper* graphicsLayer, TextureMapper* textureMapper);
193
194     static int compareGraphicsLayersZValue(const void* a, const void* b);
195     static void sortByZOrder(Vector<TextureMapperNode* >& array, int first, int last);
196
197     BitmapTexture* texture() { return m_ownedTiles.isEmpty() ? 0 : m_ownedTiles[0].texture.get(); }
198
199     void paintRecursive(TextureMapperPaintOptions);
200     bool paintReflection(const TextureMapperPaintOptions&, BitmapTexture* surface);
201     void paintSelf(const TextureMapperPaintOptions&);
202     void paintSelfAndChildren(const TextureMapperPaintOptions&, TextureMapperPaintOptions& optionsForDescendants);
203     void renderContent(TextureMapper*, GraphicsLayer*);
204
205     void syncAnimations(GraphicsLayerTextureMapper*);
206     void applyAnimation(const TextureMapperAnimation&, double runningTime);
207     void applyAnimationFrame(const TextureMapperAnimation&, const AnimationValue* from, const AnimationValue* to, float progress);
208     void applyOpacityAnimation(float fromOpacity, float toOpacity, double);
209     void applyTransformAnimation(const TextureMapperAnimation&, const TransformOperations* start, const TransformOperations* end, double);
210     bool hasOpacityAnimation() const;
211     bool hasTransformAnimation() const;
212
213     struct TransformData {
214         TransformationMatrix target;
215         TransformationMatrix replica;
216         TransformationMatrix forDescendants;
217         TransformationMatrix local;
218         TransformationMatrix base;
219         TransformationMatrix perspective;
220         float centerZ;
221         TransformData() { }
222     };
223
224     TransformData m_transforms;
225
226     inline FloatRect targetRect() const
227     {
228         return m_currentContent.contentType == HTMLContentType ? entireRect() : m_state.contentsRect;
229     }
230
231     inline FloatRect entireRect() const
232     {
233         return FloatRect(0, 0, m_size.width(), m_size.height());
234     }
235
236     FloatSize contentSize() const
237     {
238         return m_currentContent.contentType == DirectImageContentType && m_currentContent.image ? m_currentContent.image->size() : m_size;
239     }
240     struct OwnedTile {
241         FloatRect rect;
242         RefPtr<BitmapTexture> texture;
243         bool needsReset;
244     };
245
246     Vector<OwnedTile> m_ownedTiles;
247
248 #if USE(TILED_BACKING_STORE)
249     struct ExternallyManagedTileBuffer {
250         FloatRect sourceRect;
251         FloatRect targetRect;
252         RefPtr<BitmapTexture> texture;
253     };
254
255     struct ExternallyManagedTile {
256         bool isBackBufferUpdated;
257         bool isDirectlyCompositedImage;
258         float scale;
259         ExternallyManagedTileBuffer frontBuffer;
260         ExternallyManagedTileBuffer backBuffer;
261
262         ExternallyManagedTile(float scale = 1.0)
263             : isBackBufferUpdated(false)
264             , isDirectlyCompositedImage(false)
265             , scale(scale)
266         {
267         }
268     };
269
270     HashMap<int, ExternallyManagedTile> m_externallyManagedTiles;
271 #endif
272
273     ContentData m_currentContent;
274
275     Vector<TextureMapperNode*> m_children;
276     TextureMapperNode* m_parent;
277     TextureMapperNode* m_effectTarget;
278     FloatSize m_size;
279     float m_opacity;
280     String m_name;
281     int m_id;
282
283     struct State {
284         FloatPoint pos;
285         FloatPoint3D anchorPoint;
286         FloatSize size;
287         TransformationMatrix transform;
288         TransformationMatrix childrenTransform;
289         float opacity;
290         FloatRect contentsRect;
291         int descendantsWithContent;
292         TextureMapperNode* maskLayer;
293         TextureMapperNode* replicaLayer;
294         bool preserves3D : 1;
295         bool masksToBounds : 1;
296         bool drawsContent : 1;
297         bool contentsOpaque : 1;
298         bool backfaceVisibility : 1;
299         bool visible : 1;
300         bool needsReset: 1;
301         bool mightHaveOverlaps : 1;
302         bool needsRepaint;
303         IntRect visibleRect;
304         float contentScale;
305 #if USE(TILED_BACKING_STORE)
306         TileOwnership tileOwnership;
307 #endif
308         State()
309             : opacity(1.f)
310             , maskLayer(0)
311             , replicaLayer(0)
312             , preserves3D(false)
313             , masksToBounds(false)
314             , drawsContent(false)
315             , contentsOpaque(false)
316             , backfaceVisibility(false)
317             , visible(true)
318             , needsReset(false)
319             , mightHaveOverlaps(false)
320             , needsRepaint(false)
321             , contentScale(1.0f)
322 #if USE(TILED_BACKING_STORE)
323             , tileOwnership(OwnedTiles)
324 #endif
325         {
326         }
327     };
328
329     State m_state;
330     TextureMapperSurfaceManager* m_surfaceManager;
331     TextureMapper* m_textureMapper;
332
333     Vector<RefPtr<TextureMapperAnimation> > m_animations;
334 };
335
336
337 TextureMapperNode* toTextureMapperNode(GraphicsLayer*);
338
339 }
340 #endif // TextureMapperNode_h