Accelerated overflow scrolling for Coordinated Graphics.
[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 "CoordinatedGraphicsState.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 "TextureMapperFPSCounter.h"
36 #include "TextureMapperLayer.h"
37 #include "Timer.h"
38 #include <wtf/Functional.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/ThreadingPrimitives.h>
41 #include <wtf/Vector.h>
42
43 #if USE(GRAPHICS_SURFACE)
44 #include "TextureMapperSurfaceBackingStore.h"
45 #endif
46
47 namespace WebCore {
48
49 class CoordinatedBackingStore;
50 class CustomFilterProgram;
51 class CustomFilterProgramInfo;
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     virtual void commitScrollOffset(uint32_t layerID, const IntSize& offset) = 0;
63 };
64
65 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public GraphicsLayerClient, public TextureMapperLayer::ScrollingClient {
66 public:
67     explicit CoordinatedGraphicsScene(CoordinatedGraphicsSceneClient*);
68     virtual ~CoordinatedGraphicsScene();
69     void paintToCurrentGLContext(const TransformationMatrix&, float, const FloatRect&, TextureMapper::PaintFlags = 0);
70     void paintToGraphicsContext(PlatformGraphicsContext*);
71     void setScrollPosition(const FloatPoint&);
72     void detach();
73     void appendUpdate(const Function<void()>&);
74
75     WebCore::TextureMapperLayer* findScrollableContentsLayerAt(const WebCore::FloatPoint&);
76
77     virtual void commitScrollOffset(uint32_t layerID, const IntSize& offset);
78
79     // 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.
80     // Currently, QQuickWebPage::updatePaintNode() locks the main thread before calling both methods.
81     void purgeGLResources();
82     void setActive(bool);
83
84     void commitSceneState(const CoordinatedGraphicsState&);
85
86     void createLayers(const Vector<CoordinatedLayerID>&);
87     void deleteLayers(const Vector<CoordinatedLayerID>&);
88
89 #if ENABLE(CSS_SHADERS)
90     void injectCachedCustomFilterPrograms(const FilterOperations& filters) const;
91     void createCustomFilterProgram(int id, const CustomFilterProgramInfo&);
92     void removeCustomFilterProgram(int id);
93 #endif
94
95     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>);
96     void removeUpdateAtlas(uint32_t atlasID);
97     void createImageBacking(CoordinatedImageBackingID);
98     void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>);
99     void clearImageBackingContents(CoordinatedImageBackingID);
100     void removeImageBacking(CoordinatedImageBackingID);
101     void setAnimationsLocked(bool);
102     void setBackgroundColor(const Color&);
103     void setDrawsBackground(bool enable) { m_setDrawsBackground = enable; }
104
105 #if ENABLE(REQUEST_ANIMATION_FRAME)
106     void requestAnimationFrame();
107 #endif
108
109 private:
110     void setRootLayerID(CoordinatedLayerID);
111     void setLayerState(CoordinatedLayerID, const CoordinatedGraphicsLayerState&);
112     void setLayerChildrenIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
113     void updateTilesIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
114     void createTilesIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
115     void removeTilesIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
116 #if ENABLE(CSS_FILTERS)
117     void setLayerFiltersIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
118 #endif
119     void setLayerAnimationsIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
120 #if USE(GRAPHICS_SURFACE)
121     void createCanvasIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
122     void syncCanvasIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
123     void destroyCanvasIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
124 #endif
125     void setLayerRepaintCountIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
126
127     GraphicsLayer* layerByID(CoordinatedLayerID id)
128     {
129         ASSERT(m_layers.contains(id));
130         ASSERT(id != InvalidCoordinatedLayerID);
131         return m_layers.get(id);
132     }
133     GraphicsLayer* getLayerByIDIfExists(CoordinatedLayerID);
134     GraphicsLayer* rootLayer() { return m_rootLayer.get(); }
135
136     void syncRemoteContent();
137     void adjustPositionForFixedLayers();
138
139     // Reimplementations from GraphicsLayerClient.
140     virtual void notifyAnimationStarted(const GraphicsLayer*, double) { }
141     virtual void notifyFlushRequired(const GraphicsLayer*) { }
142     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE { }
143
144     void dispatchOnMainThread(const Function<void()>&);
145     void updateViewport();
146 #if ENABLE(REQUEST_ANIMATION_FRAME)
147     void animationFrameReady();
148 #endif
149     void renderNextFrame();
150     void purgeBackingStores();
151
152     void createLayer(CoordinatedLayerID);
153     void deleteLayer(CoordinatedLayerID);
154
155     void assignImageBackingToLayer(GraphicsLayer*, CoordinatedImageBackingID);
156     void removeReleasedImageBackingsIfNeeded();
157     void ensureRootLayer();
158     void commitPendingBackingStoreOperations();
159
160     void prepareContentBackingStore(GraphicsLayer*);
161     void createBackingStoreIfNeeded(GraphicsLayer*);
162     void removeBackingStoreIfNeeded(GraphicsLayer*);
163     void resetBackingStoreSizeToLayerSize(GraphicsLayer*);
164
165     void dispatchCommitScrollOffset(uint32_t layerID, const IntSize& offset);
166
167     // Render queue can be accessed ony from main thread or updatePaintNode call stack!
168     Vector<Function<void()> > m_renderQueue;
169     Mutex m_renderQueueMutex;
170
171     OwnPtr<TextureMapper> m_textureMapper;
172
173     typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
174     ImageBackingMap m_imageBackings;
175     Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
176
177     typedef HashMap<GraphicsLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
178     BackingStoreMap m_backingStores;
179
180     HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
181
182 #if USE(GRAPHICS_SURFACE)
183     typedef HashMap<GraphicsLayer*, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
184     SurfaceBackingStoreMap m_surfaceBackingStores;
185 #endif
186
187     typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
188     SurfaceMap m_surfaces;
189
190     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
191     CoordinatedGraphicsSceneClient* m_client;
192     bool m_isActive;
193
194     OwnPtr<GraphicsLayer> m_rootLayer;
195
196     typedef HashMap<CoordinatedLayerID, OwnPtr<GraphicsLayer> > LayerMap;
197     LayerMap m_layers;
198     typedef HashMap<CoordinatedLayerID, GraphicsLayer*> LayerRawPtrMap;
199     LayerRawPtrMap m_fixedLayers;
200     CoordinatedLayerID m_rootLayerID;
201     FloatPoint m_scrollPosition;
202     FloatPoint m_renderedContentsScrollPosition;
203     bool m_animationsLocked;
204 #if ENABLE(REQUEST_ANIMATION_FRAME)
205     bool m_animationFrameRequested;
206 #endif
207     Color m_backgroundColor;
208     bool m_setDrawsBackground;
209
210 #if ENABLE(CSS_SHADERS)
211     typedef HashMap<int, RefPtr<CustomFilterProgram> > CustomFilterProgramMap;
212     CustomFilterProgramMap m_customFilterPrograms;
213 #endif
214
215     TextureMapperFPSCounter m_fpsCounter;
216 };
217
218 } // namespace WebCore
219
220 #endif // USE(COORDINATED_GRAPHICS)
221
222 #endif // CoordinatedGraphicsScene_h
223
224