1/*
2    Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3    Copyright (C) 2013 Company 100, Inc.
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19*/
20
21#include "config.h"
22
23#if USE(COORDINATED_GRAPHICS)
24#include "CoordinatedLayerTreeHostProxy.h"
25
26#include "CoordinatedDrawingAreaProxy.h"
27#include "CoordinatedLayerTreeHostMessages.h"
28#include "CoordinatedLayerTreeHostProxyMessages.h"
29#include "WebPageProxy.h"
30#include "WebProcessProxy.h"
31#include <WebCore/CoordinatedGraphicsState.h>
32
33namespace WebKit {
34
35using namespace WebCore;
36
37CoordinatedLayerTreeHostProxy::CoordinatedLayerTreeHostProxy(CoordinatedDrawingAreaProxy* drawingAreaProxy)
38    : m_drawingAreaProxy(drawingAreaProxy)
39    , m_scene(adoptRef(new CoordinatedGraphicsScene(this)))
40{
41    m_drawingAreaProxy->page()->process().addMessageReceiver(Messages::CoordinatedLayerTreeHostProxy::messageReceiverName(), m_drawingAreaProxy->page()->pageID(), *this);
42}
43
44CoordinatedLayerTreeHostProxy::~CoordinatedLayerTreeHostProxy()
45{
46    m_drawingAreaProxy->page()->process().removeMessageReceiver(Messages::CoordinatedLayerTreeHostProxy::messageReceiverName(), m_drawingAreaProxy->page()->pageID());
47    m_scene->detach();
48}
49
50void CoordinatedLayerTreeHostProxy::updateViewport()
51{
52    m_drawingAreaProxy->updateViewport();
53}
54
55void CoordinatedLayerTreeHostProxy::dispatchUpdate(std::function<void()> function)
56{
57    m_scene->appendUpdate(WTF::move(function));
58}
59
60void CoordinatedLayerTreeHostProxy::commitCoordinatedGraphicsState(const CoordinatedGraphicsState& graphicsState)
61{
62    RefPtr<CoordinatedGraphicsScene> sceneProtector(m_scene);
63    dispatchUpdate([=] {
64        sceneProtector->commitSceneState(graphicsState);
65    });
66
67    updateViewport();
68#if USE(TILED_BACKING_STORE)
69    m_drawingAreaProxy->page()->didRenderFrame(graphicsState.contentsSize, graphicsState.coveredRect);
70#endif
71}
72
73void CoordinatedLayerTreeHostProxy::setVisibleContentsRect(const FloatRect& rect, const FloatPoint& trajectoryVector)
74{
75    // Inform the renderer to adjust viewport-fixed layers.
76    RefPtr<CoordinatedGraphicsScene> sceneProtector(m_scene);
77    const FloatPoint& scrollPosition = rect.location();
78    dispatchUpdate([=] {
79        sceneProtector->setScrollPosition(scrollPosition);
80    });
81
82    if (rect == m_lastSentVisibleRect && trajectoryVector == m_lastSentTrajectoryVector)
83        return;
84
85    m_drawingAreaProxy->page()->process().send(Messages::CoordinatedLayerTreeHost::SetVisibleContentsRect(rect, trajectoryVector), m_drawingAreaProxy->page()->pageID());
86    m_lastSentVisibleRect = rect;
87    m_lastSentTrajectoryVector = trajectoryVector;
88}
89
90void CoordinatedLayerTreeHostProxy::renderNextFrame()
91{
92    m_drawingAreaProxy->page()->process().send(Messages::CoordinatedLayerTreeHost::RenderNextFrame(), m_drawingAreaProxy->page()->pageID());
93}
94
95void CoordinatedLayerTreeHostProxy::purgeBackingStores()
96{
97    m_drawingAreaProxy->page()->process().send(Messages::CoordinatedLayerTreeHost::PurgeBackingStores(), m_drawingAreaProxy->page()->pageID());
98}
99
100void CoordinatedLayerTreeHostProxy::setBackgroundColor(const Color& color)
101{
102    RefPtr<CoordinatedGraphicsScene> sceneProtector(m_scene);
103    dispatchUpdate([=] {
104        sceneProtector->setBackgroundColor(color);
105    });
106}
107
108void CoordinatedLayerTreeHostProxy::commitScrollOffset(uint32_t layerID, const IntSize& offset)
109{
110    m_drawingAreaProxy->page()->process().send(Messages::CoordinatedLayerTreeHost::CommitScrollOffset(layerID, offset), m_drawingAreaProxy->page()->pageID());
111}
112
113}
114#endif // USE(COORDINATED_GRAPHICS)
115