DrawingAreaProxyImpl.cpp [plain text]
#include "config.h"
#include "DrawingAreaProxyImpl.h"
#include "DrawingAreaMessages.h"
#include "DrawingAreaProxyMessages.h"
#include "LayerTreeContext.h"
#include "Region.h"
#include "UpdateInfo.h"
#include "WebPageProxy.h"
#include "WebProcessProxy.h"
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)
{
}
DrawingAreaProxyImpl::~DrawingAreaProxyImpl()
{
#if USE(ACCELERATED_COMPOSITING)
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) {
sendUpdateBackingStoreState(RespondImmediately);
if (!m_hasReceivedFirstUpdate)
return;
if (m_isWaitingForDidUpdateBackingStoreState) {
waitForAndDispatchDidUpdateBackingStoreState();
}
if (!m_backingStore || isInAcceleratedCompositingMode())
return;
} else {
ASSERT(!m_isWaitingForDidUpdateBackingStoreState);
if (!m_backingStore) {
return;
}
}
m_backingStore->paint(context, rect);
unpaintedRegion.subtract(IntRect(IntPoint(), m_backingStore->size()));
discardBackingStoreSoon();
}
void DrawingAreaProxyImpl::didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*)
{
ASSERT_NOT_REACHED();
}
void DrawingAreaProxyImpl::didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*)
{
ASSERT_NOT_REACHED();
}
bool DrawingAreaProxyImpl::paint(const WebCore::IntRect&, PlatformDrawingContext)
{
ASSERT_NOT_REACHED();
return false;
}
void DrawingAreaProxyImpl::sizeDidChange()
{
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyImpl::deviceScaleFactorDidChange()
{
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyImpl::visibilityDidChange()
{
if (!m_webPageProxy->isViewVisible()) {
m_webPageProxy->process()->send(Messages::DrawingArea::SuspendPainting(), m_webPageProxy->pageID());
return;
}
m_webPageProxy->process()->send(Messages::DrawingArea::ResumePainting(), m_webPageProxy->pageID());
#if USE(ACCELERATED_COMPOSITING)
if (!m_backingStore && m_layerTreeContext.isEmpty())
backingStoreStateDidChange(DoNotRespondImmediately);
#endif
}
void DrawingAreaProxyImpl::setPageIsVisible(bool)
{
}
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;
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;
m_webPageProxy->process()->responsivenessTimer()->stop();
if (m_nextBackingStoreStateID != m_currentBackingStoreStateID)
sendUpdateBackingStoreState(RespondImmediately);
else
m_hasReceivedFirstUpdate = true;
#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);
}
}
if (isInAcceleratedCompositingMode()) {
ASSERT(!m_backingStore);
return;
}
#endif
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);
#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::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);
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())
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) {
m_webPageProxy->process()->responsivenessTimer()->start();
}
#if USE(ACCELERATED_COMPOSITING)
if (m_isWaitingForDidUpdateBackingStoreState && !m_layerTreeContext.isEmpty()) {
waitForAndDispatchDidUpdateBackingStoreState();
}
#endif
}
void DrawingAreaProxyImpl::waitForAndDispatchDidUpdateBackingStoreState()
{
ASSERT(m_isWaitingForDidUpdateBackingStoreState);
if (!m_webPageProxy->isValid())
return;
if (m_webPageProxy->process()->isLaunching())
return;
#if USE(ACCELERATED_COMPOSITING)
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);
}
void DrawingAreaProxyImpl::exitAcceleratedCompositingMode()
{
ASSERT(isInAcceleratedCompositingMode());
m_layerTreeContext = LayerTreeContext();
m_webPageProxy->exitAcceleratedCompositingMode();
}
#endif
void DrawingAreaProxyImpl::discardBackingStoreSoon()
{
if (!m_isBackingStoreDiscardable || m_discardBackingStoreTimer.isActive())
return;
static const double discardBackingStoreDelay = 2;
m_discardBackingStoreTimer.startOneShot(discardBackingStoreDelay);
}
void DrawingAreaProxyImpl::discardBackingStore()
{
m_backingStore = nullptr;
backingStoreStateDidChange(DoNotRespondImmediately);
}
}