#include "config.h"
#include "AffineTransform.h"
#include "BitmapImage.h"
#include "BitmapImageSingleFrameSkia.h"
#include "FloatConversion.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageObserver.h"
#include "Logging.h"
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#include "PlatformString.h"
#include "SkPixelRef.h"
#include "SkRect.h"
#include "SkShader.h"
#include "SkiaUtils.h"
#include "Texture.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/platform_canvas.h"
#if PLATFORM(CHROMIUM)
#include "TraceEvent.h"
#endif
namespace WebCore {
enum ResamplingMode {
RESAMPLE_NONE,
RESAMPLE_LINEAR,
RESAMPLE_AWESOME,
};
static ResamplingMode computeResamplingMode(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, int srcWidth, int srcHeight, float destWidth, float destHeight)
{
if (platformContext->hasImageResamplingHint()) {
IntSize srcSize;
FloatSize dstSize;
platformContext->getImageResamplingHint(&srcSize, &dstSize);
srcWidth = srcSize.width();
srcHeight = srcSize.height();
destWidth = dstSize.width();
destHeight = dstSize.height();
}
int destIWidth = static_cast<int>(destWidth);
int destIHeight = static_cast<int>(destHeight);
const float kFractionalChangeThreshold = 0.025f;
const int kSmallImageSizeThreshold = 8;
const float kLargeStretch = 3.0f;
if (srcWidth == destIWidth && srcHeight == destIHeight) {
return RESAMPLE_NONE;
}
if (srcWidth <= kSmallImageSizeThreshold
|| srcHeight <= kSmallImageSizeThreshold
|| destWidth <= kSmallImageSizeThreshold
|| destHeight <= kSmallImageSizeThreshold) {
return RESAMPLE_NONE;
}
if (srcHeight * kLargeStretch <= destHeight || srcWidth * kLargeStretch <= destWidth) {
if (srcWidth == destWidth || srcHeight == destHeight)
return RESAMPLE_NONE;
return RESAMPLE_LINEAR;
}
if ((fabs(destWidth - srcWidth) / srcWidth < kFractionalChangeThreshold)
&& (fabs(destHeight - srcHeight) / srcHeight < kFractionalChangeThreshold)) {
return RESAMPLE_NONE;
}
if (!bitmap.isDataComplete())
return RESAMPLE_LINEAR;
if (platformContext->interpolationQuality() == InterpolationHigh
&& !(platformContext->canvas()->getTotalMatrix().getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)))
return RESAMPLE_AWESOME;
return RESAMPLE_LINEAR;
}
static void drawResampledBitmap(SkCanvas& canvas, SkPaint& paint, const NativeImageSkia& bitmap, const SkIRect& srcIRect, const SkRect& destRect)
{
#if PLATFORM(CHROMIUM)
TRACE_EVENT("drawResampledBitmap", &canvas, 0);
#endif
SkRect destRectTransformed;
canvas.getTotalMatrix().mapRect(&destRectTransformed, destRect);
SkIRect destRectTransformedRounded;
destRectTransformed.round(&destRectTransformedRounded);
SkRect destRectVisibleSubset;
ClipRectToCanvas(canvas, destRect, &destRectVisibleSubset);
if (!destRectVisibleSubset.intersect(destRect))
return;
SkRect destRectVisibleSubsetTransformed;
canvas.getTotalMatrix().mapRect(&destRectVisibleSubsetTransformed, destRectVisibleSubset);
SkRect destBitmapSubsetTransformed = destRectVisibleSubsetTransformed;
destBitmapSubsetTransformed.offset(-destRectTransformed.fLeft,
-destRectTransformed.fTop);
SkIRect destBitmapSubsetTransformedRounded;
destBitmapSubsetTransformed.round(&destBitmapSubsetTransformedRounded);
if (!destBitmapSubsetTransformedRounded.intersect(
0, 0, destRectTransformedRounded.width(), destRectTransformedRounded.height()))
return;
SkBitmap resampled = bitmap.resizedBitmap(srcIRect,
destRectTransformedRounded.width(),
destRectTransformedRounded.height(),
destBitmapSubsetTransformedRounded);
canvas.drawBitmapRect(resampled, 0, destRectVisibleSubset, &paint);
}
static bool hasNon90rotation(PlatformContextSkia* context)
{
return !context->canvas()->getTotalMatrix().rectStaysRect();
}
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkIRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp)
{
#if PLATFORM(CHROMIUM)
TRACE_EVENT("paintSkBitmap", platformContext, 0);
#endif
SkPaint paint;
paint.setXfermodeMode(compOp);
paint.setFilterBitmap(true);
paint.setAlpha(platformContext->getNormalizedAlpha());
paint.setLooper(platformContext->getDrawLooper());
paint.setAntiAlias(hasNon90rotation(platformContext));
SkCanvas* canvas = platformContext->canvas();
ResamplingMode resampling;
if (platformContext->isAccelerated())
resampling = RESAMPLE_LINEAR;
else
resampling = platformContext->printing() ? RESAMPLE_NONE :
computeResamplingMode(platformContext, bitmap, srcRect.width(), srcRect.height(), SkScalarToFloat(destRect.width()), SkScalarToFloat(destRect.height()));
if (resampling == RESAMPLE_AWESOME) {
drawResampledBitmap(*canvas, paint, bitmap, srcRect, destRect);
} else {
canvas->drawBitmapRect(bitmap.bitmap(), &srcRect, destRect, &paint);
}
platformContext->didDrawRect(destRect, paint, &bitmap.bitmap());
}
static void TransformDimensions(const SkMatrix& matrix, float srcWidth, float srcHeight, float* destWidth, float* destHeight) {
SkPoint src_points[3]; src_points[0].set(0, 0);
src_points[1].set(SkFloatToScalar(srcWidth), 0);
src_points[2].set(0, SkFloatToScalar(srcHeight));
SkPoint dest_points[3];
matrix.mapPoints(dest_points, src_points, 3);
*destWidth = SkScalarToFloat((dest_points[1] - dest_points[0]).length());
*destHeight = SkScalarToFloat((dest_points[2] - dest_points[0]).length());
}
FloatRect normalizeRect(const FloatRect& rect)
{
FloatRect norm = rect;
if (norm.width() < 0) {
norm.setX(norm.x() + norm.width());
norm.setWidth(-norm.width());
}
if (norm.height() < 0) {
norm.setY(norm.y() + norm.height());
norm.setHeight(-norm.height());
}
return norm;
}
bool FrameData::clear(bool clearMetadata)
{
if (clearMetadata)
m_haveMetadata = false;
if (m_frame) {
delete m_frame;
m_frame = 0;
return true;
}
return false;
}
void Image::drawPattern(GraphicsContext* context,
const FloatRect& floatSrcRect,
const AffineTransform& patternTransform,
const FloatPoint& phase,
ColorSpace styleColorSpace,
CompositeOperator compositeOp,
const FloatRect& destRect)
{
#if PLATFORM(CHROMIUM)
TRACE_EVENT("Image::drawPattern", this, 0);
#endif
FloatRect normSrcRect = normalizeRect(floatSrcRect);
if (destRect.isEmpty() || normSrcRect.isEmpty())
return;
NativeImageSkia* bitmap = nativeImageForCurrentFrame();
if (!bitmap)
return;
SkIRect srcRect = enclosingIntRect(normSrcRect);
float destBitmapWidth, destBitmapHeight;
TransformDimensions(patternTransform, srcRect.width(), srcRect.height(),
&destBitmapWidth, &destBitmapHeight);
ResamplingMode resampling;
if (context->platformContext()->isAccelerated() || context->platformContext()->printing())
resampling = RESAMPLE_LINEAR;
else
resampling = computeResamplingMode(context->platformContext(), *bitmap, srcRect.width(), srcRect.height(), destBitmapWidth, destBitmapHeight);
SkMatrix matrix(patternTransform);
SkShader* shader;
if (resampling == RESAMPLE_AWESOME) {
int width = static_cast<int>(destBitmapWidth);
int height = static_cast<int>(destBitmapHeight);
SkBitmap resampled = bitmap->resizedBitmap(srcRect, width, height);
shader = SkShader::CreateBitmapShader(resampled, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
matrix.setScaleX(SkIntToScalar(1));
matrix.setScaleY(SkIntToScalar(1));
} else {
SkBitmap srcSubset;
bitmap->bitmap().extractSubset(&srcSubset, srcRect);
shader = SkShader::CreateBitmapShader(srcSubset, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
}
float adjustedX = phase.x() + normSrcRect.x() *
narrowPrecisionToFloat(patternTransform.a());
float adjustedY = phase.y() + normSrcRect.y() *
narrowPrecisionToFloat(patternTransform.d());
matrix.postTranslate(SkFloatToScalar(adjustedX),
SkFloatToScalar(adjustedY));
shader->setLocalMatrix(matrix);
SkPaint paint;
paint.setShader(shader)->unref();
paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp));
paint.setFilterBitmap(resampling == RESAMPLE_LINEAR);
context->platformContext()->paintSkPaint(destRect, paint);
}
void BitmapImage::initPlatformData()
{
}
void BitmapImage::invalidatePlatformData()
{
}
void BitmapImage::checkForSolidColor()
{
m_isSolidColor = false;
m_checkedForSolidColor = true;
if (frameCount() > 1)
return;
WebCore::NativeImageSkia* frame = frameAtIndex(0);
if (frame && size().width() == 1 && size().height() == 1) {
SkAutoLockPixels lock(frame->bitmap());
if (!frame->bitmap().getPixels())
return;
m_isSolidColor = true;
m_solidColor = Color(frame->bitmap().getColor(0, 0));
}
}
void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect,
const FloatRect& srcRect, ColorSpace colorSpace, CompositeOperator compositeOp)
{
if (!m_source.initialized())
return;
startAnimation();
NativeImageSkia* bm = nativeImageForCurrentFrame();
if (!bm)
return;
FloatRect normDstRect = normalizeRect(dstRect);
FloatRect normSrcRect = normalizeRect(srcRect);
if (normSrcRect.isEmpty() || normDstRect.isEmpty())
return;
paintSkBitmap(ctxt->platformContext(),
*bm,
enclosingIntRect(normSrcRect),
normDstRect,
WebCoreCompositeToSkiaComposite(compositeOp));
if (ImageObserver* observer = imageObserver())
observer->didDraw(this);
}
void BitmapImageSingleFrameSkia::draw(GraphicsContext* ctxt,
const FloatRect& dstRect,
const FloatRect& srcRect,
ColorSpace styleColorSpace,
CompositeOperator compositeOp)
{
FloatRect normDstRect = normalizeRect(dstRect);
FloatRect normSrcRect = normalizeRect(srcRect);
if (normSrcRect.isEmpty() || normDstRect.isEmpty())
return;
paintSkBitmap(ctxt->platformContext(),
m_nativeImage,
enclosingIntRect(normSrcRect),
normDstRect,
WebCoreCompositeToSkiaComposite(compositeOp));
if (ImageObserver* observer = imageObserver())
observer->didDraw(this);
}
BitmapImageSingleFrameSkia::BitmapImageSingleFrameSkia(const SkBitmap& bitmap)
: m_nativeImage(bitmap)
{
}
PassRefPtr<BitmapImageSingleFrameSkia> BitmapImageSingleFrameSkia::create(const SkBitmap& bitmap, bool copyPixels)
{
if (copyPixels) {
SkBitmap temp;
if (!bitmap.deepCopyTo(&temp, bitmap.config()))
bitmap.copyTo(&temp, bitmap.config());
return adoptRef(new BitmapImageSingleFrameSkia(temp));
}
return adoptRef(new BitmapImageSingleFrameSkia(bitmap));
}
}