2014-06-11 12:53:02 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
|
|
|
|
2014-09-02 14:20:33 -07:00
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#include <cmath>
|
|
|
|
|
2014-08-31 22:28:53 -07:00
|
|
|
#include "DrawTargetTiled.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
|
2014-06-11 12:53:02 -07:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
DrawTargetTiled::DrawTargetTiled()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DrawTargetTiled::Init(const TileSet& aTiles)
|
|
|
|
{
|
|
|
|
if (!aTiles.mTileCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-06 05:40:03 -07:00
|
|
|
mTiles.reserve(aTiles.mTileCount);
|
|
|
|
for (size_t i = 0; i < aTiles.mTileCount; ++i) {
|
|
|
|
mTiles.push_back(aTiles.mTiles[i]);
|
|
|
|
if (!aTiles.mTiles[i].mDrawTarget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mTiles[0].mDrawTarget->GetFormat() != mTiles.back().mDrawTarget->GetFormat() ||
|
|
|
|
mTiles[0].mDrawTarget->GetBackendType() != mTiles.back().mDrawTarget->GetBackendType()) {
|
2014-06-11 12:53:02 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t newXMost = max(mRect.XMost(),
|
|
|
|
mTiles[i].mTileOrigin.x + mTiles[i].mDrawTarget->GetSize().width);
|
|
|
|
uint32_t newYMost = max(mRect.YMost(),
|
|
|
|
mTiles[i].mTileOrigin.y + mTiles[i].mDrawTarget->GetSize().height);
|
2014-08-28 09:45:48 -07:00
|
|
|
mRect.x = min(mRect.x, mTiles[i].mTileOrigin.x);
|
|
|
|
mRect.y = min(mRect.y, mTiles[i].mTileOrigin.y);
|
2014-06-11 12:53:02 -07:00
|
|
|
mRect.width = newXMost - mRect.x;
|
|
|
|
mRect.height = newYMost - mRect.y;
|
|
|
|
}
|
2014-08-28 20:07:35 -07:00
|
|
|
mFormat = mTiles[0].mDrawTarget->GetFormat();
|
2014-06-11 12:53:02 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-01 18:07:57 -07:00
|
|
|
class SnapshotTiled : public SourceSurface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SnapshotTiled(const vector<Tile>& aTiles, const IntRect& aRect)
|
|
|
|
: mRect(aRect)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < aTiles.size(); i++) {
|
|
|
|
mSnapshots.push_back(aTiles[i].mDrawTarget->Snapshot());
|
|
|
|
mOrigins.push_back(aTiles[i].mTileOrigin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SurfaceType GetType() const { return SurfaceType::TILED; }
|
|
|
|
virtual IntSize GetSize() const { return IntSize(mRect.XMost(), mRect.YMost()); }
|
|
|
|
virtual SurfaceFormat GetFormat() const { return mSnapshots[0]->GetFormat(); }
|
|
|
|
|
|
|
|
virtual TemporaryRef<DataSourceSurface> GetDataSurface()
|
|
|
|
{
|
|
|
|
RefPtr<DataSourceSurface> surf = Factory::CreateDataSourceSurface(GetSize(), GetFormat());
|
|
|
|
if (MOZ2D_WARN_IF(!surf)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface mappedSurf;
|
|
|
|
surf->Map(DataSourceSurface::MapType::WRITE, &mappedSurf);
|
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
Factory::CreateDrawTargetForData(BackendType::CAIRO, mappedSurf.mData,
|
|
|
|
GetSize(), mappedSurf.mStride, GetFormat());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mSnapshots.size(); i++) {
|
|
|
|
RefPtr<DataSourceSurface> dataSurf = mSnapshots[i]->GetDataSurface();
|
|
|
|
dt->CopySurface(dataSurf, IntRect(IntPoint(0, 0), mSnapshots[i]->GetSize()), mOrigins[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
surf->Unmap();
|
|
|
|
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
vector<RefPtr<SourceSurface>> mSnapshots;
|
|
|
|
vector<IntPoint> mOrigins;
|
|
|
|
IntRect mRect;
|
|
|
|
};
|
|
|
|
|
2014-06-11 12:53:02 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetTiled::Snapshot()
|
|
|
|
{
|
|
|
|
return new SnapshotTiled(mTiles, mRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TILED_COMMAND(command) \
|
|
|
|
void \
|
|
|
|
DrawTargetTiled::command() \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) { \
|
2014-09-01 18:07:57 -07:00
|
|
|
\
|
|
|
|
\
|
|
|
|
mTiles[i].mDrawTarget->command(); \
|
2014-06-11 12:53:02 -07:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define TILED_COMMAND1(command, type1) \
|
|
|
|
void \
|
|
|
|
DrawTargetTiled::command(type1 arg1) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) { \
|
2014-09-01 18:07:57 -07:00
|
|
|
\
|
|
|
|
\
|
|
|
|
mTiles[i].mDrawTarget->command(arg1); \
|
2014-06-11 12:53:02 -07:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define TILED_COMMAND3(command, type1, type2, type3) \
|
|
|
|
void \
|
|
|
|
DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) { \
|
2014-09-01 18:07:57 -07:00
|
|
|
\
|
|
|
|
\
|
|
|
|
mTiles[i].mDrawTarget->command(arg1, arg2, arg3); \
|
2014-06-11 12:53:02 -07:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define TILED_COMMAND4(command, type1, type2, type3, type4) \
|
|
|
|
void \
|
|
|
|
DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) { \
|
2014-09-01 18:07:57 -07:00
|
|
|
\
|
|
|
|
\
|
|
|
|
mTiles[i].mDrawTarget->command(arg1, arg2, arg3, arg4); \
|
2014-06-11 12:53:02 -07:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define TILED_COMMAND5(command, type1, type2, type3, type4, type5) \
|
|
|
|
void \
|
|
|
|
DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) { \
|
2014-09-01 18:07:57 -07:00
|
|
|
\
|
|
|
|
\
|
|
|
|
mTiles[i].mDrawTarget->command(arg1, arg2, arg3, arg4, arg5); \
|
2014-06-11 12:53:02 -07:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
TILED_COMMAND(Flush)
|
|
|
|
TILED_COMMAND4(DrawFilter, FilterNode*, const Rect&, const Point&, const DrawOptions&)
|
|
|
|
TILED_COMMAND1(ClearRect, const Rect&)
|
|
|
|
TILED_COMMAND4(MaskSurface, const Pattern&, SourceSurface*, Point, const DrawOptions&)
|
|
|
|
TILED_COMMAND4(StrokeRect, const Rect&, const Pattern&, const StrokeOptions&, const DrawOptions&)
|
|
|
|
TILED_COMMAND5(StrokeLine, const Point&, const Point&, const Pattern&, const StrokeOptions&, const DrawOptions&)
|
|
|
|
TILED_COMMAND5(FillGlyphs, ScaledFont*, const GlyphBuffer&, const Pattern&, const DrawOptions&, const GlyphRenderingOptions*)
|
|
|
|
TILED_COMMAND3(Mask, const Pattern&, const Pattern&, const DrawOptions&)
|
2014-09-01 18:07:57 -07:00
|
|
|
TILED_COMMAND1(PushClip, const Path*)
|
|
|
|
TILED_COMMAND1(PushClipRect, const Rect&)
|
|
|
|
TILED_COMMAND(PopClip)
|
2014-06-11 12:53:02 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetTiled::CopySurface(SourceSurface *aSurface,
|
|
|
|
const IntRect &aSourceRect,
|
|
|
|
const IntPoint &aDestination)
|
|
|
|
{
|
|
|
|
// CopySurface ignores the transform, account for that here.
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
IntRect src = aSourceRect;
|
|
|
|
src.x += mTiles[i].mTileOrigin.x;
|
|
|
|
src.width -= mTiles[i].mTileOrigin.x;
|
|
|
|
src.y = mTiles[i].mTileOrigin.y;
|
|
|
|
src.height -= mTiles[i].mTileOrigin.y;
|
|
|
|
|
|
|
|
if (src.width <= 0 || src.height <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTiles[i].mDrawTarget->CopySurface(aSurface, src, aDestination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetTiled::SetTransform(const Matrix& aTransform)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
Matrix mat = aTransform;
|
|
|
|
mat.PostTranslate(Float(-mTiles[i].mTileOrigin.x), Float(-mTiles[i].mTileOrigin.y));
|
|
|
|
mTiles[i].mDrawTarget->SetTransform(mat);
|
|
|
|
}
|
|
|
|
DrawTarget::SetTransform(aTransform);
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:20:10 -07:00
|
|
|
void
|
|
|
|
DrawTargetTiled::DrawSurface(SourceSurface* aSurface, const Rect& aDest, const Rect& aSource, const DrawSurfaceOptions& aSurfaceOptions, const DrawOptions& aDrawOptions)
|
|
|
|
{
|
|
|
|
Rect deviceRect = mTransform.TransformBounds(aDest);
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
if (deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
|
|
|
|
mTiles[i].mTileOrigin.y,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().width,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().height))) {
|
|
|
|
mTiles[i].mDrawTarget->DrawSurface(aSurface, aDest, aSource, aSurfaceOptions, aDrawOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetTiled::FillRect(const Rect& aRect, const Pattern& aPattern, const DrawOptions& aDrawOptions)
|
|
|
|
{
|
|
|
|
Rect deviceRect = mTransform.TransformBounds(aRect);
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
if (deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
|
|
|
|
mTiles[i].mTileOrigin.y,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().width,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().height))) {
|
|
|
|
mTiles[i].mDrawTarget->FillRect(aRect, aPattern, aDrawOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:20:33 -07:00
|
|
|
// The logic for this comes from _cairo_stroke_style_max_distance_from_path
|
|
|
|
static Rect
|
|
|
|
PathExtentsToMaxStrokeExtents(const StrokeOptions &aStrokeOptions,
|
|
|
|
const Rect &aRect,
|
|
|
|
const Matrix &aTransform)
|
|
|
|
{
|
|
|
|
double styleExpansionFactor = 0.5f;
|
|
|
|
|
|
|
|
if (aStrokeOptions.mLineCap == CapStyle::SQUARE) {
|
|
|
|
styleExpansionFactor = M_SQRT1_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStrokeOptions.mLineJoin == JoinStyle::MITER &&
|
|
|
|
styleExpansionFactor < M_SQRT2 * aStrokeOptions.mMiterLimit) {
|
|
|
|
styleExpansionFactor = M_SQRT2 * aStrokeOptions.mMiterLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
styleExpansionFactor *= aStrokeOptions.mLineWidth;
|
|
|
|
|
|
|
|
double dx = styleExpansionFactor * hypot(aTransform._11, aTransform._21);
|
|
|
|
double dy = styleExpansionFactor * hypot(aTransform._22, aTransform._12);
|
|
|
|
|
|
|
|
Rect result = aRect;
|
|
|
|
result.Inflate(dx, dy);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:20:10 -07:00
|
|
|
void
|
|
|
|
DrawTargetTiled::Stroke(const Path* aPath, const Pattern& aPattern, const StrokeOptions& aStrokeOptions, const DrawOptions& aDrawOptions)
|
|
|
|
{
|
2014-09-02 14:20:33 -07:00
|
|
|
// Approximate the stroke extents, since Path::GetStrokeExtents can be slow
|
|
|
|
Rect deviceRect = PathExtentsToMaxStrokeExtents(aStrokeOptions,
|
|
|
|
aPath->GetBounds(mTransform),
|
|
|
|
mTransform);
|
2014-09-02 14:20:10 -07:00
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
if (deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
|
|
|
|
mTiles[i].mTileOrigin.y,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().width,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().height))) {
|
|
|
|
mTiles[i].mDrawTarget->Stroke(aPath, aPattern, aStrokeOptions, aDrawOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetTiled::Fill(const Path* aPath, const Pattern& aPattern, const DrawOptions& aDrawOptions)
|
|
|
|
{
|
|
|
|
Rect deviceRect = aPath->GetBounds(mTransform);
|
|
|
|
for (size_t i = 0; i < mTiles.size(); i++) {
|
|
|
|
if (deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
|
|
|
|
mTiles[i].mTileOrigin.y,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().width,
|
|
|
|
mTiles[i].mDrawTarget->GetSize().height))) {
|
|
|
|
mTiles[i].mDrawTarget->Fill(aPath, aPattern, aDrawOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 12:53:02 -07:00
|
|
|
}
|
|
|
|
}
|