66954ff5817cabf4ce88ee166a8106958a17a01e
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / CoordinatedGraphics / 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 <WebCore/CoordinatedGraphicsLayer.h>
33 #include <WebCore/CoordinatedGraphicsState.h>
34 #include <WebCore/CoordinatedImageBacking.h>
35 #include <WebCore/FloatPoint.h>
36 #include <WebCore/GraphicsLayerClient.h>
37 #include <WebCore/GraphicsLayerFactory.h>
38 #include <WebCore/IntRect.h>
39 #include <WebCore/NicosiaBuffer.h>
40 #include <WebCore/UpdateAtlas.h>
41
42 namespace Nicosia {
43 class PaintingEngine;
44 }
45
46 namespace WebCore {
47 class GraphicsContext;
48 class GraphicsLayer;
49 class Page;
50 }
51
52 namespace WebKit {
53
54 class CompositingCoordinator final : public WebCore::GraphicsLayerClient
55     , public WebCore::CoordinatedGraphicsLayerClient
56     , public WebCore::CoordinatedImageBacking::Client
57     , public WebCore::UpdateAtlas::Client
58     , public WebCore::GraphicsLayerFactory {
59     WTF_MAKE_NONCOPYABLE(CompositingCoordinator);
60 public:
61     class Client {
62     public:
63         virtual void didFlushRootLayer(const WebCore::FloatRect& visibleContentRect) = 0;
64         virtual void notifyFlushRequired() = 0;
65         virtual void commitSceneState(const WebCore::CoordinatedGraphicsState&) = 0;
66         virtual void releaseUpdateAtlases(const Vector<uint32_t>&) = 0;
67     };
68
69     CompositingCoordinator(WebCore::Page*, CompositingCoordinator::Client&);
70     virtual ~CompositingCoordinator();
71
72     void invalidate();
73     void clearUpdateAtlases();
74
75     void setRootCompositingLayer(WebCore::GraphicsLayer*);
76     void setViewOverlayRootLayer(WebCore::GraphicsLayer*);
77     void sizeDidChange(const WebCore::IntSize&);
78     void deviceOrPageScaleFactorChanged();
79
80     void setVisibleContentsRect(const WebCore::FloatRect&, const WebCore::FloatPoint&);
81     void renderNextFrame();
82
83     void createRootLayer(const WebCore::IntSize&);
84     WebCore::GraphicsLayer* rootLayer() const { return m_rootLayer.get(); }
85     WebCore::GraphicsLayer* rootCompositingLayer() const { return m_rootCompositingLayer; }
86     WebCore::CoordinatedGraphicsLayer* mainContentsLayer();
87
88     void forceFrameSync() { m_shouldSyncFrame = true; }
89
90     bool flushPendingLayerChanges();
91     WebCore::CoordinatedGraphicsState& state() { return m_state; }
92
93     void syncDisplayState();
94
95     double nextAnimationServiceTime() const;
96
97 private:
98     enum ReleaseAtlasPolicy {
99         ReleaseInactive,
100         ReleaseUnused
101     };
102
103     // GraphicsLayerClient
104     void notifyFlushRequired(const WebCore::GraphicsLayer*) override;
105     float deviceScaleFactor() const override;
106     float pageScaleFactor() const override;
107
108     // CoordinatedImageBacking::Client
109     void createImageBacking(WebCore::CoordinatedImageBackingID) override;
110     void updateImageBacking(WebCore::CoordinatedImageBackingID, RefPtr<Nicosia::Buffer>&&) override;
111     void clearImageBackingContents(WebCore::CoordinatedImageBackingID) override;
112     void removeImageBacking(WebCore::CoordinatedImageBackingID) override;
113
114     // CoordinatedGraphicsLayerClient
115     bool isFlushingLayerChanges() const override { return m_isFlushingLayerChanges; }
116     WebCore::FloatRect visibleContentsRect() const override;
117     Ref<WebCore::CoordinatedImageBacking> createImageBackingIfNeeded(WebCore::Image&) override;
118     void detachLayer(WebCore::CoordinatedGraphicsLayer*) override;
119     Ref<Nicosia::Buffer> getCoordinatedBuffer(const WebCore::IntSize&, Nicosia::Buffer::Flags, uint32_t&, WebCore::IntRect&) override;
120     Nicosia::PaintingEngine& paintingEngine() override;
121     void syncLayerState(WebCore::CoordinatedLayerID, WebCore::CoordinatedGraphicsLayerState&) override;
122
123     // UpdateAtlas::Client
124     void createUpdateAtlas(WebCore::UpdateAtlas::ID, Ref<Nicosia::Buffer>&&) override;
125     void removeUpdateAtlas(WebCore::UpdateAtlas::ID) override;
126
127     // GraphicsLayerFactory
128     std::unique_ptr<WebCore::GraphicsLayer> createGraphicsLayer(WebCore::GraphicsLayer::Type, WebCore::GraphicsLayerClient&) override;
129
130     void initializeRootCompositingLayerIfNeeded();
131     void flushPendingImageBackingChanges();
132     void clearPendingStateChanges();
133
134     void purgeBackingStores();
135
136     void scheduleReleaseInactiveAtlases();
137     void releaseInactiveAtlasesTimerFired();
138     void releaseAtlases(ReleaseAtlasPolicy);
139
140     double timestamp() const;
141
142     WebCore::Page* m_page;
143     CompositingCoordinator::Client& m_client;
144
145     std::unique_ptr<WebCore::GraphicsLayer> m_rootLayer;
146     WebCore::GraphicsLayer* m_rootCompositingLayer { nullptr };
147     WebCore::GraphicsLayer* m_overlayCompositingLayer { nullptr };
148
149     WebCore::CoordinatedGraphicsState m_state;
150
151     HashMap<WebCore::CoordinatedLayerID, WebCore::CoordinatedGraphicsLayer*> m_registeredLayers;
152     HashMap<WebCore::CoordinatedImageBackingID, RefPtr<WebCore::CoordinatedImageBacking>> m_imageBackings;
153
154     std::unique_ptr<Nicosia::PaintingEngine> m_paintingEngine;
155     Vector<std::unique_ptr<WebCore::UpdateAtlas>> m_updateAtlases;
156     Vector<uint32_t> m_atlasesToRemove;
157
158     // We don't send the messages related to releasing resources to renderer during purging, because renderer already had removed all resources.
159     bool m_isDestructing { false };
160     bool m_isPurging { false };
161     bool m_isFlushingLayerChanges { false };
162     bool m_shouldSyncFrame { false };
163     bool m_didInitializeRootCompositingLayer { false };
164
165     WebCore::FloatRect m_visibleContentsRect;
166     RunLoop::Timer<CompositingCoordinator> m_releaseInactiveAtlasesTimer;
167
168     double m_lastAnimationServiceTime { 0 };
169 };
170
171 }
172
173 #endif // namespace WebKit
174
175 #endif // CompositingCoordinator_h