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