45cc54932df729bbd0446f47ecb2c32dc27849cd
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkAAClip.h
1
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9 #ifndef SkAAClip_DEFINED
10 #define SkAAClip_DEFINED
11
12 #include "SkBlitter.h"
13 #include "SkRegion.h"
14
15 class SkAAClip {
16 public:
17     SkAAClip();
18     SkAAClip(const SkAAClip&);
19     ~SkAAClip();
20
21     SkAAClip& operator=(const SkAAClip&);
22     friend bool operator==(const SkAAClip&, const SkAAClip&);
23     friend bool operator!=(const SkAAClip& a, const SkAAClip& b) {
24         return !(a == b);
25     }
26
27     void swap(SkAAClip&);
28
29     bool isEmpty() const { return NULL == fRunHead; }
30     const SkIRect& getBounds() const { return fBounds; }
31
32     // Returns true iff the clip is not empty, and is just a hard-edged rect (no partial alpha).
33     // If true, getBounds() can be used in place of this clip.
34     bool isRect() const;
35
36     bool setEmpty();
37     bool setRect(const SkIRect&);
38     bool setRect(const SkRect&, bool doAA = true);
39     bool setPath(const SkPath&, const SkRegion* clip = NULL, bool doAA = true);
40     bool setRegion(const SkRegion&);
41     bool set(const SkAAClip&);
42
43     bool op(const SkAAClip&, const SkAAClip&, SkRegion::Op);
44
45     // Helpers for op()
46     bool op(const SkIRect&, SkRegion::Op);
47     bool op(const SkRect&, SkRegion::Op, bool doAA);
48     bool op(const SkAAClip&, SkRegion::Op);
49
50     bool translate(int dx, int dy, SkAAClip* dst) const;
51     bool translate(int dx, int dy) {
52         return this->translate(dx, dy, this);
53     }
54
55     /**
56      *  Allocates a mask the size of the aaclip, and expands its data into
57      *  the mask, using kA8_Format
58      */
59     void copyToMask(SkMask*) const;
60
61     // called internally
62
63     bool quickContains(int left, int top, int right, int bottom) const;
64     bool quickContains(const SkIRect& r) const {
65         return this->quickContains(r.fLeft, r.fTop, r.fRight, r.fBottom);
66     }
67
68     const uint8_t* findRow(int y, int* lastYForRow = NULL) const;
69     const uint8_t* findX(const uint8_t data[], int x, int* initialCount = NULL) const;
70
71     class Iter;
72     struct RunHead;
73     struct YOffset;
74     class Builder;
75
76 #ifdef SK_DEBUG
77     void validate() const;
78     void debug(bool compress_y=false) const;
79 #else
80     void validate() const {}
81     void debug(bool compress_y=false) const {}
82 #endif
83
84 private:
85     SkIRect  fBounds;
86     RunHead* fRunHead;
87
88     void freeRuns();
89     bool trimBounds();
90     bool trimTopBottom();
91     bool trimLeftRight();
92
93     friend class Builder;
94     class BuilderBlitter;
95     friend class BuilderBlitter;
96 };
97
98 ///////////////////////////////////////////////////////////////////////////////
99
100 class SkAAClipBlitter : public SkBlitter {
101 public:
102     SkAAClipBlitter() : fScanlineScratch(NULL) {}
103     virtual ~SkAAClipBlitter();
104
105     void init(SkBlitter* blitter, const SkAAClip* aaclip) {
106         SkASSERT(aaclip && !aaclip->isEmpty());
107         fBlitter = blitter;
108         fAAClip = aaclip;
109         fAAClipBounds = aaclip->getBounds();
110     }
111
112     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
113     virtual void blitAntiH(int x, int y, const SkAlpha[],
114                            const int16_t runs[]) SK_OVERRIDE;
115     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
116     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
117     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
118     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
119
120 private:
121     SkBlitter*      fBlitter;
122     const SkAAClip* fAAClip;
123     SkIRect         fAAClipBounds;
124
125     // point into fScanlineScratch
126     int16_t*        fRuns;
127     SkAlpha*        fAA;
128
129     enum {
130         kSize = 32 * 32
131     };
132     SkAutoSMalloc<kSize> fGrayMaskScratch;  // used for blitMask
133     void* fScanlineScratch;  // enough for a mask at 32bit, or runs+aa
134
135     void ensureRunsAndAA();
136 };
137
138 #endif