1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (C) 2012 Company 100, Inc.
* Copyright (C) 2014-2019 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "LayerTreeHost.h"
#if USE(COORDINATED_GRAPHICS)
#include "DrawingArea.h"
#include "WebPage.h"
#include "WebPageProxyMessages.h"
#include "WebProcess.h"
#include <WebCore/AsyncScrollingCoordinator.h>
#include <WebCore/Chrome.h>
#include <WebCore/LocalFrame.h>
#include <WebCore/LocalFrameView.h>
#include <WebCore/PageOverlayController.h>
#include <WebCore/RenderLayerBacking.h>
#include <WebCore/RenderView.h>
#include <WebCore/ThreadedScrollingTree.h>
#if USE(GLIB_EVENT_LOOP)
#include <wtf/glib/RunLoopSourcePriority.h>
#endif
namespace WebKit {
using namespace WebCore;
LayerTreeHost::LayerTreeHost(WebPage& webPage, WebCore::PlatformDisplayID displayID)
: m_webPage(webPage)
, m_surface(AcceleratedSurface::create(webPage, *this))
, m_viewportController(webPage.size())
, m_layerFlushTimer(RunLoop::main(), this, &LayerTreeHost::layerFlushTimerFired)
, m_coordinator(webPage, *this)
, m_displayID(displayID)
{
#if USE(GLIB_EVENT_LOOP)
m_layerFlushTimer.setPriority(RunLoopSourcePriority::LayerFlushTimer);
m_layerFlushTimer.setName("[WebKit] LayerTreeHost");
#endif
scheduleLayerFlush();
if (auto* frameView = m_webPage.localMainFrameView()) {
auto contentsSize = frameView->contentsSize();
if (!contentsSize.isEmpty())
m_viewportController.didChangeContentsSize(contentsSize);
}
IntSize scaledSize(m_webPage.size());
scaledSize.scale(m_webPage.deviceScaleFactor());
float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
TextureMapper::PaintFlags paintFlags = 0;
if (m_surface->shouldPaintMirrored())
paintFlags |= TextureMapper::PaintingMirrored;
m_compositor = ThreadedCompositor::create(*this, *this, m_displayID, scaledSize, scaleFactor, paintFlags);
m_layerTreeContext.contextID = m_surface->surfaceID();
m_surface->didCreateCompositingRunLoop(m_compositor->compositingRunLoop());
didChangeViewport();
}
LayerTreeHost::~LayerTreeHost()
{
cancelPendingLayerFlush();
m_surface->willDestroyCompositingRunLoop();
m_coordinator.invalidate();
m_compositor->invalidate();
m_surface = nullptr;
}
void LayerTreeHost::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
{
if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
return;
m_layerFlushSchedulingEnabled = layerFlushingEnabled;
if (m_layerFlushSchedulingEnabled) {
m_compositor->resume();
scheduleLayerFlush();
return;
}
cancelPendingLayerFlush();
m_compositor->suspend();
}
void LayerTreeHost::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
{
m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
}
void LayerTreeHost::scheduleLayerFlush()
{
if (!m_layerFlushSchedulingEnabled)
return;
if (m_isWaitingForRenderer) {
m_scheduledWhileWaitingForRenderer = true;
return;
}
if (!m_layerFlushTimer.isActive())
m_layerFlushTimer.startOneShot(0_s);
}
void LayerTreeHost::cancelPendingLayerFlush()
{
m_layerFlushTimer.stop();
}
void LayerTreeHost::layerFlushTimerFired()
{
if (m_isSuspended || m_isWaitingForRenderer)
return;
if (!m_coordinator.rootCompositingLayer())
return;
// If a force-repaint callback was registered, we should force a 'frame sync' that
// will guarantee us a call to renderNextFrame() once the update is complete.
if (m_forceRepaintAsync.callback)
m_coordinator.forceFrameSync();
OptionSet<FinalizeRenderingUpdateFlags> flags;
#if PLATFORM(GTK)
if (!m_transientZoom)
flags.add(FinalizeRenderingUpdateFlags::ApplyScrollingTreeLayerPositions);
#else
flags.add(FinalizeRenderingUpdateFlags::ApplyScrollingTreeLayerPositions);
#endif
bool didSync = m_coordinator.flushPendingLayerChanges(flags);
#if PLATFORM(GTK)
// If we have an active transient zoom, we want the zoom to win over any changes
// that WebCore makes to the relevant layers, so re-apply our changes after flushing.
if (m_transientZoom)
applyTransientZoomToLayers(m_transientZoomScale, m_transientZoomOrigin);
#endif
if (m_notifyAfterScheduledLayerFlush && didSync) {
m_webPage.drawingArea()->layerHostDidFlushLayers();
m_notifyAfterScheduledLayerFlush = false;
}
}
void LayerTreeHost::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
{
m_coordinator.setRootCompositingLayer(graphicsLayer);
}
void LayerTreeHost::setViewOverlayRootLayer(GraphicsLayer* viewOverlayRootLayer)
{
m_viewOverlayRootLayer = viewOverlayRootLayer;
m_coordinator.setViewOverlayRootLayer(viewOverlayRootLayer);
}
void LayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
{
auto* frameView = m_webPage.localMainFrameView();
if (!frameView || !frameView->delegatesScrolling())
return;
m_viewportController.didScroll(rect.location());
if (m_isDiscardable)
m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
else
didChangeViewport();
}
void LayerTreeHost::forceRepaint()
{
// This is necessary for running layout tests. Since in this case we are not waiting for a UIProcess to reply nicely.
// Instead we are just triggering forceRepaint. But we still want to have the scripted animation callbacks being executed.
m_coordinator.syncDisplayState();
// We need to schedule another flush, otherwise the forced paint might cancel a later expected flush.
scheduleLayerFlush();
if (!m_isWaitingForRenderer) {
OptionSet<FinalizeRenderingUpdateFlags> flags;
#if PLATFORM(GTK)
if (!m_transientZoom)
flags.add(FinalizeRenderingUpdateFlags::ApplyScrollingTreeLayerPositions);
#else
flags.add(FinalizeRenderingUpdateFlags::ApplyScrollingTreeLayerPositions);
#endif
m_coordinator.flushPendingLayerChanges(flags);
}
m_compositor->forceRepaint();
}
void LayerTreeHost::forceRepaintAsync(CompletionHandler<void()>&& callback)
{
scheduleLayerFlush();
// We want a clean repaint, meaning that if we're currently waiting for the renderer
// to finish an update, we'll have to schedule another flush when it's done.
ASSERT(!m_forceRepaintAsync.callback);
m_forceRepaintAsync.callback = WTFMove(callback);
m_forceRepaintAsync.needsFreshFlush = m_scheduledWhileWaitingForRenderer;
}
void LayerTreeHost::sizeDidChange(const IntSize& size)
{
if (m_isDiscardable) {
m_discardableSyncActions.add(DiscardableSyncActions::UpdateSize);
m_viewportController.didChangeViewportSize(size);
return;
}
if (m_surface->hostResize(size))
m_layerTreeContext.contextID = m_surface->surfaceID();
m_coordinator.sizeDidChange(size);
scheduleLayerFlush();
m_viewportController.didChangeViewportSize(size);
IntSize scaledSize(size);
scaledSize.scale(m_webPage.deviceScaleFactor());
m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
didChangeViewport();
}
void LayerTreeHost::targetRefreshRateDidChange(uint32_t rate)
{
m_compositor->targetRefreshRateDidChange(rate);
}
void LayerTreeHost::pauseRendering()
{
m_isSuspended = true;
m_compositor->suspend();
}
void LayerTreeHost::resumeRendering()
{
m_isSuspended = false;
m_compositor->resume();
scheduleLayerFlush();
}
GraphicsLayerFactory* LayerTreeHost::graphicsLayerFactory()
{
return &m_coordinator;
}
void LayerTreeHost::contentsSizeChanged(const IntSize& newSize)
{
m_viewportController.didChangeContentsSize(newSize);
if (m_isDiscardable)
m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
else
didChangeViewport();
}
void LayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
{
m_viewportController.didChangeViewportAttributes(WTFMove(attr));
if (m_isDiscardable)
m_discardableSyncActions.add(DiscardableSyncActions::UpdateViewport);
else
didChangeViewport();
}
void LayerTreeHost::didChangeViewport()
{
FloatRect visibleRect(m_viewportController.visibleContentsRect());
if (visibleRect.isEmpty())
return;
// When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
// in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
// See https://bugs.webkit.org/show_bug.cgi?id=160450.
auto* localMainFrame = dynamicDowncast<WebCore::LocalFrame>(m_webPage.corePage()->mainFrame());
auto* view = localMainFrame ? localMainFrame->view() : nullptr;
if (!view)
return;
auto* scrollbar = view->verticalScrollbar();
if (scrollbar && !scrollbar->isOverlayScrollbar())
visibleRect.expand(scrollbar->width(), 0);
scrollbar = view->horizontalScrollbar();
if (scrollbar && !scrollbar->isOverlayScrollbar())
visibleRect.expand(0, scrollbar->height());
m_coordinator.setVisibleContentsRect(visibleRect);
scheduleLayerFlush();
float pageScale = m_viewportController.pageScaleFactor();
IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
if (m_lastScrollPosition != scrollPosition) {
m_lastScrollPosition = scrollPosition;
m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
if (!view->useFixedLayout())
view->notifyScrollPositionChanged(m_lastScrollPosition);
}
if (m_lastPageScaleFactor != pageScale) {
m_lastPageScaleFactor = pageScale;
m_webPage.scalePage(pageScale, m_lastScrollPosition);
}
}
void LayerTreeHost::setIsDiscardable(bool discardable)
{
m_isDiscardable = discardable;
if (m_isDiscardable) {
m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
return;
}
if (m_discardableSyncActions.isEmpty())
return;
if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
// Size changes already sets the scale factor and updates the viewport.
sizeDidChange(m_webPage.size());
return;
}
if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
deviceOrPageScaleFactorChanged();
if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
didChangeViewport();
}
void LayerTreeHost::deviceOrPageScaleFactorChanged()
{
if (m_isDiscardable) {
m_discardableSyncActions.add(DiscardableSyncActions::UpdateScale);
return;
}
if (m_surface->hostResize(m_webPage.size()))
m_layerTreeContext.contextID = m_surface->surfaceID();
m_coordinator.deviceOrPageScaleFactorChanged();
m_webPage.corePage()->pageOverlayController().didChangeDeviceScaleFactor();
IntSize scaledSize(m_webPage.size());
scaledSize.scale(m_webPage.deviceScaleFactor());
m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
didChangeViewport();
}
RefPtr<DisplayRefreshMonitor> LayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
{
ASSERT(m_displayID == displayID);
return Ref { m_compositor->displayRefreshMonitor() };
}
void LayerTreeHost::didFlushRootLayer(const FloatRect& visibleContentRect)
{
// Because our view-relative overlay root layer is not attached to the FrameView's GraphicsLayer tree, we need to flush it manually.
if (m_viewOverlayRootLayer)
m_viewOverlayRootLayer->flushCompositingState(visibleContentRect);
}
void LayerTreeHost::commitSceneState(const RefPtr<Nicosia::Scene>& state)
{
m_isWaitingForRenderer = true;
m_compositor->updateSceneState(state);
}
void LayerTreeHost::updateScene()
{
m_compositor->updateScene();
}
void LayerTreeHost::frameComplete()
{
m_compositor->frameComplete();
}
uint64_t LayerTreeHost::nativeSurfaceHandleForCompositing()
{
m_surface->initialize();
return m_surface->window();
}
void LayerTreeHost::didCreateGLContext()
{
m_surface->didCreateGLContext();
}
void LayerTreeHost::willDestroyGLContext()
{
m_surface->willDestroyGLContext();
}
void LayerTreeHost::didDestroyGLContext()
{
m_surface->finalize();
}
void LayerTreeHost::resize(const IntSize& size)
{
m_surface->clientResize(size);
}
void LayerTreeHost::willRenderFrame()
{
m_surface->willRenderFrame();
}
void LayerTreeHost::didRenderFrame()
{
m_surface->didRenderFrame();
}
void LayerTreeHost::displayDidRefresh(PlatformDisplayID displayID)
{
WebProcess::singleton().eventDispatcher().notifyScrollingTreesDisplayDidRefresh(displayID);
}
void LayerTreeHost::requestDisplayRefreshMonitorUpdate()
{
// Flush layers to cause a repaint. If m_isWaitingForRenderer was true at this point, the layer
// flush won't do anything, but that means there's a painting ongoing that will send the
// display refresh notification when it's done.
m_coordinator.forceFrameSync();
scheduleLayerFlush();
}
void LayerTreeHost::handleDisplayRefreshMonitorUpdate(bool hasBeenRescheduled)
{
// Call renderNextFrame. If hasBeenRescheduled is true, the layer flush will force a repaint
// that will cause the display refresh notification to come.
renderNextFrame(hasBeenRescheduled);
}
void LayerTreeHost::renderNextFrame(bool forceRepaint)
{
m_isWaitingForRenderer = false;
bool scheduledWhileWaitingForRenderer = std::exchange(m_scheduledWhileWaitingForRenderer, false);
m_coordinator.renderNextFrame();
if (m_forceRepaintAsync.callback) {
// If the asynchronous force-repaint needs a separate fresh flush, it was due to
// the force-repaint request being registered while CoordinatedLayerTreeHost was
// waiting for the renderer to finish an update.
ASSERT(!m_forceRepaintAsync.needsFreshFlush || scheduledWhileWaitingForRenderer);
// Execute the callback if another layer flush and the subsequent state update
// aren't needed. If they are, the callback will be executed when this function
// is called after the next update.
if (!m_forceRepaintAsync.needsFreshFlush)
m_forceRepaintAsync.callback();
m_forceRepaintAsync.needsFreshFlush = false;
}
if (scheduledWhileWaitingForRenderer || m_layerFlushTimer.isActive() || forceRepaint) {
m_layerFlushTimer.stop();
if (forceRepaint)
m_coordinator.forceFrameSync();
layerFlushTimerFired();
}
}
#if PLATFORM(GTK)
FloatPoint LayerTreeHost::constrainTransientZoomOrigin(double scale, FloatPoint origin) const
{
auto* frameView = m_webPage.localMainFrameView();
if (!frameView)
return origin;
FloatRect visibleContentRect = frameView->visibleContentRectIncludingScrollbars();
FloatPoint constrainedOrigin = visibleContentRect.location();
constrainedOrigin.moveBy(-origin);
IntSize scaledTotalContentsSize = frameView->totalContentsSize();
scaledTotalContentsSize.scale(scale * m_webPage.viewScaleFactor() / m_webPage.totalScaleFactor());
// Scaling may have exposed the overhang area, so we need to constrain the final
// layer position exactly like scrolling will once it's committed, to ensure that
// scrolling doesn't make the view jump.
constrainedOrigin = ScrollableArea::constrainScrollPositionForOverhang(roundedIntRect(visibleContentRect),
scaledTotalContentsSize, roundedIntPoint(constrainedOrigin), frameView->scrollOrigin(),
frameView->headerHeight(), frameView->footerHeight());
constrainedOrigin.moveBy(-visibleContentRect.location());
constrainedOrigin = -constrainedOrigin;
return constrainedOrigin;
}
CoordinatedGraphicsLayer* LayerTreeHost::layerForTransientZoom() const
{
auto* frameView = m_webPage.localMainFrameView();
if (!frameView)
return nullptr;
RenderLayerBacking* renderViewBacking = frameView->renderView()->layer()->backing();
if (GraphicsLayer* contentsContainmentLayer = renderViewBacking->contentsContainmentLayer())
return &downcast<CoordinatedGraphicsLayer>(*contentsContainmentLayer);
return &downcast<CoordinatedGraphicsLayer>(*renderViewBacking->graphicsLayer());
}
void LayerTreeHost::applyTransientZoomToLayers(double scale, FloatPoint origin)
{
// FIXME: Scrollbars should stay in-place and change height while zooming.
FloatPoint constrainedOrigin = constrainTransientZoomOrigin(scale, origin);
auto* zoomLayer = layerForTransientZoom();
TransformationMatrix transform;
transform.translate(constrainedOrigin.x(), constrainedOrigin.y());
transform.scale(scale);
zoomLayer->setTransform(transform);
zoomLayer->setAnchorPoint(FloatPoint3D());
zoomLayer->setPosition(FloatPoint());
}
void LayerTreeHost::adjustTransientZoom(double scale, FloatPoint origin)
{
m_transientZoom = true;
m_transientZoomScale = scale;
m_transientZoomOrigin = origin;
applyTransientZoomToLayers(m_transientZoomScale, m_transientZoomOrigin);
}
void LayerTreeHost::commitTransientZoom(double scale, FloatPoint origin)
{
if (m_transientZoomScale == scale) {
// If the page scale is already the target scale, setPageScaleFactor() will short-circuit
// and not apply the transform, so we can't depend on it to do so.
TransformationMatrix finalTransform;
finalTransform.scale(scale);
layerForTransientZoom()->setTransform(finalTransform);
}
m_transientZoom = false;
m_transientZoomScale = 1;
m_transientZoomOrigin = FloatPoint();
}
#endif
} // namespace WebKit
#endif // USE(COORDINATED_GRAPHICS)
|