a4c177449245d8a1233e8656d0458f30438248b6
[WebKit-https.git] / Source / WebKit / 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 namespace WebKit {
40 using namespace WebCore;
41
42 Ref<ThreadedCoordinatedLayerTreeHost> ThreadedCoordinatedLayerTreeHost::create(WebPage& webPage)
43 {
44     return adoptRef(*new ThreadedCoordinatedLayerTreeHost(webPage));
45 }
46
47 ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
48 {
49 }
50
51 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
52     : CoordinatedLayerTreeHost(webPage)
53     , m_compositorClient(*this)
54     , m_surface(AcceleratedSurface::create(webPage, *this))
55     , m_viewportController(webPage.size())
56 {
57     if (FrameView* frameView = m_webPage.mainFrameView()) {
58         auto contentsSize = frameView->contentsSize();
59         if (!contentsSize.isEmpty())
60             m_viewportController.didChangeContentsSize(contentsSize);
61     }
62
63     IntSize scaledSize(m_webPage.size());
64     scaledSize.scale(m_webPage.deviceScaleFactor());
65     float scaleFactor = m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor();
66
67     if (m_surface) {
68         TextureMapper::PaintFlags paintFlags = 0;
69
70         if (m_surface->shouldPaintMirrored())
71             paintFlags |= TextureMapper::PaintingMirrored;
72
73         m_compositor = ThreadedCompositor::create(m_compositorClient, webPage, scaledSize, scaleFactor, ThreadedCompositor::ShouldDoFrameSync::Yes, paintFlags);
74         m_layerTreeContext.contextID = m_surface->surfaceID();
75     } else
76         m_compositor = ThreadedCompositor::create(m_compositorClient, webPage, scaledSize, scaleFactor);
77
78     didChangeViewport();
79 }
80
81 void ThreadedCoordinatedLayerTreeHost::invalidate()
82 {
83     m_compositor->invalidate();
84     CoordinatedLayerTreeHost::invalidate();
85     m_surface = nullptr;
86 }
87
88 void ThreadedCoordinatedLayerTreeHost::forceRepaint()
89 {
90     CoordinatedLayerTreeHost::forceRepaint();
91     m_compositor->forceRepaint();
92 }
93
94 void ThreadedCoordinatedLayerTreeHost::frameComplete()
95 {
96     m_compositor->frameComplete();
97 }
98
99 uint64_t ThreadedCoordinatedLayerTreeHost::nativeSurfaceHandleForCompositing()
100 {
101     if (!m_surface)
102         return m_layerTreeContext.contextID;
103
104     m_surface->initialize();
105     return m_surface->window();
106 }
107
108 void ThreadedCoordinatedLayerTreeHost::didDestroyGLContext()
109 {
110     if (m_surface)
111         m_surface->finalize();
112 }
113
114 void ThreadedCoordinatedLayerTreeHost::willRenderFrame()
115 {
116     if (m_surface)
117         m_surface->willRenderFrame();
118 }
119
120 void ThreadedCoordinatedLayerTreeHost::didRenderFrame()
121 {
122     if (m_surface)
123         m_surface->didRenderFrame();
124 }
125
126 void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const IntRect& rect)
127 {
128     FrameView* frameView = m_webPage.mainFrameView();
129     if (!frameView || !frameView->delegatesScrolling())
130         return;
131
132     m_viewportController.didScroll(rect.location());
133     if (m_isDiscardable)
134         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
135     else
136         didChangeViewport();
137 }
138
139 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
140 {
141     m_viewportController.didChangeContentsSize(newSize);
142     if (m_isDiscardable)
143         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
144     else
145         didChangeViewport();
146 }
147
148 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
149 {
150     if (m_isDiscardable) {
151         m_discardableSyncActions |= DiscardableSyncActions::UpdateScale;
152         return;
153     }
154
155     if (m_surface && m_surface->resize(m_webPage.size()))
156         m_layerTreeContext.contextID = m_surface->surfaceID();
157
158     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
159     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
160 }
161
162 void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
163 {
164     if (m_isDiscardable) {
165         m_discardableSyncActions |= DiscardableSyncActions::UpdateBackground;
166         return;
167     }
168
169     CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
170     m_compositor->setDrawsBackground(m_webPage.drawsBackground());
171 }
172
173 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
174 {
175     if (m_isDiscardable) {
176         m_discardableSyncActions |= DiscardableSyncActions::UpdateSize;
177         m_viewportController.didChangeViewportSize(size);
178         return;
179     }
180
181     if (m_surface && m_surface->resize(size))
182         m_layerTreeContext.contextID = m_surface->surfaceID();
183
184     CoordinatedLayerTreeHost::sizeDidChange(size);
185     m_viewportController.didChangeViewportSize(size);
186     IntSize scaledSize(size);
187     scaledSize.scale(m_webPage.deviceScaleFactor());
188     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
189     didChangeViewport();
190 }
191
192 void ThreadedCoordinatedLayerTreeHost::didChangeViewportAttributes(ViewportAttributes&& attr)
193 {
194     m_viewportController.didChangeViewportAttributes(WTFMove(attr));
195     if (m_isDiscardable)
196         m_discardableSyncActions |= DiscardableSyncActions::UpdateViewport;
197     else
198         didChangeViewport();
199 }
200
201 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
202 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
203 {
204     m_layerTreeContext.contextID = handle;
205     m_compositor->setNativeSurfaceHandleForCompositing(handle);
206     scheduleLayerFlush();
207 }
208 #endif
209
210 void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
211 {
212     FloatRect visibleRect(m_viewportController.visibleContentsRect());
213     if (visibleRect.isEmpty())
214         return;
215
216     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
217     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
218     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
219     FrameView* view = m_webPage.corePage()->mainFrame().view();
220     Scrollbar* scrollbar = view->verticalScrollbar();
221     if (scrollbar && !scrollbar->isOverlayScrollbar())
222         visibleRect.expand(scrollbar->width(), 0);
223     scrollbar = view->horizontalScrollbar();
224     if (scrollbar && !scrollbar->isOverlayScrollbar())
225         visibleRect.expand(0, scrollbar->height());
226
227     CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect, FloatPoint::zero());
228
229     float pageScale = m_viewportController.pageScaleFactor();
230     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
231     if (m_lastScrollPosition != scrollPosition) {
232         m_lastScrollPosition = scrollPosition;
233         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
234
235         if (!view->useFixedLayout())
236             view->notifyScrollPositionChanged(m_lastScrollPosition);
237     }
238
239     if (m_lastPageScaleFactor != pageScale) {
240         m_lastPageScaleFactor = pageScale;
241         m_webPage.scalePage(pageScale, m_lastScrollPosition);
242     }
243 }
244
245 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
246 {
247     CoordinatedLayerTreeHost::commitSceneState(state);
248     m_compositor->updateSceneState(state);
249 }
250
251 void ThreadedCoordinatedLayerTreeHost::releaseUpdateAtlases(const Vector<uint32_t>& atlasesToRemove)
252 {
253     m_compositor->releaseUpdateAtlases(atlasesToRemove);
254 }
255
256 void ThreadedCoordinatedLayerTreeHost::setIsDiscardable(bool discardable)
257 {
258     m_isDiscardable = discardable;
259     if (m_isDiscardable) {
260         m_discardableSyncActions = OptionSet<DiscardableSyncActions>();
261         return;
262     }
263
264     if (m_discardableSyncActions.isEmpty())
265         return;
266
267     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateBackground))
268         pageBackgroundTransparencyChanged();
269
270     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateSize)) {
271         // Size changes already sets the scale factor and updates the viewport.
272         sizeDidChange(m_webPage.size());
273         return;
274     }
275
276     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateScale))
277         deviceOrPageScaleFactorChanged();
278
279     if (m_discardableSyncActions.contains(DiscardableSyncActions::UpdateViewport))
280         didChangeViewport();
281 }
282
283 #if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
284 RefPtr<WebCore::DisplayRefreshMonitor> ThreadedCoordinatedLayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
285 {
286     return m_compositor->displayRefreshMonitor(displayID);
287 }
288 #endif
289
290 } // namespace WebKit
291
292 #endif // USE(COORDINATED_GRAPHICS)