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