1 /* 2 * Copyright 2014-2015, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #ifndef ALPHA_MASK_H 7 #define ALPHA_MASK_H 8 9 #include <Referenceable.h> 10 11 #include "agg_clipped_alpha_mask.h" 12 #include "ServerPicture.h" 13 14 #include "DrawState.h" 15 #include "drawing/Painter/defines.h" 16 #include "IntRect.h" 17 18 #include <Locker.h> 19 20 21 class BShape; 22 class ServerBitmap; 23 class ServerPicture; 24 class shape_data; 25 class UtilityBitmap; 26 27 28 // #pragma mark - AlphaMask 29 30 31 class AlphaMask : public BReferenceable { 32 public: 33 AlphaMask(AlphaMask* previousMask, 34 bool inverse); 35 AlphaMask(AlphaMask* previousMask, 36 AlphaMask* other); 37 AlphaMask(uint8 backgroundOpacity); 38 virtual ~AlphaMask(); 39 40 IntPoint SetCanvasGeometry(IntPoint origin, 41 IntRect bounds); 42 43 scanline_unpacked_masked_type* Scanline() 44 { return &fScanline; } 45 46 agg::clipped_alpha_mask* Mask() 47 { return &fMask; } 48 49 size_t BitmapSize() const; 50 51 protected: 52 ServerBitmap* _CreateTemporaryBitmap(BRect bounds) const; 53 void _Generate(); 54 void _SetNoClipping(); 55 const IntRect& _PreviousMaskBounds() const; 56 virtual void _AddToCache() = 0; 57 58 private: 59 virtual ServerBitmap* _RenderSource(const IntRect& canvasBounds) = 0; 60 virtual IntPoint _Offset() = 0; 61 62 void _AttachMaskToBuffer(); 63 64 protected: 65 BReference<AlphaMask> fPreviousMask; 66 IntRect fBounds; 67 bool fClippedToCanvas; 68 BLocker fLock; 69 70 private: 71 friend class AlphaMaskCache; 72 73 IntPoint fCanvasOrigin; 74 IntRect fCanvasBounds; 75 const bool fInverse; 76 uint8 fBackgroundOpacity; 77 78 int32 fNextMaskCount; 79 bool fInCache; 80 uint32 fIndirectCacheReferences; 81 // number of times this mask has been 82 // seen as "previous mask" of another 83 // one in the cache, without being 84 // in the cache itself 85 86 UtilityBitmap* fBits; 87 agg::rendering_buffer fBuffer; 88 agg::clipped_alpha_mask fMask; 89 scanline_unpacked_masked_type fScanline; 90 }; 91 92 93 class UniformAlphaMask : public AlphaMask { 94 public: 95 UniformAlphaMask(uint8 opacity); 96 97 private: 98 virtual ServerBitmap* _RenderSource(const IntRect& canvasBounds); 99 virtual IntPoint _Offset(); 100 virtual void _AddToCache(); 101 }; 102 103 104 // #pragma mark - VectorAlphaMask 105 106 107 template<class VectorMaskType> 108 class VectorAlphaMask : public AlphaMask { 109 public: 110 VectorAlphaMask(AlphaMask* previousMask, 111 BPoint where, bool inverse); 112 VectorAlphaMask(AlphaMask* previousMask, 113 VectorAlphaMask* other); 114 115 private: 116 virtual ServerBitmap* _RenderSource(const IntRect& canvasBounds); 117 virtual IntPoint _Offset(); 118 119 protected: 120 BPoint fWhere; 121 }; 122 123 124 // #pragma mark - PictureAlphaMask 125 126 127 class PictureAlphaMask : public VectorAlphaMask<PictureAlphaMask> { 128 public: 129 PictureAlphaMask(AlphaMask* previousMask, 130 ServerPicture* picture, 131 const DrawState& drawState, BPoint where, 132 bool inverse); 133 virtual ~PictureAlphaMask(); 134 135 void DrawVectors(Canvas* canvas); 136 BRect DetermineBoundingBox() const; 137 const DrawState& GetDrawState() const; 138 139 private: 140 virtual void _AddToCache(); 141 142 private: 143 BReference<ServerPicture> fPicture; 144 DrawState* fDrawState; 145 }; 146 147 148 // #pragma mark - ShapeAlphaMask 149 150 151 class ShapeAlphaMask : public VectorAlphaMask<ShapeAlphaMask> { 152 private: 153 ShapeAlphaMask(AlphaMask* previousMask, 154 const shape_data& shape, 155 BPoint where, bool inverse); 156 ShapeAlphaMask(AlphaMask* previousMask, 157 ShapeAlphaMask* other); 158 159 public: 160 virtual ~ShapeAlphaMask(); 161 162 static ShapeAlphaMask* Create(AlphaMask* previousMask, 163 const shape_data& shape, 164 BPoint where, bool inverse); 165 166 void DrawVectors(Canvas* canvas); 167 BRect DetermineBoundingBox() const; 168 const DrawState& GetDrawState() const; 169 170 private: 171 virtual void _AddToCache(); 172 173 private: 174 friend class AlphaMaskCache; 175 176 shape_data* fShape; 177 BRect fShapeBounds; 178 static DrawState* fDrawState; 179 }; 180 181 182 #endif // ALPHA_MASK_H 183