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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/* -*- 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/. */
#include "PathRecording.h"
#include "DrawEventRecorder.h"
#include "RecordedEventImpl.h"
namespace mozilla {
namespace gfx {
#define NEXT_PARAMS(_type) \
const _type params = *reinterpret_cast<const _type*>(nextByte); \
nextByte += sizeof(_type);
bool PathOps::StreamToSink(PathSink& aPathSink) const {
if (mPathData.empty()) {
return true;
}
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
while (nextByte < end) {
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
switch (opType) {
case OpType::OP_MOVETO: {
NEXT_PARAMS(Point)
aPathSink.MoveTo(params);
break;
}
case OpType::OP_LINETO: {
NEXT_PARAMS(Point)
aPathSink.LineTo(params);
break;
}
case OpType::OP_BEZIERTO: {
NEXT_PARAMS(ThreePoints)
aPathSink.BezierTo(params.p1, params.p2, params.p3);
break;
}
case OpType::OP_QUADRATICBEZIERTO: {
NEXT_PARAMS(TwoPoints)
aPathSink.QuadraticBezierTo(params.p1, params.p2);
break;
}
case OpType::OP_ARC: {
NEXT_PARAMS(ArcParams)
aPathSink.Arc(params.origin, params.radius, params.startAngle,
params.endAngle, params.antiClockwise);
break;
}
case OpType::OP_CLOSE:
aPathSink.Close();
break;
default:
return false;
}
}
return true;
}
#define CHECKED_NEXT_PARAMS(_type) \
if (nextByte + sizeof(_type) > end) { \
return false; \
} \
NEXT_PARAMS(_type)
bool PathOps::CheckedStreamToSink(PathSink& aPathSink) const {
if (mPathData.empty()) {
return true;
}
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
while (true) {
if (nextByte == end) {
break;
}
if (nextByte + sizeof(OpType) > end) {
return false;
}
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
switch (opType) {
case OpType::OP_MOVETO: {
CHECKED_NEXT_PARAMS(Point)
aPathSink.MoveTo(params);
break;
}
case OpType::OP_LINETO: {
CHECKED_NEXT_PARAMS(Point)
aPathSink.LineTo(params);
break;
}
case OpType::OP_BEZIERTO: {
CHECKED_NEXT_PARAMS(ThreePoints)
aPathSink.BezierTo(params.p1, params.p2, params.p3);
break;
}
case OpType::OP_QUADRATICBEZIERTO: {
CHECKED_NEXT_PARAMS(TwoPoints)
aPathSink.QuadraticBezierTo(params.p1, params.p2);
break;
}
case OpType::OP_ARC: {
CHECKED_NEXT_PARAMS(ArcParams)
aPathSink.Arc(params.origin, params.radius, params.startAngle,
params.endAngle, params.antiClockwise);
break;
}
case OpType::OP_CLOSE:
aPathSink.Close();
break;
default:
return false;
}
}
return true;
}
#undef CHECKED_NEXT_PARAMS
PathOps PathOps::TransformedCopy(const Matrix& aTransform) const {
PathOps newPathOps;
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
while (nextByte < end) {
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
switch (opType) {
case OpType::OP_MOVETO: {
NEXT_PARAMS(Point)
newPathOps.MoveTo(aTransform.TransformPoint(params));
break;
}
case OpType::OP_LINETO: {
NEXT_PARAMS(Point)
newPathOps.LineTo(aTransform.TransformPoint(params));
break;
}
case OpType::OP_BEZIERTO: {
NEXT_PARAMS(ThreePoints)
newPathOps.BezierTo(aTransform.TransformPoint(params.p1),
aTransform.TransformPoint(params.p2),
aTransform.TransformPoint(params.p3));
break;
}
case OpType::OP_QUADRATICBEZIERTO: {
NEXT_PARAMS(TwoPoints)
newPathOps.QuadraticBezierTo(aTransform.TransformPoint(params.p1),
aTransform.TransformPoint(params.p2));
break;
}
case OpType::OP_ARC: {
NEXT_PARAMS(ArcParams)
ArcToBezier(&newPathOps, params.origin,
gfx::Size(params.radius, params.radius), params.startAngle,
params.endAngle, params.antiClockwise, 0.0f, aTransform);
break;
}
case OpType::OP_CLOSE:
newPathOps.Close();
break;
default:
MOZ_CRASH("We control mOpTypes, so this should never happen.");
}
}
return newPathOps;
}
Maybe<Circle> PathOps::AsCircle() const {
if (mPathData.empty()) {
return Nothing();
}
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
if (opType == OpType::OP_ARC) {
NEXT_PARAMS(ArcParams)
if (fabs(fabs(params.startAngle - params.endAngle) - 2 * M_PI) < 1e-6) {
// we have a full circle
if (nextByte < end) {
const OpType nextOpType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
if (nextOpType == OpType::OP_CLOSE) {
if (nextByte == end) {
return Some(Circle{params.origin, params.radius, true});
}
}
} else {
// the circle wasn't closed
return Some(Circle{params.origin, params.radius, false});
}
}
}
return Nothing();
}
Maybe<Line> PathOps::AsLine() const {
if (mPathData.empty()) {
return Nothing();
}
Line retval;
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
if (opType == OpType::OP_MOVETO) {
MOZ_ASSERT(nextByte != end);
NEXT_PARAMS(Point)
retval.origin = params;
} else {
return Nothing();
}
if (nextByte >= end) {
return Nothing();
}
opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
if (opType == OpType::OP_LINETO) {
MOZ_ASSERT(nextByte != end);
NEXT_PARAMS(Point)
if (nextByte == end) {
retval.destination = params;
return Some(retval);
}
}
return Nothing();
}
#undef NEXT_PARAMS
size_t PathOps::NumberOfOps() const {
size_t size = 0;
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
while (nextByte < end) {
size++;
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
switch (opType) {
case OpType::OP_MOVETO:
nextByte += sizeof(Point);
break;
case OpType::OP_LINETO:
nextByte += sizeof(Point);
break;
case OpType::OP_BEZIERTO:
nextByte += sizeof(ThreePoints);
break;
case OpType::OP_QUADRATICBEZIERTO:
nextByte += sizeof(TwoPoints);
break;
case OpType::OP_ARC:
nextByte += sizeof(ArcParams);
break;
case OpType::OP_CLOSE:
break;
default:
MOZ_CRASH("We control mOpTypes, so this should never happen.");
}
}
return size;
}
bool PathOps::IsEmpty() const {
const uint8_t* nextByte = mPathData.data();
const uint8_t* end = nextByte + mPathData.size();
while (nextByte < end) {
const OpType opType = *reinterpret_cast<const OpType*>(nextByte);
nextByte += sizeof(OpType);
switch (opType) {
case OpType::OP_MOVETO:
nextByte += sizeof(Point);
break;
case OpType::OP_CLOSE:
break;
default:
return false;
}
}
return true;
}
void PathBuilderRecording::MoveTo(const Point& aPoint) {
mPathOps.MoveTo(aPoint);
mBeginPoint = aPoint;
mCurrentPoint = aPoint;
}
void PathBuilderRecording::LineTo(const Point& aPoint) {
mPathOps.LineTo(aPoint);
mCurrentPoint = aPoint;
}
void PathBuilderRecording::BezierTo(const Point& aCP1, const Point& aCP2,
const Point& aCP3) {
mPathOps.BezierTo(aCP1, aCP2, aCP3);
mCurrentPoint = aCP3;
}
void PathBuilderRecording::QuadraticBezierTo(const Point& aCP1,
const Point& aCP2) {
mPathOps.QuadraticBezierTo(aCP1, aCP2);
mCurrentPoint = aCP2;
}
void PathBuilderRecording::Close() {
mPathOps.Close();
mCurrentPoint = mBeginPoint;
}
void PathBuilderRecording::Arc(const Point& aOrigin, float aRadius,
float aStartAngle, float aEndAngle,
bool aAntiClockwise) {
mPathOps.Arc(aOrigin, aRadius, aStartAngle, aEndAngle, aAntiClockwise);
mCurrentPoint = aOrigin + Point(cosf(aEndAngle), sinf(aEndAngle)) * aRadius;
}
already_AddRefed<Path> PathBuilderRecording::Finish() {
return MakeAndAddRef<PathRecording>(mBackendType, std::move(mPathOps),
mFillRule, mCurrentPoint, mBeginPoint);
}
PathRecording::PathRecording(BackendType aBackend, PathOps&& aOps,
FillRule aFillRule, const Point& aCurrentPoint,
const Point& aBeginPoint)
: mBackendType(aBackend),
mPathOps(std::move(aOps)),
mFillRule(aFillRule),
mCurrentPoint(aCurrentPoint),
mBeginPoint(aBeginPoint) {}
PathRecording::~PathRecording() {
for (size_t i = 0; i < mStoredRecorders.size(); i++) {
mStoredRecorders[i]->RemoveStoredObject(this);
mStoredRecorders[i]->RecordEvent(RecordedPathDestruction(this));
}
}
void PathRecording::EnsurePath() const {
if (mPath) {
return;
}
if (RefPtr<PathBuilder> pathBuilder =
Factory::CreatePathBuilder(mBackendType, mFillRule)) {
if (!mPathOps.StreamToSink(*pathBuilder)) {
MOZ_ASSERT(false, "Failed to stream PathOps to PathBuilder");
} else {
mPath = pathBuilder->Finish();
MOZ_ASSERT(!!mPath, "Failed finishing Path from PathBuilder");
}
} else {
MOZ_ASSERT(false, "Failed to create PathBuilder for PathRecording");
}
}
already_AddRefed<PathBuilder> PathRecording::CopyToBuilder(
FillRule aFillRule) const {
RefPtr<PathBuilderRecording> recording =
new PathBuilderRecording(mBackendType, PathOps(mPathOps), aFillRule);
recording->SetCurrentPoint(mCurrentPoint);
recording->SetBeginPoint(mBeginPoint);
return recording.forget();
}
already_AddRefed<PathBuilder> PathRecording::TransformedCopyToBuilder(
const Matrix& aTransform, FillRule aFillRule) const {
RefPtr<PathBuilderRecording> recording = new PathBuilderRecording(
mBackendType, mPathOps.TransformedCopy(aTransform), aFillRule);
recording->SetCurrentPoint(aTransform.TransformPoint(mCurrentPoint));
recording->SetBeginPoint(aTransform.TransformPoint(mBeginPoint));
return recording.forget();
}
} // namespace gfx
} // namespace mozilla
|