2 Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
20 #ifndef CoordinatedGraphicsScene_h
21 #define CoordinatedGraphicsScene_h
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"
33 #include "TextureMapper.h"
34 #include "TextureMapperBackingStore.h"
35 #include "TextureMapperFPSCounter.h"
36 #include "TextureMapperLayer.h"
38 #include <wtf/Functional.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/ThreadingPrimitives.h>
41 #include <wtf/Vector.h>
43 #if USE(GRAPHICS_SURFACE)
44 #include "TextureMapperSurfaceBackingStore.h"
49 class CoordinatedBackingStore;
50 class CustomFilterProgram;
51 class CustomFilterProgramInfo;
53 class CoordinatedGraphicsSceneClient {
55 virtual ~CoordinatedGraphicsSceneClient() { }
56 #if ENABLE(REQUEST_ANIMATION_FRAME)
57 virtual void animationFrameReady() = 0;
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;
65 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public GraphicsLayerClient, public TextureMapperLayer::ScrollingClient {
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&);
73 void appendUpdate(const Function<void()>&);
75 WebCore::TextureMapperLayer* findScrollableContentsLayerAt(const WebCore::FloatPoint&);
77 virtual void commitScrollOffset(uint32_t layerID, const IntSize& offset);
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();
84 void commitSceneState(const CoordinatedGraphicsState&);
86 void createLayers(const Vector<CoordinatedLayerID>&);
87 void deleteLayers(const Vector<CoordinatedLayerID>&);
89 #if ENABLE(CSS_SHADERS)
90 void injectCachedCustomFilterPrograms(const FilterOperations& filters) const;
91 void createCustomFilterProgram(int id, const CustomFilterProgramInfo&);
92 void removeCustomFilterProgram(int id);
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; }
105 #if ENABLE(REQUEST_ANIMATION_FRAME)
106 void requestAnimationFrame();
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&);
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&);
125 void setLayerRepaintCountIfNeeded(GraphicsLayer*, const CoordinatedGraphicsLayerState&);
127 GraphicsLayer* layerByID(CoordinatedLayerID id)
129 ASSERT(m_layers.contains(id));
130 ASSERT(id != InvalidCoordinatedLayerID);
131 return m_layers.get(id);
133 GraphicsLayer* getLayerByIDIfExists(CoordinatedLayerID);
134 GraphicsLayer* rootLayer() { return m_rootLayer.get(); }
136 void syncRemoteContent();
137 void adjustPositionForFixedLayers();
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 { }
144 void dispatchOnMainThread(const Function<void()>&);
145 void updateViewport();
146 #if ENABLE(REQUEST_ANIMATION_FRAME)
147 void animationFrameReady();
149 void renderNextFrame();
150 void purgeBackingStores();
152 void createLayer(CoordinatedLayerID);
153 void deleteLayer(CoordinatedLayerID);
155 void assignImageBackingToLayer(GraphicsLayer*, CoordinatedImageBackingID);
156 void removeReleasedImageBackingsIfNeeded();
157 void ensureRootLayer();
158 void commitPendingBackingStoreOperations();
160 void prepareContentBackingStore(GraphicsLayer*);
161 void createBackingStoreIfNeeded(GraphicsLayer*);
162 void removeBackingStoreIfNeeded(GraphicsLayer*);
163 void resetBackingStoreSizeToLayerSize(GraphicsLayer*);
165 void dispatchCommitScrollOffset(uint32_t layerID, const IntSize& offset);
167 // Render queue can be accessed ony from main thread or updatePaintNode call stack!
168 Vector<Function<void()> > m_renderQueue;
169 Mutex m_renderQueueMutex;
171 OwnPtr<TextureMapper> m_textureMapper;
173 typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
174 ImageBackingMap m_imageBackings;
175 Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
177 typedef HashMap<GraphicsLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
178 BackingStoreMap m_backingStores;
180 HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
182 #if USE(GRAPHICS_SURFACE)
183 typedef HashMap<GraphicsLayer*, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
184 SurfaceBackingStoreMap m_surfaceBackingStores;
187 typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
188 SurfaceMap m_surfaces;
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;
194 OwnPtr<GraphicsLayer> m_rootLayer;
196 typedef HashMap<CoordinatedLayerID, OwnPtr<GraphicsLayer> > LayerMap;
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;
207 Color m_backgroundColor;
208 bool m_setDrawsBackground;
210 #if ENABLE(CSS_SHADERS)
211 typedef HashMap<int, RefPtr<CustomFilterProgram> > CustomFilterProgramMap;
212 CustomFilterProgramMap m_customFilterPrograms;
215 TextureMapperFPSCounter m_fpsCounter;
218 } // namespace WebCore
220 #endif // USE(COORDINATED_GRAPHICS)
222 #endif // CoordinatedGraphicsScene_h