ImageQualityController.cpp [plain text]
#include "config.h"
#include "ImageQualityController.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "Page.h"
#include "RenderBoxModelObject.h"
#include "RenderView.h"
namespace WebCore {
static const double cInterpolationCutoff = 800. * 800.;
static const Seconds lowQualityTimeThreshold { 500_ms };
ImageQualityController::ImageQualityController(const RenderView& renderView)
: m_renderView(renderView)
, m_timer(*this, &ImageQualityController::highQualityRepaintTimerFired)
{
}
void ImageQualityController::removeLayer(RenderBoxModelObject* object, LayerSizeMap* innerMap, const void* layer)
{
if (!innerMap)
return;
innerMap->remove(layer);
if (innerMap->isEmpty())
removeObject(object);
}
void ImageQualityController::set(RenderBoxModelObject* object, LayerSizeMap* innerMap, const void* layer, const LayoutSize& size)
{
if (innerMap)
innerMap->set(layer, size);
else {
LayerSizeMap newInnerMap;
newInnerMap.set(layer, size);
m_objectLayerSizeMap.set(object, newInnerMap);
}
}
void ImageQualityController::removeObject(RenderBoxModelObject* object)
{
m_objectLayerSizeMap.remove(object);
if (m_objectLayerSizeMap.isEmpty()) {
m_animatedResizeIsActive = false;
m_timer.stop();
}
}
void ImageQualityController::highQualityRepaintTimerFired()
{
if (m_renderView.renderTreeBeingDestroyed())
return;
if (!m_animatedResizeIsActive && !m_liveResizeOptimizationIsActive)
return;
m_animatedResizeIsActive = false;
if (m_renderView.frameView().inLiveResize()) {
restartTimer();
return;
}
for (auto it = m_objectLayerSizeMap.begin(), end = m_objectLayerSizeMap.end(); it != end; ++it)
it->key->repaint();
m_liveResizeOptimizationIsActive = false;
}
void ImageQualityController::restartTimer()
{
m_timer.startOneShot(lowQualityTimeThreshold);
}
std::optional<InterpolationQuality> ImageQualityController::interpolationQualityFromStyle(const RenderStyle& style)
{
switch (style.imageRendering()) {
case ImageRenderingOptimizeSpeed:
return InterpolationLow;
case ImageRenderingCrispEdges:
case ImageRenderingPixelated:
return InterpolationNone;
case ImageRenderingOptimizeQuality:
return InterpolationDefault; case ImageRenderingAuto:
break;
}
return std::nullopt;
}
InterpolationQuality ImageQualityController::chooseInterpolationQuality(GraphicsContext& context, RenderBoxModelObject* object, Image& image, const void* layer, const LayoutSize& size)
{
if (!(image.isBitmapImage() || image.isPDFDocumentImage()) || context.paintingDisabled())
return InterpolationDefault;
if (std::optional<InterpolationQuality> styleInterpolation = interpolationQualityFromStyle(object->style()))
return styleInterpolation.value();
IntSize imageSize(image.width(), image.height());
auto i = m_objectLayerSizeMap.find(object);
LayerSizeMap* innerMap = i != m_objectLayerSizeMap.end() ? &i->value : 0;
LayoutSize oldSize;
bool isFirstResize = true;
if (innerMap) {
LayerSizeMap::iterator j = innerMap->find(layer);
if (j != innerMap->end()) {
isFirstResize = false;
oldSize = j->value;
}
}
if (Frame* frame = object->document().frame()) {
bool frameViewIsCurrentlyInLiveResize = frame->view() && frame->view()->inLiveResize();
if (frameViewIsCurrentlyInLiveResize) {
set(object, innerMap, layer, size);
restartTimer();
m_liveResizeOptimizationIsActive = true;
return InterpolationLow;
}
if (m_liveResizeOptimizationIsActive)
return InterpolationDefault;
}
const AffineTransform& currentTransform = context.getCTM();
bool contextIsScaled = !currentTransform.isIdentityOrTranslationOrFlipped();
if (!contextIsScaled && size == imageSize) {
removeLayer(object, innerMap, layer);
return InterpolationDefault;
}
if (m_renderView.page().inLowQualityImageInterpolationMode()) {
double totalPixels = static_cast<double>(image.width()) * static_cast<double>(image.height());
if (totalPixels > cInterpolationCutoff)
return InterpolationLow;
}
if (m_animatedResizeIsActive) {
set(object, innerMap, layer, size);
restartTimer();
return InterpolationLow;
}
if (isFirstResize || oldSize == size) {
restartTimer();
set(object, innerMap, layer, size);
return InterpolationDefault;
}
if (!m_timer.isActive()) {
removeLayer(object, innerMap, layer);
return InterpolationDefault;
}
set(object, innerMap, layer, size);
m_animatedResizeIsActive = true;
restartTimer();
return InterpolationLow;
}
}