Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2010 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 SkRasterClip_DEFINED
      9 #define SkRasterClip_DEFINED
     10 
     11 #include "SkRegion.h"
     12 #include "SkAAClip.h"
     13 
     14 class SkRasterClip {
     15 public:
     16     SkRasterClip();
     17     SkRasterClip(const SkIRect&);
     18     SkRasterClip(const SkRasterClip&);
     19     ~SkRasterClip();
     20 
     21     bool isBW() const { return fIsBW; }
     22     bool isAA() const { return !fIsBW; }
     23     const SkRegion& bwRgn() const { SkASSERT(fIsBW); return fBW; }
     24     const SkAAClip& aaRgn() const { SkASSERT(!fIsBW); return fAA; }
     25 
     26     bool isEmpty() const {
     27         SkASSERT(this->computeIsEmpty() == fIsEmpty);
     28         return fIsEmpty;
     29     }
     30 
     31     bool isRect() const {
     32         SkASSERT(this->computeIsRect() == fIsRect);
     33         return fIsRect;
     34     }
     35 
     36     bool isComplex() const;
     37     const SkIRect& getBounds() const;
     38 
     39     bool setEmpty();
     40     bool setRect(const SkIRect&);
     41 
     42     bool setPath(const SkPath& path, const SkRegion& clip, bool doAA);
     43     bool setPath(const SkPath& path, const SkIRect& clip, bool doAA);
     44 
     45     bool op(const SkIRect&, SkRegion::Op);
     46     bool op(const SkRegion&, SkRegion::Op);
     47     bool op(const SkRasterClip&, SkRegion::Op);
     48     bool op(const SkRect&, SkRegion::Op, bool doAA);
     49 
     50     void translate(int dx, int dy, SkRasterClip* dst) const;
     51     void translate(int dx, int dy) {
     52         this->translate(dx, dy, this);
     53     }
     54 
     55     bool quickContains(const SkIRect& rect) const;
     56     bool quickContains(int left, int top, int right, int bottom) const {
     57         return quickContains(SkIRect::MakeLTRB(left, top, right, bottom));
     58     }
     59 
     60     /**
     61      *  Return true if this region is empty, or if the specified rectangle does
     62      *  not intersect the region. Returning false is not a guarantee that they
     63      *  intersect, but returning true is a guarantee that they do not.
     64      */
     65     bool quickReject(const SkIRect& rect) const {
     66         return this->isEmpty() || rect.isEmpty() ||
     67                !SkIRect::Intersects(this->getBounds(), rect);
     68     }
     69 
     70     // hack for SkCanvas::getTotalClip
     71     const SkRegion& forceGetBW();
     72 
     73 #ifdef SK_DEBUG
     74     void validate() const;
     75 #else
     76     void validate() const {}
     77 #endif
     78 
     79 private:
     80     SkRegion    fBW;
     81     SkAAClip    fAA;
     82     bool        fIsBW;
     83     // these 2 are caches based on querying the right obj based on fIsBW
     84     bool        fIsEmpty;
     85     bool        fIsRect;
     86 
     87     bool computeIsEmpty() const {
     88         return fIsBW ? fBW.isEmpty() : fAA.isEmpty();
     89     }
     90 
     91     bool computeIsRect() const {
     92         return fIsBW ? fBW.isRect() : false;
     93     }
     94 
     95     bool updateCacheAndReturnNonEmpty() {
     96         fIsEmpty = this->computeIsEmpty();
     97         fIsRect = this->computeIsRect();
     98         return !fIsEmpty;
     99     }
    100 
    101     void convertToAA();
    102 };
    103 
    104 class SkAutoRasterClipValidate : SkNoncopyable {
    105 public:
    106     SkAutoRasterClipValidate(const SkRasterClip& rc) : fRC(rc) {
    107         fRC.validate();
    108     }
    109     ~SkAutoRasterClipValidate() {
    110         fRC.validate();
    111     }
    112 private:
    113     const SkRasterClip& fRC;
    114 };
    115 
    116 #ifdef SK_DEBUG
    117     #define AUTO_RASTERCLIP_VALIDATE(rc)    SkAutoRasterClipValidate arcv(rc)
    118 #else
    119     #define AUTO_RASTERCLIP_VALIDATE(rc)
    120 #endif
    121 
    122 ///////////////////////////////////////////////////////////////////////////////
    123 
    124 /**
    125  *  Encapsulates the logic of deciding if we need to change/wrap the blitter
    126  *  for aaclipping. If so, getRgn and getBlitter return modified values. If
    127  *  not, they return the raw blitter and (bw) clip region.
    128  *
    129  *  We need to keep the constructor/destructor cost as small as possible, so we
    130  *  can freely put this guy on the stack, and not pay too much for the case when
    131  *  we're really BW anyways.
    132  */
    133 class SkAAClipBlitterWrapper {
    134 public:
    135     SkAAClipBlitterWrapper();
    136     SkAAClipBlitterWrapper(const SkRasterClip&, SkBlitter*);
    137     SkAAClipBlitterWrapper(const SkAAClip*, SkBlitter*);
    138 
    139     void init(const SkRasterClip&, SkBlitter*);
    140 
    141     const SkIRect& getBounds() const {
    142         SkASSERT(fClipRgn);
    143         return fClipRgn->getBounds();
    144     }
    145     const SkRegion& getRgn() const {
    146         SkASSERT(fClipRgn);
    147         return *fClipRgn;
    148     }
    149     SkBlitter* getBlitter() {
    150         SkASSERT(fBlitter);
    151         return fBlitter;
    152     }
    153 
    154 private:
    155     SkRegion        fBWRgn;
    156     SkAAClipBlitter fAABlitter;
    157     // what we return
    158     const SkRegion* fClipRgn;
    159     SkBlitter* fBlitter;
    160 };
    161 
    162 #endif
    163