Bug 1054838, part 3 - Convert gfxPattern to Moz2D. r=Bas

This commit is contained in:
Matt Woodrow 2014-09-14 10:58:56 +01:00
parent f46ede4015
commit 11f07a34b1
3 changed files with 117 additions and 234 deletions

View File

@ -120,6 +120,21 @@ inline ExtendMode ToExtendMode(gfxPattern::GraphicsExtend aExtend)
} }
} }
inline gfxPattern::GraphicsPatternType
ThebesPatternType(PatternType aType)
{
switch (aType) {
case PatternType::SURFACE:
return gfxPattern::PATTERN_SURFACE;
case PatternType::LINEAR_GRADIENT:
return gfxPattern::PATTERN_LINEAR;
case PatternType::RADIAL_GRADIENT:
return gfxPattern::PATTERN_RADIAL;
default:
return gfxPattern::PATTERN_SOLID;
}
}
inline gfxPattern::GraphicsExtend ThebesExtend(ExtendMode aExtend) inline gfxPattern::GraphicsExtend ThebesExtend(ExtendMode aExtend)
{ {
switch (aExtend) { switch (aExtend) {

View File

@ -9,6 +9,7 @@
#include "gfxPlatform.h" #include "gfxPlatform.h"
#include "gfx2DGlue.h" #include "gfx2DGlue.h"
#include "gfxGradientCache.h" #include "gfxGradientCache.h"
#include "mozilla/gfx/2D.h"
#include "cairo.h" #include "cairo.h"
@ -17,66 +18,72 @@
using namespace mozilla::gfx; using namespace mozilla::gfx;
gfxPattern::gfxPattern(const gfxRGBA& aColor) gfxPattern::gfxPattern(const gfxRGBA& aColor)
: mGfxPattern(nullptr) : mExtend(EXTEND_NONE)
{ {
mPattern = cairo_pattern_create_rgba(aColor.r, aColor.g, aColor.b, aColor.a); mGfxPattern =
new (mColorPattern.addr()) ColorPattern(Color(aColor.r, aColor.g, aColor.b, aColor.a));
} }
// linear // linear
gfxPattern::gfxPattern(gfxFloat x0, gfxFloat y0, gfxFloat x1, gfxFloat y1) gfxPattern::gfxPattern(gfxFloat x0, gfxFloat y0, gfxFloat x1, gfxFloat y1)
: mGfxPattern(nullptr) : mExtend(EXTEND_NONE)
{ {
mPattern = cairo_pattern_create_linear(x0, y0, x1, y1); mGfxPattern =
new (mLinearGradientPattern.addr()) LinearGradientPattern(Point(x0, y0),
Point(x1, y1),
nullptr);
} }
// radial // radial
gfxPattern::gfxPattern(gfxFloat cx0, gfxFloat cy0, gfxFloat radius0, gfxPattern::gfxPattern(gfxFloat cx0, gfxFloat cy0, gfxFloat radius0,
gfxFloat cx1, gfxFloat cy1, gfxFloat radius1) gfxFloat cx1, gfxFloat cy1, gfxFloat radius1)
: mGfxPattern(nullptr) : mExtend(EXTEND_NONE)
{ {
mPattern = cairo_pattern_create_radial(cx0, cy0, radius0, mGfxPattern =
cx1, cy1, radius1); new (mRadialGradientPattern.addr()) RadialGradientPattern(Point(cx0, cy0),
Point(cx1, cy1),
radius0, radius1,
nullptr);
} }
// Azure // Azure
gfxPattern::gfxPattern(SourceSurface *aSurface, const Matrix &aTransform) gfxPattern::gfxPattern(SourceSurface *aSurface, const Matrix &aTransform)
: mPattern(nullptr) : mTransform(aTransform)
, mGfxPattern(nullptr)
, mSourceSurface(aSurface)
, mTransform(aTransform)
, mExtend(EXTEND_NONE) , mExtend(EXTEND_NONE)
, mFilter(mozilla::gfx::Filter::GOOD)
{ {
mGfxPattern = new (mSurfacePattern.addr())
SurfacePattern(aSurface, ToExtendMode(mExtend), aTransform,
mozilla::gfx::Filter::GOOD);
} }
gfxPattern::~gfxPattern() gfxPattern::~gfxPattern()
{ {
cairo_pattern_destroy(mPattern); if (mGfxPattern) {
mGfxPattern->~Pattern();
if (mGfxPattern) { }
mGfxPattern->~Pattern();
}
} }
void void
gfxPattern::AddColorStop(gfxFloat offset, const gfxRGBA& c) gfxPattern::AddColorStop(gfxFloat offset, const gfxRGBA& c)
{ {
if (mPattern) { if (mGfxPattern->GetType() != PatternType::LINEAR_GRADIENT &&
mStops = nullptr; mGfxPattern->GetType() != PatternType::RADIAL_GRADIENT) {
if (gfxPlatform::GetCMSMode() == eCMSMode_All) { return;
gfxRGBA cms;
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
if (transform)
gfxPlatform::TransformPixel(c, cms, transform);
// Use the original alpha to avoid unnecessary float->byte->float
// conversion errors
cairo_pattern_add_color_stop_rgba(mPattern, offset,
cms.r, cms.g, cms.b, c.a);
}
else
cairo_pattern_add_color_stop_rgba(mPattern, offset, c.r, c.g, c.b, c.a);
} }
mStops = nullptr;
gfxRGBA color = c;
if (gfxPlatform::GetCMSMode() == eCMSMode_All) {
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
if (transform) {
gfxPlatform::TransformPixel(color, color, transform);
}
}
GradientStop stop;
stop.offset = offset;
stop.color = ToColor(color);
mStopsList.AppendElement(stop);
} }
void void
@ -88,210 +95,92 @@ gfxPattern::SetColorStops(GradientStops* aStops)
void void
gfxPattern::CacheColorStops(DrawTarget *aDT) gfxPattern::CacheColorStops(DrawTarget *aDT)
{ {
if (mPattern) { mStops = gfxGradientCache::GetOrCreateGradientStops(aDT, mStopsList,
mStops = nullptr; ToExtendMode(mExtend));
nsTArray<GradientStop> stops;
int count = 0;
cairo_pattern_get_color_stop_count(mPattern, &count);
stops.SetLength(count);
for (int n = 0; n < count; ++n) {
double offset, r, g, b, a;
cairo_pattern_get_color_stop_rgba(mPattern, n, &offset, &r, &g, &b, &a);
stops[n].color = Color(r, g, b, a);
stops[n].offset = offset;
}
GraphicsExtend extend = GraphicsExtend(cairo_pattern_get_extend(mPattern));
mStops = gfxGradientCache::GetOrCreateGradientStops(aDT, stops,
ToExtendMode(extend));
}
} }
void void
gfxPattern::SetMatrix(const gfxMatrix& matrix) gfxPattern::SetMatrix(const gfxMatrix& matrix)
{ {
if (mPattern) { mTransform = ToMatrix(matrix);
cairo_matrix_t mat = *reinterpret_cast<const cairo_matrix_t*>(&matrix); // Cairo-pattern matrices specify the conversion from DrawTarget to pattern
cairo_pattern_set_matrix(mPattern, &mat); // space. Azure pattern matrices specify the conversion from pattern to
} else { // DrawTarget space.
mTransform = ToMatrix(matrix); mTransform.Invert();
// Cairo-pattern matrices specify the conversion from DrawTarget to pattern
// space. Azure pattern matrices specify the conversion from pattern to
// DrawTarget space.
mTransform.Invert();
}
} }
gfxMatrix gfxMatrix
gfxPattern::GetMatrix() const gfxPattern::GetMatrix() const
{ {
if (mPattern) { // invert at the higher precision of gfxMatrix
cairo_matrix_t mat; // cause we need to convert at some point anyways
cairo_pattern_get_matrix(mPattern, &mat); gfxMatrix mat = ThebesMatrix(mTransform);
return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat)); mat.Invert();
} else { return mat;
// invert at the higher precision of gfxMatrix
// cause we need to convert at some point anyways
gfxMatrix mat = ThebesMatrix(mTransform);
mat.Invert();
return mat;
}
} }
gfxMatrix gfxMatrix
gfxPattern::GetInverseMatrix() const gfxPattern::GetInverseMatrix() const
{ {
if (mPattern) { return ThebesMatrix(mTransform);
cairo_matrix_t mat;
cairo_pattern_get_matrix(mPattern, &mat);
cairo_matrix_invert(&mat);
return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat));
} else {
return ThebesMatrix(mTransform);
}
} }
Pattern* Pattern*
gfxPattern::GetPattern(DrawTarget *aTarget, Matrix *aPatternTransform) gfxPattern::GetPattern(DrawTarget *aTarget, Matrix *aPatternTransform)
{ {
if (mGfxPattern) { if (!mStops &&
mGfxPattern->~Pattern(); !mStopsList.IsEmpty()) {
mGfxPattern = nullptr; mStops = aTarget->CreateGradientStops(mStopsList.Elements(),
mStopsList.Length(),
ToExtendMode(mExtend));
} }
if (!mPattern) { Matrix* matrix = nullptr;
Matrix adjustedMatrix = mTransform; switch (mGfxPattern->GetType()) {
if (aPatternTransform) case PatternType::SURFACE:
AdjustTransformForPattern(adjustedMatrix, aTarget->GetTransform(), aPatternTransform); matrix = &mSurfacePattern.addr()->mMatrix;
mGfxPattern = new (mSurfacePattern.addr()) mSurfacePattern.addr()->mExtendMode = ToExtendMode(mExtend);
SurfacePattern(mSourceSurface, ToExtendMode(mExtend), adjustedMatrix, mFilter); break;
return mGfxPattern; case PatternType::LINEAR_GRADIENT:
} matrix = &mLinearGradientPattern.addr()->mMatrix;
mLinearGradientPattern.addr()->mStops = mStops;
GraphicsExtend extend = GraphicsExtend(cairo_pattern_get_extend(mPattern)); break;
case PatternType::RADIAL_GRADIENT:
switch (cairo_pattern_get_type(mPattern)) { matrix = &mRadialGradientPattern.addr()->mMatrix;
case CAIRO_PATTERN_TYPE_SOLID: mRadialGradientPattern.addr()->mStops = mStops;
{ break;
double r, g, b, a;
cairo_pattern_get_rgba(mPattern, &r, &g, &b, &a);
new (mColorPattern.addr()) ColorPattern(Color(r, g, b, a));
return mColorPattern.addr();
}
case CAIRO_PATTERN_TYPE_LINEAR:
{
double x1, y1, x2, y2;
cairo_pattern_get_linear_points(mPattern, &x1, &y1, &x2, &y2);
if (!mStops) {
int count = 0;
cairo_pattern_get_color_stop_count(mPattern, &count);
std::vector<GradientStop> stops;
for (int i = 0; i < count; i++) {
GradientStop stop;
double r, g, b, a, offset;
cairo_pattern_get_color_stop_rgba(mPattern, i, &offset, &r, &g, &b, &a);
stop.offset = offset;
stop.color = Color(Float(r), Float(g), Float(b), Float(a));
stops.push_back(stop);
}
mStops = aTarget->CreateGradientStops(&stops.front(), count, ToExtendMode(extend));
}
if (mStops) {
cairo_matrix_t mat;
cairo_pattern_get_matrix(mPattern, &mat);
gfxMatrix matrix(*reinterpret_cast<gfxMatrix*>(&mat));
Matrix newMat = ToMatrix(matrix);
AdjustTransformForPattern(newMat, aTarget->GetTransform(), aPatternTransform);
mGfxPattern = new (mLinearGradientPattern.addr())
LinearGradientPattern(Point(x1, y1), Point(x2, y2), mStops, newMat);
return mGfxPattern;
}
break;
}
case CAIRO_PATTERN_TYPE_RADIAL:
{
if (!mStops) {
int count = 0;
cairo_pattern_get_color_stop_count(mPattern, &count);
std::vector<GradientStop> stops;
for (int i = 0; i < count; i++) {
GradientStop stop;
double r, g, b, a, offset;
cairo_pattern_get_color_stop_rgba(mPattern, i, &offset, &r, &g, &b, &a);
stop.offset = offset;
stop.color = Color(Float(r), Float(g), Float(b), Float(a));
stops.push_back(stop);
}
mStops = aTarget->CreateGradientStops(&stops.front(), count, ToExtendMode(extend));
}
if (mStops) {
cairo_matrix_t mat;
cairo_pattern_get_matrix(mPattern, &mat);
gfxMatrix matrix(*reinterpret_cast<gfxMatrix*>(&mat));
Matrix newMat = ToMatrix(matrix);
AdjustTransformForPattern(newMat, aTarget->GetTransform(), aPatternTransform);
double x1, y1, x2, y2, r1, r2;
cairo_pattern_get_radial_circles(mPattern, &x1, &y1, &r1, &x2, &y2, &r2);
mGfxPattern = new (mRadialGradientPattern.addr())
RadialGradientPattern(Point(x1, y1), Point(x2, y2), r1, r2, mStops, newMat);
return mGfxPattern;
}
break;
}
default: default:
/* Reassure the compiler we are handling all the enum values. */ /* Reassure the compiler we are handling all the enum values. */
break; break;
} }
new (mColorPattern.addr()) ColorPattern(Color(0, 0, 0, 0)); if (matrix) {
return mColorPattern.addr(); *matrix = mTransform;
if (aPatternTransform) {
AdjustTransformForPattern(*matrix,
aTarget->GetTransform(),
aPatternTransform);
}
}
return mGfxPattern;
} }
void void
gfxPattern::SetExtend(GraphicsExtend extend) gfxPattern::SetExtend(GraphicsExtend extend)
{ {
if (mPattern) { mExtend = extend;
mStops = nullptr; mStops = nullptr;
if (extend == EXTEND_PAD_EDGE) {
extend = EXTEND_PAD;
}
cairo_pattern_set_extend(mPattern, (cairo_extend_t)extend);
} else {
// This is always a surface pattern and will default to EXTEND_PAD
// for EXTEND_PAD_EDGE.
mExtend = extend;
}
} }
bool bool
gfxPattern::IsOpaque() gfxPattern::IsOpaque()
{ {
if (mPattern) { if (mGfxPattern->GetType() != PatternType::SURFACE) {
return false; return false;
} }
if (mSourceSurface->GetFormat() == SurfaceFormat::B8G8R8X8) { if (mSurfacePattern.addr()->mSurface->GetFormat() == SurfaceFormat::B8G8R8X8) {
return true; return true;
} }
return false; return false;
@ -300,64 +189,49 @@ gfxPattern::IsOpaque()
gfxPattern::GraphicsExtend gfxPattern::GraphicsExtend
gfxPattern::Extend() const gfxPattern::Extend() const
{ {
if (mPattern) { return mExtend;
return GraphicsExtend(cairo_pattern_get_extend(mPattern));
} else {
return mExtend;
}
} }
void void
gfxPattern::SetFilter(GraphicsFilter filter) gfxPattern::SetFilter(GraphicsFilter filter)
{ {
if (mPattern) { if (mGfxPattern->GetType() != PatternType::SURFACE) {
cairo_pattern_set_filter(mPattern, (cairo_filter_t)(int)filter); return;
} else {
mFilter = ToFilter(filter);
} }
mSurfacePattern.addr()->mFilter = ToFilter(filter);
} }
GraphicsFilter GraphicsFilter
gfxPattern::Filter() const gfxPattern::Filter() const
{ {
if (mPattern) { if (mGfxPattern->GetType() != PatternType::SURFACE) {
return (GraphicsFilter)cairo_pattern_get_filter(mPattern); return GraphicsFilter::FILTER_GOOD;
} else {
return ThebesFilter(mFilter);
} }
return ThebesFilter(mSurfacePattern.addr()->mFilter);
} }
bool bool
gfxPattern::GetSolidColor(gfxRGBA& aColor) gfxPattern::GetSolidColor(gfxRGBA& aColor)
{ {
return cairo_pattern_get_rgba(mPattern, if (mGfxPattern->GetType() == PatternType::COLOR) {
&aColor.r, aColor = ThebesColor(mColorPattern.addr()->mColor);
&aColor.g, return true;
&aColor.b, }
&aColor.a) == CAIRO_STATUS_SUCCESS;
return false;
} }
gfxPattern::GraphicsPatternType gfxPattern::GraphicsPatternType
gfxPattern::GetType() const gfxPattern::GetType() const
{ {
if (mPattern) { return ThebesPatternType(mGfxPattern->GetType());
return (GraphicsPatternType) cairo_pattern_get_type(mPattern);
} else {
// We should never be trying to get the type off an Azure gfx Pattern.
MOZ_ASSERT(0);
return PATTERN_SURFACE;
}
} }
int int
gfxPattern::CairoStatus() gfxPattern::CairoStatus()
{ {
if (mPattern) { return CAIRO_STATUS_SUCCESS;
return cairo_pattern_status(mPattern);
} else {
// An Azure pattern as this point is never in error status.
return CAIRO_STATUS_SUCCESS;
}
} }
void void

View File

@ -93,16 +93,10 @@ public:
/* returns TRUE if it succeeded */ /* returns TRUE if it succeeded */
bool GetSolidColor(gfxRGBA& aColor); bool GetSolidColor(gfxRGBA& aColor);
bool IsAzure() { return !mPattern; }
mozilla::TemporaryRef<mozilla::gfx::SourceSurface> GetAzureSurface() { return mSourceSurface; }
private: private:
// Private destructor, to discourage deletion outside of Release(): // Private destructor, to discourage deletion outside of Release():
~gfxPattern(); ~gfxPattern();
cairo_pattern_t *mPattern;
/** /**
* aPatternTransform is the cairo pattern transform --- from user space at * aPatternTransform is the cairo pattern transform --- from user space at
* the time the pattern was set, to pattern space. * the time the pattern was set, to pattern space.
@ -130,8 +124,8 @@ private:
mozilla::RefPtr<mozilla::gfx::SourceSurface> mSourceSurface; mozilla::RefPtr<mozilla::gfx::SourceSurface> mSourceSurface;
mozilla::gfx::Matrix mTransform; mozilla::gfx::Matrix mTransform;
mozilla::RefPtr<mozilla::gfx::GradientStops> mStops; mozilla::RefPtr<mozilla::gfx::GradientStops> mStops;
nsTArray<mozilla::gfx::GradientStop> mStopsList;
GraphicsExtend mExtend; GraphicsExtend mExtend;
mozilla::gfx::Filter mFilter;
}; };
#endif /* GFX_PATTERN_H */ #endif /* GFX_PATTERN_H */