DrawingAreaProxyCoordinatedGraphics.cpp [plain text]
#include "config.h"
#include "DrawingAreaProxyCoordinatedGraphics.h"
#include "DrawingAreaMessages.h"
#include "DrawingAreaProxyMessages.h"
#include "LayerTreeContext.h"
#include "UpdateInfo.h"
#include "WebPageProxy.h"
#include "WebPreferences.h"
#include "WebProcessProxy.h"
#include <WebCore/PlatformDisplay.h>
#include <WebCore/Region.h>
#if PLATFORM(GTK)
#include <gtk/gtk.h>
#endif
#if USE(GLIB_EVENT_LOOP)
#include <wtf/glib/RunLoopSourcePriority.h>
#endif
#if USE(DIRECT2D)
#include <d2d1.h>
#include <d3d11_1.h>
#endif
namespace WebKit {
using namespace WebCore;
DrawingAreaProxyCoordinatedGraphics::DrawingAreaProxyCoordinatedGraphics(WebPageProxy& webPageProxy, WebProcessProxy& process)
: DrawingAreaProxy(DrawingAreaTypeCoordinatedGraphics, webPageProxy, process)
#if !PLATFORM(WPE)
, m_discardBackingStoreTimer(RunLoop::current(), this, &DrawingAreaProxyCoordinatedGraphics::discardBackingStore)
#endif
{
#if USE(GLIB_EVENT_LOOP) && !PLATFORM(WPE)
m_discardBackingStoreTimer.setPriority(RunLoopSourcePriority::ReleaseUnusedResourcesTimer);
#endif
}
DrawingAreaProxyCoordinatedGraphics::~DrawingAreaProxyCoordinatedGraphics()
{
if (isInAcceleratedCompositingMode())
exitAcceleratedCompositingMode();
}
#if !PLATFORM(WPE)
void DrawingAreaProxyCoordinatedGraphics::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();
}
#endif
void DrawingAreaProxyCoordinatedGraphics::sizeDidChange()
{
#if USE(DIRECT2D)
m_backingStore = nullptr;
#endif
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyCoordinatedGraphics::deviceScaleFactorDidChange()
{
#if USE(DIRECT2D)
m_backingStore = nullptr;
#endif
backingStoreStateDidChange(RespondImmediately);
}
void DrawingAreaProxyCoordinatedGraphics::waitForBackingStoreUpdateOnNextPaint()
{
m_hasReceivedFirstUpdate = true;
}
void DrawingAreaProxyCoordinatedGraphics::setBackingStoreIsDiscardable(bool isBackingStoreDiscardable)
{
#if !PLATFORM(WPE)
if (m_isBackingStoreDiscardable == isBackingStoreDiscardable)
return;
m_isBackingStoreDiscardable = isBackingStoreDiscardable;
if (m_isBackingStoreDiscardable)
discardBackingStoreSoon();
else
m_discardBackingStoreTimer.stop();
#endif
}
void DrawingAreaProxyCoordinatedGraphics::update(uint64_t backingStoreStateID, const UpdateInfo& updateInfo)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
#if !PLATFORM(WPE)
incorporateUpdate(updateInfo);
#endif
send(Messages::DrawingArea::DidUpdate());
}
void DrawingAreaProxyCoordinatedGraphics::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;
process().stopResponsivenessTimer();
if (layerTreeContext != m_layerTreeContext) {
if (layerTreeContext.isEmpty() && !m_layerTreeContext.isEmpty()) {
exitAcceleratedCompositingMode();
ASSERT(m_layerTreeContext.isEmpty());
} else if (!layerTreeContext.isEmpty() && m_layerTreeContext.isEmpty()) {
enterAcceleratedCompositingMode(layerTreeContext);
ASSERT(layerTreeContext == m_layerTreeContext);
} else {
updateAcceleratedCompositingMode(layerTreeContext);
ASSERT(layerTreeContext == m_layerTreeContext);
}
}
if (m_nextBackingStoreStateID != m_currentBackingStoreStateID)
sendUpdateBackingStoreState(RespondImmediately);
else
m_hasReceivedFirstUpdate = true;
#if !PLATFORM(WPE)
if (isInAcceleratedCompositingMode()) {
ASSERT(!m_backingStore);
return;
}
if (m_backingStore && (m_backingStore->size() != updateInfo.viewSize || m_backingStore->deviceScaleFactor() != updateInfo.deviceScaleFactor))
m_backingStore = nullptr;
incorporateUpdate(updateInfo);
#endif
}
void DrawingAreaProxyCoordinatedGraphics::enterAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
enterAcceleratedCompositingMode(layerTreeContext);
}
void DrawingAreaProxyCoordinatedGraphics::exitAcceleratedCompositingMode(uint64_t backingStoreStateID, const UpdateInfo& updateInfo)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
exitAcceleratedCompositingMode();
#if !PLATFORM(WPE)
incorporateUpdate(updateInfo);
#endif
}
void DrawingAreaProxyCoordinatedGraphics::updateAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
{
ASSERT_ARG(backingStoreStateID, backingStoreStateID <= m_currentBackingStoreStateID);
if (backingStoreStateID < m_currentBackingStoreStateID)
return;
updateAcceleratedCompositingMode(layerTreeContext);
}
#if !PLATFORM(WPE)
void DrawingAreaProxyCoordinatedGraphics::incorporateUpdate(const UpdateInfo& updateInfo)
{
ASSERT(!isInAcceleratedCompositingMode());
if (updateInfo.updateRectBounds.isEmpty())
return;
if (!m_backingStore)
m_backingStore = makeUnique<BackingStore>(updateInfo.viewSize, updateInfo.deviceScaleFactor, m_webPageProxy);
m_backingStore->incorporateUpdate(updateInfo);
Region damageRegion;
if (updateInfo.scrollRect.isEmpty()) {
for (const auto& rect : updateInfo.updateRects)
damageRegion.unite(rect);
} else
damageRegion = IntRect(IntPoint(), m_webPageProxy.viewSize());
m_webPageProxy.setViewNeedsDisplay(damageRegion);
}
#endif
bool DrawingAreaProxyCoordinatedGraphics::alwaysUseCompositing() const
{
return m_webPageProxy.preferences().acceleratedCompositingEnabled() && m_webPageProxy.preferences().forceCompositingMode();
}
void DrawingAreaProxyCoordinatedGraphics::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(!isInAcceleratedCompositingMode());
#if !PLATFORM(WPE)
m_backingStore = nullptr;
#endif
m_layerTreeContext = layerTreeContext;
m_webPageProxy.enterAcceleratedCompositingMode(layerTreeContext);
}
void DrawingAreaProxyCoordinatedGraphics::exitAcceleratedCompositingMode()
{
ASSERT(isInAcceleratedCompositingMode());
m_layerTreeContext = { };
m_webPageProxy.exitAcceleratedCompositingMode();
}
void DrawingAreaProxyCoordinatedGraphics::updateAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(isInAcceleratedCompositingMode());
m_layerTreeContext = layerTreeContext;
m_webPageProxy.updateAcceleratedCompositingMode(layerTreeContext);
}
void DrawingAreaProxyCoordinatedGraphics::backingStoreStateDidChange(RespondImmediatelyOrNot respondImmediatelyOrNot)
{
++m_nextBackingStoreStateID;
sendUpdateBackingStoreState(respondImmediatelyOrNot);
}
void DrawingAreaProxyCoordinatedGraphics::sendUpdateBackingStoreState(RespondImmediatelyOrNot respondImmediatelyOrNot)
{
ASSERT(m_currentBackingStoreStateID < m_nextBackingStoreStateID);
if (!m_webPageProxy.hasRunningProcess())
return;
if (m_isWaitingForDidUpdateBackingStoreState)
return;
if (m_webPageProxy.viewSize().isEmpty() && !m_webPageProxy.useFixedLayout())
return;
m_isWaitingForDidUpdateBackingStoreState = respondImmediatelyOrNot == RespondImmediately;
send(Messages::DrawingArea::UpdateBackingStoreState(m_nextBackingStoreStateID, respondImmediatelyOrNot == RespondImmediately, m_webPageProxy.deviceScaleFactor(), m_size, m_scrollOffset));
m_scrollOffset = IntSize();
if (m_isWaitingForDidUpdateBackingStoreState) {
process().startResponsivenessTimer();
}
if (m_isWaitingForDidUpdateBackingStoreState && !m_layerTreeContext.isEmpty()) {
waitForAndDispatchDidUpdateBackingStoreState();
}
}
void DrawingAreaProxyCoordinatedGraphics::waitForAndDispatchDidUpdateBackingStoreState()
{
ASSERT(m_isWaitingForDidUpdateBackingStoreState);
if (!m_webPageProxy.hasRunningProcess())
return;
if (process().state() == WebProcessProxy::State::Launching)
return;
if (!m_webPageProxy.isViewVisible())
return;
#if PLATFORM(WAYLAND) && USE(EGL)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland && isInAcceleratedCompositingMode())
return;
#endif
process().connection()->waitForAndDispatchImmediately<Messages::DrawingAreaProxy::DidUpdateBackingStoreState>(m_identifier.toUInt64(), Seconds::fromMilliseconds(500));
}
#if !PLATFORM(WPE)
void DrawingAreaProxyCoordinatedGraphics::discardBackingStoreSoon()
{
if (!m_backingStore || !m_isBackingStoreDiscardable || m_discardBackingStoreTimer.isActive())
return;
static const Seconds discardBackingStoreDelay = 2_s;
m_discardBackingStoreTimer.startOneShot(discardBackingStoreDelay);
}
void DrawingAreaProxyCoordinatedGraphics::discardBackingStore()
{
if (!m_backingStore)
return;
m_backingStore = nullptr;
backingStoreStateDidChange(DoNotRespondImmediately);
}
#endif
DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::DrawingMonitor(WebPageProxy& webPage)
: m_timer(RunLoop::main(), this, &DrawingMonitor::stop)
#if PLATFORM(GTK)
, m_webPage(webPage)
#endif
{
#if USE(GLIB_EVENT_LOOP)
#if PLATFORM(GTK)
m_timer.setPriority(GDK_PRIORITY_REDRAW - 10);
#else
m_timer.setPriority(RunLoopSourcePriority::RunLoopDispatcher);
#endif
#endif
}
DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::~DrawingMonitor()
{
m_callback = nullptr;
stop();
}
int DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::webViewDrawCallback(DrawingAreaProxyCoordinatedGraphics::DrawingMonitor* monitor)
{
monitor->didDraw();
return false;
}
void DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::start(WTF::Function<void(CallbackBase::Error)>&& callback)
{
m_startTime = MonotonicTime::now();
m_callback = WTFMove(callback);
#if PLATFORM(GTK)
gtk_widget_queue_draw(m_webPage.viewWidget());
#if USE(GTK4)
m_timer.startOneShot(16_ms);
#else
g_signal_connect_swapped(m_webPage.viewWidget(), "draw", reinterpret_cast<GCallback>(webViewDrawCallback), this);
m_timer.startOneShot(100_ms);
#endif
#else
m_timer.startOneShot(0_s);
#endif
}
void DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::stop()
{
m_timer.stop();
#if PLATFORM(GTK) && !USE(GTK4)
g_signal_handlers_disconnect_by_func(m_webPage.viewWidget(), reinterpret_cast<gpointer>(webViewDrawCallback), this);
#endif
m_startTime = MonotonicTime();
if (m_callback) {
m_callback(CallbackBase::Error::None);
m_callback = nullptr;
}
}
void DrawingAreaProxyCoordinatedGraphics::DrawingMonitor::didDraw()
{
if (MonotonicTime::now() - m_startTime > 100_ms)
stop();
else
m_timer.startOneShot(16_ms);
}
void DrawingAreaProxyCoordinatedGraphics::dispatchAfterEnsuringDrawing(WTF::Function<void(CallbackBase::Error)>&& callbackFunction)
{
if (!m_webPageProxy.hasRunningProcess()) {
callbackFunction(CallbackBase::Error::OwnerWasInvalidated);
return;
}
if (!m_drawingMonitor)
m_drawingMonitor = makeUnique<DrawingAreaProxyCoordinatedGraphics::DrawingMonitor>(m_webPageProxy);
m_drawingMonitor->start(WTFMove(callbackFunction));
}
}