[GTK] Use the DisplayRefreshMonitor facilities
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / CoordinatedGraphics / ThreadedCoordinatedLayerTreeHost.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4  * Copyright (C) 2012 Company 100, Inc.
5  * Copyright (C) 2014 Igalia S.L.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "ThreadedCoordinatedLayerTreeHost.h"
31
32 #if USE(COORDINATED_GRAPHICS_THREADED)
33
34 #include "AcceleratedSurface.h"
35 #include "WebPage.h"
36 #include <WebCore/FrameView.h>
37 #include <WebCore/MainFrame.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 Ref<ThreadedCoordinatedLayerTreeHost> ThreadedCoordinatedLayerTreeHost::create(WebPage& webPage)
44 {
45     return adoptRef(*new ThreadedCoordinatedLayerTreeHost(webPage));
46 }
47
48 ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
49 {
50 }
51
52 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
53     : CoordinatedLayerTreeHost(webPage)
54     , m_compositorClient(*this)
55     , m_surface(AcceleratedSurface::create(webPage))
56     , m_viewportController(webPage.size())
57 {
58     if (FrameView* frameView = m_webPage.mainFrameView()) {
59         auto contentsSize = frameView->contentsSize();
60         if (!contentsSize.isEmpty())
61             m_viewportController.didChangeContentsSize(contentsSize);
62     }
63
64     IntSize scaledSize(m_webPage.size());
65     scaledSize.scale(m_webPage.deviceScaleFactor());
66     float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
67
68     if (m_surface) {
69         TextureMapper::PaintFlags paintFlags = 0;
70
71         if (m_surface->shouldPaintMirrored())
72             paintFlags |= TextureMapper::PaintingMirrored;
73
74         // Do not do frame sync when rendering offscreen in the web process to ensure that SwapBuffers never blocks.
75         // Rendering to the actual screen will happen later anyway since the UI process schedules a redraw for every update,
76         // the compositor will take care of syncing to vblank.
77         m_compositor = ThreadedCompositor::create(m_compositorClient, scaledSize, scaleFactor, m_surface->window(), ThreadedCompositor::ShouldDoFrameSync::Yes, paintFlags);
78         m_layerTreeContext.contextID = m_surface->surfaceID();
79     } else
80         m_compositor = ThreadedCompositor::create(m_compositorClient, scaledSize, scaleFactor);
81
82     didChangeViewport();
83 }
84
85 void ThreadedCoordinatedLayerTreeHost::invalidate()
86 {
87     m_compositor->invalidate();
88     CoordinatedLayerTreeHost::invalidate();
89     m_surface = nullptr;
90 }
91
92 void ThreadedCoordinatedLayerTreeHost::forceRepaint()
93 {
94     CoordinatedLayerTreeHost::forceRepaint();
95     m_compositor->forceRepaint();
96 }
97
98 void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
99 {
100     FrameView* frameView = m_webPage.mainFrameView();
101     if (!frameView || !frameView->delegatesScrolling())
102         return;
103
104     m_viewportController.didScroll(rect.location());
105     if (m_isDiscardable)
106         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
107     else
108         didChangeViewport();
109 }
110
111 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
112 {
113     m_viewportController.didChangeContentsSize(newSize);
114     if (m_isDiscardable)
115         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
116     else
117         didChangeViewport();
118 }
119
120 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
121 {
122     if (m_isDiscardable) {
123         m_discardableSyncActions |= DiscardableSyncActions::UpdateScale;
124         return;
125     }
126
127     if (m_surface && m_surface->resize(m_webPage.size()))
128         m_layerTreeContext.contextID = m_surface->surfaceID();
129
130     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
131     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
132 }
133
134 void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
135 {
136     if (m_isDiscardable) {
137         m_discardableSyncActions |= DiscardableSyncActions::UpdateBackground;
138         return;
139     }
140
141     CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
142     m_compositor->setDrawsBackground(m_webPage.drawsBackground());
143 }
144
145 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
146 {
147     if (m_isDiscardable) {
148         m_discardableSyncActions |= DiscardableSyncActions::UpdateSize;
149         m_viewportController.didChangeViewportSize(size);
150         return;
151     }
152
153     if (m_surface && m_surface->resize(size))
154         m_layerTreeContext.contextID = m_surface->surfaceID();
155
156     CoordinatedLayerTreeHost::sizeDidChange(size);
157     m_viewportController.didChangeViewportSize(size);
158     IntSize scaledSize(size);
159     scaledSize.scale(m_webPage.deviceScaleFactor());
160     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
161     didChangeViewport();
162 }
163
164 void ThreadedCoordinatedLayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
165 {
166     m_viewportController.didChangeViewportAttributes(WTFMove(attr));
167     if (m_isDiscardable)
168         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
169     else
170         didChangeViewport();
171 }
172
173 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
174 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
175 {
176     m_layerTreeContext.contextID = handle;
177     m_compositor->setNativeSurfaceHandleForCompositing(handle);
178     scheduleLayerFlush();
179 }
180 #endif
181
182 void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
183 {
184     FloatRect visibleRect(m_viewportController.visibleContentsRect());
185     if (visibleRect.isEmpty())
186         return;
187
188     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
189     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
190     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
191     FrameView* view = m_webPage.corePage()->mainFrame().view();
192     Scrollbar* scrollbar = view->verticalScrollbar();
193     if (scrollbar && !scrollbar->isOverlayScrollbar())
194         visibleRect.expand(scrollbar->width(), 0);
195     scrollbar = view->horizontalScrollbar();
196     if (scrollbar && !scrollbar->isOverlayScrollbar())
197         visibleRect.expand(0, scrollbar->height());
198
199     CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect, FloatPoint::zero());
200
201     float pageScale = m_viewportController.pageScaleFactor();
202     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
203     if (m_lastScrollPosition != scrollPosition) {
204         m_lastScrollPosition = scrollPosition;
205         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
206
207         if (!view->useFixedLayout())
208             view->notifyScrollPositionChanged(m_lastScrollPosition);
209     }
210
211     if (m_lastPageScaleFactor != pageScale) {
212         m_lastPageScaleFactor = pageScale;
213         m_webPage.scalePage(pageScale, m_lastScrollPosition);
214     }
215 }
216
217 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
218 {
219     CoordinatedLayerTreeHost::commitSceneState(state);
220     m_compositor->updateSceneState(state);
221 }
222
223 void ThreadedCoordinatedLayerTreeHost::setIsDiscardable(bool discardable)
224 {
225     m_isDiscardable = discardable;
226     if (m_isDiscardable) {
227         m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
228         return;
229     }
230
231     if (m_discardableSyncActions.isEmpty())
232         return;
233
234     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateBackground))
235         pageBackgroundTransparencyChanged();
236
237     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
238         // Size changes already sets the scale factor and updates the viewport.
239         sizeDidChange(m_webPage.size());
240         return;
241     }
242
243     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
244         deviceOrPageScaleFactorChanged();
245
246     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
247         didChangeViewport();
248 }
249
250 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
251 RefPtr<WebCore::DisplayRefreshMonitor> ThreadedCoordinatedLayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
252 {
253     return m_compositor->displayRefreshMonitor(displayID);
254 }
255 #endif
256
257 } // namespace WebKit
258
259 #endif // USE(COORDINATED_GRAPHICS)