[ThreadedCompositor] Scrolling artifacts on accelerated subframes
[WebKit-https.git] / Source / WebKit2 / 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/FrameView.h>
37 #include <WebCore/MainFrame.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 Ref<ThreadedCoordinatedLayerTreeHost> ThreadedCoordinatedLayerTreeHost::create(WebPage& webPage)
44 {
45     return adoptRef(*new ThreadedCoordinatedLayerTreeHost(webPage));
46 }
47
48 ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
49 {
50 }
51
52 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
53     : CoordinatedLayerTreeHost(webPage)
54     , m_compositorClient(*this)
55     , m_surface(AcceleratedSurface::create(webPage))
56 {
57     if (m_surface) {
58         // Do not do frame sync when rendering offscreen in the web process to ensure that SwapBuffers never blocks.
59         // Rendering to the actual screen will happen later anyway since the UI process schedules a redraw for every update,
60         // the compositor will take care of syncing to vblank.
61         m_compositor = ThreadedCompositor::create(m_compositorClient, m_surface->window(), ThreadedCompositor::ShouldDoFrameSync::No);
62         m_layerTreeContext.contextID = m_surface->surfaceID();
63     } else
64         m_compositor = ThreadedCompositor::create(m_compositorClient);
65 }
66
67 void ThreadedCoordinatedLayerTreeHost::invalidate()
68 {
69     m_compositor->invalidate();
70     CoordinatedLayerTreeHost::invalidate();
71     m_surface = nullptr;
72 }
73
74 void ThreadedCoordinatedLayerTreeHost::forceRepaint()
75 {
76     CoordinatedLayerTreeHost::forceRepaint();
77     m_compositor->forceRepaint();
78 }
79
80 void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
81 {
82     FrameView* frameView = m_webPage.mainFrameView();
83     if (!frameView || !frameView->delegatesScrolling())
84         return;
85
86     m_viewportController.didScroll(rect.location());
87     didChangeViewport();
88 }
89
90 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
91 {
92     m_viewportController.didChangeContentsSize(newSize);
93     didChangeViewport();
94 }
95
96 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
97 {
98     if (m_surface && m_surface->resize(m_webPage.size()))
99         m_layerTreeContext.contextID = m_surface->surfaceID();
100
101     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
102     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
103 }
104
105 void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
106 {
107     CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
108     m_compositor->setDrawsBackground(m_webPage.drawsBackground());
109 }
110
111 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
112 {
113     if (m_surface && m_surface->resize(size))
114         m_layerTreeContext.contextID = m_surface->surfaceID();
115
116     CoordinatedLayerTreeHost::sizeDidChange(size);
117     m_viewportController.didChangeViewportSize(size);
118     IntSize scaledSize(size);
119     scaledSize.scale(m_webPage.deviceScaleFactor());
120     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
121     didChangeViewport();
122 }
123
124 void ThreadedCoordinatedLayerTreeHost::didChangeViewportProperties(const ViewportAttributes& attr)
125 {
126     m_viewportController.didChangeViewportAttributes(attr);
127     didChangeViewport();
128 }
129
130 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
131 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
132 {
133     m_layerTreeContext.contextID = handle;
134     m_compositor->setNativeSurfaceHandleForCompositing(handle);
135     scheduleLayerFlush();
136 }
137 #endif
138
139 void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
140 {
141     FloatRect visibleRect(m_viewportController.visibleContentsRect());
142     if (visibleRect.isEmpty())
143         return;
144
145     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
146     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
147     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
148     FrameView* view = m_webPage.corePage()->mainFrame().view();
149     Scrollbar* scrollbar = view->verticalScrollbar();
150     if (scrollbar && !scrollbar->isOverlayScrollbar())
151         visibleRect.expand(scrollbar->width(), 0);
152     scrollbar = view->horizontalScrollbar();
153     if (scrollbar && !scrollbar->isOverlayScrollbar())
154         visibleRect.expand(0, scrollbar->height());
155
156     CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect, FloatPoint::zero());
157
158     float pageScale = m_viewportController.pageScaleFactor();
159     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
160     if (m_lastScrollPosition != scrollPosition) {
161         m_lastScrollPosition = scrollPosition;
162         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
163
164         if (!view->useFixedLayout())
165             view->notifyScrollPositionChanged(m_lastScrollPosition);
166     }
167
168     if (m_lastPageScaleFactor != pageScale) {
169         m_lastPageScaleFactor = pageScale;
170         m_webPage.scalePage(pageScale, m_lastScrollPosition);
171     }
172 }
173
174 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
175 {
176     CoordinatedLayerTreeHost::commitSceneState(state);
177     m_compositor->updateSceneState(state);
178 }
179
180 } // namespace WebKit
181
182 #endif // USE(COORDINATED_GRAPHICS)