Coordinated Graphics: remove the DidChangeScrollPosition message.
[WebKit-https.git] / Source / WebCore / platform / graphics / texmap / coordinated / CoordinatedGraphicsScene.h
1 /*
2     Copyright (C) 2012 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 CoordinatedGraphicsScene_h
21 #define CoordinatedGraphicsScene_h
22
23 #if USE(COORDINATED_GRAPHICS)
24 #include "CoordinatedLayerInfo.h"
25 #include "CoordinatedSurface.h"
26 #include "GraphicsContext.h"
27 #include "GraphicsLayer.h"
28 #include "GraphicsLayerAnimation.h"
29 #include "GraphicsSurface.h"
30 #include "IntRect.h"
31 #include "IntSize.h"
32 #include "RunLoop.h"
33 #include "TextureMapper.h"
34 #include "TextureMapperBackingStore.h"
35 #include "Timer.h"
36 #include <wtf/Functional.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/ThreadingPrimitives.h>
39 #include <wtf/Vector.h>
40
41 #if USE(GRAPHICS_SURFACE)
42 #include "TextureMapperSurfaceBackingStore.h"
43 #endif
44
45 namespace WebCore {
46
47 class CoordinatedBackingStore;
48 class CoordinatedLayerInfo;
49 class CustomFilterProgram;
50 class CustomFilterProgramInfo;
51 class TextureMapperLayer;
52
53 class CoordinatedGraphicsSceneClient {
54 public:
55     virtual ~CoordinatedGraphicsSceneClient() { }
56 #if ENABLE(REQUEST_ANIMATION_FRAME)
57     virtual void animationFrameReady() = 0;
58 #endif
59     virtual void purgeBackingStores() = 0;
60     virtual void renderNextFrame() = 0;
61     virtual void updateViewport() = 0;
62 };
63
64 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public GraphicsLayerClient {
65 public:
66     struct TileUpdate {
67         IntRect sourceRect;
68         IntRect tileRect;
69         uint32_t atlasID;
70         IntPoint offset;
71         TileUpdate(const IntRect& source, const IntRect& tile, uint32_t atlas, const IntPoint& newOffset)
72             : sourceRect(source)
73             , tileRect(tile)
74             , atlasID(atlas)
75             , offset(newOffset)
76         {
77         }
78     };
79     explicit CoordinatedGraphicsScene(CoordinatedGraphicsSceneClient*);
80     virtual ~CoordinatedGraphicsScene();
81     void paintToCurrentGLContext(const TransformationMatrix&, float, const FloatRect&, TextureMapper::PaintFlags = 0);
82 #if PLATFORM(QT)
83     void paintToGraphicsContext(QPainter*);
84 #elif USE(CAIRO)
85     void paintToGraphicsContext(cairo_t*);
86 #endif
87     void setContentsSize(const FloatSize&);
88     void setVisibleContentsRect(const FloatRect&);
89 #if USE(GRAPHICS_SURFACE)
90     void createCanvas(CoordinatedLayerID, const IntSize&, PassRefPtr<GraphicsSurface>);
91     void syncCanvas(CoordinatedLayerID, uint32_t frontBuffer);
92     void destroyCanvas(CoordinatedLayerID);
93 #endif
94     void setLayerRepaintCount(CoordinatedLayerID, int value);
95
96     void detach();
97     void appendUpdate(const Function<void()>&);
98
99     // The painting thread must lock the main thread to use below two methods, because two methods access members that the main thread manages. See m_client.
100     // Currently, QQuickWebPage::updatePaintNode() locks the main thread before calling both methods.
101     void purgeGLResources();
102     void setActive(bool);
103
104     void createLayers(const Vector<CoordinatedLayerID>&);
105     void deleteLayers(const Vector<CoordinatedLayerID>&);
106     void setRootLayerID(CoordinatedLayerID);
107     void setLayerChildren(CoordinatedLayerID, const Vector<CoordinatedLayerID>&);
108     void setLayerState(CoordinatedLayerID, const CoordinatedLayerInfo&);
109 #if ENABLE(CSS_FILTERS)
110     void setLayerFilters(CoordinatedLayerID, const FilterOperations&);
111 #endif
112 #if ENABLE(CSS_SHADERS)
113     void injectCachedCustomFilterPrograms(const FilterOperations& filters) const;
114     void createCustomFilterProgram(int id, const CustomFilterProgramInfo&);
115     void removeCustomFilterProgram(int id);
116 #endif
117
118     void createTile(CoordinatedLayerID, uint32_t tileID, float scale);
119     void removeTile(CoordinatedLayerID, uint32_t tileID);
120     void updateTile(CoordinatedLayerID, uint32_t tileID, const TileUpdate&);
121     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>);
122     void removeUpdateAtlas(uint32_t atlasID);
123     void flushLayerChanges(const FloatPoint& scrollPosition);
124     void createImageBacking(CoordinatedImageBackingID);
125     void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>);
126     void clearImageBackingContents(CoordinatedImageBackingID);
127     void removeImageBacking(CoordinatedImageBackingID);
128     void setLayerAnimations(CoordinatedLayerID, const GraphicsLayerAnimations&);
129     void setAnimationsLocked(bool);
130     void setBackgroundColor(const Color&);
131     void setDrawsBackground(bool enable) { m_setDrawsBackground = enable; }
132
133 #if ENABLE(REQUEST_ANIMATION_FRAME)
134     void requestAnimationFrame();
135 #endif
136
137 private:
138     GraphicsLayer* layerByID(CoordinatedLayerID id)
139     {
140         ASSERT(m_layers.contains(id));
141         ASSERT(id != InvalidCoordinatedLayerID);
142         return m_layers.get(id);
143     }
144     GraphicsLayer* getLayerByIDIfExists(CoordinatedLayerID);
145     GraphicsLayer* rootLayer() { return m_rootLayer.get(); }
146
147     void syncRemoteContent();
148     void adjustPositionForFixedLayers();
149
150     // Reimplementations from GraphicsLayerClient.
151     virtual void notifyAnimationStarted(const GraphicsLayer*, double) { }
152     virtual void notifyFlushRequired(const GraphicsLayer*) { }
153     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE { }
154
155     void dispatchOnMainThread(const Function<void()>&);
156     void updateViewport();
157 #if ENABLE(REQUEST_ANIMATION_FRAME)
158     void animationFrameReady();
159 #endif
160     void renderNextFrame();
161     void purgeBackingStores();
162
163     void createLayer(CoordinatedLayerID);
164     void deleteLayer(CoordinatedLayerID);
165
166     void assignImageBackingToLayer(GraphicsLayer*, CoordinatedImageBackingID);
167     void removeReleasedImageBackingsIfNeeded();
168     void ensureRootLayer();
169     void commitPendingBackingStoreOperations();
170
171     void prepareContentBackingStore(GraphicsLayer*);
172     void createBackingStoreIfNeeded(GraphicsLayer*);
173     void removeBackingStoreIfNeeded(GraphicsLayer*);
174     void resetBackingStoreSizeToLayerSize(GraphicsLayer*);
175
176     void updateFPS(const FloatPoint&, const TransformationMatrix& = TransformationMatrix());
177
178     FloatSize m_contentsSize;
179     FloatRect m_visibleContentsRect;
180
181     // Render queue can be accessed ony from main thread or updatePaintNode call stack!
182     Vector<Function<void()> > m_renderQueue;
183     Mutex m_renderQueueMutex;
184
185     OwnPtr<TextureMapper> m_textureMapper;
186
187     typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
188     ImageBackingMap m_imageBackings;
189     Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
190
191     typedef HashMap<GraphicsLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
192     BackingStoreMap m_backingStores;
193
194     HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
195
196 #if USE(GRAPHICS_SURFACE)
197     typedef HashMap<CoordinatedLayerID, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
198     SurfaceBackingStoreMap m_surfaceBackingStores;
199 #endif
200
201     typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
202     SurfaceMap m_surfaces;
203
204     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
205     CoordinatedGraphicsSceneClient* m_client;
206     bool m_isActive;
207
208     OwnPtr<GraphicsLayer> m_rootLayer;
209
210     typedef HashMap<CoordinatedLayerID, OwnPtr<GraphicsLayer> > LayerMap;
211     LayerMap m_layers;
212     typedef HashMap<CoordinatedLayerID, GraphicsLayer*> LayerRawPtrMap;
213     LayerRawPtrMap m_fixedLayers;
214     CoordinatedLayerID m_rootLayerID;
215     FloatPoint m_renderedContentsScrollPosition;
216     bool m_animationsLocked;
217 #if ENABLE(REQUEST_ANIMATION_FRAME)
218     bool m_animationFrameRequested;
219 #endif
220     Color m_backgroundColor;
221     bool m_setDrawsBackground;
222
223 #if ENABLE(CSS_SHADERS)
224     typedef HashMap<int, RefPtr<CustomFilterProgram> > CustomFilterProgramMap;
225     CustomFilterProgramMap m_customFilterPrograms;
226 #endif
227
228     bool m_isShowingFPS;
229     double m_fpsInterval;
230     double m_fpsTimestamp;
231     int m_lastFPS;
232     int m_frameCount;
233 };
234
235 } // namespace WebCore
236
237 #endif // USE(COORDINATED_GRAPHICS)
238
239 #endif // CoordinatedGraphicsScene_h
240
241