2012-05-29 21:42:41 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "gfxPattern.h"
|
2014-10-17 04:53:15 -07:00
|
|
|
|
|
|
|
#include "gfxUtils.h"
|
|
|
|
#include "gfxTypes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "gfxASurface.h"
|
2007-11-09 10:14:59 -08:00
|
|
|
#include "gfxPlatform.h"
|
2013-10-07 16:15:59 -07:00
|
|
|
#include "gfx2DGlue.h"
|
2013-11-07 20:50:39 -08:00
|
|
|
#include "gfxGradientCache.h"
|
2014-09-14 02:58:56 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPattern::gfxPattern(const gfxRGBA& aColor)
|
2014-09-14 02:58:56 -07:00
|
|
|
: mExtend(EXTEND_NONE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-17 04:53:16 -07:00
|
|
|
mGfxPattern.InitColorPattern(ToDeviceColor(aColor));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// linear
|
|
|
|
gfxPattern::gfxPattern(gfxFloat x0, gfxFloat y0, gfxFloat x1, gfxFloat y1)
|
2014-09-14 02:58:56 -07:00
|
|
|
: mExtend(EXTEND_NONE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
mGfxPattern.InitLinearGradientPattern(Point(x0, y0), Point(x1, y1), nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// radial
|
|
|
|
gfxPattern::gfxPattern(gfxFloat cx0, gfxFloat cy0, gfxFloat radius0,
|
|
|
|
gfxFloat cx1, gfxFloat cy1, gfxFloat radius1)
|
2014-09-14 02:58:56 -07:00
|
|
|
: mExtend(EXTEND_NONE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
mGfxPattern.InitRadialGradientPattern(Point(cx0, cy0), Point(cx1, cy1),
|
|
|
|
radius0, radius1, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
// Azure
|
2014-09-15 08:24:03 -07:00
|
|
|
gfxPattern::gfxPattern(SourceSurface *aSurface, const Matrix &aPatternToUserSpace)
|
|
|
|
: mPatternToUserSpace(aPatternToUserSpace)
|
2013-06-19 14:06:42 -07:00
|
|
|
, mExtend(EXTEND_NONE)
|
2012-01-04 23:17:51 -08:00
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
mGfxPattern.InitSurfacePattern(aSurface, ToExtendMode(mExtend), Matrix(), // matrix is overridden in GetPattern()
|
|
|
|
mozilla::gfx::Filter::GOOD);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxPattern::AddColorStop(gfxFloat offset, const gfxRGBA& c)
|
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
if (mGfxPattern.GetPattern()->GetType() != PatternType::LINEAR_GRADIENT &&
|
|
|
|
mGfxPattern.GetPattern()->GetType() != PatternType::RADIAL_GRADIENT) {
|
2014-09-14 02:58:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStops = nullptr;
|
|
|
|
|
|
|
|
GradientStop stop;
|
|
|
|
stop.offset = offset;
|
2014-10-17 04:53:15 -07:00
|
|
|
stop.color = ToDeviceColor(c);
|
2014-09-14 02:58:56 -07:00
|
|
|
mStopsList.AppendElement(stop);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-15 10:54:49 -08:00
|
|
|
void
|
2014-06-12 12:24:05 -07:00
|
|
|
gfxPattern::SetColorStops(GradientStops* aStops)
|
2013-02-15 10:54:49 -08:00
|
|
|
{
|
|
|
|
mStops = aStops;
|
|
|
|
}
|
|
|
|
|
2013-11-07 20:50:39 -08:00
|
|
|
void
|
2014-09-29 06:15:19 -07:00
|
|
|
gfxPattern::CacheColorStops(const DrawTarget *aDT)
|
2013-11-07 20:50:39 -08:00
|
|
|
{
|
2014-09-14 02:58:56 -07:00
|
|
|
mStops = gfxGradientCache::GetOrCreateGradientStops(aDT, mStopsList,
|
|
|
|
ToExtendMode(mExtend));
|
2013-11-07 20:50:39 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-09-15 08:24:03 -07:00
|
|
|
gfxPattern::SetMatrix(const gfxMatrix& aPatternToUserSpace)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-09-15 08:24:03 -07:00
|
|
|
mPatternToUserSpace = ToMatrix(aPatternToUserSpace);
|
2014-09-14 02:58:56 -07:00
|
|
|
// Cairo-pattern matrices specify the conversion from DrawTarget to pattern
|
|
|
|
// space. Azure pattern matrices specify the conversion from pattern to
|
|
|
|
// DrawTarget space.
|
2014-09-15 08:24:03 -07:00
|
|
|
mPatternToUserSpace.Invert();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix
|
|
|
|
gfxPattern::GetMatrix() const
|
|
|
|
{
|
2014-09-14 02:58:56 -07:00
|
|
|
// invert at the higher precision of gfxMatrix
|
|
|
|
// cause we need to convert at some point anyways
|
2014-09-15 08:24:03 -07:00
|
|
|
gfxMatrix mat = ThebesMatrix(mPatternToUserSpace);
|
2014-09-14 02:58:56 -07:00
|
|
|
mat.Invert();
|
|
|
|
return mat;
|
2013-06-21 11:55:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix
|
|
|
|
gfxPattern::GetInverseMatrix() const
|
|
|
|
{
|
2014-09-15 08:24:03 -07:00
|
|
|
return ThebesMatrix(mPatternToUserSpace);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Pattern*
|
2014-09-29 06:26:15 -07:00
|
|
|
gfxPattern::GetPattern(const DrawTarget *aTarget,
|
2014-09-15 08:24:03 -07:00
|
|
|
Matrix *aOriginalUserToDevice)
|
|
|
|
{
|
|
|
|
Matrix patternToUser = mPatternToUserSpace;
|
|
|
|
|
|
|
|
if (aOriginalUserToDevice &&
|
|
|
|
*aOriginalUserToDevice != aTarget->GetTransform()) {
|
|
|
|
// mPatternToUserSpace maps from pattern space to the original user space,
|
|
|
|
// but aTarget now has a transform to a different user space. In order for
|
|
|
|
// the Pattern* that we return to be usable in aTarget's new user space we
|
|
|
|
// need the Pattern's mMatrix to be the transform from pattern space to
|
|
|
|
// aTarget's -new- user space. That transform is equivalent to the
|
|
|
|
// transform from pattern space to original user space (patternToUser),
|
|
|
|
// multiplied by the transform from original user space to device space,
|
|
|
|
// multiplied by the transform from device space to current user space.
|
|
|
|
|
|
|
|
Matrix deviceToCurrentUser = aTarget->GetTransform();
|
|
|
|
deviceToCurrentUser.Invert();
|
|
|
|
|
|
|
|
patternToUser = patternToUser * *aOriginalUserToDevice * deviceToCurrentUser;
|
|
|
|
}
|
|
|
|
patternToUser.NudgeToIntegers();
|
|
|
|
|
2014-09-14 02:58:56 -07:00
|
|
|
if (!mStops &&
|
|
|
|
!mStopsList.IsEmpty()) {
|
|
|
|
mStops = aTarget->CreateGradientStops(mStopsList.Elements(),
|
|
|
|
mStopsList.Length(),
|
|
|
|
ToExtendMode(mExtend));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:12:06 -07:00
|
|
|
switch (mGfxPattern.GetPattern()->GetType()) {
|
|
|
|
case PatternType::SURFACE: {
|
|
|
|
SurfacePattern* surfacePattern = static_cast<SurfacePattern*>(mGfxPattern.GetPattern());
|
|
|
|
surfacePattern->mMatrix = patternToUser;
|
|
|
|
surfacePattern->mExtendMode = ToExtendMode(mExtend);
|
2014-09-14 02:58:56 -07:00
|
|
|
break;
|
2014-09-29 06:12:06 -07:00
|
|
|
}
|
|
|
|
case PatternType::LINEAR_GRADIENT: {
|
|
|
|
LinearGradientPattern* linearGradientPattern = static_cast<LinearGradientPattern*>(mGfxPattern.GetPattern());
|
|
|
|
linearGradientPattern->mMatrix = patternToUser;
|
|
|
|
linearGradientPattern->mStops = mStops;
|
2014-09-14 02:58:56 -07:00
|
|
|
break;
|
2014-09-29 06:12:06 -07:00
|
|
|
}
|
|
|
|
case PatternType::RADIAL_GRADIENT: {
|
|
|
|
RadialGradientPattern* radialGradientPattern = static_cast<RadialGradientPattern*>(mGfxPattern.GetPattern());
|
|
|
|
radialGradientPattern->mMatrix = patternToUser;
|
|
|
|
radialGradientPattern->mStops = mStops;
|
2014-09-14 02:58:56 -07:00
|
|
|
break;
|
2014-09-29 06:12:06 -07:00
|
|
|
}
|
2012-02-04 04:22:47 -08:00
|
|
|
default:
|
|
|
|
/* Reassure the compiler we are handling all the enum values. */
|
|
|
|
break;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:12:06 -07:00
|
|
|
return mGfxPattern.GetPattern();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxPattern::SetExtend(GraphicsExtend extend)
|
|
|
|
{
|
2014-09-14 02:58:56 -07:00
|
|
|
mExtend = extend;
|
|
|
|
mStops = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
gfxPattern::IsOpaque()
|
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
if (mGfxPattern.GetPattern()->GetType() != PatternType::SURFACE) {
|
2014-09-14 02:58:55 -07:00
|
|
|
return false;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:12:06 -07:00
|
|
|
if (static_cast<SurfacePattern*>(mGfxPattern.GetPattern())->mSurface->GetFormat() == SurfaceFormat::B8G8R8X8) {
|
2012-01-04 23:17:51 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxPattern::GraphicsExtend
|
|
|
|
gfxPattern::Extend() const
|
|
|
|
{
|
2014-09-14 02:58:56 -07:00
|
|
|
return mExtend;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-21 08:36:38 -07:00
|
|
|
gfxPattern::SetFilter(GraphicsFilter filter)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
if (mGfxPattern.GetPattern()->GetType() != PatternType::SURFACE) {
|
2014-09-14 02:58:56 -07:00
|
|
|
return;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2014-09-14 02:58:56 -07:00
|
|
|
|
2014-09-29 06:12:06 -07:00
|
|
|
static_cast<SurfacePattern*>(mGfxPattern.GetPattern())->mFilter = ToFilter(filter);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:01:19 -07:00
|
|
|
GraphicsFilter
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPattern::Filter() const
|
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
if (mGfxPattern.GetPattern()->GetType() != PatternType::SURFACE) {
|
2014-09-14 02:58:56 -07:00
|
|
|
return GraphicsFilter::FILTER_GOOD;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2014-09-29 06:12:06 -07:00
|
|
|
return ThebesFilter(static_cast<const SurfacePattern*>(mGfxPattern.GetPattern())->mFilter);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPattern::GetSolidColor(gfxRGBA& aColor)
|
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
if (mGfxPattern.GetPattern()->GetType() == PatternType::COLOR) {
|
|
|
|
aColor = ThebesColor(static_cast<ColorPattern*>(mGfxPattern.GetPattern())->mColor);
|
2014-09-14 02:58:56 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-05 22:51:13 -08:00
|
|
|
gfxPattern::GraphicsPatternType
|
|
|
|
gfxPattern::GetType() const
|
|
|
|
{
|
2014-09-29 06:12:06 -07:00
|
|
|
return ThebesPatternType(mGfxPattern.GetPattern()->GetType());
|
2008-03-05 22:51:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gfxPattern::CairoStatus()
|
|
|
|
{
|
2014-09-14 02:58:56 -07:00
|
|
|
return CAIRO_STATUS_SUCCESS;
|
2008-03-05 22:51:13 -08:00
|
|
|
}
|