initial import
[vuplus_webkit] / Source / WebKit2 / WebProcess / WebPage / TiledDrawingArea.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "TiledDrawingArea.h"
28
29 #if ENABLE(TILED_BACKING_STORE)
30
31 #include "DrawingAreaProxyMessages.h"
32 #include "MessageID.h"
33 #include "UpdateInfo.h"
34 #include "WebCoreArgumentCoders.h"
35 #include "WebPage.h"
36 #include "WebProcess.h"
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 TiledDrawingArea::TiledDrawingArea(WebPage* webPage)
43     : DrawingArea(DrawingAreaTypeTiled, webPage)
44     , m_suspended(false)
45     , m_isWaitingForUIProcess(false)
46     , m_didSendTileUpdate(false)
47     , m_mainBackingStore(adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this))))
48 {
49 }
50
51 TiledDrawingArea::~TiledDrawingArea()
52 {
53 }
54
55 void TiledDrawingArea::scroll(const IntRect& scrollRect, const IntSize& scrollDelta)
56 {
57     // FIXME: Do something much smarter.
58     setNeedsDisplay(scrollRect);
59 }
60
61 void TiledDrawingArea::setNeedsDisplay(const IntRect& rect)
62 {
63     m_mainBackingStore->invalidate(rect);
64 }
65
66 void TiledDrawingArea::setSize(const IntSize& viewSize)
67 {
68     ASSERT(!m_suspended);
69     ASSERT_ARG(viewSize, !viewSize.isEmpty());
70
71     m_webPage->setSize(viewSize);
72 }
73
74 void TiledDrawingArea::setVisibleContentRectAndScale(const WebCore::IntRect& visibleContentsRect, float scale)
75 {
76     m_visibleContentRect = visibleContentsRect;
77
78     if (scale != m_mainBackingStore->contentsScale()) {
79         // Keep the tiles for the previous scale until enough content is available to be shown on the screen for the new scale.
80         // If we already have a previous set of tiles it means that two scale changed happened successively.
81         // In that case, make sure that our current main tiles have more content to show than the "previous previous"
82         // within the visible rect before replacing it.
83         if (!m_previousBackingStore || m_mainBackingStore->coverageRatio(m_visibleContentRect) > m_previousBackingStore->coverageRatio(m_visibleContentRect))
84             m_previousBackingStore = m_mainBackingStore.release();
85
86         m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this)));
87         m_mainBackingStore->setContentsScale(scale);
88     } else
89         m_mainBackingStore->adjustVisibleRect();
90 }
91
92 void TiledDrawingArea::setVisibleContentRectTrajectoryVector(const WebCore::FloatPoint& trajectoryVector)
93 {
94     m_mainBackingStore->setVisibleRectTrajectoryVector(trajectoryVector);
95 }
96
97 void TiledDrawingArea::renderNextFrame()
98 {
99     m_isWaitingForUIProcess = false;
100     m_mainBackingStore->updateTileBuffers();
101 }
102
103 void TiledDrawingArea::suspendPainting()
104 {
105     ASSERT(!m_suspended);
106
107     m_suspended = true;
108 }
109
110 void TiledDrawingArea::resumePainting()
111 {
112     ASSERT(m_suspended);
113
114     m_suspended = false;
115     m_mainBackingStore->updateTileBuffers();
116 }
117
118 void TiledDrawingArea::tiledBackingStorePaintBegin()
119 {
120     m_webPage->layoutIfNeeded();
121 }
122
123 void TiledDrawingArea::tiledBackingStorePaint(GraphicsContext* graphicsContext, const IntRect& contentRect)
124 {
125     m_webPage->drawRect(*graphicsContext, contentRect);
126 }
127
128 void TiledDrawingArea::tiledBackingStorePaintEnd(const Vector<IntRect>& paintedArea)
129 {
130     if (m_didSendTileUpdate) {
131         // Since we know that all tile updates following a page invalidate will all be rendered
132         // in one paint pass for all the tiles, we can send the swap tile message here.
133         m_webPage->send(Messages::DrawingAreaProxy::DidRenderFrame());
134         m_isWaitingForUIProcess = true;
135         m_didSendTileUpdate = false;
136
137         // Make sure that we destroy the previous backing store and remove its tiles only after DidRenderFrame
138         // was sent to swap recently created tiles' buffer. Else a frame could be rendered after the previous
139         // tiles were removed and before the new tile have their first back buffer swapped.
140         if (m_previousBackingStore && m_mainBackingStore->coverageRatio(m_visibleContentRect) >= 1.0f)
141             m_previousBackingStore.clear();
142     }
143 }
144
145 bool TiledDrawingArea::tiledBackingStoreUpdatesAllowed() const
146 {
147     return !m_suspended && !m_isWaitingForUIProcess;
148 }
149
150 IntRect TiledDrawingArea::tiledBackingStoreContentsRect()
151 {
152     return IntRect(IntPoint::zero(), m_webPage->size());
153 }
154
155 IntRect TiledDrawingArea::tiledBackingStoreVisibleRect()
156 {
157     return m_visibleContentRect;
158 }
159
160 Color TiledDrawingArea::tiledBackingStoreBackgroundColor() const
161 {
162     return Color::transparent;
163 }
164
165 void TiledDrawingArea::createTile(int tileID, const UpdateInfo& updateInfo)
166 {
167     m_webPage->send(Messages::DrawingAreaProxy::CreateTile(tileID, updateInfo));
168     m_didSendTileUpdate = true;
169 }
170 void TiledDrawingArea::updateTile(int tileID, const UpdateInfo& updateInfo)
171 {
172     m_webPage->send(Messages::DrawingAreaProxy::UpdateTile(tileID, updateInfo));
173     m_didSendTileUpdate = true;
174 }
175 void TiledDrawingArea::removeTile(int tileID)
176 {
177     m_webPage->send(Messages::DrawingAreaProxy::RemoveTile(tileID));
178 }
179
180 } // namespace WebKit
181
182 #endif // TILED_BACKING_STORE