Bug 1089177, part 1 - Expose PatternFromState to help with Moz2D porting. r=mattwoodrow

This commit is contained in:
Jonathan Watt 2014-10-28 14:40:39 +00:00
parent d6bd4e18e3
commit 99eeb78806
2 changed files with 48 additions and 42 deletions

View File

@ -36,53 +36,38 @@ using namespace mozilla::gfx;
UserDataKey gfxContext::sDontUseAsSourceKey;
/* This class lives on the stack and allows gfxContext users to easily, and
* performantly get a gfx::Pattern to use for drawing in their current context.
*/
class PatternFromState
PatternFromState::operator mozilla::gfx::Pattern&()
{
public:
explicit PatternFromState(gfxContext *aContext) : mContext(aContext), mPattern(nullptr) {}
~PatternFromState() { if (mPattern) { mPattern->~Pattern(); } }
gfxContext::AzureState &state = mContext->CurrentState();
operator mozilla::gfx::Pattern&()
{
gfxContext::AzureState &state = mContext->CurrentState();
if (state.pattern) {
return *state.pattern->GetPattern(mContext->mDT, state.patternTransformChanged ? &state.patternTransform : nullptr);
} else if (state.sourceSurface) {
Matrix transform = state.surfTransform;
if (state.patternTransformChanged) {
Matrix mat = mContext->GetDTTransform();
if (!mat.Invert()) {
mPattern = new (mColorPattern.addr())
ColorPattern(Color()); // transparent black to paint nothing
return *mPattern;
}
transform = transform * state.patternTransform * mat;
}
mPattern = new (mSurfacePattern.addr())
SurfacePattern(state.sourceSurface, ExtendMode::CLAMP, transform);
return *mPattern;
} else {
mPattern = new (mColorPattern.addr())
ColorPattern(state.color);
return *mPattern;
}
if (state.pattern) {
return *state.pattern->GetPattern(mContext->mDT, state.patternTransformChanged ? &state.patternTransform : nullptr);
}
private:
union {
mozilla::AlignedStorage2<mozilla::gfx::ColorPattern> mColorPattern;
mozilla::AlignedStorage2<mozilla::gfx::SurfacePattern> mSurfacePattern;
};
if (state.sourceSurface) {
Matrix transform = state.surfTransform;
if (state.patternTransformChanged) {
Matrix mat = mContext->GetDTTransform();
if (!mat.Invert()) {
mPattern = new (mColorPattern.addr())
ColorPattern(Color()); // transparent black to paint nothing
return *mPattern;
}
transform = transform * state.patternTransform * mat;
}
mPattern = new (mSurfacePattern.addr())
SurfacePattern(state.sourceSurface, ExtendMode::CLAMP, transform);
return *mPattern;
}
mPattern = new (mColorPattern.addr())
ColorPattern(state.color);
return *mPattern;
}
gfxContext *mContext;
Pattern *mPattern;
};
gfxContext::gfxContext(DrawTarget *aTarget, const Point& aDeviceOffset)
: mPathIsRect(false)

View File

@ -844,4 +844,25 @@ private:
bool mSubpixelAntialiasingEnabled;
};
/* This class lives on the stack and allows gfxContext users to easily, and
* performantly get a gfx::Pattern to use for drawing in their current context.
*/
class PatternFromState
{
public:
explicit PatternFromState(gfxContext *aContext) : mContext(aContext), mPattern(nullptr) {}
~PatternFromState() { if (mPattern) { mPattern->~Pattern(); } }
operator mozilla::gfx::Pattern&();
private:
union {
mozilla::AlignedStorage2<mozilla::gfx::ColorPattern> mColorPattern;
mozilla::AlignedStorage2<mozilla::gfx::SurfacePattern> mSurfacePattern;
};
gfxContext *mContext;
mozilla::gfx::Pattern *mPattern;
};
#endif /* GFX_CONTEXT_H */