2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "AcceleratedDrawingArea.h"
30 #include "DrawingAreaProxyMessages.h"
31 #include "LayerTreeHost.h"
32 #include "UpdateInfo.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>
41 using namespace WebCore;
45 AcceleratedDrawingArea::~AcceleratedDrawingArea()
48 m_layerTreeHost->invalidate();
51 AcceleratedDrawingArea::AcceleratedDrawingArea(WebPage& webPage, const WebPageCreationParameters& parameters)
52 #if USE(COORDINATED_GRAPHICS_MULTIPROCESS)
53 : DrawingArea(DrawingAreaTypeCoordinated, webPage)
55 : DrawingArea(DrawingAreaTypeImpl, webPage)
57 , m_exitCompositingTimer(RunLoop::main(), this, &AcceleratedDrawingArea::exitAcceleratedCompositingMode)
59 if (!m_webPage.isVisible())
63 void AcceleratedDrawingArea::setNeedsDisplay()
65 if (!m_isPaintingEnabled)
69 m_layerTreeHost->setNonCompositedContentsNeedDisplay();
72 void AcceleratedDrawingArea::setNeedsDisplayInRect(const IntRect& rect)
74 if (!m_isPaintingEnabled)
78 m_layerTreeHost->setNonCompositedContentsNeedDisplayInRect(rect);
81 void AcceleratedDrawingArea::scroll(const IntRect& scrollRect, const IntSize& scrollDelta)
83 if (!m_isPaintingEnabled)
87 m_layerTreeHost->scrollNonCompositedContents(scrollRect);
90 void AcceleratedDrawingArea::pageBackgroundTransparencyChanged()
93 m_layerTreeHost->pageBackgroundTransparencyChanged();
96 void AcceleratedDrawingArea::setLayerTreeStateIsFrozen(bool isFrozen)
98 if (m_layerTreeStateIsFrozen == isFrozen)
101 m_layerTreeStateIsFrozen = isFrozen;
104 m_layerTreeHost->setLayerFlushSchedulingEnabled(!isFrozen);
107 m_exitCompositingTimer.stop();
108 else if (m_wantsToExitAcceleratedCompositingMode)
109 exitAcceleratedCompositingModeSoon();
112 void AcceleratedDrawingArea::forceRepaint()
116 m_webPage.layoutIfNeeded();
118 if (!m_layerTreeHost)
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();
127 // Call setShouldNotifyAfterNextScheduledLayerFlush(false) here to
128 // prevent layerHostDidFlushLayers() from being called a second time.
129 m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(false);
130 layerHostDidFlushLayers();
134 bool AcceleratedDrawingArea::forceRepaintAsync(uint64_t callbackID)
136 return m_layerTreeHost && m_layerTreeHost->forceRepaintAsync(callbackID);
139 void AcceleratedDrawingArea::setPaintingEnabled(bool paintingEnabled)
141 m_isPaintingEnabled = paintingEnabled;
144 void AcceleratedDrawingArea::updatePreferences(const WebPreferencesStore& store)
146 m_webPage.corePage()->settings().setForceCompositingMode(store.getBoolValueForKey(WebPreferencesKey::forceCompositingModeKey()));
147 if (!m_layerTreeHost)
148 enterAcceleratedCompositingMode(nullptr);
151 void AcceleratedDrawingArea::mainFrameContentSizeChanged(const IntSize& size)
153 if (m_webPage.useFixedLayout() && m_layerTreeHost)
154 m_layerTreeHost->sizeDidChange(size);
155 m_webPage.mainFrame()->pageOverlayController().didChangeDocumentSize();
158 void AcceleratedDrawingArea::layerHostDidFlushLayers()
160 ASSERT(m_layerTreeHost);
161 m_layerTreeHost->forceRepaint();
163 if (m_shouldSendDidUpdateBackingStoreState && !exitAcceleratedCompositingModePending()) {
164 sendDidUpdateBackingStoreState();
168 ASSERT(!m_compositingAccordingToProxyMessages);
169 if (!exitAcceleratedCompositingModePending()) {
170 m_webPage.send(Messages::DrawingAreaProxy::EnterAcceleratedCompositingMode(m_backingStoreStateID, m_layerTreeHost->layerTreeContext()));
171 m_compositingAccordingToProxyMessages = true;
175 GraphicsLayerFactory* AcceleratedDrawingArea::graphicsLayerFactory()
177 return m_layerTreeHost ? m_layerTreeHost->graphicsLayerFactory() : nullptr;
180 void AcceleratedDrawingArea::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
182 ASSERT(m_layerTreeHost);
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.
189 // We're already in accelerated compositing mode, but the root compositing layer changed.
191 m_exitCompositingTimer.stop();
192 m_wantsToExitAcceleratedCompositingMode = false;
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);
199 m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
202 void AcceleratedDrawingArea::scheduleCompositingLayerFlush()
205 m_layerTreeHost->scheduleLayerFlush();
208 void AcceleratedDrawingArea::scheduleCompositingLayerFlushImmediately()
210 scheduleCompositingLayerFlush();
213 void AcceleratedDrawingArea::updateBackingStoreState(uint64_t stateID, bool respondImmediately, float deviceScaleFactor, const IntSize& size, const IntSize& scrollOffset)
215 ASSERT(!m_inUpdateBackingStoreState);
216 m_inUpdateBackingStoreState = true;
218 ASSERT_ARG(stateID, stateID >= m_backingStoreStateID);
219 if (stateID != m_backingStoreStateID) {
220 m_backingStoreStateID = stateID;
221 m_shouldSendDidUpdateBackingStoreState = true;
223 m_webPage.setDeviceScaleFactor(deviceScaleFactor);
224 m_webPage.setSize(size);
225 m_webPage.layoutIfNeeded();
226 m_webPage.scrollMainFrameIfNotAtMaxScrollPosition(scrollOffset);
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());
234 m_layerTreeHost->sizeDidChange(m_webPage.size());
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;
245 didUpdateBackingStoreState();
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)
253 sendDidUpdateBackingStoreState();
256 m_inUpdateBackingStoreState = false;
259 void AcceleratedDrawingArea::sendDidUpdateBackingStoreState()
261 ASSERT(m_shouldSendDidUpdateBackingStoreState);
263 m_shouldSendDidUpdateBackingStoreState = false;
265 UpdateInfo updateInfo;
266 updateInfo.viewSize = m_webPage.size();
267 updateInfo.deviceScaleFactor = m_webPage.corePage()->deviceScaleFactor();
269 LayerTreeContext layerTreeContext;
270 if (m_layerTreeHost) {
271 layerTreeContext = m_layerTreeHost->layerTreeContext();
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();
281 m_webPage.send(Messages::DrawingAreaProxy::DidUpdateBackingStoreState(m_backingStoreStateID, updateInfo, layerTreeContext));
282 m_compositingAccordingToProxyMessages = !layerTreeContext.isEmpty();
285 void AcceleratedDrawingArea::suspendPainting()
287 ASSERT(!m_isPaintingSuspended);
290 m_layerTreeHost->pauseRendering();
292 m_isPaintingSuspended = true;
294 m_webPage.corePage()->suspendScriptedAnimations();
297 void AcceleratedDrawingArea::resumePainting()
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>.
306 m_layerTreeHost->resumeRendering();
308 m_isPaintingSuspended = false;
310 // FIXME: We shouldn't always repaint everything here.
313 m_webPage.corePage()->resumeScriptedAnimations();
316 void AcceleratedDrawingArea::enterAcceleratedCompositingMode(GraphicsLayer* graphicsLayer)
318 m_exitCompositingTimer.stop();
319 m_wantsToExitAcceleratedCompositingMode = false;
321 ASSERT(!m_layerTreeHost);
322 m_layerTreeHost = LayerTreeHost::create(m_webPage);
323 #if USE(TEXTURE_MAPPER) && PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
324 if (m_nativeSurfaceHandleForCompositing)
325 m_layerTreeHost->setNativeSurfaceHandleForCompositing(m_nativeSurfaceHandleForCompositing);
327 if (!m_inUpdateBackingStoreState)
328 m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(true);
329 if (m_isPaintingSuspended)
330 m_layerTreeHost->pauseRendering();
332 m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
335 void AcceleratedDrawingArea::exitAcceleratedCompositingModeSoon()
337 if (m_layerTreeStateIsFrozen) {
338 m_wantsToExitAcceleratedCompositingMode = true;
342 if (exitAcceleratedCompositingModePending())
345 m_exitCompositingTimer.startOneShot(0);
348 #if USE(COORDINATED_GRAPHICS_MULTIPROCESS)
349 void AcceleratedDrawingArea::didReceiveCoordinatedLayerTreeHostMessage(IPC::Connection& connection, IPC::Decoder& decoder)
351 m_layerTreeHost->didReceiveCoordinatedLayerTreeHostMessage(connection, decoder);
355 #if USE(TEXTURE_MAPPER) && PLATFORM(GTK) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
356 void AcceleratedDrawingArea::setNativeSurfaceHandleForCompositing(uint64_t handle)
358 m_nativeSurfaceHandleForCompositing = handle;
359 if (m_layerTreeHost) {
360 m_webPage.corePage()->settings().setAcceleratedCompositingEnabled(true);
361 m_layerTreeHost->setNativeSurfaceHandleForCompositing(handle);
365 void AcceleratedDrawingArea::destroyNativeSurfaceHandleForCompositing(bool& handled)
368 setNativeSurfaceHandleForCompositing(0);
372 void AcceleratedDrawingArea::viewStateDidChange(ViewState::Flags changed, bool, const Vector<uint64_t>&)
374 if (changed & ViewState::IsVisible) {
375 if (m_webPage.isVisible())
382 void AcceleratedDrawingArea::attachViewOverlayGraphicsLayer(Frame* frame, GraphicsLayer* viewOverlayRootLayer)
384 if (!frame->isMainFrame())
388 m_layerTreeHost->setViewOverlayRootLayer(viewOverlayRootLayer);
391 } // namespace WebKit