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