#include "config.h"
#include "SkiaUtils.h"
#include "ImageBuffer.h"
#include "SharedBuffer.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkMatrix.h"
#include "SkRegion.h"
namespace WebCore {
static const struct CompositOpToXfermodeMode {
uint8_t mCompositOp;
uint8_t m_xfermodeMode;
} gMapCompositOpsToXfermodeModes[] = {
{ CompositeClear, SkXfermode::kClear_Mode },
{ CompositeCopy, SkXfermode::kSrc_Mode },
{ CompositeSourceOver, SkXfermode::kSrcOver_Mode },
{ CompositeSourceIn, SkXfermode::kSrcIn_Mode },
{ CompositeSourceOut, SkXfermode::kSrcOut_Mode },
{ CompositeSourceAtop, SkXfermode::kSrcATop_Mode },
{ CompositeDestinationOver, SkXfermode::kDstOver_Mode },
{ CompositeDestinationIn, SkXfermode::kDstIn_Mode },
{ CompositeDestinationOut, SkXfermode::kDstOut_Mode },
{ CompositeDestinationAtop, SkXfermode::kDstATop_Mode },
{ CompositeXOR, SkXfermode::kXor_Mode },
{ CompositePlusDarker, SkXfermode::kDarken_Mode },
{ CompositeHighlight, SkXfermode::kSrcOver_Mode }, { CompositePlusLighter, SkXfermode::kPlus_Mode }
};
SkXfermode::Mode WebCoreCompositeToSkiaComposite(CompositeOperator op)
{
const CompositOpToXfermodeMode* table = gMapCompositOpsToXfermodeModes;
for (unsigned i = 0; i < SK_ARRAY_COUNT(gMapCompositOpsToXfermodeModes); i++) {
if (table[i].mCompositOp == op)
return (SkXfermode::Mode)table[i].m_xfermodeMode;
}
SkDEBUGF(("GraphicsContext::setCompositeOperation uknown CompositeOperator %d\n", op));
return SkXfermode::kSrcOver_Mode; }
static U8CPU InvScaleByte(U8CPU component, uint32_t scale)
{
SkASSERT(component == (uint8_t)component);
return (component * scale + 0x8000) >> 16;
}
SkColor SkPMColorToColor(SkPMColor pm)
{
if (0 == pm)
return 0;
unsigned a = SkGetPackedA32(pm);
uint32_t scale = (255 << 16) / a;
return SkColorSetARGB(a,
InvScaleByte(SkGetPackedR32(pm), scale),
InvScaleByte(SkGetPackedG32(pm), scale),
InvScaleByte(SkGetPackedB32(pm), scale));
}
Color SkPMColorToWebCoreColor(SkPMColor pm)
{
return SkPMColorToColor(pm);
}
void IntersectRectAndRegion(const SkRegion& region, const SkRect& srcRect, SkRect* destRect) {
SkIRect srcRectRounded;
srcRect.roundOut(&srcRectRounded);
SkRegion::Cliperator cliperator(region, srcRectRounded);
if (cliperator.done()) {
destRect->setEmpty();
return;
}
SkIRect currentVisibleRect = cliperator.rect();
cliperator.next();
while (!cliperator.done()) {
currentVisibleRect.join(cliperator.rect());
cliperator.next();
}
destRect->set(currentVisibleRect);
}
void ClipRectToCanvas(const SkCanvas& canvas, const SkRect& srcRect, SkRect* destRect) {
SkRect transformedSrc;
canvas.getTotalMatrix().mapRect(&transformedSrc, srcRect);
SkRect transformedDest;
IntersectRectAndRegion(canvas.getTotalClip(), transformedSrc, &transformedDest);
SkMatrix inverseTransform;
canvas.getTotalMatrix().invert(&inverseTransform);
inverseTransform.mapRect(destRect, transformedDest);
}
bool SkPathContainsPoint(SkPath* originalPath, const FloatPoint& point, SkPath::FillType ft)
{
SkRegion rgn;
SkRegion clip;
SkPath::FillType originalFillType = originalPath->getFillType();
const SkPath* path = originalPath;
SkPath scaledPath;
int scale = 1;
SkRect bounds = originalPath->getBounds();
SkScalar fX = SkFloatToScalar(point.x());
SkScalar fY = SkFloatToScalar(point.y());
if (fX < bounds.fLeft || fX > bounds.fRight || fY < bounds.fTop || fY > bounds.fBottom)
return false;
originalPath->setFillType(ft);
const SkScalar kMaxCoordinate = SkIntToScalar(1<<15);
SkScalar biggestCoord = std::max(std::max(std::max(bounds.fRight, bounds.fBottom), -bounds.fLeft), -bounds.fTop);
if (biggestCoord > kMaxCoordinate) {
scale = SkScalarCeil(SkScalarDiv(biggestCoord, kMaxCoordinate));
SkMatrix m;
m.setScale(SkScalarInvert(SkIntToScalar(scale)), SkScalarInvert(SkIntToScalar(scale)));
originalPath->transform(m, &scaledPath);
path = &scaledPath;
}
int x = static_cast<int>(floorf(point.x() / scale));
int y = static_cast<int>(floorf(point.y() / scale));
clip.setRect(x - 1, y - 1, x + 1, y + 1);
bool contains = rgn.setPath(*path, clip);
originalPath->setFillType(originalFillType);
return contains;
}
GraphicsContext* scratchContext()
{
static ImageBuffer* scratch = 0;
if (!scratch)
scratch = ImageBuffer::create(IntSize(1, 1)).release();
return scratch->context();
}
}