requestFrameAnimation() callback timestamp should be very close to Performance.now()
[WebKit-https.git] / Source / WebCore / platform / graphics / texmap / coordinated / CompositingCoordinator.h
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2013 Company 100, Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef CompositingCoordinator_h
28 #define CompositingCoordinator_h
29
30 #if USE(COORDINATED_GRAPHICS)
31
32 #include "CoordinatedGraphicsLayer.h"
33 #include "CoordinatedGraphicsState.h"
34 #include "CoordinatedImageBacking.h"
35 #include "FloatPoint.h"
36 #include "GraphicsLayerClient.h"
37 #include "GraphicsLayerFactory.h"
38 #include "IntRect.h"
39 #include "Timer.h"
40 #include "UpdateAtlas.h"
41
42 namespace WebCore {
43
44 class Page;
45 class GraphicsContext;
46 class GraphicsLayer;
47 class CoordinatedSurface;
48
49 class CompositingCoordinator : public GraphicsLayerClient
50     , public CoordinatedGraphicsLayerClient
51     , public CoordinatedImageBacking::Client
52     , public UpdateAtlas::Client
53     , public GraphicsLayerFactory {
54     WTF_MAKE_NONCOPYABLE(CompositingCoordinator); WTF_MAKE_FAST_ALLOCATED;
55 public:
56     class Client {
57     public:
58         virtual void didFlushRootLayer(const FloatRect& visibleContentRect) = 0;
59         virtual void notifyFlushRequired() = 0;
60         virtual void commitSceneState(const CoordinatedGraphicsState&) = 0;
61         virtual void paintLayerContents(const GraphicsLayer*, GraphicsContext&, const IntRect& clipRect) = 0;
62     };
63
64     CompositingCoordinator(Page*, CompositingCoordinator::Client*);
65     virtual ~CompositingCoordinator();
66
67     void setRootCompositingLayer(GraphicsLayer*);
68     void setViewOverlayRootLayer(GraphicsLayer*);
69     void sizeDidChange(const IntSize& newSize);
70     void deviceOrPageScaleFactorChanged();
71
72     void setVisibleContentsRect(const FloatRect&, const FloatPoint&);
73     void renderNextFrame();
74     void purgeBackingStores();
75     void commitScrollOffset(uint32_t layerID, const IntSize& offset);
76
77     void createRootLayer(const IntSize&);
78     void clearRootLayer() { m_rootLayer = nullptr; }
79     GraphicsLayer* rootLayer() const { return m_rootLayer.get(); }
80     CoordinatedGraphicsLayer* mainContentsLayer();
81
82     bool flushPendingLayerChanges();
83     CoordinatedGraphicsState& state() { return m_state; }
84
85     void syncDisplayState();
86
87 #if ENABLE(REQUEST_ANIMATION_FRAME)
88     double nextAnimationServiceTime() const;
89 #endif
90
91 private:
92     // GraphicsLayerClient
93     void notifyAnimationStarted(const GraphicsLayer*, const String&, double time) override;
94     void notifyFlushRequired(const GraphicsLayer*) override;
95     void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clipRect) override;
96     float deviceScaleFactor() const override;
97     float pageScaleFactor() const override;
98
99     // CoordinatedImageBacking::Client
100     void createImageBacking(CoordinatedImageBackingID) override;
101     void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>) override;
102     void clearImageBackingContents(CoordinatedImageBackingID) override;
103     void removeImageBacking(CoordinatedImageBackingID) override;
104
105     // CoordinatedGraphicsLayerClient
106     bool isFlushingLayerChanges() const override { return m_isFlushingLayerChanges; }
107     FloatRect visibleContentsRect() const override;
108     PassRefPtr<CoordinatedImageBacking> createImageBackingIfNeeded(Image*) override;
109     void detachLayer(CoordinatedGraphicsLayer*) override;
110     bool paintToSurface(const WebCore::IntSize&, WebCore::CoordinatedSurface::Flags, uint32_t& /* atlasID */, WebCore::IntPoint&, WebCore::CoordinatedSurface::Client*) override;
111     void syncLayerState(CoordinatedLayerID, CoordinatedGraphicsLayerState&) override;
112
113     // UpdateAtlas::Client
114     void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>) override;
115     void removeUpdateAtlas(uint32_t atlasID) override;
116
117     // GraphicsLayerFactory
118     std::unique_ptr<GraphicsLayer> createGraphicsLayer(GraphicsLayer::Type, GraphicsLayerClient&) override;
119
120     void initializeRootCompositingLayerIfNeeded();
121     void flushPendingImageBackingChanges();
122     void clearPendingStateChanges();
123
124     void scheduleReleaseInactiveAtlases();
125
126     void releaseInactiveAtlasesTimerFired();
127
128     double timestamp() const;
129
130     Page* m_page;
131     CompositingCoordinator::Client* m_client;
132
133     std::unique_ptr<GraphicsLayer> m_rootLayer;
134     GraphicsLayer* m_rootCompositingLayer;
135     GraphicsLayer* m_overlayCompositingLayer;
136
137     CoordinatedGraphicsState m_state;
138
139     typedef HashMap<CoordinatedLayerID, CoordinatedGraphicsLayer*> LayerMap;
140     LayerMap m_registeredLayers;
141     typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedImageBacking> > ImageBackingMap;
142     ImageBackingMap m_imageBackings;
143     Vector<std::unique_ptr<UpdateAtlas>> m_updateAtlases;
144
145     // We don't send the messages related to releasing resources to renderer during purging, because renderer already had removed all resources.
146     bool m_isDestructing;
147     bool m_isPurging;
148     bool m_isFlushingLayerChanges;
149
150     FloatRect m_visibleContentsRect;
151
152     bool m_shouldSyncFrame;
153     bool m_didInitializeRootCompositingLayer;
154     Timer m_releaseInactiveAtlasesTimer;
155
156 #if ENABLE(REQUEST_ANIMATION_FRAME)
157     double m_lastAnimationServiceTime;
158 #endif
159 };
160
161 }
162
163 #endif
164
165 #endif // CompositingCoordinator_h