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