This source file includes following definitions.
- WebCoreCompositeToSkiaComposite
- InvScaleByte
- SkPMColorToColor
- SkPathContainsPoint
- affineTransformToSkMatrix
#include "config.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "SkColorPriv.h"
#include "SkRegion.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/ImageBuffer.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 },
{ CompositePlusLighter, SkXfermode::kPlus_Mode }
};
static const uint8_t gMapBlendOpsToXfermodeModes[] = {
SkXfermode::kClear_Mode,
SkXfermode::kMultiply_Mode,
SkXfermode::kScreen_Mode,
SkXfermode::kOverlay_Mode,
SkXfermode::kDarken_Mode,
SkXfermode::kLighten_Mode,
SkXfermode::kColorDodge_Mode,
SkXfermode::kColorBurn_Mode,
SkXfermode::kHardLight_Mode,
SkXfermode::kSoftLight_Mode,
SkXfermode::kDifference_Mode,
SkXfermode::kExclusion_Mode,
SkXfermode::kHue_Mode,
SkXfermode::kSaturation_Mode,
SkXfermode::kColor_Mode,
SkXfermode::kLuminosity_Mode
};
PassRefPtr<SkXfermode> WebCoreCompositeToSkiaComposite(CompositeOperator op, blink::WebBlendMode blendMode)
{
if (blendMode != blink::WebBlendModeNormal) {
if ((uint8_t)blendMode >= SK_ARRAY_COUNT(gMapBlendOpsToXfermodeModes)) {
SkDEBUGF(("GraphicsContext::setPlatformCompositeOperation unknown blink::WebBlendMode %d\n", blendMode));
return adoptRef(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
}
SkXfermode::Mode mode = (SkXfermode::Mode)gMapBlendOpsToXfermodeModes[(uint8_t)blendMode];
return adoptRef(SkXfermode::Create(mode));
}
const CompositOpToXfermodeMode* table = gMapCompositOpsToXfermodeModes;
for (unsigned i = 0; i < SK_ARRAY_COUNT(gMapCompositOpsToXfermodeModes); i++) {
if (table[i].mCompositOp == op)
return adoptRef(SkXfermode::Create((SkXfermode::Mode)table[i].m_xfermodeMode));
}
SkDEBUGF(("GraphicsContext::setPlatformCompositeOperation unknown CompositeOperator %d\n", op));
return adoptRef(SkXfermode::Create(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 (!pm)
return 0;
unsigned a = SkGetPackedA32(pm);
if (!a) {
SkASSERT(false);
return 0;
}
uint32_t scale = (255 << 16) / a;
return SkColorSetARGB(a,
InvScaleByte(SkGetPackedR32(pm), scale),
InvScaleByte(SkGetPackedG32(pm), scale),
InvScaleByte(SkGetPackedB32(pm), scale));
}
bool SkPathContainsPoint(const SkPath& originalPath, const FloatPoint& point, SkPath::FillType ft)
{
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;
SkScalar biggestCoord = std::max(std::max(std::max(bounds.fRight, bounds.fBottom), -bounds.fLeft), -bounds.fTop);
if (SkScalarNearlyZero(biggestCoord))
return false;
biggestCoord = std::max(std::max(biggestCoord, fX + 1), fY + 1);
const SkScalar kMaxCoordinate = SkIntToScalar(1 << 15);
SkScalar scale = SkScalarDiv(kMaxCoordinate, biggestCoord);
SkRegion rgn;
SkRegion clip;
SkMatrix m;
SkPath scaledPath(originalPath);
scaledPath.setFillType(ft);
m.setScale(scale, scale);
scaledPath.transform(m, 0);
int x = static_cast<int>(floorf(0.5f + point.x() * scale));
int y = static_cast<int>(floorf(0.5f + point.y() * scale));
clip.setRect(x - 1, y - 1, x + 1, y + 1);
return rgn.setPath(scaledPath, clip);
}
SkMatrix affineTransformToSkMatrix(const AffineTransform& source)
{
SkMatrix result;
result.setScaleX(WebCoreDoubleToSkScalar(source.a()));
result.setSkewX(WebCoreDoubleToSkScalar(source.c()));
result.setTranslateX(WebCoreDoubleToSkScalar(source.e()));
result.setScaleY(WebCoreDoubleToSkScalar(source.d()));
result.setSkewY(WebCoreDoubleToSkScalar(source.b()));
result.setTranslateY(WebCoreDoubleToSkScalar(source.f()));
result.setPerspX(0);
result.setPerspY(0);
result.set(SkMatrix::kMPersp2, SK_Scalar1);
return result;
}
}