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 "CoordinatedLayerInfo.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"
36 #include <wtf/Functional.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/ThreadingPrimitives.h>
39 #include <wtf/Vector.h>
41 #if USE(GRAPHICS_SURFACE)
42 #include "TextureMapperSurfaceBackingStore.h"
47 class CoordinatedBackingStore;
48 class CoordinatedLayerInfo;
49 class CustomFilterProgram;
50 class CustomFilterProgramInfo;
51 class TextureMapperLayer;
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;
64 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public GraphicsLayerClient {
71 TileUpdate(const IntRect& source, const IntRect& tile, uint32_t atlas, const IntPoint& newOffset)
79 explicit CoordinatedGraphicsScene(CoordinatedGraphicsSceneClient*);
80 virtual ~CoordinatedGraphicsScene();
81 void paintToCurrentGLContext(const TransformationMatrix&, float, const FloatRect&, TextureMapper::PaintFlags = 0);
83 void paintToGraphicsContext(QPainter*);
85 void paintToGraphicsContext(cairo_t*);
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);
94 void setLayerRepaintCount(CoordinatedLayerID, int value);
97 void appendUpdate(const Function<void()>&);
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);
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&);
112 #if ENABLE(CSS_SHADERS)
113 void injectCachedCustomFilterPrograms(const FilterOperations& filters) const;
114 void createCustomFilterProgram(int id, const CustomFilterProgramInfo&);
115 void removeCustomFilterProgram(int id);
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; }
133 #if ENABLE(REQUEST_ANIMATION_FRAME)
134 void requestAnimationFrame();
138 GraphicsLayer* layerByID(CoordinatedLayerID id)
140 ASSERT(m_layers.contains(id));
141 ASSERT(id != InvalidCoordinatedLayerID);
142 return m_layers.get(id);
144 GraphicsLayer* getLayerByIDIfExists(CoordinatedLayerID);
145 GraphicsLayer* rootLayer() { return m_rootLayer.get(); }
147 void syncRemoteContent();
148 void adjustPositionForFixedLayers();
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 { }
155 void dispatchOnMainThread(const Function<void()>&);
156 void updateViewport();
157 #if ENABLE(REQUEST_ANIMATION_FRAME)
158 void animationFrameReady();
160 void renderNextFrame();
161 void purgeBackingStores();
163 void createLayer(CoordinatedLayerID);
164 void deleteLayer(CoordinatedLayerID);
166 void assignImageBackingToLayer(GraphicsLayer*, CoordinatedImageBackingID);
167 void removeReleasedImageBackingsIfNeeded();
168 void ensureRootLayer();
169 void commitPendingBackingStoreOperations();
171 void prepareContentBackingStore(GraphicsLayer*);
172 void createBackingStoreIfNeeded(GraphicsLayer*);
173 void removeBackingStoreIfNeeded(GraphicsLayer*);
174 void resetBackingStoreSizeToLayerSize(GraphicsLayer*);
176 void updateFPS(const FloatPoint&, const TransformationMatrix& = TransformationMatrix());
178 FloatSize m_contentsSize;
179 FloatRect m_visibleContentsRect;
181 // Render queue can be accessed ony from main thread or updatePaintNode call stack!
182 Vector<Function<void()> > m_renderQueue;
183 Mutex m_renderQueueMutex;
185 OwnPtr<TextureMapper> m_textureMapper;
187 typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
188 ImageBackingMap m_imageBackings;
189 Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
191 typedef HashMap<GraphicsLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
192 BackingStoreMap m_backingStores;
194 HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
196 #if USE(GRAPHICS_SURFACE)
197 typedef HashMap<CoordinatedLayerID, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
198 SurfaceBackingStoreMap m_surfaceBackingStores;
201 typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
202 SurfaceMap m_surfaces;
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;
208 OwnPtr<GraphicsLayer> m_rootLayer;
210 typedef HashMap<CoordinatedLayerID, OwnPtr<GraphicsLayer> > LayerMap;
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;
220 Color m_backgroundColor;
221 bool m_setDrawsBackground;
223 #if ENABLE(CSS_SHADERS)
224 typedef HashMap<int, RefPtr<CustomFilterProgram> > CustomFilterProgramMap;
225 CustomFilterProgramMap m_customFilterPrograms;
229 double m_fpsInterval;
230 double m_fpsTimestamp;
235 } // namespace WebCore
237 #endif // USE(COORDINATED_GRAPHICS)
239 #endif // CoordinatedGraphicsScene_h