[GTK] SetNativeSurfaceHandleForCompositing should not be compiled in for wayland...
[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     m_viewportController.didScroll(rect.location());
83     didChangeViewport();
84 }
85
86 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const IntSize& newSize)
87 {
88     m_viewportController.didChangeContentsSize(newSize);
89     didChangeViewport();
90 }
91
92 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
93 {
94     if (m_surface && m_surface->resize(m_webPage.size()))
95         m_layerTreeContext.contextID = m_surface->surfaceID();
96
97     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
98     m_compositor->setScaleFactor(m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
99 }
100
101 void ThreadedCoordinatedLayerTreeHost::pageBackgroundTransparencyChanged()
102 {
103     CoordinatedLayerTreeHost::pageBackgroundTransparencyChanged();
104     m_compositor->setDrawsBackground(m_webPage.drawsBackground());
105 }
106
107 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
108 {
109     if (m_surface && m_surface->resize(size))
110         m_layerTreeContext.contextID = m_surface->surfaceID();
111
112     CoordinatedLayerTreeHost::sizeDidChange(size);
113     m_viewportController.didChangeViewportSize(size);
114     IntSize scaledSize(size);
115     scaledSize.scale(m_webPage.deviceScaleFactor());
116     m_compositor->setViewportSize(scaledSize, m_webPage.deviceScaleFactor() * m_viewportController.pageScaleFactor());
117     didChangeViewport();
118 }
119
120 void ThreadedCoordinatedLayerTreeHost::didChangeViewportProperties(const ViewportAttributes& attr)
121 {
122     m_viewportController.didChangeViewportAttributes(attr);
123     didChangeViewport();
124 }
125
126 #if PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
127 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
128 {
129     m_layerTreeContext.contextID = handle;
130     m_compositor->setNativeSurfaceHandleForCompositing(handle);
131     scheduleLayerFlush();
132 }
133 #endif
134
135 void ThreadedCoordinatedLayerTreeHost::didChangeViewport()
136 {
137     FloatRect visibleRect(m_viewportController.visibleContentsRect());
138     if (visibleRect.isEmpty())
139         return;
140
141     // When using non overlay scrollbars, the contents size doesn't include the scrollbars, but we need to include them
142     // in the visible area used by the compositor to ensure that the scrollbar layers are also updated.
143     // See https://bugs.webkit.org/show_bug.cgi?id=160450.
144     FrameView* view = m_webPage.corePage()->mainFrame().view();
145     Scrollbar* scrollbar = view->verticalScrollbar();
146     if (scrollbar && !scrollbar->isOverlayScrollbar())
147         visibleRect.expand(scrollbar->width(), 0);
148     scrollbar = view->horizontalScrollbar();
149     if (scrollbar && !scrollbar->isOverlayScrollbar())
150         visibleRect.expand(0, scrollbar->height());
151
152     CoordinatedLayerTreeHost::setVisibleContentsRect(visibleRect, FloatPoint::zero());
153
154     float pageScale = m_viewportController.pageScaleFactor();
155     IntPoint scrollPosition = roundedIntPoint(visibleRect.location());
156     if (m_lastScrollPosition != scrollPosition) {
157         m_lastScrollPosition = scrollPosition;
158         m_compositor->setScrollPosition(m_lastScrollPosition, m_webPage.deviceScaleFactor() * pageScale);
159
160         if (!view->useFixedLayout())
161             view->notifyScrollPositionChanged(m_lastScrollPosition);
162     }
163
164     if (m_lastPageScaleFactor != pageScale) {
165         m_lastPageScaleFactor = pageScale;
166         m_webPage.scalePage(pageScale, m_lastScrollPosition);
167     }
168 }
169
170 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
171 {
172     CoordinatedLayerTreeHost::commitSceneState(state);
173     m_compositor->updateSceneState(state);
174 }
175
176 } // namespace WebKit
177
178 #endif // USE(COORDINATED_GRAPHICS)