Stop using PassRefPtr in platform/graphics
[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
41 namespace WebCore {
42 class Page;
43 class GraphicsContext;
44 class GraphicsLayer;
45 class CoordinatedSurface;
46 }
47
48 namespace WebKit {
49
50 class CompositingCoordinator final : public WebCore::GraphicsLayerClient
51     , public WebCore::CoordinatedGraphicsLayerClient
52     , public WebCore::CoordinatedImageBacking::Client
53     , public UpdateAtlas::Client
54     , public WebCore::GraphicsLayerFactory {
55     WTF_MAKE_NONCOPYABLE(CompositingCoordinator);
56 public:
57     class Client {
58     public:
59         virtual void didFlushRootLayer(const WebCore::FloatRect& visibleContentRect) = 0;
60         virtual void notifyFlushRequired() = 0;
61         virtual void commitSceneState(const WebCore::CoordinatedGraphicsState&) = 0;
62         virtual void paintLayerContents(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, const WebCore::IntRect& clipRect) = 0;
63         virtual void releaseUpdateAtlases(Vector<uint32_t>&&) = 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::GraphicsLayer* rootCompositingLayer() const { return m_rootCompositingLayer; }
83     WebCore::CoordinatedGraphicsLayer* mainContentsLayer();
84
85     bool flushPendingLayerChanges();
86     WebCore::CoordinatedGraphicsState& state() { return m_state; }
87
88     void syncDisplayState();
89
90     double nextAnimationServiceTime() const;
91
92 private:
93     enum ReleaseAtlasPolicy {
94         ReleaseInactive,
95         ReleaseUnused
96     };
97
98     // GraphicsLayerClient
99     void notifyAnimationStarted(const WebCore::GraphicsLayer*, const String&, double time) override;
100     void notifyFlushRequired(const WebCore::GraphicsLayer*) override;
101     void paintContents(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& clipRect) override;
102     float deviceScaleFactor() const override;
103     float pageScaleFactor() const override;
104
105     // CoordinatedImageBacking::Client
106     void createImageBacking(WebCore::CoordinatedImageBackingID) override;
107     void updateImageBacking(WebCore::CoordinatedImageBackingID, RefPtr<WebCore::CoordinatedSurface>&&) override;
108     void clearImageBackingContents(WebCore::CoordinatedImageBackingID) override;
109     void removeImageBacking(WebCore::CoordinatedImageBackingID) override;
110
111     // CoordinatedGraphicsLayerClient
112     bool isFlushingLayerChanges() const override { return m_isFlushingLayerChanges; }
113     WebCore::FloatRect visibleContentsRect() const override;
114     Ref<WebCore::CoordinatedImageBacking> createImageBackingIfNeeded(WebCore::Image&) override;
115     void detachLayer(WebCore::CoordinatedGraphicsLayer*) override;
116     bool paintToSurface(const WebCore::IntSize&, WebCore::CoordinatedSurface::Flags, uint32_t& /* atlasID */, WebCore::IntPoint&, WebCore::CoordinatedSurface::Client&) override;
117     void syncLayerState(WebCore::CoordinatedLayerID, WebCore::CoordinatedGraphicsLayerState&) override;
118
119     // UpdateAtlas::Client
120     void createUpdateAtlas(uint32_t atlasID, RefPtr<WebCore::CoordinatedSurface>&&) override;
121     void removeUpdateAtlas(uint32_t atlasID) override;
122
123     // GraphicsLayerFactory
124     std::unique_ptr<WebCore::GraphicsLayer> createGraphicsLayer(WebCore::GraphicsLayer::Type, WebCore::GraphicsLayerClient&) override;
125
126     void initializeRootCompositingLayerIfNeeded();
127     void flushPendingImageBackingChanges();
128     void clearPendingStateChanges();
129
130     void purgeBackingStores();
131
132     void scheduleReleaseInactiveAtlases();
133     void releaseInactiveAtlasesTimerFired();
134     void releaseAtlases(ReleaseAtlasPolicy);
135
136     double timestamp() const;
137
138     WebCore::Page* m_page;
139     CompositingCoordinator::Client& m_client;
140
141     std::unique_ptr<WebCore::GraphicsLayer> m_rootLayer;
142     WebCore::GraphicsLayer* m_rootCompositingLayer { nullptr };
143     WebCore::GraphicsLayer* m_overlayCompositingLayer { nullptr };
144
145     WebCore::CoordinatedGraphicsState m_state;
146
147     typedef HashMap<WebCore::CoordinatedLayerID, WebCore::CoordinatedGraphicsLayer*> LayerMap;
148     LayerMap m_registeredLayers;
149     typedef HashMap<WebCore::CoordinatedImageBackingID, RefPtr<WebCore::CoordinatedImageBacking> > ImageBackingMap;
150     ImageBackingMap m_imageBackings;
151     Vector<std::unique_ptr<UpdateAtlas>> m_updateAtlases;
152     Vector<uint32_t> m_atlasesToRemove;
153
154     // We don't send the messages related to releasing resources to renderer during purging, because renderer already had removed all resources.
155     bool m_isDestructing { false };
156     bool m_isPurging { false };
157     bool m_isFlushingLayerChanges { false };
158     bool m_shouldSyncFrame { false };
159     bool m_didInitializeRootCompositingLayer { false };
160
161     WebCore::FloatRect m_visibleContentsRect;
162     RunLoop::Timer<CompositingCoordinator> m_releaseInactiveAtlasesTimer;
163
164     double m_lastAnimationServiceTime { 0 };
165 };
166
167 }
168
169 #endif // namespace WebKit
170
171 #endif // CompositingCoordinator_h