aa2a0331c1cfa5b1667ca3754fe66dd1ec514698
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / CoordinatedGraphics / LayerTreeHost.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-2019 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 "LayerTreeHost.h"
31
32 #if USE(COORDINATED_GRAPHICS)
33
34 #include "DrawingArea.h"
35 #include "WebPage.h"
36 #include "WebPageProxyMessages.h"
37 #include <WebCore/Frame.h>
38 #include <WebCore/FrameView.h>
39 #include <WebCore/PageOverlayController.h>
40
41 #if USE(GLIB_EVENT_LOOP)
42 #include <wtf/glib/RunLoopSourcePriority.h>
43 #endif
44
45 namespace WebKit {
46 using namespace WebCore;
47
48 static const PlatformDisplayID primaryDisplayID = 0;
49 #if PLATFORM(GTK)
50 static const PlatformDisplayID compositingDisplayID = 1;
51 #else
52 static const PlatformDisplayID compositingDisplayID = primaryDisplayID;
53 #endif
54
55 LayerTreeHost::LayerTreeHost(WebPage& webPage)
56     : m_webPage(webPage)
57     , m_coordinator(webPage.corePage(), *this)
58     , m_compositorClient(*this)
59     , m_surface(AcceleratedSurface::create(webPage, *this))
60     , m_viewportController(webPage.size())
61     , m_layerFlushTimer(RunLoop::main(), this, &LayerTreeHost::layerFlushTimerFired)
62 {
63 #if USE(GLIB_EVENT_LOOP)
64     m_layerFlushTimer.setPriority(RunLoopSourcePriority::LayerFlushTimer);
65     m_layerFlushTimer.setName("[WebKit] LayerTreeHost");
66 #endif
67     m_coordinator.createRootLayer(m_webPage.size());
68     scheduleLayerFlush();
69
70     if (FrameView* frameView = m_webPage.mainFrameView()) {
71         auto contentsSize = frameView->contentsSize();
72         if (!contentsSize.isEmpty())
73             m_viewportController.didChangeContentsSize(contentsSize);
74     }
75
76     IntSize scaledSize(m_webPage.size());
77     scaledSize.scale(m_webPage.deviceScaleFactor());
78     float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
79
80     if (m_surface) {
81         TextureMapper::PaintFlags paintFlags = 0;
82
83         if (m_surface->shouldPaintMirrored())
84             paintFlags |= TextureMapper::PaintingMirrored;
85
86         m_compositor = ThreadedCompositor::create(m_compositorClient, m_compositorClient, compositingDisplayID, scaledSize, scaleFactor, ThreadedCompositor::ShouldDoFrameSync::Yes, paintFlags);
87         m_layerTreeContext.contextID = m_surface->surfaceID();
88     } else
89         m_compositor = ThreadedCompositor::create(m_compositorClient, m_compositorClient, compositingDisplayID, scaledSize, scaleFactor);
90
91     m_webPage.windowScreenDidChange(compositingDisplayID);
92
93     didChangeViewport();
94 }
95
96 LayerTreeHost::~LayerTreeHost()
97 {
98     ASSERT(!m_isValid);
99 }
100
101 void LayerTreeHost::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
102 {
103     if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
104         return;
105
106     m_layerFlushSchedulingEnabled = layerFlushingEnabled;
107
108     if (m_layerFlushSchedulingEnabled) {
109         scheduleLayerFlush();
110         return;
111     }
112
113     cancelPendingLayerFlush();
114 }
115
116 void LayerTreeHost::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
117 {
118     m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
119 }
120
121 void LayerTreeHost::scheduleLayerFlush()
122 {
123     if (!m_layerFlushSchedulingEnabled)
124         return;
125
126     if (m_isWaitingForRenderer) {
127         m_scheduledWhileWaitingForRenderer = true;
128         return;
129     }
130
131     if (!m_layerFlushTimer.isActive())
132         m_layerFlushTimer.startOneShot(0_s);
133 }
134
135 void LayerTreeHost::cancelPendingLayerFlush()
136 {
137     m_layerFlushTimer.stop();
138 }
139
140 void LayerTreeHost::layerFlushTimerFired()
141 {
142     if (m_isSuspended || m_isWaitingForRenderer)
143         return;
144
145     m_coordinator.syncDisplayState();
146     m_webPage.flushPendingEditorStateUpdate();
147     m_webPage.willDisplayPage();
148
149     if (!m_isValid || !m_coordinator.rootCompositingLayer())
150         return;
151
152     // If a force-repaint callback was registered, we should force a 'frame sync' that
153     // will guarantee us a call to renderNextFrame() once the update is complete.
154     if (m_forceRepaintAsync.callbackID)
155         m_coordinator.forceFrameSync();
156
157     bool didSync = m_coordinator.flushPendingLayerChanges();
158
159     if (m_notifyAfterScheduledLayerFlush && didSync) {
160         m_webPage.drawingArea()->layerHostDidFlushLayers();
161         m_notifyAfterScheduledLayerFlush = false;
162     }
163 }
164
165 void LayerTreeHost::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
166 {
167     m_coordinator.setRootCompositingLayer(graphicsLayer);
168 }
169
170 void LayerTreeHost::setViewOverlayRootLayer(GraphicsLayer* viewOverlayRootLayer)
171 {
172     m_viewOverlayRootLayer = viewOverlayRootLayer;
173     m_coordinator.setViewOverlayRootLayer(viewOverlayRootLayer);
174 }
175
176 void LayerTreeHost::invalidate()
177 {
178     ASSERT(m_isValid);
179     m_isValid = false;
180
181     cancelPendingLayerFlush();
182
183     m_coordinator.invalidate();
184     m_compositor->invalidate();
185     m_surface = nullptr;
186 }
187
188 void LayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
189 {
190     auto* frameView = m_webPage.mainFrameView();
191     if (!frameView || !frameView->delegatesScrolling())
192         return;
193
194     m_viewportController.didScroll(rect.location());
195     if (m_isDiscardable)
196         m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
197     else
198         didChangeViewport();
199 }
200
201 void LayerTreeHost::forceRepaint()
202 {
203     // This is necessary for running layout tests. Since in this case we are not waiting for a UIProcess to reply nicely.
204     // Instead we are just triggering forceRepaint. But we still want to have the scripted animation callbacks being executed.
205     m_coordinator.syncDisplayState();
206
207     // We need to schedule another flush, otherwise the forced paint might cancel a later expected flush.
208     scheduleLayerFlush();
209
210     if (!m_isWaitingForRenderer)
211         m_coordinator.flushPendingLayerChanges();
212     m_compositor->forceRepaint();
213 }
214
215 bool LayerTreeHost::forceRepaintAsync(CallbackID callbackID)
216 {
217     scheduleLayerFlush();
218
219     // We want a clean repaint, meaning that if we're currently waiting for the renderer
220     // to finish an update, we'll have to schedule another flush when it's done.
221     ASSERT(!m_forceRepaintAsync.callbackID);
222     m_forceRepaintAsync.callbackID = OptionalCallbackID(callbackID);
223     m_forceRepaintAsync.needsFreshFlush = m_scheduledWhileWaitingForRenderer;
224     return true;
225 }
226
227 void LayerTreeHost::sizeDidChange(const IntSize& size)
228 {
229     if (m_isDiscardable) {
230         m_discardableSyncActions.add(DiscardableSyncActions::UpdateSize);
231         m_viewportController.didChangeViewportSize(size);
232         return;
233     }
234
235     if (m_surface && m_surface->hostResize(size))
236         m_layerTreeContext.contextID = m_surface->surfaceID();
237
238     m_coordinator.sizeDidChange(size);
239     scheduleLayerFlush();
240
241     m_viewportController.didChangeViewportSize(size);
242     IntSize scaledSize(size);
243     scaledSize.scale(m_webPage.deviceScaleFactor());
244     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
245     didChangeViewport();
246 }
247
248 void LayerTreeHost::pauseRendering()
249 {
250     m_isSuspended = true;
251 }
252
253 void LayerTreeHost::resumeRendering()
254 {
255     m_isSuspended = false;
256     scheduleLayerFlush();
257 }
258
259 GraphicsLayerFactory* LayerTreeHost::graphicsLayerFactory()
260 {
261     return &m_coordinator;
262 }
263
264 void LayerTreeHost::contentsSizeChanged(const IntSize& newSize)
265 {
266     m_viewportController.didChangeContentsSize(newSize);
267     if (m_isDiscardable)
268         m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
269     else
270         didChangeViewport();
271 }
272
273 void LayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
274 {
275     m_viewportController.didChangeViewportAttributes(WTFMove(attr));
276     if (m_isDiscardable)
277         m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
278     else
279         didChangeViewport();
280 }
281
282 void LayerTreeHost::didChangeViewport()
283 {
284     FloatRect visibleRect(m_viewportController.visibleContentsRect());
285     if (visibleRect.isEmpty())
286         return;
287
288     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
289     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
290     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
291     FrameView* view = m_webPage.corePage()->mainFrame().view();
292     Scrollbar* scrollbar = view->verticalScrollbar();
293     if (scrollbar && !scrollbar->isOverlayScrollbar())
294         visibleRect.expand(scrollbar->width(), 0);
295     scrollbar = view->horizontalScrollbar();
296     if (scrollbar && !scrollbar->isOverlayScrollbar())
297         visibleRect.expand(0, scrollbar->height());
298
299     m_coordinator.setVisibleContentsRect(visibleRect);
300     scheduleLayerFlush();
301
302     float pageScale = m_viewportController.pageScaleFactor();
303     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
304     if (m_lastScrollPosition != scrollPosition) {
305         m_lastScrollPosition = scrollPosition;
306         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
307
308         if (!view->useFixedLayout())
309             view->notifyScrollPositionChanged(m_lastScrollPosition);
310     }
311
312     if (m_lastPageScaleFactor != pageScale) {
313         m_lastPageScaleFactor = pageScale;
314         m_webPage.scalePage(pageScale, m_lastScrollPosition);
315     }
316 }
317
318 void LayerTreeHost::setIsDiscardable(bool discardable)
319 {
320     m_isDiscardable = discardable;
321     if (m_isDiscardable) {
322         m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
323         m_webPage.windowScreenDidChange(primaryDisplayID);
324         return;
325     }
326     m_webPage.windowScreenDidChange(compositingDisplayID);
327
328     if (m_discardableSyncActions.isEmpty())
329         return;
330
331     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
332         // Size changes already sets the scale factor and updates the viewport.
333         sizeDidChange(m_webPage.size());
334         return;
335     }
336
337     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
338         deviceOrPageScaleFactorChanged();
339
340     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
341         didChangeViewport();
342 }
343
344 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
345 void LayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
346 {
347     m_layerTreeContext.contextID = handle;
348     m_compositor->setNativeSurfaceHandleForCompositing(handle);
349     scheduleLayerFlush();
350 }
351 #endif
352
353 void LayerTreeHost::deviceOrPageScaleFactorChanged()
354 {
355     if (m_isDiscardable) {
356         m_discardableSyncActions.add(DiscardableSyncActions::UpdateScale);
357         return;
358     }
359
360     if (m_surface && m_surface->hostResize(m_webPage.size()))
361         m_layerTreeContext.contextID = m_surface->surfaceID();
362
363     m_coordinator.deviceOrPageScaleFactorChanged();
364     m_webPage.corePage()->pageOverlayController().didChangeDeviceScaleFactor();
365     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
366 }
367
368 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
369 RefPtr<DisplayRefreshMonitor> LayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
370 {
371     return m_compositor->displayRefreshMonitor(displayID);
372 }
373 #endif
374
375 void LayerTreeHost::didFlushRootLayer(const FloatRect& visibleContentRect)
376 {
377     // Because our view-relative overlay root layer is not attached to the FrameView's GraphicsLayer tree, we need to flush it manually.
378     if (m_viewOverlayRootLayer)
379         m_viewOverlayRootLayer->flushCompositingState(visibleContentRect);
380 }
381
382 void LayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
383 {
384     m_isWaitingForRenderer = true;
385     m_compositor->updateSceneState(state);
386 }
387
388 void LayerTreeHost::frameComplete()
389 {
390     m_compositor->frameComplete();
391 }
392
393 uint64_t LayerTreeHost::nativeSurfaceHandleForCompositing()
394 {
395     if (!m_surface)
396         return m_layerTreeContext.contextID;
397
398     m_surface->initialize();
399     return m_surface->window();
400 }
401
402 void LayerTreeHost::didDestroyGLContext()
403 {
404     if (m_surface)
405         m_surface->finalize();
406 }
407
408 void LayerTreeHost::willRenderFrame()
409 {
410     if (m_surface)
411         m_surface->willRenderFrame();
412 }
413
414 void LayerTreeHost::didRenderFrame()
415 {
416     if (m_surface)
417         m_surface->didRenderFrame();
418 }
419
420 void LayerTreeHost::requestDisplayRefreshMonitorUpdate()
421 {
422     // Flush layers to cause a repaint. If m_isWaitingForRenderer was true at this point, the layer
423     // flush won't do anything, but that means there's a painting ongoing that will send the
424     // display refresh notification when it's done.
425     m_coordinator.forceFrameSync();
426     scheduleLayerFlush();
427 }
428
429 void LayerTreeHost::handleDisplayRefreshMonitorUpdate(bool hasBeenRescheduled)
430 {
431     // Call renderNextFrame. If hasBeenRescheduled is true, the layer flush will force a repaint
432     // that will cause the display refresh notification to come.
433     renderNextFrame(hasBeenRescheduled);
434     m_compositor->handleDisplayRefreshMonitorUpdate();
435 }
436
437 void LayerTreeHost::renderNextFrame(bool forceRepaint)
438 {
439     m_isWaitingForRenderer = false;
440     bool scheduledWhileWaitingForRenderer = std::exchange(m_scheduledWhileWaitingForRenderer, false);
441     m_coordinator.renderNextFrame();
442
443     if (m_forceRepaintAsync.callbackID) {
444         // If the asynchronous force-repaint needs a separate fresh flush, it was due to
445         // the force-repaint request being registered while CoordinatedLayerTreeHost was
446         // waiting for the renderer to finish an update.
447         ASSERT(!m_forceRepaintAsync.needsFreshFlush || scheduledWhileWaitingForRenderer);
448
449         // Execute the callback if another layer flush and the subsequent state update
450         // aren't needed. If they are, the callback will be executed when this function
451         // is called after the next update.
452         if (!m_forceRepaintAsync.needsFreshFlush) {
453             m_webPage.send(Messages::WebPageProxy::VoidCallback(m_forceRepaintAsync.callbackID.callbackID()));
454             m_forceRepaintAsync.callbackID = OptionalCallbackID();
455         }
456         m_forceRepaintAsync.needsFreshFlush = false;
457     }
458
459     if (scheduledWhileWaitingForRenderer || m_layerFlushTimer.isActive() || forceRepaint) {
460         m_layerFlushTimer.stop();
461         if (forceRepaint)
462             m_coordinator.forceFrameSync();
463         layerFlushTimerFired();
464     }
465 }
466
467 } // namespace WebKit
468
469 #endif // USE(COORDINATED_GRAPHICS)