b147de45b73c26617e6be68de39bcc5dfe65495d
[WebKit-https.git] / Source / WebCore / platform / graphics / texmap / coordinated / CoordinatedGraphicsScene.h
1 /*
2     Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
3     Copyright (C) 2013 Company 100, Inc.
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19 */
20
21 #ifndef CoordinatedGraphicsScene_h
22 #define CoordinatedGraphicsScene_h
23
24 #if USE(COORDINATED_GRAPHICS)
25 #include "CoordinatedGraphicsState.h"
26 #include "CoordinatedSurface.h"
27 #include "GraphicsContext.h"
28 #include "GraphicsLayer.h"
29 #include "GraphicsLayerAnimation.h"
30 #include "GraphicsSurface.h"
31 #include "IntRect.h"
32 #include "IntSize.h"
33 #include "TextureMapper.h"
34 #include "TextureMapperBackingStore.h"
35 #include "TextureMapperFPSCounter.h"
36 #include "TextureMapperLayer.h"
37 #include "Timer.h"
38 #include <functional>
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
51 class CoordinatedGraphicsSceneClient {
52 public:
53     virtual ~CoordinatedGraphicsSceneClient() { }
54     virtual void purgeBackingStores() = 0;
55     virtual void renderNextFrame() = 0;
56     virtual void updateViewport() = 0;
57     virtual void commitScrollOffset(uint32_t layerID, const IntSize& offset) = 0;
58 };
59
60 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public TextureMapperLayer::ScrollingClient {
61 public:
62     explicit CoordinatedGraphicsScene(CoordinatedGraphicsSceneClient*);
63     virtual ~CoordinatedGraphicsScene();
64     void paintToCurrentGLContext(const TransformationMatrix&, float, const FloatRect&, TextureMapper::PaintFlags = 0);
65     void paintToGraphicsContext(PlatformGraphicsContext*);
66     void setScrollPosition(const FloatPoint&);
67     void detach();
68     void appendUpdate(std::function<void()>);
69
70     WebCore::TextureMapperLayer* findScrollableContentsLayerAt(const WebCore::FloatPoint&);
71
72     virtual void commitScrollOffset(uint32_t layerID, const IntSize& offset);
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
78     bool isActive() const { return m_isActive; }
79     void setActive(bool);
80
81     void commitSceneState(const CoordinatedGraphicsState&);
82
83     void setBackgroundColor(const Color&);
84     void setDrawsBackground(bool enable) { m_setDrawsBackground = enable; }
85
86 private:
87     void setRootLayerID(CoordinatedLayerID);
88     void createLayers(const Vector<CoordinatedLayerID>&);
89     void deleteLayers(const Vector<CoordinatedLayerID>&);
90     void setLayerState(CoordinatedLayerID, const CoordinatedGraphicsLayerState&);
91     void setLayerChildrenIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
92     void updateTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
93     void createTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
94     void removeTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
95 #if ENABLE(CSS_FILTERS)
96     void setLayerFiltersIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
97 #endif
98     void setLayerAnimationsIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
99 #if USE(GRAPHICS_SURFACE)
100     void createCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
101     void syncCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
102     void destroyCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
103 #endif
104     void setLayerRepaintCountIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
105
106     void syncUpdateAtlases(const CoordinatedGraphicsState&);
107     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>);
108     void removeUpdateAtlas(uint32_t atlasID);
109
110     void syncImageBackings(const CoordinatedGraphicsState&);
111     void createImageBacking(CoordinatedImageBackingID);
112     void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>);
113     void clearImageBackingContents(CoordinatedImageBackingID);
114     void removeImageBacking(CoordinatedImageBackingID);
115
116     TextureMapperLayer* layerByID(CoordinatedLayerID id)
117     {
118         ASSERT(m_layers.contains(id));
119         ASSERT(id != InvalidCoordinatedLayerID);
120         return m_layers.get(id);
121     }
122     TextureMapperLayer* getLayerByIDIfExists(CoordinatedLayerID);
123     TextureMapperLayer* rootLayer() { return m_rootLayer.get(); }
124
125     void syncRemoteContent();
126     void adjustPositionForFixedLayers();
127
128     void dispatchOnMainThread(std::function<void()>);
129     void updateViewport();
130     void renderNextFrame();
131     void purgeBackingStores();
132
133     void createLayer(CoordinatedLayerID);
134     void deleteLayer(CoordinatedLayerID);
135
136     void assignImageBackingToLayer(TextureMapperLayer*, CoordinatedImageBackingID);
137     void removeReleasedImageBackingsIfNeeded();
138     void ensureRootLayer();
139     void commitPendingBackingStoreOperations();
140
141     void prepareContentBackingStore(TextureMapperLayer*);
142     void createBackingStoreIfNeeded(TextureMapperLayer*);
143     void removeBackingStoreIfNeeded(TextureMapperLayer*);
144     void resetBackingStoreSizeToLayerSize(TextureMapperLayer*);
145
146     void dispatchCommitScrollOffset(uint32_t layerID, const IntSize& offset);
147
148     // Render queue can be accessed ony from main thread or updatePaintNode call stack!
149     Vector<std::function<void()>> m_renderQueue;
150     Mutex m_renderQueueMutex;
151
152     OwnPtr<TextureMapper> m_textureMapper;
153
154     typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
155     ImageBackingMap m_imageBackings;
156     Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
157
158     typedef HashMap<TextureMapperLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
159     BackingStoreMap m_backingStores;
160
161     HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
162
163 #if USE(GRAPHICS_SURFACE)
164     typedef HashMap<TextureMapperLayer*, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
165     SurfaceBackingStoreMap m_surfaceBackingStores;
166 #endif
167
168     typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
169     SurfaceMap m_surfaces;
170
171     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
172     CoordinatedGraphicsSceneClient* m_client;
173     bool m_isActive;
174
175     std::unique_ptr<TextureMapperLayer> m_rootLayer;
176
177     typedef HashMap<CoordinatedLayerID, std::unique_ptr<TextureMapperLayer>> LayerMap;
178     LayerMap m_layers;
179     typedef HashMap<CoordinatedLayerID, TextureMapperLayer*> LayerRawPtrMap;
180     LayerRawPtrMap m_fixedLayers;
181     CoordinatedLayerID m_rootLayerID;
182     FloatPoint m_scrollPosition;
183     FloatPoint m_renderedContentsScrollPosition;
184     Color m_backgroundColor;
185     bool m_setDrawsBackground;
186
187     TextureMapperFPSCounter m_fpsCounter;
188 };
189
190 } // namespace WebCore
191
192 #endif // USE(COORDINATED_GRAPHICS)
193
194 #endif // CoordinatedGraphicsScene_h
195
196