[GTK] Use the DisplayRefreshMonitor facilities
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / CoordinatedGraphics / ThreadedCoordinatedLayerTreeHost.cpp
index 38bfbc3..60e9167 100644 (file)
 
 #if USE(COORDINATED_GRAPHICS_THREADED)
 
-#include "DrawingAreaImpl.h"
-#include "NotImplemented.h"
-#include "ThreadSafeCoordinatedSurface.h"
+#include "AcceleratedSurface.h"
 #include "WebPage.h"
-#include <WebCore/CoordinatedGraphicsLayer.h>
-#include <WebCore/CoordinatedGraphicsState.h>
-#include <WebCore/Frame.h>
 #include <WebCore/FrameView.h>
-#include <WebCore/GraphicsContext.h>
 #include <WebCore/MainFrame.h>
-#include <WebCore/Page.h>
-#include <wtf/CurrentTime.h>
 
 using namespace WebCore;
 
@@ -58,142 +50,127 @@ ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
 }
 
 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
-    : LayerTreeHost(webPage)
-    , m_forceRepaintAsyncCallbackID(0)
-    , m_notifyAfterScheduledLayerFlush(false)
-    , m_isSuspended(false)
-    , m_isWaitingForRenderer(false)
-    , m_layerFlushTimer(RunLoop::main(), *this, &ThreadedCoordinatedLayerTreeHost::performScheduledLayerFlush)
-    , m_layerFlushSchedulingEnabled(true)
-{
-    m_coordinator = std::make_unique<CompositingCoordinator>(m_webPage.corePage(), this);
-
-    m_coordinator->createRootLayer(m_webPage.size());
-
-    CoordinatedSurface::setFactory(createCoordinatedSurface);
-
-    m_compositor = ThreadedCompositor::create(this);
-    scheduleLayerFlush();
-}
-
-RefPtr<CoordinatedSurface> ThreadedCoordinatedLayerTreeHost::createCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags)
-{
-    return ThreadSafeCoordinatedSurface::create(size, flags);
-}
-
-void ThreadedCoordinatedLayerTreeHost::scheduleLayerFlush()
-{
-    if (!m_layerFlushSchedulingEnabled)
-        return;
-
-    if (!m_layerFlushTimer.isActive())
-        m_layerFlushTimer.startOneShot(0);
-}
-
-void ThreadedCoordinatedLayerTreeHost::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
-{
-    if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
-        return;
+    : CoordinatedLayerTreeHost(webPage)
+    , m_compositorClient(*this)
+    , m_surface(AcceleratedSurface::create(webPage))
+    , m_viewportController(webPage.size())
+{
+    if (FrameView* frameView = m_webPage.mainFrameView()) {
+        auto contentsSize = frameView->contentsSize();
+        if (!contentsSize.isEmpty())
+            m_viewportController.didChangeContentsSize(contentsSize);
+    }
 
-    m_layerFlushSchedulingEnabled = layerFlushingEnabled;
+    IntSize scaledSize(m_webPage.size());
+    scaledSize.scale(m_webPage.deviceScaleFactor());
+    float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
 
-    if (m_layerFlushSchedulingEnabled) {
-        scheduleLayerFlush();
-        return;
-    }
+    if (m_surface) {
+        TextureMapper::PaintFlags paintFlags = 0;
 
-    cancelPendingLayerFlush();
-}
+        if (m_surface->shouldPaintMirrored())
+            paintFlags |= TextureMapper::PaintingMirrored;
 
-void ThreadedCoordinatedLayerTreeHost::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
-{
-    m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
-}
+        // Do not do frame sync when rendering offscreen in the web process to ensure that SwapBuffers never blocks.
+        // Rendering to the actual screen will happen later anyway since the UI process schedules a redraw for every update,
+        // the compositor will take care of syncing to vblank.
+        m_compositor = ThreadedCompositor::create(m_compositorClient, scaledSize, scaleFactor, m_surface->window(), ThreadedCompositor::ShouldDoFrameSync::Yes, paintFlags);
+        m_layerTreeContext.contextID = m_surface->surfaceID();
+    } else
+        m_compositor = ThreadedCompositor::create(m_compositorClient, scaledSize, scaleFactor);
 
-void ThreadedCoordinatedLayerTreeHost::setRootCompositingLayer(WebCore::GraphicsLayer* graphicsLayer)
-{
-    m_coordinator->setRootCompositingLayer(graphicsLayer);
+    didChangeViewport();
 }
 
 void ThreadedCoordinatedLayerTreeHost::invalidate()
 {
-    notImplemented();
-}
-
-void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const WebCore::IntRect& rect)
-{
-    m_compositor->scrollTo(rect.location());
-    scheduleLayerFlush();
+    m_compositor->invalidate();
+    CoordinatedLayerTreeHost::invalidate();
+    m_surface = nullptr;
 }
 
 void ThreadedCoordinatedLayerTreeHost::forceRepaint()
 {
-    notImplemented();
+    CoordinatedLayerTreeHost::forceRepaint();
+    m_compositor->forceRepaint();
 }
 
-bool ThreadedCoordinatedLayerTreeHost::forceRepaintAsync(uint64_t callbackID)
+void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
 {
-    // We expect the UI process to not require a new repaint until the previous one has finished.
-    ASSERT(!m_forceRepaintAsyncCallbackID);
-    m_forceRepaintAsyncCallbackID = callbackID;
-    scheduleLayerFlush();
-    return true;
+    FrameView* frameView = m_webPage.mainFrameView();
+    if (!frameView || !frameView->delegatesScrolling())
+        return;
+
+    m_viewportController.didScroll(rect.location());
+    if (m_isDiscardable)
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
+    else
+        didChangeViewport();
 }
 
-void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const WebCore::IntSize& newSize)
+void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
 {
-    m_compositor->didChangeContentsSize(newSize);
+    m_viewportController.didChangeContentsSize(newSize);
+    if (m_isDiscardable)
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
+    else
+        didChangeViewport();
 }
 
 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
 {
-    m_coordinator->deviceOrPageScaleFactorChanged();
-    m_compositor->setDeviceScaleFactor(m_webPage.deviceScaleFactor());
-}
+    if (m_isDiscardable) {
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateScale;
+        return;
+    }
 
-void ThreadedCoordinatedLayerTreeHost::pauseRendering()
-{
-    m_isSuspended = true;
-}
+    if (m_surface && m_surface->resize(m_webPage.size()))
+        m_layerTreeContext.contextID = m_surface->surfaceID();
 
-void ThreadedCoordinatedLayerTreeHost::resumeRendering()
-{
-    m_isSuspended = false;
-    scheduleLayerFlush();
+    CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
+    m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
 }
 
-GraphicsLayerFactory* ThreadedCoordinatedLayerTreeHost::graphicsLayerFactory()
+void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
 {
-    return m_coordinator.get();
-}
+    if (m_isDiscardable) {
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateBackground;
+        return;
+    }
 
-void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const WebCore::IntSize& size)
-{
-    m_coordinator->sizeDidChange(size);
-    m_compositor->didChangeViewportSize(size);
+    CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
+    m_compositor->setDrawsBackground(m_webPage.drawsBackground());
 }
 
-void ThreadedCoordinatedLayerTreeHost::didChangeViewportProperties(const WebCore::ViewportAttributes& attr)
+void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
 {
-    m_compositor->didChangeViewportAttribute(attr);
-}
+    if (m_isDiscardable) {
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateSize;
+        m_viewportController.didChangeViewportSize(size);
+        return;
+    }
 
-void ThreadedCoordinatedLayerTreeHost::compositorDidFlushLayers()
-{
-    static_cast<DrawingAreaImpl*>(m_webPage.drawingArea())->layerHostDidFlushLayers();
-}
+    if (m_surface && m_surface->resize(size))
+        m_layerTreeContext.contextID = m_surface->surfaceID();
 
-void ThreadedCoordinatedLayerTreeHost::didScaleFactorChanged(float scale, const IntPoint& origin)
-{
-    m_webPage.scalePage(scale, origin);
+    CoordinatedLayerTreeHost::sizeDidChange(size);
+    m_viewportController.didChangeViewportSize(size);
+    IntSize scaledSize(size);
+    scaledSize.scale(m_webPage.deviceScaleFactor());
+    m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
+    didChangeViewport();
 }
 
-void ThreadedCoordinatedLayerTreeHost::setViewOverlayRootLayer(GraphicsLayer* graphicsLayer)
+void ThreadedCoordinatedLayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
 {
-    m_coordinator->setViewOverlayRootLayer(graphicsLayer);
+    m_viewportController.didChangeViewportAttributes(WTFMove(attr));
+    if (m_isDiscardable)
+        m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
+    else
+        didChangeViewport();
 }
 
-#if PLATFORM(GTK)
+#if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
 {
     m_layerTreeContext.contextID = handle;
@@ -202,88 +179,80 @@ void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint
 }
 #endif
 
-#if ENABLE(REQUEST_ANIMATION_FRAME)
-void ThreadedCoordinatedLayerTreeHost::scheduleAnimation()
+void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
 {
-    if (m_isWaitingForRenderer)
-        return;
-
-    if (m_layerFlushTimer.isActive())
+    FloatRect visibleRect(m_viewportController.visibleContentsRect());
+    if (visibleRect.isEmpty())
         return;
 
-    m_layerFlushTimer.startOneShot(m_coordinator->nextAnimationServiceTime());
-    scheduleLayerFlush();
-}
-#endif
-
-void ThreadedCoordinatedLayerTreeHost::setVisibleContentsRect(const FloatRect& rect, const FloatPoint& trajectoryVector, float scale)
-{
-    m_coordinator->setVisibleContentsRect(rect, trajectoryVector);
-    if (m_lastScrollPosition != roundedIntPoint(rect.location())) {
-        m_lastScrollPosition = roundedIntPoint(rect.location());
-
-        if (!m_webPage.corePage()->mainFrame().view()->useFixedLayout())
-            m_webPage.corePage()->mainFrame().view()->notifyScrollPositionChanged(m_lastScrollPosition);
+    // 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.
+    FrameView* view = m_webPage.corePage()->mainFrame().view();
+    Scrollbar* scrollbar = view->verticalScrollbar();
+    if (scrollbar && !scrollbar->isOverlayScrollbar())
+        visibleRect.expand(scrollbar->width(), 0);
+    scrollbar = view->horizontalScrollbar();
+    if (scrollbar && !scrollbar->isOverlayScrollbar())
+        visibleRect.expand(0, scrollbar->height());
+
+    CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect, FloatPoint::zero());
+
+    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_lastScaleFactor != scale) {
-        m_lastScaleFactor = scale;
-        didScaleFactorChanged(m_lastScaleFactor, m_lastScrollPosition);
+    if (m_lastPageScaleFactor != pageScale) {
+        m_lastPageScaleFactor = pageScale;
+        m_webPage.scalePage(pageScale, m_lastScrollPosition);
     }
-
-    scheduleLayerFlush();
 }
 
-void ThreadedCoordinatedLayerTreeHost::cancelPendingLayerFlush()
+void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
 {
-    m_layerFlushTimer.stop();
+    CoordinatedLayerTreeHost::commitSceneState(state);
+    m_compositor->updateSceneState(state);
 }
 
-void ThreadedCoordinatedLayerTreeHost::performScheduledLayerFlush()
+void ThreadedCoordinatedLayerTreeHost::setIsDiscardable(bool discardable)
 {
-    if (m_isSuspended || m_isWaitingForRenderer)
+    m_isDiscardable = discardable;
+    if (m_isDiscardable) {
+        m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
         return;
-
-    m_coordinator->syncDisplayState();
-    bool didSync = m_coordinator->flushPendingLayerChanges();
-
-    if (m_notifyAfterScheduledLayerFlush && didSync) {
-        compositorDidFlushLayers();
-        m_notifyAfterScheduledLayerFlush = false;
     }
-}
 
-void ThreadedCoordinatedLayerTreeHost::purgeBackingStores()
-{
-    m_coordinator->purgeBackingStores();
-}
+    if (m_discardableSyncActions.isEmpty())
+        return;
 
-void ThreadedCoordinatedLayerTreeHost::renderNextFrame()
-{
-    m_isWaitingForRenderer = false;
-    m_coordinator->renderNextFrame();
-    scheduleLayerFlush();
-}
+    if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateBackground))
+        pageBackgroundTransparencyChanged();
 
-void ThreadedCoordinatedLayerTreeHost::commitScrollOffset(uint32_t layerID, const IntSize& offset)
-{
-    m_coordinator->commitScrollOffset(layerID, offset);
-}
+    if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
+        // Size changes already sets the scale factor and updates the viewport.
+        sizeDidChange(m_webPage.size());
+        return;
+    }
 
-void ThreadedCoordinatedLayerTreeHost::notifyFlushRequired()
-{
-    scheduleLayerFlush();
-}
+    if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
+        deviceOrPageScaleFactorChanged();
 
-void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
-{
-    m_isWaitingForRenderer = true;
-    m_compositor->updateSceneState(state);
+    if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
+        didChangeViewport();
 }
 
-void ThreadedCoordinatedLayerTreeHost::paintLayerContents(const GraphicsLayer*, GraphicsContext&, const IntRect&)
+#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
+RefPtr<WebCore::DisplayRefreshMonitor> ThreadedCoordinatedLayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
 {
+    return m_compositor->displayRefreshMonitor(displayID);
 }
+#endif
 
 } // namespace WebKit