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