DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/* -*- 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
 * 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>
#include <ostream>

#include "PathHelpers.h"
#include "RecordingTypes.h"

namespace mozilla {
namespace gfx {

class PathOps {
 public:
  PathOps() {}

  template <class S>
  explicit PathOps(S& aStream);

  PathOps(PathOps&& aOther) : mPathData(std::move(aOther.mPathData)) {}

  PathOps(const PathOps& aOther) : mPathData(aOther.mPathData) {}

  PathOps& operator=(PathOps&& aOther) {
    mPathData = std::move(aOther.mPathData);
    return *this;
  }

  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:
  void operator=(const PathOps&) = delete;  // assign using std::move()!

  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);
}

class PathRecording;
class DrawEventRecorderPrivate;

class PathBuilderRecording final : public PathBuilder {
 public:
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathBuilderRecording, override)

  PathBuilderRecording(PathBuilder* aBuilder, FillRule aFillRule)
      : mPathBuilder(aBuilder), mFillRule(aFillRule) {}

  PathBuilderRecording(PathBuilder* aBuilder, const PathOps& aPathOps,
                       FillRule aFillRule)
      : mPathBuilder(aBuilder), mFillRule(aFillRule), mPathOps(aPathOps) {}

  /* 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.
   */
  void MoveTo(const Point& aPoint) final;

  /* Add a linesegment to the current figure */
  void LineTo(const Point& aPoint) final;

  /* Add a cubic bezier curve to the current figure */
  void BezierTo(const Point& aCP1, const Point& aCP2, const Point& aCP3) final;

  /* Add a quadratic bezier curve to the current figure */
  void QuadraticBezierTo(const Point& aCP1, const Point& aCP2) final;

  /* Close the current figure, this will essentially generate a line segment
   * from the current point to the starting point for the current figure
   */
  void Close() final;

  /* Add an arc to the current figure */
  void Arc(const Point& aOrigin, float aRadius, float aStartAngle,
           float aEndAngle, bool aAntiClockwise) final;

  /* Point the current subpath is at - or where the next subpath will start
   * if there is no active subpath.
   */
  Point CurrentPoint() const final { return mPathBuilder->CurrentPoint(); }

  Point BeginPoint() const final { return mPathBuilder->BeginPoint(); }

  void SetCurrentPoint(const Point& aPoint) final {
    mPathBuilder->SetCurrentPoint(aPoint);
  }

  void SetBeginPoint(const Point& aPoint) final {
    mPathBuilder->SetBeginPoint(aPoint);
  }

  already_AddRefed<Path> Finish() final;

  BackendType GetBackendType() const final { return BackendType::RECORDING; }

 private:
  RefPtr<PathBuilder> mPathBuilder;
  FillRule mFillRule;
  PathOps mPathOps;
};

class PathRecording final : public Path {
 public:
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathRecording, override)

  PathRecording(Path* aPath, PathOps&& aOps, FillRule aFillRule,
                const Point& aCurrentPoint, const Point& aBeginPoint)
      : mPath(aPath),
        mPathOps(std::move(aOps)),
        mFillRule(aFillRule),
        mCurrentPoint(aCurrentPoint),
        mBeginPoint(aBeginPoint) {}

  ~PathRecording();

  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 {
    return mPath->ContainsPoint(aPoint, aTransform);
  }
  bool StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
                           const Point& aPoint,
                           const Matrix& aTransform) const final {
    return mPath->StrokeContainsPoint(aStrokeOptions, aPoint, aTransform);
  }

  Rect GetBounds(const Matrix& aTransform = Matrix()) const final {
    return mPath->GetBounds(aTransform);
  }

  Rect GetStrokedBounds(const StrokeOptions& aStrokeOptions,
                        const Matrix& aTransform = Matrix()) const final {
    return mPath->GetStrokedBounds(aStrokeOptions, aTransform);
  }

  void StreamToSink(PathSink* aSink) const final { mPath->StreamToSink(aSink); }

  FillRule GetFillRule() const final { return mFillRule; }

 private:
  friend class DrawTargetWrapAndRecord;
  friend class DrawTargetRecording;
  friend class RecordedPathCreation;

  RefPtr<Path> mPath;
  PathOps mPathOps;
  FillRule mFillRule;
  Point mCurrentPoint;
  Point mBeginPoint;

  // Event recorders that have this path in their event stream.
  std::vector<RefPtr<DrawEventRecorderPrivate>> mStoredRecorders;
};

}  // namespace gfx
}  // namespace mozilla

#endif /* MOZILLA_GFX_PATHRECORDING_H_ */