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