a3ef243a49a51c7b8b4ca7e76e1feafae8c8566b
[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     void didChangeScrollPosition(const FloatPoint& position);
90 #if USE(GRAPHICS_SURFACE)
91     void createCanvas(CoordinatedLayerID, const IntSize&, PassRefPtr<GraphicsSurface>);
92     void syncCanvas(CoordinatedLayerID, uint32_t frontBuffer);
93     void destroyCanvas(CoordinatedLayerID);
94 #endif
95     void setLayerRepaintCount(CoordinatedLayerID, int value);
96
97     void detach();
98     void appendUpdate(const Function<void()>&);
99
100     // 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.
101     // Currently, QQuickWebPage::updatePaintNode() locks the main thread before calling both methods.
102     void purgeGLResources();
103     void setActive(bool);
104
105     void createLayers(const Vector<CoordinatedLayerID>&);
106     void deleteLayers(const Vector<CoordinatedLayerID>&);
107     void setRootLayerID(CoordinatedLayerID);
108     void setLayerChildren(CoordinatedLayerID, const Vector<CoordinatedLayerID>&);
109     void setLayerState(CoordinatedLayerID, const CoordinatedLayerInfo&);
110 #if ENABLE(CSS_FILTERS)
111     void setLayerFilters(CoordinatedLayerID, const FilterOperations&);
112 #endif
113 #if ENABLE(CSS_SHADERS)
114     void injectCachedCustomFilterPrograms(const FilterOperations& filters) const;
115     void createCustomFilterProgram(int id, const CustomFilterProgramInfo&);
116     void removeCustomFilterProgram(int id);
117 #endif
118
119     void createTile(CoordinatedLayerID, uint32_t tileID, float scale);
120     void removeTile(CoordinatedLayerID, uint32_t tileID);
121     void updateTile(CoordinatedLayerID, uint32_t tileID, const TileUpdate&);
122     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>);
123     void removeUpdateAtlas(uint32_t atlasID);
124     void flushLayerChanges();
125     void createImageBacking(CoordinatedImageBackingID);
126     void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>);
127     void clearImageBackingContents(CoordinatedImageBackingID);
128     void removeImageBacking(CoordinatedImageBackingID);
129     void setLayerAnimations(CoordinatedLayerID, const GraphicsLayerAnimations&);
130     void setAnimationsLocked(bool);
131     void setBackgroundColor(const Color&);
132     void setDrawsBackground(bool enable) { m_setDrawsBackground = enable; }
133
134 #if ENABLE(REQUEST_ANIMATION_FRAME)
135     void requestAnimationFrame();
136 #endif
137
138 private:
139     GraphicsLayer* layerByID(CoordinatedLayerID id)
140     {
141         ASSERT(m_layers.contains(id));
142         ASSERT(id != InvalidCoordinatedLayerID);
143         return m_layers.get(id);
144     }
145     GraphicsLayer* getLayerByIDIfExists(CoordinatedLayerID);
146     GraphicsLayer* rootLayer() { return m_rootLayer.get(); }
147
148     void syncRemoteContent();
149     void adjustPositionForFixedLayers();
150
151     // Reimplementations from GraphicsLayerClient.
152     virtual void notifyAnimationStarted(const GraphicsLayer*, double) { }
153     virtual void notifyFlushRequired(const GraphicsLayer*) { }
154     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE { }
155
156     void dispatchOnMainThread(const Function<void()>&);
157     void updateViewport();
158 #if ENABLE(REQUEST_ANIMATION_FRAME)
159     void animationFrameReady();
160 #endif
161     void renderNextFrame();
162     void purgeBackingStores();
163
164     void createLayer(CoordinatedLayerID);
165     void deleteLayer(CoordinatedLayerID);
166
167     void assignImageBackingToLayer(GraphicsLayer*, CoordinatedImageBackingID);
168     void removeReleasedImageBackingsIfNeeded();
169     void ensureRootLayer();
170     void commitPendingBackingStoreOperations();
171
172     void prepareContentBackingStore(GraphicsLayer*);
173     void createBackingStoreIfNeeded(GraphicsLayer*);
174     void removeBackingStoreIfNeeded(GraphicsLayer*);
175     void resetBackingStoreSizeToLayerSize(GraphicsLayer*);
176
177     void updateFPS(const FloatPoint&, const TransformationMatrix& = TransformationMatrix());
178
179     FloatSize m_contentsSize;
180     FloatRect m_visibleContentsRect;
181
182     // Render queue can be accessed ony from main thread or updatePaintNode call stack!
183     Vector<Function<void()> > m_renderQueue;
184     Mutex m_renderQueueMutex;
185
186     OwnPtr<TextureMapper> m_textureMapper;
187
188     typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
189     ImageBackingMap m_imageBackings;
190     Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
191
192     typedef HashMap<GraphicsLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
193     BackingStoreMap m_backingStores;
194
195     HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
196
197 #if USE(GRAPHICS_SURFACE)
198     typedef HashMap<CoordinatedLayerID, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
199     SurfaceBackingStoreMap m_surfaceBackingStores;
200 #endif
201
202     typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
203     SurfaceMap m_surfaces;
204
205     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
206     CoordinatedGraphicsSceneClient* m_client;
207     bool m_isActive;
208
209     OwnPtr<GraphicsLayer> m_rootLayer;
210
211     typedef HashMap<CoordinatedLayerID, OwnPtr<GraphicsLayer> > LayerMap;
212     LayerMap m_layers;
213     typedef HashMap<CoordinatedLayerID, GraphicsLayer*> LayerRawPtrMap;
214     LayerRawPtrMap m_fixedLayers;
215     CoordinatedLayerID m_rootLayerID;
216     FloatPoint m_renderedContentsScrollPosition;
217     FloatPoint m_pendingRenderedContentsScrollPosition;
218     bool m_animationsLocked;
219 #if ENABLE(REQUEST_ANIMATION_FRAME)
220     bool m_animationFrameRequested;
221 #endif
222     Color m_backgroundColor;
223     bool m_setDrawsBackground;
224
225 #if ENABLE(CSS_SHADERS)
226     typedef HashMap<int, RefPtr<CustomFilterProgram> > CustomFilterProgramMap;
227     CustomFilterProgramMap m_customFilterPrograms;
228 #endif
229
230     bool m_isShowingFPS;
231     double m_fpsInterval;
232     double m_fpsTimestamp;
233     int m_lastFPS;
234     int m_frameCount;
235 };
236
237 } // namespace WebCore
238
239 #endif // USE(COORDINATED_GRAPHICS)
240
241 #endif // CoordinatedGraphicsScene_h
242
243