#include "config.h"
#include "BitmapImage.h"
#if PLATFORM(CAIRO)
#include "AffineTransform.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include <cairo.h>
#include <math.h>
namespace WebCore {
void FrameData::clear()
{
if (m_frame) {
cairo_surface_destroy(m_frame);
m_frame = 0;
m_duration = 0.;
m_hasAlpha = true;
}
}
void BitmapImage::draw(GraphicsContext* context, const FloatRect& dst, const FloatRect& src, CompositeOperator op)
{
cairo_t* cr = context->platformContext();
if (!m_source.initialized())
return;
cairo_surface_t* image = frameAtIndex(m_currentFrame);
if (!image) return;
IntSize selfSize = size();
FloatRect srcRect(src);
FloatRect dstRect(dst);
cairo_save(cr);
if (op == CompositeSourceOver && !frameHasAlphaAtIndex(m_currentFrame))
context->setCompositeOperation(CompositeCopy);
else
context->setCompositeOperation(op);
cairo_pattern_t* pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
float scaleX = srcRect.width() / dstRect.width();
float scaleY = srcRect.height() / dstRect.height();
cairo_matrix_t matrix = { scaleX, 0, 0 , scaleY, srcRect.x(), srcRect.y() };
cairo_pattern_set_matrix(pattern, &matrix);
cairo_translate(cr, dstRect.x(), dstRect.y());
cairo_set_source(cr, pattern);
cairo_rectangle(cr, 0, 0, dstRect.width(), dstRect.height());
cairo_fill(cr);
cairo_pattern_destroy(pattern);
cairo_restore(cr);
startAnimation();
}
void Image::drawPattern(GraphicsContext* context, const FloatRect& tileRect, const AffineTransform& patternTransform,
const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect)
{
cairo_surface_t* image = nativeImageForCurrentFrame();
if (!image) return;
cairo_t* cr = context->platformContext();
context->save();
cairo_pattern_t* pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
cairo_matrix_t pattern_matrix = cairo_matrix_t(patternTransform);
cairo_matrix_t phase_matrix = {1, 0, 0, 1, phase.x(), phase.y()};
cairo_matrix_t combined;
cairo_matrix_multiply(&combined, &pattern_matrix, &phase_matrix);
cairo_matrix_invert(&combined);
cairo_pattern_set_matrix(pattern, &combined);
context->setCompositeOperation(op);
cairo_set_source(cr, pattern);
cairo_rectangle(cr, destRect.x(), destRect.y(), destRect.width(), destRect.height());
cairo_fill(cr);
cairo_pattern_destroy(pattern);
context->restore();
}
void BitmapImage::checkForSolidColor()
{
m_isSolidColor = false;
}
}
#endif // PLATFORM(CAIRO)