Bug 1085159 - Port the code that uses nsRenderingContext::DrawLine() to Moz2D. r=mattwoodrow

--HG--
extra : rebase_source : 70553292850517d695994c0a3422fab8ea0b760a
This commit is contained in:
Jonathan Watt 2014-10-20 10:55:48 +01:00
parent dec99cb936
commit 5e3c3b8150
9 changed files with 122 additions and 134 deletions

View File

@ -111,58 +111,6 @@ nsRenderingContext::SetColor(nscolor aColor)
mThebes->SetColor(gfxRGBA(aColor));
}
//
// shapes
//
void
nsRenderingContext::DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt)
{
DrawLine(aStartPt.x, aStartPt.y, aEndPt.x, aEndPt.y);
}
void
nsRenderingContext::DrawLine(nscoord aX0, nscoord aY0,
nscoord aX1, nscoord aY1)
{
gfxPoint p0 = gfxPoint(FROM_TWIPS(aX0), FROM_TWIPS(aY0));
gfxPoint p1 = gfxPoint(FROM_TWIPS(aX1), FROM_TWIPS(aY1));
// we can't draw thick lines with gfx, so we always assume we want
// pixel-aligned lines if the rendering context is at 1.0 scale
gfxMatrix savedMatrix = mThebes->CurrentMatrix();
if (!savedMatrix.HasNonTranslation()) {
p0 = mThebes->UserToDevice(p0);
p1 = mThebes->UserToDevice(p1);
p0.Round();
p1.Round();
mThebes->SetMatrix(gfxMatrix());
mThebes->NewPath();
// snap straight lines
if (p0.x == p1.x) {
mThebes->Line(p0 + gfxPoint(0.5, 0),
p1 + gfxPoint(0.5, 0));
} else if (p0.y == p1.y) {
mThebes->Line(p0 + gfxPoint(0, 0.5),
p1 + gfxPoint(0, 0.5));
} else {
mThebes->Line(p0, p1);
}
mThebes->Stroke();
mThebes->SetMatrix(savedMatrix);
} else {
mThebes->NewPath();
mThebes->Line(p0, p1);
mThebes->Stroke();
}
}
//
// text

View File

@ -52,11 +52,6 @@ public:
void IntersectClip(const nsRect& aRect);
void SetColor(nscolor aColor);
// Shapes
void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt);
void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1);
// Text
void SetFont(nsFontMetrics *aFontMetrics);

View File

@ -3507,14 +3507,18 @@ DrawSolidBorderSegment(nsRenderingContext& aContext,
// simple line or rectangle
if ((NS_SIDE_TOP == aStartBevelSide) || (NS_SIDE_BOTTOM == aStartBevelSide)) {
if (1 == aRect.height)
aContext.DrawLine(aRect.TopLeft(), aRect.BottomLeft());
StrokeLineWithSnapping(aRect.TopLeft(), aRect.BottomLeft(),
appUnitsPerDevPixel, *drawTarget,
color, StrokeOptions(), drawOptions);
else
drawTarget->FillRect(NSRectToRect(aRect, appUnitsPerDevPixel, *drawTarget),
color, drawOptions);
}
else {
if (1 == aRect.width)
aContext.DrawLine(aRect.TopLeft(), aRect.TopRight());
StrokeLineWithSnapping(aRect.TopLeft(), aRect.TopRight(),
appUnitsPerDevPixel, *drawTarget,
color, StrokeOptions(), drawOptions);
else
drawTarget->FillRect(NSRectToRect(aRect, appUnitsPerDevPixel, *drawTarget),
color, drawOptions);

View File

@ -7060,6 +7060,19 @@ Rect NSRectToRect(const nsRect& aRect, double aAppUnitsPerPixel,
return rect;
}
void StrokeLineWithSnapping(const nsPoint& aP1, const nsPoint& aP2,
int32_t aAppUnitsPerDevPixel,
DrawTarget& aDrawTarget,
const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aDrawOptions)
{
Point p1 = NSPointToPoint(aP1, aAppUnitsPerDevPixel);
Point p2 = NSPointToPoint(aP2, aAppUnitsPerDevPixel);
SnapLineToDevicePixelsForStroking(p1, p2, aDrawTarget);
aDrawTarget.StrokeLine(p1, p2, aPattern, aStrokeOptions, aDrawOptions);
}
namespace layout {

View File

@ -2425,6 +2425,13 @@ gfx::Rect NSRectToRect(const nsRect& aRect, double aAppUnitsPerPixel);
gfx::Rect NSRectToRect(const nsRect& aRect, double aAppUnitsPerPixel,
const gfx::DrawTarget& aSnapDT);
void StrokeLineWithSnapping(const nsPoint& aP1, const nsPoint& aP2,
int32_t aAppUnitsPerDevPixel,
gfx::DrawTarget& aDrawTarget,
const gfx::Pattern& aPattern,
const gfx::StrokeOptions& aStrokeOptions = gfx::StrokeOptions(),
const gfx::DrawOptions& aDrawOptions = gfx::DrawOptions());
namespace layout {
/**

View File

@ -11,6 +11,7 @@
#include "gfxUtils.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/Likely.h"
#include "nsGenericHTMLElement.h"
@ -111,7 +112,7 @@ public:
void SetVisibility(bool aVisibility);
void SetColor(nscolor aColor);
void PaintBorder(nsRenderingContext& aRenderingContext, nsPoint aPt);
void PaintBorder(DrawTarget* aDrawTarget, nsPoint aPt);
protected:
nsHTMLFramesetBorderFrame(nsStyleContext* aContext, int32_t aWidth, bool aVertical, bool aVisible);
@ -1491,7 +1492,7 @@ void nsDisplayFramesetBorder::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
static_cast<nsHTMLFramesetBorderFrame*>(mFrame)->
PaintBorder(*aCtx, ToReferenceFrame());
PaintBorder(aCtx->GetDrawTarget(), ToReferenceFrame());
}
void
@ -1503,49 +1504,52 @@ nsHTMLFramesetBorderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
new (aBuilder) nsDisplayFramesetBorder(aBuilder, this));
}
void nsHTMLFramesetBorderFrame::PaintBorder(nsRenderingContext& aRenderingContext,
void nsHTMLFramesetBorderFrame::PaintBorder(DrawTarget* aDrawTarget,
nsPoint aPt)
{
nscolor WHITE = NS_RGB(255, 255, 255);
nscolor bgColor =
LookAndFeel::GetColor(LookAndFeel::eColorID_WidgetBackground,
NS_RGB(200,200,200));
nscolor fgColor =
LookAndFeel::GetColor(LookAndFeel::eColorID_WidgetForeground,
NS_RGB(0,0,0));
nscolor hltColor =
LookAndFeel::GetColor(LookAndFeel::eColorID_Widget3DHighlight,
NS_RGB(255,255,255));
nscolor sdwColor =
LookAndFeel::GetColor(LookAndFeel::eColorID_Widget3DShadow,
NS_RGB(128,128,128));
gfxContext* ctx = aRenderingContext.ThebesContext();
gfxPoint toRefFrame =
nsLayoutUtils::PointToGfxPoint(aPt, PresContext()->AppUnitsPerDevPixel());
gfxContextMatrixAutoSaveRestore autoSR(ctx);
ctx->SetMatrix(ctx->CurrentMatrix().Translate(toRefFrame));
nscoord widthInPixels = nsPresContext::AppUnitsToIntCSSPixels(mWidth);
nscoord pixelWidth = nsPresContext::CSSPixelsToAppUnits(1);
if (widthInPixels <= 0)
return;
nsPoint start(0,0);
nsPoint end((mVertical) ? 0 : mRect.width, (mVertical) ? mRect.height : 0);
ColorPattern bgColor(ToDeviceColor(
LookAndFeel::GetColor(LookAndFeel::eColorID_WidgetBackground,
NS_RGB(200, 200, 200))));
nscolor color = WHITE;
ColorPattern fgColor(ToDeviceColor(
LookAndFeel::GetColor(LookAndFeel::eColorID_WidgetForeground,
NS_RGB(0, 0, 0))));
ColorPattern hltColor(ToDeviceColor(
LookAndFeel::GetColor(LookAndFeel::eColorID_Widget3DHighlight,
NS_RGB(255, 255, 255))));
ColorPattern sdwColor(ToDeviceColor(
LookAndFeel::GetColor(LookAndFeel::eColorID_Widget3DShadow,
NS_RGB(128, 128, 128))));
ColorPattern color(ToDeviceColor(NS_RGB(255, 255, 255))); // default to white
if (mVisibility || mVisibilityOverride) {
color = (NO_COLOR == mColor) ? bgColor : mColor;
color = (NO_COLOR == mColor) ? bgColor :
ColorPattern(ToDeviceColor(mColor));
}
aRenderingContext.SetColor(color);
int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
Point toRefFrame = NSPointToPoint(aPt, appUnitsPerDevPixel);
AutoRestoreTransform autoRestoreTransform(aDrawTarget);
aDrawTarget->SetTransform(
aDrawTarget->GetTransform().PreTranslate(toRefFrame));
nsPoint start(0, 0);
nsPoint end = mVertical ? nsPoint(0, mRect.height) : nsPoint(mRect.width, 0);
// draw grey or white first
for (int i = 0; i < widthInPixels; i++) {
aRenderingContext.DrawLine (start, end);
StrokeLineWithSnapping(start, end, appUnitsPerDevPixel, *aDrawTarget,
color);
if (mVertical) {
start.x += pixelWidth;
end.x = start.x;
@ -1559,30 +1563,30 @@ void nsHTMLFramesetBorderFrame::PaintBorder(nsRenderingContext& aRenderingContex
return;
if (widthInPixels >= 5) {
aRenderingContext.SetColor(hltColor);
start.x = (mVertical) ? pixelWidth : 0;
start.y = (mVertical) ? 0 : pixelWidth;
end.x = (mVertical) ? start.x : mRect.width;
end.y = (mVertical) ? mRect.height : start.y;
aRenderingContext.DrawLine(start, end);
StrokeLineWithSnapping(start, end, appUnitsPerDevPixel, *aDrawTarget,
hltColor);
}
if (widthInPixels >= 2) {
aRenderingContext.SetColor(sdwColor);
start.x = (mVertical) ? mRect.width - (2 * pixelWidth) : 0;
start.y = (mVertical) ? 0 : mRect.height - (2 * pixelWidth);
end.x = (mVertical) ? start.x : mRect.width;
end.y = (mVertical) ? mRect.height : start.y;
aRenderingContext.DrawLine(start, end);
StrokeLineWithSnapping(start, end, appUnitsPerDevPixel, *aDrawTarget,
sdwColor);
}
if (widthInPixels >= 1) {
aRenderingContext.SetColor(fgColor);
start.x = (mVertical) ? mRect.width - pixelWidth : 0;
start.y = (mVertical) ? 0 : mRect.height - pixelWidth;
end.x = (mVertical) ? start.x : mRect.width;
end.y = (mVertical) ? mRect.height : start.y;
aRenderingContext.DrawLine(start, end);
StrokeLineWithSnapping(start, end, appUnitsPerDevPixel, *aDrawTarget,
fgColor);
}
}

View File

@ -335,21 +335,28 @@ nsTableCellFrame::DecorateForSelection(nsRenderingContext& aRenderingContext,
nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
aRenderingContext.SetColor(bordercolor);
aRenderingContext.DrawLine(onePixel, 0, mRect.width, 0);
aRenderingContext.DrawLine(0, onePixel, 0, mRect.height);
aRenderingContext.DrawLine(onePixel, mRect.height, mRect.width, mRect.height);
aRenderingContext.DrawLine(mRect.width, onePixel, mRect.width, mRect.height);
StrokeLineWithSnapping(nsPoint(onePixel, 0), nsPoint(mRect.width, 0),
appUnitsPerDevPixel, *drawTarget, color);
StrokeLineWithSnapping(nsPoint(0, onePixel), nsPoint(0, mRect.height),
appUnitsPerDevPixel, *drawTarget, color);
StrokeLineWithSnapping(nsPoint(onePixel, mRect.height),
nsPoint(mRect.width, mRect.height),
appUnitsPerDevPixel, *drawTarget, color);
StrokeLineWithSnapping(nsPoint(mRect.width, onePixel),
nsPoint(mRect.width, mRect.height),
appUnitsPerDevPixel, *drawTarget, color);
//middle
nsRect r(onePixel, onePixel,
mRect.width - onePixel, mRect.height - onePixel);
Rect devPixelRect = NSRectToRect(r, appUnitsPerDevPixel, *drawTarget);
drawTarget->StrokeRect(devPixelRect, color);
//shading
aRenderingContext.DrawLine(2*onePixel, mRect.height-2*onePixel,
mRect.width-onePixel, mRect.height- (2*onePixel));
aRenderingContext.DrawLine(mRect.width - (2*onePixel), 2*onePixel,
mRect.width - (2*onePixel), mRect.height-onePixel);
StrokeLineWithSnapping(nsPoint(2*onePixel, mRect.height-2*onePixel),
nsPoint(mRect.width-onePixel, mRect.height- (2*onePixel)),
appUnitsPerDevPixel, *drawTarget, color);
StrokeLineWithSnapping(nsPoint(mRect.width - (2*onePixel), 2*onePixel),
nsPoint(mRect.width - (2*onePixel), mRect.height-onePixel),
appUnitsPerDevPixel, *drawTarget, color);
}
}
}

View File

@ -37,6 +37,7 @@
#include "mozilla/gfx/2D.h"
#include "nsBoxLayoutState.h"
#include "mozilla/dom/Touch.h"
#include "mozilla/Move.h"
#include "nsStyleContext.h"
#include "nsPlaceholderFrame.h"
#include "nsPresContext.h"
@ -1292,7 +1293,7 @@ nsDisplayXULDebug::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
static_cast<nsBoxFrame*>(mFrame)->
PaintXULDebugOverlay(*aCtx, ToReferenceFrame());
PaintXULDebugOverlay(*aCtx->GetDrawTarget(), ToReferenceFrame());
}
static void
@ -1472,8 +1473,7 @@ nsBoxFrame::PaintXULDebugBackground(nsRenderingContext& aRenderingContext,
}
void
nsBoxFrame::PaintXULDebugOverlay(nsRenderingContext& aRenderingContext,
nsPoint aPt)
nsBoxFrame::PaintXULDebugOverlay(DrawTarget& aDrawTarget, nsPoint aPt)
nsMargin border;
GetBorder(border);
@ -1516,14 +1516,12 @@ nsBoxFrame::PaintXULDebugOverlay(nsRenderingContext& aRenderingContext,
nscoord flex = kid->GetFlex(state);
if (!kid->IsCollapsed()) {
aRenderingContext.SetColor(NS_RGB(255,255,255));
if (isHorizontal)
borderSize = cr.width;
else
borderSize = cr.height;
DrawSpacer(GetPresContext(), aRenderingContext, isHorizontal, flex, x, y, borderSize, spacerSize);
DrawSpacer(GetPresContext(), aDrawTarget, isHorizontal, flex, x, y, borderSize, spacerSize);
}
kid = GetNextBox(kid);
@ -1598,28 +1596,34 @@ nsBoxFrame::GetDebug(bool& aDebug)
#ifdef DEBUG_LAYOUT
void
nsBoxFrame::DrawLine(nsRenderingContext& aRenderingContext, bool aHorizontal, nscoord x1, nscoord y1, nscoord x2, nscoord y2)
nsBoxFrame::DrawLine(DrawTarget& aDrawTarget, bool aHorizontal, nscoord x1, nscoord y1, nscoord x2, nscoord y2)
{
if (aHorizontal)
aRenderingContext.DrawLine(x1,y1,x2,y2);
else
aRenderingContext.DrawLine(y1,x1,y2,x2);
nsPoint p1(x1, y1);
nsPoint p2(x2, y2);
if (!aHorizontal) {
Swap(p1.x, p1.y);
Swap(p2.x, p2.y);
}
ColorPattern white(ToDeviceColor(Color(1.f, 1.f, 1.f, 1.f)));
StrokeLineWithSnapping(p1, p2, PresContext()->AppUnitsPerDevPixel(),
aDrawTarget, color);
}
void
nsBoxFrame::FillRect(nsRenderingContext& aRenderingContext, bool aHorizontal, nscoord x, nscoord y, nscoord width, nscoord height)
nsBoxFrame::FillRect(DrawTarget& aDrawTarget, bool aHorizontal, nscoord x, nscoord y, nscoord width, nscoord height)
{
DrawTarget* drawTarget = aRenderingContext->GetDrawTarget();
Rect rect = NSRectToRect(aHorizontal ? nsRect(x, y, width, height) :
nsRect(y, x, height, width),
PresContext()->AppUnitsPerDevPixel(),
*drawTarget);
aDrawTarget);
ColorPattern white(ToDeviceColor(Color(1.f, 1.f, 1.f, 1.f)));
drawTarget->FillRect(rect, white);
aDrawTarget.FillRect(rect, white);
}
void
nsBoxFrame::DrawSpacer(nsPresContext* aPresContext, nsRenderingContext& aRenderingContext, bool aHorizontal, int32_t flex, nscoord x, nscoord y, nscoord size, nscoord spacerSize)
nsBoxFrame::DrawSpacer(nsPresContext* aPresContext, DrawTarget& aDrawTarget,
bool aHorizontal, int32_t flex, nscoord x, nscoord y,
nscoord size, nscoord spacerSize)
{
nscoord onePixel = aPresContext->IntScaledPixelsToTwips(1);
@ -1639,21 +1643,19 @@ nsBoxFrame::DrawSpacer(nsPresContext* aPresContext, nsRenderingContext& aRenderi
int halfCoilSize = coilSize/2;
if (flex == 0) {
DrawLine(aRenderingContext, aHorizontal, x,y + spacerSize/2, x + size, y + spacerSize/2);
DrawLine(aDrawTarget, aHorizontal, x,y + spacerSize/2, x + size, y + spacerSize/2);
} else {
for (int i=0; i < coils; i++)
{
DrawLine(aRenderingContext, aHorizontal, offset, center+halfSpacer, offset+halfCoilSize, center-halfSpacer);
DrawLine(aRenderingContext, aHorizontal, offset+halfCoilSize, center-halfSpacer, offset+coilSize, center+halfSpacer);
DrawLine(aDrawTarget, aHorizontal, offset, center+halfSpacer, offset+halfCoilSize, center-halfSpacer);
DrawLine(aDrawTarget, aHorizontal, offset+halfCoilSize, center-halfSpacer, offset+coilSize, center+halfSpacer);
offset += coilSize;
}
}
FillRect(aRenderingContext, aHorizontal, x + size - spacerSize/2, y, spacerSize/2, spacerSize);
FillRect(aRenderingContext, aHorizontal, x, y, spacerSize/2, spacerSize);
//DrawKnob(aPresContext, aRenderingContext, x + size - spacerSize, y, spacerSize);
FillRect(aDrawTarget, aHorizontal, x + size - spacerSize/2, y, spacerSize/2, spacerSize);
FillRect(aDrawTarget, aHorizontal, x, y, spacerSize/2, spacerSize);
}
void

View File

@ -21,6 +21,12 @@
class nsBoxLayoutState;
namespace mozilla {
namespace gfx {
class DrawTarget;
}
}
nsIFrame* NS_NewBoxFrame(nsIPresShell* aPresShell,
nsStyleContext* aContext,
bool aIsRoot,
@ -30,6 +36,8 @@ nsIFrame* NS_NewBoxFrame(nsIPresShell* aPresShell,
class nsBoxFrame : public nsContainerFrame
{
typedef mozilla::gfx::DrawTarget DrawTarget;
public:
NS_DECL_FRAMEARENA_HELPERS
#ifdef DEBUG
@ -174,7 +182,7 @@ protected:
virtual void GetBoxName(nsAutoString& aName) MOZ_OVERRIDE;
void PaintXULDebugBackground(nsRenderingContext& aRenderingContext,
nsPoint aPt);
void PaintXULDebugOverlay(nsRenderingContext& aRenderingContext,
void PaintXULDebugOverlay(DrawTarget& aRenderingContext,
nsPoint aPt);
#endif
@ -224,9 +232,9 @@ private:
void GetValue(nsPresContext* aPresContext, const nsSize& a, const nsSize& b, char* value);
void GetValue(nsPresContext* aPresContext, int32_t a, int32_t b, char* value);
void DrawSpacer(nsPresContext* aPresContext, nsRenderingContext& aRenderingContext, bool aHorizontal, int32_t flex, nscoord x, nscoord y, nscoord size, nscoord spacerSize);
void DrawLine(nsRenderingContext& aRenderingContext, bool aHorizontal, nscoord x1, nscoord y1, nscoord x2, nscoord y2);
void FillRect(nsRenderingContext& aRenderingContext, bool aHorizontal, nscoord x, nscoord y, nscoord width, nscoord height);
void DrawSpacer(nsPresContext* aPresContext, DrawTarget& aDrawTarget, bool aHorizontal, int32_t flex, nscoord x, nscoord y, nscoord size, nscoord spacerSize);
void DrawLine(DrawTarget& aDrawTarget, bool aHorizontal, nscoord x1, nscoord y1, nscoord x2, nscoord y2);
void FillRect(DrawTarget& aDrawTarget, bool aHorizontal, nscoord x, nscoord y, nscoord width, nscoord height);
#endif
virtual void UpdateMouseThrough();