Home | History | Annotate | Download | only in core
      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     bool setEmpty();
     33     bool setRect(const SkIRect&);
     34     bool setRect(const SkRect&, bool doAA = true);
     35     bool setPath(const SkPath&, const SkRegion* clip = NULL, bool doAA = true);
     36     bool setRegion(const SkRegion&);
     37     bool set(const SkAAClip&);
     38 
     39     bool op(const SkAAClip&, const SkAAClip&, SkRegion::Op);
     40 
     41     // Helpers for op()
     42     bool op(const SkIRect&, SkRegion::Op);
     43     bool op(const SkRect&, SkRegion::Op, bool doAA);
     44     bool op(const SkAAClip&, SkRegion::Op);
     45 
     46     bool translate(int dx, int dy, SkAAClip* dst) const;
     47     bool translate(int dx, int dy) {
     48         return this->translate(dx, dy, this);
     49     }
     50 
     51     /**
     52      *  Allocates a mask the size of the aaclip, and expands its data into
     53      *  the mask, using kA8_Format
     54      */
     55     void copyToMask(SkMask*) const;
     56 
     57     // called internally
     58 
     59     bool quickContains(int left, int top, int right, int bottom) const;
     60     bool quickContains(const SkIRect& r) const {
     61         return this->quickContains(r.fLeft, r.fTop, r.fRight, r.fBottom);
     62     }
     63 
     64     const uint8_t* findRow(int y, int* lastYForRow = NULL) const;
     65     const uint8_t* findX(const uint8_t data[], int x, int* initialCount = NULL) const;
     66 
     67     class Iter;
     68     struct RunHead;
     69     struct YOffset;
     70     class Builder;
     71 
     72 #ifdef SK_DEBUG
     73     void validate() const;
     74 #else
     75     void validate() const {}
     76 #endif
     77 
     78 private:
     79     SkIRect  fBounds;
     80     RunHead* fRunHead;
     81 
     82     void freeRuns();
     83     bool trimBounds();
     84     bool trimTopBottom();
     85     bool trimLeftRight();
     86 
     87     friend class Builder;
     88     class BuilderBlitter;
     89     friend class BuilderBlitter;
     90 };
     91 
     92 ///////////////////////////////////////////////////////////////////////////////
     93 
     94 class SkAAClipBlitter : public SkBlitter {
     95 public:
     96     SkAAClipBlitter() : fScanlineScratch(NULL) {}
     97     virtual ~SkAAClipBlitter();
     98 
     99     void init(SkBlitter* blitter, const SkAAClip* aaclip) {
    100         SkASSERT(aaclip && !aaclip->isEmpty());
    101         fBlitter = blitter;
    102         fAAClip = aaclip;
    103         fAAClipBounds = aaclip->getBounds();
    104     }
    105 
    106     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    107     virtual void blitAntiH(int x, int y, const SkAlpha[],
    108                            const int16_t runs[]) SK_OVERRIDE;
    109     virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
    110     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    111     virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
    112     virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
    113 
    114 private:
    115     SkBlitter*      fBlitter;
    116     const SkAAClip* fAAClip;
    117     SkIRect         fAAClipBounds;
    118 
    119     // point into fScanlineScratch
    120     int16_t*        fRuns;
    121     SkAlpha*        fAA;
    122 
    123     enum {
    124         kSize = 32 * 32
    125     };
    126     SkAutoSMalloc<kSize> fGrayMaskScratch;  // used for blitMask
    127     void* fScanlineScratch;  // enough for a mask at 32bit, or runs+aa
    128 
    129     void ensureRunsAndAA();
    130 };
    131 
    132 #endif
    133