d99f16bce750597448b1da79779bb358dfd38f7a
[WebKit-https.git] / Source / WebKit / 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/Frame.h>
37 #include <WebCore/FrameView.h>
38
39 namespace WebKit {
40 using namespace WebCore;
41
42 static const PlatformDisplayID primaryDisplayID = 0;
43 #if PLATFORM(GTK)
44 static const PlatformDisplayID compositingDisplayID = 1;
45 #else
46 static const PlatformDisplayID compositingDisplayID = primaryDisplayID;
47 #endif
48
49 Ref<ThreadedCoordinatedLayerTreeHost> ThreadedCoordinatedLayerTreeHost::create(WebPage& webPage)
50 {
51     return adoptRef(*new ThreadedCoordinatedLayerTreeHost(webPage));
52 }
53
54 ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
55 {
56 }
57
58 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
59     : CoordinatedLayerTreeHost(webPage)
60     , m_compositorClient(*this)
61     , m_surface(AcceleratedSurface::create(webPage, *this))
62     , m_viewportController(webPage.size())
63 {
64     if (FrameView* frameView = m_webPage.mainFrameView()) {
65         auto contentsSize = frameView->contentsSize();
66         if (!contentsSize.isEmpty())
67             m_viewportController.didChangeContentsSize(contentsSize);
68     }
69
70     IntSize scaledSize(m_webPage.size());
71     scaledSize.scale(m_webPage.deviceScaleFactor());
72     float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
73
74     if (m_surface) {
75         TextureMapper::PaintFlags paintFlags = 0;
76
77         if (m_surface->shouldPaintMirrored())
78             paintFlags |= TextureMapper::PaintingMirrored;
79
80         m_compositor = ThreadedCompositor::create(m_compositorClient, m_compositorClient, compositingDisplayID, scaledSize, scaleFactor, ThreadedCompositor::ShouldDoFrameSync::Yes, paintFlags);
81         m_layerTreeContext.contextID = m_surface->surfaceID();
82     } else
83         m_compositor = ThreadedCompositor::create(m_compositorClient, m_compositorClient, compositingDisplayID, scaledSize, scaleFactor);
84
85     m_webPage.windowScreenDidChange(compositingDisplayID);
86
87     didChangeViewport();
88 }
89
90 void ThreadedCoordinatedLayerTreeHost::invalidate()
91 {
92     m_compositor->invalidate();
93     CoordinatedLayerTreeHost::invalidate();
94     m_surface = nullptr;
95 }
96
97 void ThreadedCoordinatedLayerTreeHost::forceRepaint()
98 {
99     CoordinatedLayerTreeHost::forceRepaint();
100     m_compositor->forceRepaint();
101 }
102
103 void ThreadedCoordinatedLayerTreeHost::frameComplete()
104 {
105     m_compositor->frameComplete();
106 }
107
108 void ThreadedCoordinatedLayerTreeHost::requestDisplayRefreshMonitorUpdate()
109 {
110     // Flush layers to cause a repaint. If m_isWaitingForRenderer was true at this point, the layer
111     // flush won't do anything, but that means there's a painting ongoing that will send the
112     // display refresh notification when it's done.
113     flushLayersAndForceRepaint();
114 }
115
116 void ThreadedCoordinatedLayerTreeHost::handleDisplayRefreshMonitorUpdate(bool hasBeenRescheduled)
117 {
118     // Call renderNextFrame. If hasBeenRescheduled is true, the layer flush will force a repaint
119     // that will cause the display refresh notification to come.
120     renderNextFrame(hasBeenRescheduled);
121     m_compositor->handleDisplayRefreshMonitorUpdate();
122 }
123
124 uint64_t ThreadedCoordinatedLayerTreeHost::nativeSurfaceHandleForCompositing()
125 {
126     if (!m_surface)
127         return m_layerTreeContext.contextID;
128
129     m_surface->initialize();
130     return m_surface->window();
131 }
132
133 void ThreadedCoordinatedLayerTreeHost::didDestroyGLContext()
134 {
135     if (m_surface)
136         m_surface->finalize();
137 }
138
139 void ThreadedCoordinatedLayerTreeHost::willRenderFrame()
140 {
141     if (m_surface)
142         m_surface->willRenderFrame();
143 }
144
145 void ThreadedCoordinatedLayerTreeHost::didRenderFrame()
146 {
147     if (m_surface)
148         m_surface->didRenderFrame();
149 }
150
151 void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
152 {
153     FrameView* frameView = m_webPage.mainFrameView();
154     if (!frameView || !frameView->delegatesScrolling())
155         return;
156
157     m_viewportController.didScroll(rect.location());
158     if (m_isDiscardable)
159         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
160     else
161         didChangeViewport();
162 }
163
164 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
165 {
166     m_viewportController.didChangeContentsSize(newSize);
167     if (m_isDiscardable)
168         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
169     else
170         didChangeViewport();
171 }
172
173 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
174 {
175     if (m_isDiscardable) {
176         m_discardableSyncActions |= DiscardableSyncActions::UpdateScale;
177         return;
178     }
179
180     if (m_surface && m_surface->resize(m_webPage.size()))
181         m_layerTreeContext.contextID = m_surface->surfaceID();
182
183     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
184     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
185 }
186
187 void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
188 {
189     if (m_isDiscardable) {
190         m_discardableSyncActions |= DiscardableSyncActions::UpdateBackground;
191         return;
192     }
193
194     CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
195     m_compositor->setDrawsBackground(m_webPage.drawsBackground());
196 }
197
198 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
199 {
200     if (m_isDiscardable) {
201         m_discardableSyncActions |= DiscardableSyncActions::UpdateSize;
202         m_viewportController.didChangeViewportSize(size);
203         return;
204     }
205
206     if (m_surface && m_surface->resize(size))
207         m_layerTreeContext.contextID = m_surface->surfaceID();
208
209     CoordinatedLayerTreeHost::sizeDidChange(size);
210     m_viewportController.didChangeViewportSize(size);
211     IntSize scaledSize(size);
212     scaledSize.scale(m_webPage.deviceScaleFactor());
213     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
214     didChangeViewport();
215 }
216
217 void ThreadedCoordinatedLayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
218 {
219     m_viewportController.didChangeViewportAttributes(WTFMove(attr));
220     if (m_isDiscardable)
221         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
222     else
223         didChangeViewport();
224 }
225
226 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
227 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
228 {
229     m_layerTreeContext.contextID = handle;
230     m_compositor->setNativeSurfaceHandleForCompositing(handle);
231     scheduleLayerFlush();
232 }
233 #endif
234
235 void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
236 {
237     FloatRect visibleRect(m_viewportController.visibleContentsRect());
238     if (visibleRect.isEmpty())
239         return;
240
241     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
242     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
243     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
244     FrameView* view = m_webPage.corePage()->mainFrame().view();
245     Scrollbar* scrollbar = view->verticalScrollbar();
246     if (scrollbar && !scrollbar->isOverlayScrollbar())
247         visibleRect.expand(scrollbar->width(), 0);
248     scrollbar = view->horizontalScrollbar();
249     if (scrollbar && !scrollbar->isOverlayScrollbar())
250         visibleRect.expand(0, scrollbar->height());
251
252     CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect);
253
254     float pageScale = m_viewportController.pageScaleFactor();
255     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
256     if (m_lastScrollPosition != scrollPosition) {
257         m_lastScrollPosition = scrollPosition;
258         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
259
260         if (!view->useFixedLayout())
261             view->notifyScrollPositionChanged(m_lastScrollPosition);
262     }
263
264     if (m_lastPageScaleFactor != pageScale) {
265         m_lastPageScaleFactor = pageScale;
266         m_webPage.scalePage(pageScale, m_lastScrollPosition);
267     }
268 }
269
270 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
271 {
272     CoordinatedLayerTreeHost::commitSceneState(state);
273     m_compositor->updateSceneState(state);
274 }
275
276 void ThreadedCoordinatedLayerTreeHost::setIsDiscardable(bool discardable)
277 {
278     m_isDiscardable = discardable;
279     if (m_isDiscardable) {
280         m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
281         m_webPage.windowScreenDidChange(primaryDisplayID);
282         return;
283     }
284     m_webPage.windowScreenDidChange(compositingDisplayID);
285
286     if (m_discardableSyncActions.isEmpty())
287         return;
288
289     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateBackground))
290         pageBackgroundTransparencyChanged();
291
292     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
293         // Size changes already sets the scale factor and updates the viewport.
294         sizeDidChange(m_webPage.size());
295         return;
296     }
297
298     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
299         deviceOrPageScaleFactorChanged();
300
301     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
302         didChangeViewport();
303 }
304
305 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
306 RefPtr<WebCore::DisplayRefreshMonitor> ThreadedCoordinatedLayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
307 {
308     return m_compositor->displayRefreshMonitor(displayID);
309 }
310 #endif
311
312 } // namespace WebKit
313
314 #endif // USE(COORDINATED_GRAPHICS)