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