ContextShadowQt.cpp [plain text]
#include "config.h"
#include "ContextShadow.h"
#include "AffineTransform.h"
#include "GraphicsContext.h"
#include <QPainter>
#include <QTimerEvent>
namespace WebCore {
class ShadowBuffer: public QObject {
public:
ShadowBuffer(QObject* parent = 0);
QImage* scratchImage(const QSize& size);
void schedulePurge();
protected:
void timerEvent(QTimerEvent* event);
private:
QImage image;
int timerId;
};
ShadowBuffer::ShadowBuffer(QObject* parent)
: QObject(parent)
, timerId(-1)
{
}
QImage* ShadowBuffer::scratchImage(const QSize& size)
{
int width = size.width();
int height = size.height();
if (!image.isNull() && (image.width() > width) && (image.height() > height))
if (((2 * width) > image.width()) && ((2 * height) > image.height())) {
image.fill(0);
return ℑ
}
width = (1 + (width >> 5)) << 5;
height = (1 + (height >> 5)) << 5;
image = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
image.fill(0);
return ℑ
}
void ShadowBuffer::schedulePurge()
{
static const double BufferPurgeDelay = 2; if (timerId >= 0)
killTimer(timerId);
timerId = startTimer(BufferPurgeDelay * 1000);
}
void ShadowBuffer::timerEvent(QTimerEvent* event)
{
if (event->timerId() == timerId) {
killTimer(timerId);
image = QImage();
}
QObject::timerEvent(event);
}
Q_GLOBAL_STATIC(ShadowBuffer, scratchShadowBuffer)
PlatformContext ContextShadow::beginShadowLayer(GraphicsContext* context, const FloatRect& layerArea)
{
adjustBlurDistance(context);
PlatformContext p = context->platformContext();
QRect clipRect;
if (p->hasClipping())
#if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
clipRect = p->clipBoundingRect().toAlignedRect();
#else
clipRect = p->clipRegion().boundingRect();
#endif
else
clipRect = p->transform().inverted().mapRect(p->window());
IntRect clip(clipRect.x(), clipRect.y(), clipRect.width(), clipRect.height());
IntRect layerRect = calculateLayerBoundingRect(context, layerArea, clip);
if (layerRect.isEmpty())
return 0;
ShadowBuffer* shadowBuffer = scratchShadowBuffer();
QImage* shadowImage = shadowBuffer->scratchImage(layerRect.size());
m_layerImage = QImage(*shadowImage);
m_layerContext = new QPainter;
m_layerContext->begin(&m_layerImage);
m_layerContext->setFont(p->font());
m_layerContext->translate(m_layerContextTranslation);
return m_layerContext;
}
void ContextShadow::endShadowLayer(GraphicsContext* context)
{
m_layerContext->end();
delete m_layerContext;
m_layerContext = 0;
if (m_type == BlurShadow) {
blurLayerImage(m_layerImage.bits(), IntSize(m_layerImage.width(), m_layerImage.height()),
m_layerImage.bytesPerLine());
}
if (m_type != NoShadow) {
QPainter p(&m_layerImage);
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
p.fillRect(m_layerImage.rect(), m_color.rgb());
p.end();
}
context->platformContext()->drawImage(m_layerOrigin, m_layerImage, m_sourceRect);
scratchShadowBuffer()->schedulePurge();
}
}