SkAAClip.h revision 202ab2a5cabaf25a1e6ec47c0003da3213a79864
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 15class SkAAClip { 16public: 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#else 79 void validate() const {} 80#endif 81 82private: 83 SkIRect fBounds; 84 RunHead* fRunHead; 85 86 void freeRuns(); 87 bool trimBounds(); 88 bool trimTopBottom(); 89 bool trimLeftRight(); 90 91 friend class Builder; 92 class BuilderBlitter; 93 friend class BuilderBlitter; 94}; 95 96/////////////////////////////////////////////////////////////////////////////// 97 98class SkAAClipBlitter : public SkBlitter { 99public: 100 SkAAClipBlitter() : fScanlineScratch(NULL) {} 101 virtual ~SkAAClipBlitter(); 102 103 void init(SkBlitter* blitter, const SkAAClip* aaclip) { 104 SkASSERT(aaclip && !aaclip->isEmpty()); 105 fBlitter = blitter; 106 fAAClip = aaclip; 107 fAAClipBounds = aaclip->getBounds(); 108 } 109 110 virtual void blitH(int x, int y, int width) SK_OVERRIDE; 111 virtual void blitAntiH(int x, int y, const SkAlpha[], 112 const int16_t runs[]) SK_OVERRIDE; 113 virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE; 114 virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE; 115 virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE; 116 virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE; 117 118private: 119 SkBlitter* fBlitter; 120 const SkAAClip* fAAClip; 121 SkIRect fAAClipBounds; 122 123 // point into fScanlineScratch 124 int16_t* fRuns; 125 SkAlpha* fAA; 126 127 enum { 128 kSize = 32 * 32 129 }; 130 SkAutoSMalloc<kSize> fGrayMaskScratch; // used for blitMask 131 void* fScanlineScratch; // enough for a mask at 32bit, or runs+aa 132 133 void ensureRunsAndAA(); 134}; 135 136#endif 137