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
47namespace WebCore {
48
49class CoordinatedBackingStore;
50
51class CoordinatedGraphicsSceneClient {
52public:
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
60class CoordinatedGraphicsScene : public ThreadSafeRefCounted<CoordinatedGraphicsScene>, public TextureMapperLayer::ScrollingClient {
61public:
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    void setViewBackgroundColor(const Color& color) { m_viewBackgroundColor = color; }
87    Color viewBackgroundColor() const { return m_viewBackgroundColor; }
88
89private:
90    void setRootLayerID(CoordinatedLayerID);
91    void createLayers(const Vector<CoordinatedLayerID>&);
92    void deleteLayers(const Vector<CoordinatedLayerID>&);
93    void setLayerState(CoordinatedLayerID, const CoordinatedGraphicsLayerState&);
94    void setLayerChildrenIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
95    void updateTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
96    void createTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
97    void removeTilesIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
98#if ENABLE(CSS_FILTERS)
99    void setLayerFiltersIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
100#endif
101    void setLayerAnimationsIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
102#if USE(GRAPHICS_SURFACE)
103    void createCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
104    void syncCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
105    void destroyCanvasIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
106#endif
107    void setLayerRepaintCountIfNeeded(TextureMapperLayer*, const CoordinatedGraphicsLayerState&);
108
109    void syncUpdateAtlases(const CoordinatedGraphicsState&);
110    void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>);
111    void removeUpdateAtlas(uint32_t atlasID);
112
113    void syncImageBackings(const CoordinatedGraphicsState&);
114    void createImageBacking(CoordinatedImageBackingID);
115    void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>);
116    void clearImageBackingContents(CoordinatedImageBackingID);
117    void removeImageBacking(CoordinatedImageBackingID);
118
119    TextureMapperLayer* layerByID(CoordinatedLayerID id)
120    {
121        ASSERT(m_layers.contains(id));
122        ASSERT(id != InvalidCoordinatedLayerID);
123        return m_layers.get(id);
124    }
125    TextureMapperLayer* getLayerByIDIfExists(CoordinatedLayerID);
126    TextureMapperLayer* rootLayer() { return m_rootLayer.get(); }
127
128    void syncRemoteContent();
129    void adjustPositionForFixedLayers();
130
131    void dispatchOnMainThread(std::function<void()>);
132    void updateViewport();
133    void renderNextFrame();
134    void purgeBackingStores();
135
136    void createLayer(CoordinatedLayerID);
137    void deleteLayer(CoordinatedLayerID);
138
139    void assignImageBackingToLayer(TextureMapperLayer*, CoordinatedImageBackingID);
140    void removeReleasedImageBackingsIfNeeded();
141    void ensureRootLayer();
142    void commitPendingBackingStoreOperations();
143
144    void prepareContentBackingStore(TextureMapperLayer*);
145    void createBackingStoreIfNeeded(TextureMapperLayer*);
146    void removeBackingStoreIfNeeded(TextureMapperLayer*);
147    void resetBackingStoreSizeToLayerSize(TextureMapperLayer*);
148
149    void dispatchCommitScrollOffset(uint32_t layerID, const IntSize& offset);
150
151    // Render queue can be accessed ony from main thread or updatePaintNode call stack!
152    Vector<std::function<void()>> m_renderQueue;
153    Mutex m_renderQueueMutex;
154
155    std::unique_ptr<TextureMapper> m_textureMapper;
156
157    typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedBackingStore> > ImageBackingMap;
158    ImageBackingMap m_imageBackings;
159    Vector<RefPtr<CoordinatedBackingStore> > m_releasedImageBackings;
160
161    typedef HashMap<TextureMapperLayer*, RefPtr<CoordinatedBackingStore> > BackingStoreMap;
162    BackingStoreMap m_backingStores;
163
164    HashSet<RefPtr<CoordinatedBackingStore> > m_backingStoresWithPendingBuffers;
165
166#if USE(GRAPHICS_SURFACE)
167    typedef HashMap<TextureMapperLayer*, RefPtr<TextureMapperSurfaceBackingStore> > SurfaceBackingStoreMap;
168    SurfaceBackingStoreMap m_surfaceBackingStores;
169#endif
170
171    typedef HashMap<uint32_t /* atlasID */, RefPtr<CoordinatedSurface> > SurfaceMap;
172    SurfaceMap m_surfaces;
173
174    // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
175    CoordinatedGraphicsSceneClient* m_client;
176    bool m_isActive;
177
178    std::unique_ptr<TextureMapperLayer> m_rootLayer;
179
180    typedef HashMap<CoordinatedLayerID, std::unique_ptr<TextureMapperLayer>> LayerMap;
181    LayerMap m_layers;
182    typedef HashMap<CoordinatedLayerID, TextureMapperLayer*> LayerRawPtrMap;
183    LayerRawPtrMap m_fixedLayers;
184    CoordinatedLayerID m_rootLayerID;
185    FloatPoint m_scrollPosition;
186    FloatPoint m_renderedContentsScrollPosition;
187    Color m_backgroundColor;
188    Color m_viewBackgroundColor;
189    bool m_setDrawsBackground;
190
191    TextureMapperFPSCounter m_fpsCounter;
192};
193
194} // namespace WebCore
195
196#endif // USE(COORDINATED_GRAPHICS)
197
198#endif // CoordinatedGraphicsScene_h
199
200
201