ab90c85e3d57294b7214de9fdfea844cc2b1e87e
[WebKit-https.git] / Source / WebKit2 / Shared / CoordinatedGraphics / 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 <WebCore/CoordinatedGraphicsState.h>
26 #include <WebCore/CoordinatedSurface.h>
27 #include <WebCore/GraphicsContext.h>
28 #include <WebCore/GraphicsLayer.h>
29 #include <WebCore/IntRect.h>
30 #include <WebCore/IntSize.h>
31 #include <WebCore/TextureMapper.h>
32 #include <WebCore/TextureMapperBackingStore.h>
33 #include <WebCore/TextureMapperFPSCounter.h>
34 #include <WebCore/TextureMapperLayer.h>
35 #include <WebCore/Timer.h>
36 #include <functional>
37 #include <wtf/HashSet.h>
38 #include <wtf/RunLoop.h>
39 #include <wtf/ThreadingPrimitives.h>
40 #include <wtf/Vector.h>
41
42 #if USE(GRAPHICS_SURFACE)
43 #include <WebCore/GraphicsSurface.h>
44 #include <WebCore/TextureMapperSurfaceBackingStore.h>
45 #endif
46
47 namespace WebKit {
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 WebCore::IntSize& offset) = 0;
58 };
59
60 class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public WebCore::TextureMapperLayer::ScrollingClient {
61 public:
62     explicit CoordinatedGraphicsScene(CoordinatedGraphicsSceneClient*);
63     virtual ~CoordinatedGraphicsScene();
64     void paintToCurrentGLContext(const WebCore::TransformationMatrix&, float, const WebCore::FloatRect&, const WebCore::Color& backgroundColor, bool drawsBackground, const WebCore::FloatPoint&, WebCore::TextureMapper::PaintFlags = 0);
65     void paintToGraphicsContext(PlatformGraphicsContext*, const WebCore::Color& backgroundColor, bool drawsBackground);
66     void detach();
67     void appendUpdate(std::function<void()>);
68
69     WebCore::TextureMapperLayer* findScrollableContentsLayerAt(const WebCore::FloatPoint&);
70
71     virtual void commitScrollOffset(uint32_t layerID, const WebCore::IntSize& offset);
72
73     // 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.
74     // Currently, QQuickWebPage::updatePaintNode() locks the main thread before calling both methods.
75     void purgeGLResources();
76
77     bool isActive() const { return m_isActive; }
78     void setActive(bool);
79
80     void commitSceneState(const WebCore::CoordinatedGraphicsState&);
81
82     void setViewBackgroundColor(const WebCore::Color& color) { m_viewBackgroundColor = color; }
83     WebCore::Color viewBackgroundColor() const { return m_viewBackgroundColor; }
84
85 private:
86     void setRootLayerID(WebCore::CoordinatedLayerID);
87     void createLayers(const Vector<WebCore::CoordinatedLayerID>&);
88     void deleteLayers(const Vector<WebCore::CoordinatedLayerID>&);
89     void setLayerState(WebCore::CoordinatedLayerID, const WebCore::CoordinatedGraphicsLayerState&);
90     void setLayerChildrenIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
91     void updateTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
92     void createTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
93     void removeTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
94     void setLayerFiltersIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
95     void setLayerAnimationsIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
96 #if USE(GRAPHICS_SURFACE)
97     void createPlatformLayerIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
98     void syncPlatformLayerIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
99     void destroyPlatformLayerIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
100 #endif
101     void setLayerRepaintCountIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
102
103     void syncUpdateAtlases(const WebCore::CoordinatedGraphicsState&);
104     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<WebCore::CoordinatedSurface>);
105     void removeUpdateAtlas(uint32_t atlasID);
106
107     void syncImageBackings(const WebCore::CoordinatedGraphicsState&);
108     void createImageBacking(WebCore::CoordinatedImageBackingID);
109     void updateImageBacking(WebCore::CoordinatedImageBackingID, PassRefPtr<WebCore::CoordinatedSurface>);
110     void clearImageBackingContents(WebCore::CoordinatedImageBackingID);
111     void removeImageBacking(WebCore::CoordinatedImageBackingID);
112
113     WebCore::TextureMapperLayer* layerByID(WebCore::CoordinatedLayerID id)
114     {
115         ASSERT(m_layers.contains(id));
116         ASSERT(id != WebCore::InvalidCoordinatedLayerID);
117         return m_layers.get(id);
118     }
119     WebCore::TextureMapperLayer* getLayerByIDIfExists(WebCore::CoordinatedLayerID);
120     WebCore::TextureMapperLayer* rootLayer() { return m_rootLayer.get(); }
121
122     void syncRemoteContent();
123     void adjustPositionForFixedLayers(const WebCore::FloatPoint& contentPosition);
124
125     void dispatchOnMainThread(std::function<void()>);
126     void dispatchOnClientRunLoop(std::function<void()>);
127     void updateViewport();
128     void renderNextFrame();
129     void purgeBackingStores();
130
131     void createLayer(WebCore::CoordinatedLayerID);
132     void deleteLayer(WebCore::CoordinatedLayerID);
133
134     void assignImageBackingToLayer(WebCore::TextureMapperLayer*, WebCore::CoordinatedImageBackingID);
135     void removeReleasedImageBackingsIfNeeded();
136     void ensureRootLayer();
137     void commitPendingBackingStoreOperations();
138
139     void prepareContentBackingStore(WebCore::TextureMapperLayer*);
140     void createBackingStoreIfNeeded(WebCore::TextureMapperLayer*);
141     void removeBackingStoreIfNeeded(WebCore::TextureMapperLayer*);
142     void resetBackingStoreSizeToLayerSize(WebCore::TextureMapperLayer*);
143
144     void dispatchCommitScrollOffset(uint32_t layerID, const WebCore::IntSize& offset);
145
146     // Render queue can be accessed ony from main thread or updatePaintNode call stack!
147     Vector<std::function<void()>> m_renderQueue;
148     DeprecatedMutex m_renderQueueMutex;
149
150     std::unique_ptr<WebCore::TextureMapper> m_textureMapper;
151
152     typedef HashMap<WebCore::CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore>> ImageBackingMap;
153     ImageBackingMap m_imageBackings;
154     Vector<RefPtr<CoordinatedBackingStore>> m_releasedImageBackings;
155
156     typedef HashMap<WebCore::TextureMapperLayer*, RefPtr<CoordinatedBackingStore>> BackingStoreMap;
157     BackingStoreMap m_backingStores;
158
159     HashSet<RefPtr<CoordinatedBackingStore>> m_backingStoresWithPendingBuffers;
160
161 #if USE(GRAPHICS_SURFACE)
162     typedef HashMap<WebCore::TextureMapperLayer*, RefPtr<WebCore::TextureMapperSurfaceBackingStore>> SurfaceBackingStoreMap;
163     SurfaceBackingStoreMap m_surfaceBackingStores;
164 #endif
165
166     typedef HashMap<uint32_t /* atlasID */, RefPtr<WebCore::CoordinatedSurface>> SurfaceMap;
167     SurfaceMap m_surfaces;
168
169     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
170     CoordinatedGraphicsSceneClient* m_client;
171     bool m_isActive;
172
173     std::unique_ptr<WebCore::TextureMapperLayer> m_rootLayer;
174
175     typedef HashMap<WebCore::CoordinatedLayerID, std::unique_ptr<WebCore::TextureMapperLayer>> LayerMap;
176     LayerMap m_layers;
177     typedef HashMap<WebCore::CoordinatedLayerID, WebCore::TextureMapperLayer*> LayerRawPtrMap;
178     LayerRawPtrMap m_fixedLayers;
179     WebCore::CoordinatedLayerID m_rootLayerID;
180     WebCore::FloatPoint m_scrollPosition;
181     WebCore::FloatPoint m_renderedContentsScrollPosition;
182     WebCore::Color m_viewBackgroundColor;
183
184     WebCore::TextureMapperFPSCounter m_fpsCounter;
185
186     RunLoop& m_clientRunLoop;
187 };
188
189 } // namespace WebKit
190
191 #endif // USE(COORDINATED_GRAPHICS)
192
193 #endif // CoordinatedGraphicsScene_h
194
195