[GTK] Configures but fails to link with ENABLE_OPENGL=OFF
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / AcceleratedDrawingArea.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "AcceleratedDrawingArea.h"
29
30 #include "DrawingAreaProxyMessages.h"
31 #include "LayerTreeHost.h"
32 #include "UpdateInfo.h"
33 #include "WebPage.h"
34 #include "WebPageCreationParameters.h"
35 #include "WebPreferencesKeys.h"
36 #include <WebCore/MainFrame.h>
37 #include <WebCore/Page.h>
38 #include <WebCore/PageOverlayController.h>
39 #include <WebCore/Settings.h>
40
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 AcceleratedDrawingArea::~AcceleratedDrawingArea()
46 {
47     if (m_layerTreeHost)
48         m_layerTreeHost->invalidate();
49 }
50
51 AcceleratedDrawingArea::AcceleratedDrawingArea(WebPage& webPage, const WebPageCreationParameters& parameters)
52 #if USE(COORDINATED_GRAPHICS_MULTIPROCESS)
53     : DrawingArea(DrawingAreaTypeCoordinated, webPage)
54 #else
55     : DrawingArea(DrawingAreaTypeImpl, webPage)
56 #endif
57     , m_exitCompositingTimer(RunLoop::main(), this, &AcceleratedDrawingArea::exitAcceleratedCompositingMode)
58 {
59     if (!m_webPage.isVisible())
60         suspendPainting();
61 }
62
63 void AcceleratedDrawingArea::setNeedsDisplay()
64 {
65     if (!m_isPaintingEnabled)
66         return;
67
68     if (m_layerTreeHost)
69         m_layerTreeHost->setNonCompositedContentsNeedDisplay();
70 }
71
72 void AcceleratedDrawingArea::setNeedsDisplayInRect(const IntRect& rect)
73 {
74     if (!m_isPaintingEnabled)
75         return;
76
77     if (m_layerTreeHost)
78         m_layerTreeHost->setNonCompositedContentsNeedDisplayInRect(rect);
79 }
80
81 void AcceleratedDrawingArea::scroll(const IntRect& scrollRect, const IntSize& scrollDelta)
82 {
83     if (!m_isPaintingEnabled)
84         return;
85
86     if (m_layerTreeHost)
87         m_layerTreeHost->scrollNonCompositedContents(scrollRect);
88 }
89
90 void AcceleratedDrawingArea::pageBackgroundTransparencyChanged()
91 {
92     if (m_layerTreeHost)
93         m_layerTreeHost->pageBackgroundTransparencyChanged();
94 }
95
96 void AcceleratedDrawingArea::setLayerTreeStateIsFrozen(bool isFrozen)
97 {
98     if (m_layerTreeStateIsFrozen == isFrozen)
99         return;
100
101     m_layerTreeStateIsFrozen = isFrozen;
102
103     if (m_layerTreeHost)
104         m_layerTreeHost->setLayerFlushSchedulingEnabled(!isFrozen);
105
106     if (isFrozen)
107         m_exitCompositingTimer.stop();
108     else if (m_wantsToExitAcceleratedCompositingMode)
109         exitAcceleratedCompositingModeSoon();
110 }
111
112 void AcceleratedDrawingArea::forceRepaint()
113 {
114     setNeedsDisplay();
115
116     m_webPage.layoutIfNeeded();
117
118     if (!m_layerTreeHost)
119         return;
120
121     // FIXME: We need to do the same work as the layerHostDidFlushLayers function here,
122     // but clearly it doesn't make sense to call the function with that name.
123     // Consider refactoring and renaming it.
124     if (m_compositingAccordingToProxyMessages)
125         m_layerTreeHost->forceRepaint();
126     else {
127         // Call setShouldNotifyAfterNextScheduledLayerFlush(false) here to
128         // prevent layerHostDidFlushLayers() from being called a second time.
129         m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(false);
130         layerHostDidFlushLayers();
131     }
132 }
133
134 bool AcceleratedDrawingArea::forceRepaintAsync(uint64_t callbackID)
135 {
136     return m_layerTreeHost && m_layerTreeHost->forceRepaintAsync(callbackID);
137 }
138
139 void AcceleratedDrawingArea::setPaintingEnabled(bool paintingEnabled)
140 {
141     m_isPaintingEnabled = paintingEnabled;
142 }
143
144 void AcceleratedDrawingArea::updatePreferences(const WebPreferencesStore& store)
145 {
146     m_webPage.corePage()->settings().setForceCompositingMode(store.getBoolValueForKey(WebPreferencesKey::forceCompositingModeKey()));
147     if (!m_layerTreeHost)
148         enterAcceleratedCompositingMode(nullptr);
149 }
150
151 void AcceleratedDrawingArea::mainFrameContentSizeChanged(const IntSize& size)
152 {
153     if (m_webPage.useFixedLayout() && m_layerTreeHost)
154         m_layerTreeHost->sizeDidChange(size);
155     m_webPage.mainFrame()->pageOverlayController().didChangeDocumentSize();
156 }
157
158 void AcceleratedDrawingArea::layerHostDidFlushLayers()
159 {
160     ASSERT(m_layerTreeHost);
161     m_layerTreeHost->forceRepaint();
162
163     if (m_shouldSendDidUpdateBackingStoreState && !exitAcceleratedCompositingModePending()) {
164         sendDidUpdateBackingStoreState();
165         return;
166     }
167
168     ASSERT(!m_compositingAccordingToProxyMessages);
169     if (!exitAcceleratedCompositingModePending()) {
170         m_webPage.send(Messages::DrawingAreaProxy::EnterAcceleratedCompositingMode(m_backingStoreStateID, m_layerTreeHost->layerTreeContext()));
171         m_compositingAccordingToProxyMessages = true;
172     }
173 }
174
175 GraphicsLayerFactory* AcceleratedDrawingArea::graphicsLayerFactory()
176 {
177     return m_layerTreeHost ? m_layerTreeHost->graphicsLayerFactory() : nullptr;
178 }
179
180 void AcceleratedDrawingArea::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
181 {
182     ASSERT(m_layerTreeHost);
183
184     // FIXME: Instead of using nested if statements, we should keep a compositing state
185     // enum in the AcceleratedDrawingArea object and have a changeAcceleratedCompositingState function
186     // that takes the new state.
187
188     if (graphicsLayer) {
189         // We're already in accelerated compositing mode, but the root compositing layer changed.
190
191         m_exitCompositingTimer.stop();
192         m_wantsToExitAcceleratedCompositingMode = false;
193
194         // If we haven't sent the EnterAcceleratedCompositingMode message, make sure that the
195         // layer tree host calls us back after the next layer flush so we can send it then.
196         if (!m_compositingAccordingToProxyMessages)
197             m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(true);
198     }
199     m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
200 }
201
202 void AcceleratedDrawingArea::scheduleCompositingLayerFlush()
203 {
204     if (m_layerTreeHost)
205         m_layerTreeHost->scheduleLayerFlush();
206 }
207
208 void AcceleratedDrawingArea::scheduleCompositingLayerFlushImmediately()
209 {
210     scheduleCompositingLayerFlush();
211 }
212
213 void AcceleratedDrawingArea::updateBackingStoreState(uint64_t stateID, bool respondImmediately, float deviceScaleFactor, const IntSize& size, const IntSize& scrollOffset)
214 {
215     ASSERT(!m_inUpdateBackingStoreState);
216     m_inUpdateBackingStoreState = true;
217
218     ASSERT_ARG(stateID, stateID >= m_backingStoreStateID);
219     if (stateID != m_backingStoreStateID) {
220         m_backingStoreStateID = stateID;
221         m_shouldSendDidUpdateBackingStoreState = true;
222
223         m_webPage.setDeviceScaleFactor(deviceScaleFactor);
224         m_webPage.setSize(size);
225         m_webPage.layoutIfNeeded();
226         m_webPage.scrollMainFrameIfNotAtMaxScrollPosition(scrollOffset);
227
228 #if USE(COORDINATED_GRAPHICS_MULTIPROCESS)
229         // Coordinated Graphics sets the size of the root layer to contents size.
230         if (!m_webPage.useFixedLayout())
231             m_layerTreeHost->sizeDidChange(m_webPage.size());
232 #else
233         if (m_layerTreeHost)
234             m_layerTreeHost->sizeDidChange(m_webPage.size());
235 #endif
236     } else {
237         ASSERT(size == m_webPage.size());
238         if (!m_shouldSendDidUpdateBackingStoreState) {
239             // We've already sent a DidUpdateBackingStoreState message for this state. We have nothing more to do.
240             m_inUpdateBackingStoreState = false;
241             return;
242         }
243     }
244
245     didUpdateBackingStoreState();
246
247     if (respondImmediately) {
248         // Make sure to resume painting if we're supposed to respond immediately, otherwise we'll just
249         // send back an empty UpdateInfo struct.
250         if (m_isPaintingSuspended)
251             resumePainting();
252
253         sendDidUpdateBackingStoreState();
254     }
255
256     m_inUpdateBackingStoreState = false;
257 }
258
259 void AcceleratedDrawingArea::sendDidUpdateBackingStoreState()
260 {
261     ASSERT(m_shouldSendDidUpdateBackingStoreState);
262
263     m_shouldSendDidUpdateBackingStoreState = false;
264
265     UpdateInfo updateInfo;
266     updateInfo.viewSize = m_webPage.size();
267     updateInfo.deviceScaleFactor = m_webPage.corePage()->deviceScaleFactor();
268
269     LayerTreeContext layerTreeContext;
270     if (m_layerTreeHost) {
271         layerTreeContext = m_layerTreeHost->layerTreeContext();
272
273         // We don't want the layer tree host to notify after the next scheduled
274         // layer flush because that might end up sending an EnterAcceleratedCompositingMode
275         // message back to the UI process, but the updated layer tree context
276         // will be sent back in the DidUpdateBackingStoreState message.
277         m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(false);
278         m_layerTreeHost->forceRepaint();
279     }
280
281     m_webPage.send(Messages::DrawingAreaProxy::DidUpdateBackingStoreState(m_backingStoreStateID, updateInfo, layerTreeContext));
282     m_compositingAccordingToProxyMessages = !layerTreeContext.isEmpty();
283 }
284
285 void AcceleratedDrawingArea::suspendPainting()
286 {
287     ASSERT(!m_isPaintingSuspended);
288
289     if (m_layerTreeHost)
290         m_layerTreeHost->pauseRendering();
291
292     m_isPaintingSuspended = true;
293
294     m_webPage.corePage()->suspendScriptedAnimations();
295 }
296
297 void AcceleratedDrawingArea::resumePainting()
298 {
299     if (!m_isPaintingSuspended) {
300         // FIXME: We can get a call to resumePainting when painting is not suspended.
301         // This happens when sending a synchronous message to create a new page. See <rdar://problem/8976531>.
302         return;
303     }
304
305     if (m_layerTreeHost)
306         m_layerTreeHost->resumeRendering();
307
308     m_isPaintingSuspended = false;
309
310     // FIXME: We shouldn't always repaint everything here.
311     setNeedsDisplay();
312
313     m_webPage.corePage()->resumeScriptedAnimations();
314 }
315
316 void AcceleratedDrawingArea::enterAcceleratedCompositingMode(GraphicsLayer* graphicsLayer)
317 {
318     m_exitCompositingTimer.stop();
319     m_wantsToExitAcceleratedCompositingMode = false;
320
321     ASSERT(!m_layerTreeHost);
322     m_layerTreeHost = LayerTreeHost::create(m_webPage);
323 #if USE(TEXTURE_MAPPER_GL) && PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
324     if (m_nativeSurfaceHandleForCompositing)
325         m_layerTreeHost->setNativeSurfaceHandleForCompositing(m_nativeSurfaceHandleForCompositing);
326 #endif
327     if (!m_inUpdateBackingStoreState)
328         m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(true);
329     if (m_isPaintingSuspended)
330         m_layerTreeHost->pauseRendering();
331
332     m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
333 }
334
335 void AcceleratedDrawingArea::exitAcceleratedCompositingModeSoon()
336 {
337     if (m_layerTreeStateIsFrozen) {
338         m_wantsToExitAcceleratedCompositingMode = true;
339         return;
340     }
341
342     if (exitAcceleratedCompositingModePending())
343         return;
344
345     m_exitCompositingTimer.startOneShot(0);
346 }
347
348 #if USE(COORDINATED_GRAPHICS_MULTIPROCESS)
349 void AcceleratedDrawingArea::didReceiveCoordinatedLayerTreeHostMessage(IPC::Connection& connection, IPC::Decoder& decoder)
350 {
351     m_layerTreeHost->didReceiveCoordinatedLayerTreeHostMessage(connection, decoder);
352 }
353 #endif
354
355 #if USE(TEXTURE_MAPPER_GL) && PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
356 void AcceleratedDrawingArea::setNativeSurfaceHandleForCompositing(uint64_t handle)
357 {
358     m_nativeSurfaceHandleForCompositing = handle;
359     if (m_layerTreeHost) {
360         m_webPage.corePage()->settings().setAcceleratedCompositingEnabled(true);
361         m_layerTreeHost->setNativeSurfaceHandleForCompositing(handle);
362     }
363 }
364
365 void AcceleratedDrawingArea::destroyNativeSurfaceHandleForCompositing(bool& handled)
366 {
367     handled = true;
368     setNativeSurfaceHandleForCompositing(0);
369 }
370 #endif
371
372 void AcceleratedDrawingArea::viewStateDidChange(ViewState::Flags changed, bool, const Vector<uint64_t>&)
373 {
374     if (changed & ViewState::IsVisible) {
375         if (m_webPage.isVisible())
376             resumePainting();
377         else
378             suspendPainting();
379     }
380 }
381
382 void AcceleratedDrawingArea::attachViewOverlayGraphicsLayer(Frame* frame, GraphicsLayer* viewOverlayRootLayer)
383 {
384     if (!frame->isMainFrame())
385         return;
386
387     if (m_layerTreeHost)
388         m_layerTreeHost->setViewOverlayRootLayer(viewOverlayRootLayer);
389 }
390
391 } // namespace WebKit