xref: /aosp_15_r20/external/skia/src/core/SkAAClip.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 Google Inc.
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 SkAAClip_DEFINED
9 #define SkAAClip_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkRect.h"
13 #include "include/private/base/SkAssert.h"
14 #include "src/base/SkAutoMalloc.h"
15 #include "src/core/SkBlitter.h"
16 #include <cstdint>
17 #include "include/private/base/SkDebug.h"
18 
19 class SkPath;
20 class SkRegion;
21 enum class SkClipOp;
22 struct SkMask;
23 struct SkMaskBuilder;
24 
25 class SkAAClip {
26 public:
27     SkAAClip();
28     SkAAClip(const SkAAClip&);
29     ~SkAAClip();
30 
31     SkAAClip& operator=(const SkAAClip&);
32 
isEmpty()33     bool isEmpty() const { return nullptr == fRunHead; }
getBounds()34     const SkIRect& getBounds() const { return fBounds; }
35 
36     // Returns true iff the clip is not empty, and is just a hard-edged rect (no partial alpha).
37     // If true, getBounds() can be used in place of this clip.
38     bool isRect() const;
39 
40     bool setEmpty();
41     bool setRect(const SkIRect&);
42     bool setPath(const SkPath&, const SkIRect& bounds, bool doAA = true);
43     bool setRegion(const SkRegion&);
44 
45     bool op(const SkIRect&, SkClipOp);
46     bool op(const SkRect&, SkClipOp, bool doAA);
47     bool op(const SkAAClip&, SkClipOp);
48 
49     bool translate(int dx, int dy, SkAAClip* dst) const;
50 
51     /**
52      *  Allocates a mask the size of the aaclip, and expands its data into
53      *  the mask, using kA8_Format. Used for tests and visualization purposes.
54      */
55     void copyToMask(SkMaskBuilder*) const;
56 
quickContains(const SkIRect & r)57     bool quickContains(const SkIRect& r) const {
58         return this->quickContains(r.fLeft, r.fTop, r.fRight, r.fBottom);
59     }
60 
61 #ifdef SK_DEBUG
62     void validate() const;
63     void debug(bool compress_y=false) const;
64 #else
validate()65     void validate() const {}
66     void debug(bool compress_y=false) const {}
67 #endif
68 
69 private:
70     class Builder;
71     struct RunHead;
72     friend class SkAAClipBlitter;
73 
74     SkIRect  fBounds;
75     RunHead* fRunHead;
76 
77     void freeRuns();
78 
79     bool quickContains(int left, int top, int right, int bottom) const;
80 
81     bool trimBounds();
82     bool trimTopBottom();
83     bool trimLeftRight();
84 
85     // For SkAAClipBlitter and quickContains
86     const uint8_t* findRow(int y, int* lastYForRow = nullptr) const;
87     const uint8_t* findX(const uint8_t data[], int x, int* initialCount = nullptr) const;
88 };
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 
92 class SkAAClipBlitter : public SkBlitter {
93 public:
SkAAClipBlitter()94     SkAAClipBlitter() : fScanlineScratch(nullptr) {}
95     ~SkAAClipBlitter() override;
96 
init(SkBlitter * blitter,const SkAAClip * aaclip)97     void init(SkBlitter* blitter, const SkAAClip* aaclip) {
98         SkASSERT(aaclip && !aaclip->isEmpty());
99         fBlitter = blitter;
100         fAAClip = aaclip;
101         fAAClipBounds = aaclip->getBounds();
102     }
103 
104     void blitH(int x, int y, int width) override;
105     void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
106     void blitV(int x, int y, int height, SkAlpha alpha) override;
107     void blitRect(int x, int y, int width, int height) override;
108     void blitMask(const SkMask&, const SkIRect& clip) override;
109 
110 private:
111     SkBlitter*      fBlitter;
112     const SkAAClip* fAAClip;
113     SkIRect         fAAClipBounds;
114 
115     // point into fScanlineScratch
116     int16_t*        fRuns;
117     SkAlpha*        fAA;
118 
119     enum {
120         kSize = 32 * 32
121     };
122     SkAutoSMalloc<kSize> fGrayMaskScratch;  // used for blitMask
123     void* fScanlineScratch;  // enough for a mask at 32bit, or runs+aa
124 
125     void ensureRunsAndAA();
126 };
127 
128 #endif
129