1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DrawingAreaProxyImpl.h"
#include "DrawingAreaMessages.h"
#include "DrawingAreaProxyMessages.h"
#include "LayerTreeContext.h"
#include "UpdateInfo.h"
#include "WebPageGroup.h"
#include "WebPageProxy.h"
#include "WebPreferences.h"
#include "WebProcessProxy.h"
#include <WebCore/Region.h>
#if USE(COORDINATED_GRAPHICS)
#include "CoordinatedLayerTreeHostProxy.h"
#endif
using namespace WebCore;
namespace WebKit {
PassOwnPtr<DrawingAreaProxyImpl> DrawingAreaProxyImpl::create(WebPageProxy* webPageProxy)
{
return adoptPtr(new DrawingAreaProxyImpl(webPageProxy));
}
DrawingAreaProxyImpl::DrawingAreaProxyImpl(WebPageProxy* webPageProxy)
: DrawingAreaProxy(DrawingAreaTypeImpl, webPageProxy)
, m_currentBackingStoreStateID(0)
, m_nextBackingStoreStateID(0)
, m_isWaitingForDidUpdateBackingStoreState(false)
, m_hasReceivedFirstUpdate(false)
, m_isBackingStoreDiscardable(true)
, m_discardBackingStoreTimer(RunLoop::current(), this, &DrawingAreaProxyImpl::discardBackingStore)
{
#if USE(COORDINATED_GRAPHICS)
// Construct the proxy early to allow messages to be sent to the web process while AC is entered there.
if (webPageProxy->pageGroup()->preferences()->forceCompositingMode())
m_coordinatedLayerTreeHostProxy = adoptPtr(new CoordinatedLayerTreeHostProxy(this));
#endif
}
DrawingAreaProxyImpl::~DrawingAreaProxyImpl()
{
#if USE(ACCELERATED_COMPOSITING)
// Make sure to exit accelerated compositing mode.
if (isInAcceleratedCompositingMode())
exitAcceleratedCompositingMode();
#endif
}
void DrawingAreaProxyImpl::paint(BackingStore::PlatformGraphicsContext context, const IntRect& rect, Region& unpaintedRegion)
{
unpaintedRegion = rect;
if (isInAcceleratedCompositingMode())
return;
ASSERT(m_currentBackingStoreStateID <= m_nextBackingStoreStateID);
if (m_currentBackingStoreStateID < m_nextBackingStoreStateID) {
// Tell the web process to do a full backing store update now, in case we previously told
// it about our next state but didn't request an immediate update.
sendUpdateBackingStoreState(RespondImmediately);
// If we haven't yet received our first bits from the WebProcess then don't paint anything.
if (!m_hasReceivedFirstUpdate)
return;
if (m_isWaitingForDidUpdateBackingStoreState) {
// Wait for a DidUpdateBackingStoreState message that contains the new bits before we paint
// what's currently in the backing store.
waitForAndDispatchDidUpdateBackingStoreState();
}
// Dispatching DidUpdateBackingStoreState (either beneath sendUpdateBackingStoreState or
// beneath waitForAndDispatchDidUpdateBackingStoreState) could destroy our backing store or
// change the compositing mode.
if (!m_backingStore || isInAcceleratedCompositingMode())
return;
} else {
ASSERT(!m_isWaitingForDidUpdateBackingStoreState);
if (!m_backingStore) {
// The view has asked us to paint before the web process has painted anything. There's
// nothing we can do.
return;
}
}
m_backingStore->paint(context, rect);
unpaintedRegion.subtract(IntRect(IntPoint(), m_backingStore->size()));
discardBackingStoreSoon();
}
void DrawingAreaProxyImpl::sizeDidChange()
{
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyImpl::deviceScaleFactorDidChange()
{
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyImpl::layerHostingModeDidChange()
{
m_webPageProxy->process()->send(Messages::DrawingArea::SetLayerHostingMode(m_webPageProxy->layerHostingMode()), m_webPageProxy->pageID());
}
void DrawingAreaProxyImpl::visibilityDidChange()
{
if (!m_webPageProxy->suppressVisibilityUpdates()) {
if (!m_webPageProxy->isViewVisible()) {
// Suspend painting.
m_webPageProxy->process()->send(Messages::DrawingArea::SuspendPainting(), m_webPageProxy->pageID());
return;
}
// Resume painting.
m_webPageProxy->process()->send(Messages::DrawingArea::ResumePainting(), m_webPageProxy->pageID());
}
#if USE(ACCELERATED_COMPOSITING)
// If we don't have a backing store, go ahead and mark the backing store as being changed so
// that when paint we'll actually wait for something to paint and not flash white.
if (!m_backingStore && m_layerTreeContext.isEmpty())
backingStoreStateDidChange(DoNotRespondImmediately);
#endif
}
void DrawingAreaProxyImpl::setBackingStoreIsDiscardable(bool isBackingStoreDiscardable)
{
if (m_isBackingStoreDiscardable == isBackingStoreDiscardable)
return;
m_isBackingStoreDiscardable = isBackingStoreDiscardable;
if (m_isBackingStoreDiscardable)
discardBackingStoreSoon();
else
m_discardBackingStoreTimer.stop();
}
void DrawingAreaProxyImpl::waitForBackingStoreUpdateOnNextPaint()
{
m_hasReceivedFirstUpdate = true;
}
void DrawingAreaProxyImpl::update(uint64_t backingStoreStateID, const UpdateInfo& updateInfo)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
// FIXME: Handle the case where the view is hidden.
incorporateUpdate(updateInfo);
m_webPageProxy->process()->send(Messages::DrawingArea::DidUpdate(), m_webPageProxy->pageID());
}
void DrawingAreaProxyImpl::didUpdateBackingStoreState(uint64_t backingStoreStateID, const UpdateInfo& updateInfo, const LayerTreeContext& layerTreeContext)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_nextBackingStoreStateID);
ASSERT_ARG(backingStoreStateID, backingStoreStateID > m_currentBackingStoreStateID);
m_currentBackingStoreStateID = backingStoreStateID;
m_isWaitingForDidUpdateBackingStoreState = false;
// Stop the responsiveness timer that was started in sendUpdateBackingStoreState.
m_webPageProxy->process()->responsivenessTimer()->stop();
#if USE(ACCELERATED_COMPOSITING)
if (layerTreeContext != m_layerTreeContext) {
if (!m_layerTreeContext.isEmpty()) {
exitAcceleratedCompositingMode();
ASSERT(m_layerTreeContext.isEmpty());
}
if (!layerTreeContext.isEmpty()) {
enterAcceleratedCompositingMode(layerTreeContext);
ASSERT(layerTreeContext == m_layerTreeContext);
}
}
#endif
if (m_nextBackingStoreStateID != m_currentBackingStoreStateID)
sendUpdateBackingStoreState(RespondImmediately);
else
m_hasReceivedFirstUpdate = true;
#if USE(ACCELERATED_COMPOSITING)
if (isInAcceleratedCompositingMode()) {
ASSERT(!m_backingStore);
return;
}
#else
UNUSED_PARAM(layerTreeContext);
#endif
// If we have a backing store the right size, reuse it.
if (m_backingStore && (m_backingStore->size() != updateInfo.viewSize || m_backingStore->deviceScaleFactor() != updateInfo.deviceScaleFactor))
m_backingStore = nullptr;
incorporateUpdate(updateInfo);
}
void DrawingAreaProxyImpl::enterAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
#if USE(ACCELERATED_COMPOSITING)
enterAcceleratedCompositingMode(layerTreeContext);
#else
UNUSED_PARAM(layerTreeContext);
#endif
}
void DrawingAreaProxyImpl::exitAcceleratedCompositingMode(uint64_t backingStoreStateID, const UpdateInfo& updateInfo)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
#if USE(ACCELERATED_COMPOSITING)
exitAcceleratedCompositingMode();
#endif
incorporateUpdate(updateInfo);
}
void DrawingAreaProxyImpl::updateAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
#if USE(ACCELERATED_COMPOSITING)
updateAcceleratedCompositingMode(layerTreeContext);
#else
UNUSED_PARAM(layerTreeContext);
#endif
}
void DrawingAreaProxyImpl::incorporateUpdate(const UpdateInfo& updateInfo)
{
ASSERT(!isInAcceleratedCompositingMode());
if (updateInfo.updateRectBounds.isEmpty())
return;
if (!m_backingStore)
m_backingStore = BackingStore::create(updateInfo.viewSize, updateInfo.deviceScaleFactor, m_webPageProxy);
m_backingStore->incorporateUpdate(updateInfo);
bool shouldScroll = !updateInfo.scrollRect.isEmpty();
if (shouldScroll)
m_webPageProxy->scrollView(updateInfo.scrollRect, updateInfo.scrollOffset);
if (shouldScroll && !m_webPageProxy->canScrollView())
m_webPageProxy->setViewNeedsDisplay(IntRect(IntPoint(), m_webPageProxy->viewSize()));
else {
for (size_t i = 0; i < updateInfo.updateRects.size(); ++i)
m_webPageProxy->setViewNeedsDisplay(updateInfo.updateRects[i]);
}
if (WebPageProxy::debugPaintFlags() & kWKDebugFlashBackingStoreUpdates)
m_webPageProxy->flashBackingStoreUpdates(updateInfo.updateRects);
if (shouldScroll)
m_webPageProxy->displayView();
}
void DrawingAreaProxyImpl::backingStoreStateDidChange(RespondImmediatelyOrNot respondImmediatelyOrNot)
{
++m_nextBackingStoreStateID;
sendUpdateBackingStoreState(respondImmediatelyOrNot);
}
void DrawingAreaProxyImpl::sendUpdateBackingStoreState(RespondImmediatelyOrNot respondImmediatelyOrNot)
{
ASSERT(m_currentBackingStoreStateID < m_nextBackingStoreStateID);
if (!m_webPageProxy->isValid())
return;
if (m_isWaitingForDidUpdateBackingStoreState)
return;
if (m_webPageProxy->viewSize().isEmpty() && !m_webPageProxy->useFixedLayout())
return;
m_isWaitingForDidUpdateBackingStoreState = respondImmediatelyOrNot == RespondImmediately;
m_webPageProxy->process()->send(Messages::DrawingArea::UpdateBackingStoreState(m_nextBackingStoreStateID, respondImmediatelyOrNot == RespondImmediately, m_webPageProxy->deviceScaleFactor(), m_size, m_scrollOffset), m_webPageProxy->pageID());
m_scrollOffset = IntSize();
if (m_isWaitingForDidUpdateBackingStoreState) {
// Start the responsiveness timer. We will stop it when we hear back from the WebProcess
// in didUpdateBackingStoreState.
m_webPageProxy->process()->responsivenessTimer()->start();
}
#if USE(ACCELERATED_COMPOSITING)
if (m_isWaitingForDidUpdateBackingStoreState && !m_layerTreeContext.isEmpty()) {
// Wait for the DidUpdateBackingStoreState message. Normally we do this in DrawingAreaProxyImpl::paint, but that
// function is never called when in accelerated compositing mode.
waitForAndDispatchDidUpdateBackingStoreState();
}
#endif
}
void DrawingAreaProxyImpl::waitForAndDispatchDidUpdateBackingStoreState()
{
ASSERT(m_isWaitingForDidUpdateBackingStoreState);
if (!m_webPageProxy->isValid())
return;
if (m_webPageProxy->process()->isLaunching())
return;
#if USE(ACCELERATED_COMPOSITING)
// FIXME: waitForAndDispatchImmediately will always return the oldest DidUpdateBackingStoreState message that
// hasn't yet been processed. But it might be better to skip ahead to some other DidUpdateBackingStoreState
// message, if multiple DidUpdateBackingStoreState messages are waiting to be processed. For instance, we could
// choose the most recent one, or the one that is closest to our current size.
// The timeout, in seconds, we use when waiting for a DidUpdateBackingStoreState message when we're asked to paint.
static const double didUpdateBackingStoreStateTimeout = 0.5;
m_webPageProxy->process()->connection()->waitForAndDispatchImmediately<Messages::DrawingAreaProxy::DidUpdateBackingStoreState>(m_webPageProxy->pageID(), didUpdateBackingStoreStateTimeout);
#endif
}
#if USE(ACCELERATED_COMPOSITING)
void DrawingAreaProxyImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(!isInAcceleratedCompositingMode());
m_backingStore = nullptr;
m_layerTreeContext = layerTreeContext;
m_webPageProxy->enterAcceleratedCompositingMode(layerTreeContext);
#if USE(COORDINATED_GRAPHICS)
if (!m_coordinatedLayerTreeHostProxy)
m_coordinatedLayerTreeHostProxy = adoptPtr(new CoordinatedLayerTreeHostProxy(this));
#endif
}
#if USE(COORDINATED_GRAPHICS)
void DrawingAreaProxyImpl::setVisibleContentsRect(const WebCore::FloatRect& visibleContentsRect, const WebCore::FloatPoint& trajectoryVector)
{
if (m_coordinatedLayerTreeHostProxy)
m_coordinatedLayerTreeHostProxy->setVisibleContentsRect(visibleContentsRect, trajectoryVector);
}
#endif
void DrawingAreaProxyImpl::exitAcceleratedCompositingMode()
{
ASSERT(isInAcceleratedCompositingMode());
m_layerTreeContext = LayerTreeContext();
m_webPageProxy->exitAcceleratedCompositingMode();
}
void DrawingAreaProxyImpl::updateAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(isInAcceleratedCompositingMode());
m_layerTreeContext = layerTreeContext;
m_webPageProxy->updateAcceleratedCompositingMode(layerTreeContext);
}
#endif
void DrawingAreaProxyImpl::discardBackingStoreSoon()
{
if (!m_isBackingStoreDiscardable || m_discardBackingStoreTimer.isActive())
return;
// We'll wait this many seconds after the last paint before throwing away our backing store to save memory.
// FIXME: It would be smarter to make this delay based on how expensive painting is. See <http://webkit.org/b/55733>.
static const double discardBackingStoreDelay = 2;
m_discardBackingStoreTimer.startOneShot(discardBackingStoreDelay);
}
void DrawingAreaProxyImpl::discardBackingStore()
{
m_backingStore = nullptr;
backingStoreStateDidChange(DoNotRespondImmediately);
}
} // namespace WebKit
|