Debian Codegraph @

include/core/SkRegion.h

raw
1 /*
2 * Copyright 2005 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkRegion_DEFINED
9 #define SkRegion_DEFINED
10
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRect.h"
13 #include "include/private/base/SkAPI.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkTypeTraits.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <type_traits>
21
22 class SkPathBuilder;
23
24 /** \class SkRegion
25 SkRegion describes the set of pixels used to clip SkCanvas. SkRegion is compact,
26 efficiently storing a single integer rectangle, or a run length encoded array
27 of rectangles. SkRegion may reduce the current SkCanvas clip, or may be drawn as
28 one or more integer rectangles. SkRegion iterator returns the scan lines or
29 rectangles contained by it, optionally intersecting a bounding rectangle.
30 */
31 class SK_API SkRegion {
32 typedef int32_t RunType;
33 public:
34
35 /** Constructs an empty SkRegion. SkRegion is set to empty bounds
36 at (0, 0) with zero width and height.
37
38 @return empty SkRegion
39
40 example: https://fiddle.skia.org/c/@Region_empty_constructor
41 */
42 SkRegion();
43
44 /** Constructs a copy of an existing region.
45 Copy constructor makes two regions identical by value. Internally, region and
46 the returned result share pointer values. The underlying SkRect array is
47 copied when modified.
48
49 Creating a SkRegion copy is very efficient and never allocates memory.
50 SkRegion are always copied by value from the interface; the underlying shared
51 pointers are not exposed.
52
53 @param region SkRegion to copy by value
54 @return copy of SkRegion
55
56 example: https://fiddle.skia.org/c/@Region_copy_const_SkRegion
57 */
58 SkRegion(const SkRegion& region);
59
60 /** Constructs a rectangular SkRegion matching the bounds of rect.
61
62 @param rect bounds of constructed SkRegion
63 @return rectangular SkRegion
64
65 example: https://fiddle.skia.org/c/@Region_copy_const_SkIRect
66 */
67 explicit SkRegion(const SkIRect& rect);
68
69 /** Releases ownership of any shared data and deletes data if SkRegion is sole owner.
70
71 example: https://fiddle.skia.org/c/@Region_destructor
72 */
73 ~SkRegion();
74
75 /** Constructs a copy of an existing region.
76 Makes two regions identical by value. Internally, region and
77 the returned result share pointer values. The underlying SkRect array is
78 copied when modified.
79
80 Creating a SkRegion copy is very efficient and never allocates memory.
81 SkRegion are always copied by value from the interface; the underlying shared
82 pointers are not exposed.
83
84 @param region SkRegion to copy by value
85 @return SkRegion to copy by value
86
87 example: https://fiddle.skia.org/c/@Region_copy_operator
88 */
89 SkRegion& operator=(const SkRegion& region);
90
91 /** Compares SkRegion and other; returns true if they enclose exactly
92 the same area.
93
94 @param other SkRegion to compare
95 @return true if SkRegion pair are equivalent
96
97 example: https://fiddle.skia.org/c/@Region_equal1_operator
98 */
99 bool operator==(const SkRegion& other) const;
100
101 /** Compares SkRegion and other; returns true if they do not enclose the same area.
102
103 @param other SkRegion to compare
104 @return true if SkRegion pair are not equivalent
105 */
106 bool operator!=(const SkRegion& other) const {
107 return !(*this == other);
108 }
109
110 /** Sets SkRegion to src, and returns true if src bounds is not empty.
111 This makes SkRegion and src identical by value. Internally,
112 SkRegion and src share pointer values. The underlying SkRect array is
113 copied when modified.
114
115 Creating a SkRegion copy is very efficient and never allocates memory.
116 SkRegion are always copied by value from the interface; the underlying shared
117 pointers are not exposed.
118
119 @param src SkRegion to copy
120 @return copy of src
121 */
122 bool set(const SkRegion& src) {
123 *this = src;
124 return !this->isEmpty();
125 }
126
127 /** Exchanges SkIRect array of SkRegion and other. swap() internally exchanges pointers,
128 so it is lightweight and does not allocate memory.
129
130 swap() usage has largely been replaced by operator=(const SkRegion& region).
131 SkPath do not copy their content on assignment until they are written to,
132 making assignment as efficient as swap().
133
134 @param other operator=(const SkRegion& region) set
135
136 example: https://fiddle.skia.org/c/@Region_swap
137 */
138 void swap(SkRegion& other);
139
140 /** Returns true if SkRegion is empty.
141 Empty SkRegion has bounds width or height less than or equal to zero.
142 SkRegion() constructs empty SkRegion; setEmpty()
143 and setRect() with dimensionless data make SkRegion empty.
144
145 @return true if bounds has no width or height
146 */
147 bool isEmpty() const { return fRunHead == emptyRunHeadPtr(); }
148
149 /** Returns true if SkRegion is one SkIRect with positive dimensions.
150
151 @return true if SkRegion contains one SkIRect
152 */
153 bool isRect() const { return fRunHead == kRectRunHeadPtr; }
154
155 /** Returns true if SkRegion is described by more than one rectangle.
156
157 @return true if SkRegion contains more than one SkIRect
158 */
159 bool isComplex() const { return !this->isEmpty() && !this->isRect(); }
160
161 /** Returns minimum and maximum axes values of SkIRect array.
162 Returns (0, 0, 0, 0) if SkRegion is empty.
163
164 @return combined bounds of all SkIRect elements
165 */
166 const SkIRect& getBounds() const { return fBounds; }
167
168 /** Returns a value that increases with the number of
169 elements in SkRegion. Returns zero if SkRegion is empty.
170 Returns one if SkRegion equals SkIRect; otherwise, returns
171 value greater than one indicating that SkRegion is complex.
172
173 Call to compare SkRegion for relative complexity.
174
175 @return relative complexity
176
177 example: https://fiddle.skia.org/c/@Region_computeRegionComplexity
178 */
179 int computeRegionComplexity() const;
180
181 /** Appends outline of SkRegion to path builder.
182 Returns true if SkRegion is not empty; otherwise, returns false, and leaves path
183 unmodified.
184
185 @param path SkPath to append to
186 @return true if path changed
187
188 example: https://fiddle.skia.org/c/@Region_getBoundaryPath
189 */
190 bool addBoundaryPath(SkPathBuilder*) const;
191
192 /**
193 * Return the boundary of the region as a path.
194 */
195 SkPath getBoundaryPath() const;
196
197 /** Constructs an empty SkRegion. SkRegion is set to empty bounds
198 at (0, 0) with zero width and height. Always returns false.
199
200 @return false
201
202 example: https://fiddle.skia.org/c/@Region_setEmpty
203 */
204 bool setEmpty();
205
206 /** Constructs a rectangular SkRegion matching the bounds of rect.
207 If rect is empty, constructs empty and returns false.
208
209 @param rect bounds of constructed SkRegion
210 @return true if rect is not empty
211
212 example: https://fiddle.skia.org/c/@Region_setRect
213 */
214 bool setRect(const SkIRect& rect);
215
216 /** Constructs SkRegion as the union of SkIRect in rects array. If count is
217 zero, constructs empty SkRegion. Returns false if constructed SkRegion is empty.
218
219 May be faster than repeated calls to op().
220
221 @param rects array of SkIRect
222 @param count array size
223 @return true if constructed SkRegion is not empty
224
225 example: https://fiddle.skia.org/c/@Region_setRects
226 */
227 bool setRects(const SkIRect rects[], int count);
228
229 /** Constructs a copy of an existing region.
230 Makes two regions identical by value. Internally, region and
231 the returned result share pointer values. The underlying SkRect array is
232 copied when modified.
233
234 Creating a SkRegion copy is very efficient and never allocates memory.
235 SkRegion are always copied by value from the interface; the underlying shared
236 pointers are not exposed.
237
238 @param region SkRegion to copy by value
239 @return SkRegion to copy by value
240
241 example: https://fiddle.skia.org/c/@Region_setRegion
242 */
243 bool setRegion(const SkRegion& region);
244
245 /** Constructs SkRegion to match outline of path within clip.
246 Returns false if constructed SkRegion is empty.
247
248 Constructed SkRegion draws the same pixels as path through clip when
249 anti-aliasing is disabled.
250
251 @param path SkPath providing outline
252 @param clip SkRegion containing path
253 @return true if constructed SkRegion is not empty
254
255 example: https://fiddle.skia.org/c/@Region_setPath
256 */
257 bool setPath(const SkPath& path, const SkRegion& clip);
258
259 /** Returns true if SkRegion intersects rect.
260 Returns false if either rect or SkRegion is empty, or do not intersect.
261
262 @param rect SkIRect to intersect
263 @return true if rect and SkRegion have area in common
264
265 example: https://fiddle.skia.org/c/@Region_intersects
266 */
267 bool intersects(const SkIRect& rect) const;
268
269 /** Returns true if SkRegion intersects other.
270 Returns false if either other or SkRegion is empty, or do not intersect.
271
272 @param other SkRegion to intersect
273 @return true if other and SkRegion have area in common
274
275 example: https://fiddle.skia.org/c/@Region_intersects_2
276 */
277 bool intersects(const SkRegion& other) const;
278
279 /** Returns true if SkIPoint (x, y) is inside SkRegion.
280 Returns false if SkRegion is empty.
281
282 @param x test SkIPoint x-coordinate
283 @param y test SkIPoint y-coordinate
284 @return true if (x, y) is inside SkRegion
285
286 example: https://fiddle.skia.org/c/@Region_contains
287 */
288 bool contains(int32_t x, int32_t y) const;
289
290 /** Returns true if other is completely inside SkRegion.
291 Returns false if SkRegion or other is empty.
292
293 @param other SkIRect to contain
294 @return true if other is inside SkRegion
295
296 example: https://fiddle.skia.org/c/@Region_contains_2
297 */
298 bool contains(const SkIRect& other) const;
299
300 /** Returns true if other is completely inside SkRegion.
301 Returns false if SkRegion or other is empty.
302
303 @param other SkRegion to contain
304 @return true if other is inside SkRegion
305
306 example: https://fiddle.skia.org/c/@Region_contains_3
307 */
308 bool contains(const SkRegion& other) const;
309
310 /** Returns true if SkRegion is a single rectangle and contains r.
311 May return false even though SkRegion contains r.
312
313 @param r SkIRect to contain
314 @return true quickly if r points are equal or inside
315 */
316 bool quickContains(const SkIRect& r) const {
317 SkASSERT(this->isEmpty() == fBounds.isEmpty()); // valid region
318
319 return r.fLeft < r.fRight && r.fTop < r.fBottom &&
320 fRunHead == kRectRunHeadPtr && // this->isRect()
321 /* fBounds.contains(left, top, right, bottom); */
322 fBounds.fLeft <= r.fLeft && fBounds.fTop <= r.fTop &&
323 fBounds.fRight >= r.fRight && fBounds.fBottom >= r.fBottom;
324 }
325
326 /** Returns true if SkRegion does not intersect rect.
327 Returns true if rect is empty or SkRegion is empty.
328 May return false even though SkRegion does not intersect rect.
329
330 @param rect SkIRect to intersect
331 @return true if rect does not intersect
332 */
333 bool quickReject(const SkIRect& rect) const {
334 return this->isEmpty() || rect.isEmpty() ||
335 !SkIRect::Intersects(fBounds, rect);
336 }
337
338 /** Returns true if SkRegion does not intersect rgn.
339 Returns true if rgn is empty or SkRegion is empty.
340 May return false even though SkRegion does not intersect rgn.
341
342 @param rgn SkRegion to intersect
343 @return true if rgn does not intersect
344 */
345 bool quickReject(const SkRegion& rgn) const {
346 return this->isEmpty() || rgn.isEmpty() ||
347 !SkIRect::Intersects(fBounds, rgn.fBounds);
348 }
349
350 /** Offsets SkRegion by ivector (dx, dy). Has no effect if SkRegion is empty.
351
352 @param dx x-axis offset
353 @param dy y-axis offset
354 */
355 void translate(int dx, int dy) { this->translate(dx, dy, this); }
356
357 /** Offsets SkRegion by ivector (dx, dy), writing result to dst. SkRegion may be passed
358 as dst parameter, translating SkRegion in place. Has no effect if dst is nullptr.
359 If SkRegion is empty, sets dst to empty.
360
361 @param dx x-axis offset
362 @param dy y-axis offset
363 @param dst translated result
364
365 example: https://fiddle.skia.org/c/@Region_translate_2
366 */
367 void translate(int dx, int dy, SkRegion* dst) const;
368
369 /** \enum SkRegion::Op
370 The logical operations that can be performed when combining two SkRegion.
371 */
372 enum Op {
373 kDifference_Op, //!< target minus operand
374 kIntersect_Op, //!< target intersected with operand
375 kUnion_Op, //!< target unioned with operand
376 kXOR_Op, //!< target exclusive or with operand
377 kReverseDifference_Op, //!< operand minus target
378 kReplace_Op, //!< replace target with operand
379 kLastOp = kReplace_Op, //!< last operator
380 };
381
382 static const int kOpCnt = kLastOp + 1;
383
384 /** Replaces SkRegion with the result of SkRegion op rect.
385 Returns true if replaced SkRegion is not empty.
386
387 @param rect SkIRect operand
388 @return false if result is empty
389 */
390 bool op(const SkIRect& rect, Op op) {
391 if (this->isRect() && kIntersect_Op == op) {
392 if (!fBounds.intersect(rect)) {
393 return this->setEmpty();
394 }
395 return true;
396 }
397 return this->op(*this, rect, op);
398 }
399
400 /** Replaces SkRegion with the result of SkRegion op rgn.
401 Returns true if replaced SkRegion is not empty.
402
403 @param rgn SkRegion operand
404 @return false if result is empty
405 */
406 bool op(const SkRegion& rgn, Op op) { return this->op(*this, rgn, op); }
407
408 /** Replaces SkRegion with the result of rect op rgn.
409 Returns true if replaced SkRegion is not empty.
410
411 @param rect SkIRect operand
412 @param rgn SkRegion operand
413 @return false if result is empty
414
415 example: https://fiddle.skia.org/c/@Region_op_4
416 */
417 bool op(const SkIRect& rect, const SkRegion& rgn, Op op);
418
419 /** Replaces SkRegion with the result of rgn op rect.
420 Returns true if replaced SkRegion is not empty.
421
422 @param rgn SkRegion operand
423 @param rect SkIRect operand
424 @return false if result is empty
425
426 example: https://fiddle.skia.org/c/@Region_op_5
427 */
428 bool op(const SkRegion& rgn, const SkIRect& rect, Op op);
429
430 /** Replaces SkRegion with the result of rgna op rgnb.
431 Returns true if replaced SkRegion is not empty.
432
433 @param rgna SkRegion operand
434 @param rgnb SkRegion operand
435 @return false if result is empty
436
437 example: https://fiddle.skia.org/c/@Region_op_6
438 */
439 bool op(const SkRegion& rgna, const SkRegion& rgnb, Op op);
440
441 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
442 /** Private. Android framework only.
443
444 @return string representation of SkRegion
445 */
446 char* toString();
447 #endif
448
449 /** \class SkRegion::Iterator
450 Returns sequence of rectangles, sorted along y-axis, then x-axis, that make
451 up SkRegion.
452 */
453 class SK_API Iterator {
454 public:
455
456 /** Initializes SkRegion::Iterator with an empty SkRegion. done() on SkRegion::Iterator
457 returns true.
458 Call reset() to initialized SkRegion::Iterator at a later time.
459
460 @return empty SkRegion iterator
461 */
462 Iterator() : fRgn(nullptr), fDone(true) {}
463
464 /** Sets SkRegion::Iterator to return elements of SkIRect array in region.
465
466 @param region SkRegion to iterate
467 @return SkRegion iterator
468
469 example: https://fiddle.skia.org/c/@Region_Iterator_copy_const_SkRegion
470 */
471 Iterator(const SkRegion& region);
472
473 /** SkPoint SkRegion::Iterator to start of SkRegion.
474 Returns true if SkRegion was set; otherwise, returns false.
475
476 @return true if SkRegion was set
477
478 example: https://fiddle.skia.org/c/@Region_Iterator_rewind
479 */
480 bool rewind();
481
482 /** Resets iterator, using the new SkRegion.
483
484 @param region SkRegion to iterate
485
486 example: https://fiddle.skia.org/c/@Region_Iterator_reset
487 */
488 void reset(const SkRegion& region);
489
490 /** Returns true if SkRegion::Iterator is pointing to final SkIRect in SkRegion.
491
492 @return true if data parsing is complete
493 */
494 bool done() const { return fDone; }
495
496 /** Advances SkRegion::Iterator to next SkIRect in SkRegion if it is not done.
497
498 example: https://fiddle.skia.org/c/@Region_Iterator_next
499 */
500 void next();
501
502 /** Returns SkIRect element in SkRegion. Does not return predictable results if SkRegion
503 is empty.
504
505 @return part of SkRegion as SkIRect
506 */
507 const SkIRect& rect() const { return fRect; }
508
509 /** Returns SkRegion if set; otherwise, returns nullptr.
510
511 @return iterated SkRegion
512 */
513 const SkRegion* rgn() const { return fRgn; }
514
515 private:
516 const SkRegion* fRgn;
517 const SkRegion::RunType* fRuns;
518 SkIRect fRect = {0, 0, 0, 0};
519 bool fDone;
520 };
521
522 /** \class SkRegion::Cliperator
523 Returns the sequence of rectangles, sorted along y-axis, then x-axis, that make
524 up SkRegion intersected with the specified clip rectangle.
525 */
526 class SK_API Cliperator {
527 public:
528
529 /** Sets SkRegion::Cliperator to return elements of SkIRect array in SkRegion within clip.
530
531 @param region SkRegion to iterate
532 @param clip bounds of iteration
533 @return SkRegion iterator
534
535 example: https://fiddle.skia.org/c/@Region_Cliperator_const_SkRegion_const_SkIRect
536 */
537 Cliperator(const SkRegion& region, const SkIRect& clip);
538
539 /** Returns true if SkRegion::Cliperator is pointing to final SkIRect in SkRegion.
540
541 @return true if data parsing is complete
542 */
543 bool done() { return fDone; }
544
545 /** Advances iterator to next SkIRect in SkRegion contained by clip.
546
547 example: https://fiddle.skia.org/c/@Region_Cliperator_next
548 */
549 void next();
550
551 /** Returns SkIRect element in SkRegion, intersected with clip passed to
552 SkRegion::Cliperator constructor. Does not return predictable results if SkRegion
553 is empty.
554
555 @return part of SkRegion inside clip as SkIRect
556 */
557 const SkIRect& rect() const { return fRect; }
558
559 private:
560 Iterator fIter;
561 SkIRect fClip;
562 SkIRect fRect = {0, 0, 0, 0};
563 bool fDone;
564 };
565
566 /** \class SkRegion::Spanerator
567 Returns the line segment ends within SkRegion that intersect a horizontal line.
568 */
569 class SK_API Spanerator {
570 public:
571
572 /** Sets SkRegion::Spanerator to return line segments in SkRegion on scan line.
573
574 @param region SkRegion to iterate
575 @param y horizontal line to intersect
576 @param left bounds of iteration
577 @param right bounds of iteration
578 @return SkRegion iterator
579
580 example: https://fiddle.skia.org/c/@Region_Spanerator_const_SkRegion_int_int_int
581 */
582 Spanerator(const SkRegion& region, int y, int left, int right);
583
584 /** Advances iterator to next span intersecting SkRegion within line segment provided
585 in constructor. Returns true if interval was found.
586
587 @param left pointer to span start; may be nullptr
588 @param right pointer to span end; may be nullptr
589 @return true if interval was found
590
591 example: https://fiddle.skia.org/c/@Region_Spanerator_next
592 */
593 bool next(int* left, int* right);
594
595 private:
596 const SkRegion::RunType* fRuns;
597 int fLeft, fRight;
598 bool fDone;
599 };
600
601 /** Writes SkRegion to buffer, and returns number of bytes written.
602 If buffer is nullptr, returns number number of bytes that would be written.
603
604 @param buffer storage for binary data
605 @return size of SkRegion
606
607 example: https://fiddle.skia.org/c/@Region_writeToMemory
608 */
609 size_t writeToMemory(void* buffer) const;
610
611 /** Constructs SkRegion from buffer of size length. Returns bytes read.
612 Returned value will be multiple of four or zero if length was too small.
613
614 @param buffer storage for binary data
615 @param length size of buffer
616 @return bytes read
617
618 example: https://fiddle.skia.org/c/@Region_readFromMemory
619 */
620 size_t readFromMemory(const void* buffer, size_t length);
621
622 using sk_is_trivially_relocatable = std::true_type;
623
624 private:
625 static constexpr int kOpCount = kReplace_Op + 1;
626
627 // T
628 // [B N L R S]
629 // S
630 static constexpr int kRectRegionRuns = 7;
631
632 struct RunHead;
633
634 static RunHead* emptyRunHeadPtr() { return (SkRegion::RunHead*) -1; }
635 static constexpr const RunHead* const kRectRunHeadPtr = nullptr;
636
637 // allocate space for count runs
638 void allocateRuns(int count);
639 void allocateRuns(int count, int ySpanCount, int intervalCount);
640 void allocateRuns(const RunHead& src);
641
642 SkDEBUGCODE(void dump() const;)
643
644 SkIRect fBounds;
645 RunHead* fRunHead;
646
647 static_assert(::sk_is_trivially_relocatable<decltype(fBounds)>::value);
648 static_assert(::sk_is_trivially_relocatable<decltype(fRunHead)>::value);
649
650 void freeRuns();
651
652 /**
653 * Return the runs from this region, consing up fake runs if the region
654 * is empty or a rect. In those 2 cases, we use tmpStorage to hold the
655 * run data.
656 */
657 const RunType* getRuns(RunType tmpStorage[], int* intervals) const;
658
659 // This is called with runs[] that do not yet have their interval-count
660 // field set on each scanline. That is computed as part of this call
661 // (inside ComputeRunBounds).
662 bool setRuns(RunType runs[], int count);
663
664 int count_runtype_values(int* itop, int* ibot) const;
665
666 bool isValid() const;
667
668 static void BuildRectRuns(const SkIRect& bounds,
669 RunType runs[kRectRegionRuns]);
670
671 // If the runs define a simple rect, return true and set bounds to that
672 // rect. If not, return false and ignore bounds.
673 static bool RunsAreARect(const SkRegion::RunType runs[], int count,
674 SkIRect* bounds);
675
676 /**
677 * If the last arg is null, just return if the result is non-empty,
678 * else store the result in the last arg.
679 */
680 static bool Oper(const SkRegion&, const SkRegion&, SkRegion::Op, SkRegion*);
681
682 friend struct RunHead;
683 friend class Iterator;
684 friend class Spanerator;
685 friend class SkRegionPriv;
686 friend class SkRgnBuilder;
687 friend class SkFlatRegion;
688 };
689
690 #endif