Home | History | Annotate | Download | only in include
      1 /*
      2     Copyright 2010 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8          http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 
     18 #ifndef GrClip_DEFINED
     19 #define GrClip_DEFINED
     20 
     21 #include "GrClipIterator.h"
     22 #include "GrRect.h"
     23 #include "GrPath.h"
     24 #include "GrTArray.h"
     25 #include "GrTemplates.h"
     26 
     27 
     28 class GrClip {
     29 public:
     30     GrClip();
     31     GrClip(const GrClip& src);
     32     /**
     33      *  If specified, the conservativeBounds parameter already takes (tx,ty)
     34      *  into account.
     35      */
     36     GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
     37            const GrRect* conservativeBounds = NULL);
     38     GrClip(const GrIRect& rect);
     39     GrClip(const GrRect& rect);
     40 
     41     ~GrClip();
     42 
     43     GrClip& operator=(const GrClip& src);
     44 
     45     bool hasConservativeBounds() const { return fConservativeBoundsValid; }
     46 
     47     const GrRect& getConservativeBounds() const { return fConservativeBounds; }
     48 
     49     int getElementCount() const { return fList.count(); }
     50 
     51     GrClipType getElementType(int i) const { return fList[i].fType; }
     52 
     53     const GrPath& getPath(int i) const {
     54         GrAssert(kPath_ClipType == fList[i].fType);
     55         return fList[i].fPath;
     56     }
     57 
     58     GrPathFill getPathFill(int i) const {
     59         GrAssert(kPath_ClipType == fList[i].fType);
     60         return fList[i].fPathFill;
     61     }
     62 
     63     const GrRect& getRect(int i) const {
     64         GrAssert(kRect_ClipType == fList[i].fType);
     65         return fList[i].fRect;
     66     }
     67 
     68     GrSetOp getOp(int i) const { return fList[i].fOp; }
     69 
     70     bool isRect() const {
     71         if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
     72             // if we determined that the clip is a single rect
     73             // we ought to have also used that rect as the bounds.
     74             GrAssert(fConservativeBoundsValid);
     75             GrAssert(fConservativeBounds == fList[0].fRect);
     76             return true;
     77         } else {
     78             return false;
     79         }
     80     }
     81 
     82     bool isEmpty() const { return 0 == fList.count(); }
     83 
     84     /**
     85      *  Resets this clip to be empty
     86      */
     87     void setEmpty();
     88 
     89     /**
     90      *  If specified, the bounds parameter already takes (tx,ty) into account.
     91      */
     92     void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
     93                          const GrRect* conservativeBounds = NULL);
     94     void setFromRect(const GrRect& rect);
     95     void setFromIRect(const GrIRect& rect);
     96 
     97     friend bool operator==(const GrClip& a, const GrClip& b) {
     98         if (a.fList.count() != b.fList.count()) {
     99             return false;
    100         }
    101         int count = a.fList.count();
    102         for (int i = 0; i < count; ++i) {
    103             if (a.fList[i] != b.fList[i]) {
    104                 return false;
    105             }
    106         }
    107         return true;
    108     }
    109     friend bool operator!=(const GrClip& a, const GrClip& b) {
    110         return !(a == b);
    111     }
    112 
    113 private:
    114     struct Element {
    115         GrClipType  fType;
    116         GrRect      fRect;
    117         GrPath      fPath;
    118         GrPathFill  fPathFill;
    119         GrSetOp     fOp;
    120         bool operator ==(const Element& e) const {
    121             if (e.fType != fType || e.fOp != fOp) {
    122                 return false;
    123             }
    124             switch (fType) {
    125                 case kRect_ClipType:
    126                     return fRect == e.fRect;
    127                     break;
    128                 case kPath_ClipType:
    129                     return fPath == e.fPath;
    130                 default:
    131                     GrCrash("Unknown clip element type.");
    132                     return false; // suppress warning
    133             }
    134         }
    135         bool operator !=(const Element& e) const { return !(*this == e); }
    136     };
    137 
    138     GrRect              fConservativeBounds;
    139     bool                fConservativeBoundsValid;
    140 
    141     enum {
    142         kPreAllocElements = 4,
    143     };
    144     GrAlignedSTStorage<kPreAllocElements, Element>  fListStorage;
    145     GrTArray<Element>   fList;
    146 };
    147 #endif
    148 
    149