[Threaded Compositor] Web Process crash when the layer tree host is destroyed
[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 #include "WebPage.h"
34 #include <WebCore/FrameView.h>
35 #include <WebCore/MainFrame.h>
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 Ref<ThreadedCoordinatedLayerTreeHost> ThreadedCoordinatedLayerTreeHost::create(WebPage& webPage)
42 {
43     return adoptRef(*new ThreadedCoordinatedLayerTreeHost(webPage));
44 }
45
46 ThreadedCoordinatedLayerTreeHost::~ThreadedCoordinatedLayerTreeHost()
47 {
48 }
49
50 ThreadedCoordinatedLayerTreeHost::ThreadedCoordinatedLayerTreeHost(WebPage& webPage)
51     : CoordinatedLayerTreeHost(webPage)
52     , m_compositorClient(*this)
53     , m_compositor(ThreadedCompositor::create(&m_compositorClient))
54 {
55 }
56
57 void ThreadedCoordinatedLayerTreeHost::invalidate()
58 {
59     m_compositor->invalidate();
60     CoordinatedLayerTreeHost::invalidate();
61 }
62
63 void ThreadedCoordinatedLayerTreeHost::scrollNonCompositedContents(const WebCore::IntRect& rect)
64 {
65     m_compositor->scrollTo(rect.location());
66     scheduleLayerFlush();
67 }
68
69 void ThreadedCoordinatedLayerTreeHost::contentsSizeChanged(const WebCore::IntSize& newSize)
70 {
71     m_compositor->didChangeContentsSize(newSize);
72 }
73
74 void ThreadedCoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
75 {
76     CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged();
77     m_compositor->setDeviceScaleFactor(m_webPage.deviceScaleFactor());
78 }
79
80 void ThreadedCoordinatedLayerTreeHost::sizeDidChange(const IntSize& size)
81 {
82     CoordinatedLayerTreeHost::sizeDidChange(size);
83     m_compositor->didChangeViewportSize(size);
84 }
85
86 void ThreadedCoordinatedLayerTreeHost::didChangeViewportProperties(const WebCore::ViewportAttributes& attr)
87 {
88     m_compositor->didChangeViewportAttribute(attr);
89 }
90
91 void ThreadedCoordinatedLayerTreeHost::didScaleFactorChanged(float scale, const IntPoint& origin)
92 {
93     m_webPage.scalePage(scale, origin);
94 }
95
96 #if PLATFORM(GTK)
97 void ThreadedCoordinatedLayerTreeHost::setNativeSurfaceHandleForCompositing(uint64_t handle)
98 {
99     m_layerTreeContext.contextID = handle;
100     m_compositor->setNativeSurfaceHandleForCompositing(handle);
101     scheduleLayerFlush();
102 }
103 #endif
104
105 void ThreadedCoordinatedLayerTreeHost::setVisibleContentsRect(const FloatRect& rect, const FloatPoint& trajectoryVector, float scale)
106 {
107     CoordinatedLayerTreeHost::setVisibleContentsRect(rect, trajectoryVector);
108     if (m_lastScrollPosition != roundedIntPoint(rect.location())) {
109         m_lastScrollPosition = roundedIntPoint(rect.location());
110
111         if (!m_webPage.corePage()->mainFrame().view()->useFixedLayout())
112             m_webPage.corePage()->mainFrame().view()->notifyScrollPositionChanged(m_lastScrollPosition);
113     }
114
115     if (m_lastScaleFactor != scale) {
116         m_lastScaleFactor = scale;
117         didScaleFactorChanged(m_lastScaleFactor, m_lastScrollPosition);
118     }
119 }
120
121 void ThreadedCoordinatedLayerTreeHost::commitSceneState(const CoordinatedGraphicsState& state)
122 {
123     CoordinatedLayerTreeHost::commitSceneState(state);
124     m_compositor->updateSceneState(state);
125 }
126
127 } // namespace WebKit
128
129 #endif // USE(COORDINATED_GRAPHICS)