2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-09-24 19:02:50 +04:00
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_PATHRECORDING_H_
|
|
|
|
#define MOZILLA_GFX_PATHRECORDING_H_
|
|
|
|
|
|
|
|
#include "2D.h"
|
|
|
|
#include <vector>
|
2012-11-08 23:22:16 +04:00
|
|
|
#include <ostream>
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2016-08-08 19:11:38 +03:00
|
|
|
#include "PathHelpers.h"
|
2018-12-02 17:20:00 +03:00
|
|
|
#include "RecordingTypes.h"
|
2016-08-08 19:11:38 +03:00
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
class PathOps {
|
|
|
|
public:
|
2020-03-04 18:39:20 +03:00
|
|
|
PathOps() = default;
|
2018-12-02 17:20:00 +03:00
|
|
|
|
|
|
|
template <class S>
|
|
|
|
explicit PathOps(S& aStream);
|
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
PathOps(const PathOps& aOther) = default;
|
|
|
|
PathOps& operator=(const PathOps&) = delete; // assign using std::move()!
|
2018-12-02 17:20:00 +03:00
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
PathOps(PathOps&& aOther) = default;
|
|
|
|
PathOps& operator=(PathOps&& aOther) = default;
|
2018-12-02 17:20:00 +03:00
|
|
|
|
|
|
|
template <class S>
|
|
|
|
void Record(S& aStream) const;
|
|
|
|
|
|
|
|
bool StreamToSink(PathSink& aPathSink) const;
|
|
|
|
|
|
|
|
PathOps TransformedCopy(const Matrix& aTransform) const;
|
|
|
|
|
|
|
|
size_t NumberOfOps() const;
|
|
|
|
|
|
|
|
void MoveTo(const Point& aPoint) { AppendPathOp(OpType::OP_MOVETO, aPoint); }
|
|
|
|
|
|
|
|
void LineTo(const Point& aPoint) { AppendPathOp(OpType::OP_LINETO, aPoint); }
|
|
|
|
|
|
|
|
void BezierTo(const Point& aCP1, const Point& aCP2, const Point& aCP3) {
|
|
|
|
AppendPathOp(OpType::OP_BEZIERTO, ThreePoints{aCP1, aCP2, aCP3});
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuadraticBezierTo(const Point& aCP1, const Point& aCP2) {
|
|
|
|
AppendPathOp(OpType::OP_QUADRATICBEZIERTO, TwoPoints{aCP1, aCP2});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Arc(const Point& aOrigin, float aRadius, float aStartAngle,
|
|
|
|
float aEndAngle, bool aAntiClockwise) {
|
|
|
|
AppendPathOp(OpType::OP_ARC, ArcParams{aOrigin, aRadius, aStartAngle,
|
|
|
|
aEndAngle, aAntiClockwise});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
|
|
|
size_t oldSize = mPathData.size();
|
|
|
|
mPathData.resize(oldSize + sizeof(OpType));
|
|
|
|
*reinterpret_cast<OpType*>(mPathData.data() + oldSize) = OpType::OP_CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class OpType : uint32_t {
|
|
|
|
OP_MOVETO = 0,
|
|
|
|
OP_LINETO,
|
|
|
|
OP_BEZIERTO,
|
|
|
|
OP_QUADRATICBEZIERTO,
|
|
|
|
OP_ARC,
|
|
|
|
OP_CLOSE,
|
|
|
|
OP_INVALID
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void AppendPathOp(const OpType& aOpType, const T& aOpParams) {
|
|
|
|
size_t oldSize = mPathData.size();
|
|
|
|
mPathData.resize(oldSize + sizeof(OpType) + sizeof(T));
|
|
|
|
memcpy(mPathData.data() + oldSize, &aOpType, sizeof(OpType));
|
|
|
|
oldSize += sizeof(OpType);
|
|
|
|
memcpy(mPathData.data() + oldSize, &aOpParams, sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TwoPoints {
|
|
|
|
Point p1;
|
|
|
|
Point p2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ThreePoints {
|
|
|
|
Point p1;
|
|
|
|
Point p2;
|
|
|
|
Point p3;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ArcParams {
|
|
|
|
Point origin;
|
|
|
|
float radius;
|
|
|
|
float startAngle;
|
|
|
|
float endAngle;
|
|
|
|
bool antiClockwise;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<uint8_t> mPathData;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
PathOps::PathOps(S& aStream) {
|
|
|
|
ReadVector(aStream, mPathData);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
inline void PathOps::Record(S& aStream) const {
|
|
|
|
WriteVector(aStream, mPathData);
|
|
|
|
}
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
class PathRecording;
|
|
|
|
class DrawEventRecorderPrivate;
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
class PathBuilderRecording final : public PathBuilder {
|
2012-09-24 19:02:50 +04:00
|
|
|
public:
|
2017-11-06 06:37:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathBuilderRecording, override)
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
PathBuilderRecording(PathBuilder* aBuilder, FillRule aFillRule)
|
|
|
|
: mPathBuilder(aBuilder), mFillRule(aFillRule) {}
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
PathBuilderRecording(PathBuilder* aBuilder, const PathOps& aPathOps,
|
|
|
|
FillRule aFillRule)
|
|
|
|
: mPathBuilder(aBuilder), mFillRule(aFillRule), mPathOps(aPathOps) {}
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
/* Move the current point in the path, any figure currently being drawn will
|
|
|
|
* be considered closed during fill operations, however when stroking the
|
|
|
|
* closing line segment will not be drawn.
|
|
|
|
*/
|
2018-12-02 17:20:00 +03:00
|
|
|
void MoveTo(const Point& aPoint) final;
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
/* Add a linesegment to the current figure */
|
2018-12-02 17:20:00 +03:00
|
|
|
void LineTo(const Point& aPoint) final;
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
/* Add a cubic bezier curve to the current figure */
|
2018-12-02 17:20:00 +03:00
|
|
|
void BezierTo(const Point& aCP1, const Point& aCP2, const Point& aCP3) final;
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
/* Add a quadratic bezier curve to the current figure */
|
2018-12-02 17:20:00 +03:00
|
|
|
void QuadraticBezierTo(const Point& aCP1, const Point& aCP2) final;
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
/* Close the current figure, this will essentially generate a line segment
|
|
|
|
* from the current point to the starting point for the current figure
|
|
|
|
*/
|
2018-12-02 17:20:00 +03:00
|
|
|
void Close() final;
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
/* Add an arc to the current figure */
|
2018-12-02 17:20:00 +03:00
|
|
|
void Arc(const Point& aOrigin, float aRadius, float aStartAngle,
|
|
|
|
float aEndAngle, bool aAntiClockwise) final;
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
/* Point the current subpath is at - or where the next subpath will start
|
|
|
|
* if there is no active subpath.
|
|
|
|
*/
|
2019-06-05 10:55:46 +03:00
|
|
|
Point CurrentPoint() const final { return mPathBuilder->CurrentPoint(); }
|
|
|
|
|
|
|
|
Point BeginPoint() const final { return mPathBuilder->BeginPoint(); }
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
void SetCurrentPoint(const Point& aPoint) final {
|
2019-06-05 10:55:46 +03:00
|
|
|
mPathBuilder->SetCurrentPoint(aPoint);
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
void SetBeginPoint(const Point& aPoint) final {
|
2019-06-05 10:55:46 +03:00
|
|
|
mPathBuilder->SetBeginPoint(aPoint);
|
|
|
|
}
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
already_AddRefed<Path> Finish() final;
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
BackendType GetBackendType() const final { return BackendType::RECORDING; }
|
2014-10-04 15:13:29 +04:00
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> mPathBuilder;
|
2012-09-24 19:02:50 +04:00
|
|
|
FillRule mFillRule;
|
2018-12-02 17:20:00 +03:00
|
|
|
PathOps mPathOps;
|
2012-09-24 19:02:50 +04:00
|
|
|
};
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
class PathRecording final : public Path {
|
2012-09-24 19:02:50 +04:00
|
|
|
public:
|
2017-11-06 06:37:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathRecording, override)
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
PathRecording(Path* aPath, PathOps&& aOps, FillRule aFillRule,
|
2020-04-05 02:48:48 +03:00
|
|
|
const Point& aCurrentPoint, const Point& aBeginPoint);
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
~PathRecording();
|
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
BackendType GetBackendType() const final { return BackendType::RECORDING; }
|
|
|
|
already_AddRefed<PathBuilder> CopyToBuilder(FillRule aFillRule) const final;
|
|
|
|
already_AddRefed<PathBuilder> TransformedCopyToBuilder(
|
|
|
|
const Matrix& aTransform, FillRule aFillRule) const final;
|
|
|
|
bool ContainsPoint(const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const final {
|
2012-09-24 19:02:50 +04:00
|
|
|
return mPath->ContainsPoint(aPoint, aTransform);
|
|
|
|
}
|
2018-12-02 17:20:00 +03:00
|
|
|
bool StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const final {
|
2012-10-29 19:54:53 +04:00
|
|
|
return mPath->StrokeContainsPoint(aStrokeOptions, aPoint, aTransform);
|
|
|
|
}
|
2017-11-06 06:37:28 +03:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
Rect GetBounds(const Matrix& aTransform = Matrix()) const final {
|
2012-09-24 19:02:50 +04:00
|
|
|
return mPath->GetBounds(aTransform);
|
|
|
|
}
|
2017-11-06 06:37:28 +03:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
Rect GetStrokedBounds(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Matrix& aTransform = Matrix()) const final {
|
2012-09-24 19:02:50 +04:00
|
|
|
return mPath->GetStrokedBounds(aStrokeOptions, aTransform);
|
|
|
|
}
|
2013-11-07 13:10:12 +04:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
void StreamToSink(PathSink* aSink) const final { mPath->StreamToSink(aSink); }
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2018-12-02 17:20:00 +03:00
|
|
|
FillRule GetFillRule() const final { return mFillRule; }
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
private:
|
2017-06-12 23:52:29 +03:00
|
|
|
friend class DrawTargetWrapAndRecord;
|
2012-09-24 19:02:50 +04:00
|
|
|
friend class DrawTargetRecording;
|
|
|
|
friend class RecordedPathCreation;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Path> mPath;
|
2018-12-02 17:20:00 +03:00
|
|
|
PathOps mPathOps;
|
2012-09-24 19:02:50 +04:00
|
|
|
FillRule mFillRule;
|
2019-06-05 10:55:46 +03:00
|
|
|
Point mCurrentPoint;
|
|
|
|
Point mBeginPoint;
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
// Event recorders that have this path in their event stream.
|
2016-12-09 05:25:50 +03:00
|
|
|
std::vector<RefPtr<DrawEventRecorderPrivate>> mStoredRecorders;
|
2012-09-24 19:02:50 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_PATHRECORDING_H_ */
|