xref: /haiku/src/servers/app/drawing/Painter/Painter.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Copyright 2005, Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * API to the Anti-Grain Geometry based "Painter" drawing backend. Manages
6  * rendering pipe-lines for stroke, fills, bitmap and text rendering.
7  *
8  */
9 
10 #ifndef PAINTER_H
11 #define PAINTER_H
12 
13 #include <Font.h>
14 #include <Rect.h>
15 #include <FontManager.h>
16 #include <ServerFont.h>
17 
18 #include "defines.h"
19 #include "forwarding_pixfmt.h"
20 
21 #include "RGBColor.h"
22 
23 class AGGTextRenderer;
24 class BBitmap;
25 class BRegion;
26 class DrawState;
27 class DrawingModeFactory;
28 class PatternHandler;
29 class RenderingBuffer;
30 class ServerBitmap;
31 class ServerFont;
32 class Transformable;
33 
34 // TODO: API transition:
35 // * most all functions should take a DrawState* context parameter instead
36 //   of the current pattern argument, that way, each function can
37 //	 decide for itself, which pieces of information in DrawState it
38 //   needs -> well I'm not so sure about this, there could also
39 //   be a DrawState member in Painter fGraphicsState or something...
40 // * Painter itself should be made thread safe. Because no
41 //   ServerWindow is supposed to draw outside of its clipping region,
42 //   there is actually no reason to lock the DisplayDriver. Multiple
43 //   threads drawing in the frame buffer at the same time is actually
44 //   only bad if their drawing could overlap, but this is already
45 //   prevented by the clipping regions (access to those needs to be
46 //   locked).
47 //   Making Painter thread safe could introduce some overhead, since
48 //   some of the current members of Painter would need to be created
49 //   on the stack... I'll have to see about that... On multiple CPU
50 //   machines though, there would be quite an improvement. In two
51 //   years from now, most systems will be at least dual CPU
52 
53 class Painter {
54  public:
55 								Painter();
56 	virtual						~Painter();
57 
58 								// frame buffer stuff
59 			void				AttachToBuffer(RenderingBuffer* buffer);
60 			void				DetachFromBuffer();
61 
62 			void				ConstrainClipping(const BRegion& region);
63 			const BRegion*		ClippingRegion() const
64 									{ return fClippingRegion; }
65 
66 			void				SetDrawState(const DrawState* data);
67 
68 								// object settings
69 			void				SetHighColor(const rgb_color& color);
70 	inline	void				SetHighColor(uint8 r, uint8 g, uint8 b, uint8 a = 255);
71 	inline	void				SetHighColor(const RGBColor& color)
72 									{ SetHighColor(color.GetColor32()); }
73 			void				SetLowColor(const rgb_color& color);
74 	inline	void				SetLowColor(uint8 r, uint8 g, uint8 b, uint8 a = 255);
75 	inline	void				SetLowColor(const RGBColor& color)
76 									{ SetLowColor(color.GetColor32()); }
77 
78 			void				SetPenSize(float size);
79 			void				SetDrawingMode(drawing_mode mode);
80 			void				SetBlendingMode(source_alpha alphaSrcMode,
81 												alpha_function alphaFncMode);
82 			void				SetPattern(const pattern& p);
83 
84 			void				SetPenLocation(const BPoint& location);
85 			BPoint				PenLocation() const
86 									{ return fPenLocation; }
87 			void				SetFont(const ServerFont& font);
88 
89 								// painting functions
90 
91 								// lines
92 			BRect				StrokeLine(		BPoint a,
93 												BPoint b,
94 												DrawState* context);
95 
96 			BRect				StrokeLine(		BPoint b,
97 												DrawState* context);
98 
99 			// returns true if the line was either vertical or horizontal
100 			// draws a solid one pixel wide line of color c, no blending
101 			bool				StraightLine(	BPoint a,
102 												BPoint b,
103 												const rgb_color& c) const;
104 
105 								// triangles
106 			BRect				StrokeTriangle(	BPoint pt1,
107 												BPoint pt2,
108 												BPoint pt3) const;
109 
110 			BRect				FillTriangle(	BPoint pt1,
111 												BPoint pt2,
112 												BPoint pt3) const;
113 
114 								// polygons
115 			BRect				StrokePolygon(	const BPoint* ptArray,
116 												int32 numPts,
117 											    bool  closed = true) const;
118 
119 			BRect				FillPolygon(	const BPoint* ptArray,
120 												int32 numPts,
121 											    bool  closed = true) const;
122 
123 								// bezier curves
124 			BRect				StrokeBezier(	const BPoint* controlPoints) const;
125 
126 			BRect				FillBezier(		const BPoint* controlPoints) const;
127 
128 								// shapes
129 			BRect				DrawShape(		const int32& opCount,
130 												const uint32* opList,
131 												const int32& ptCount,
132 												const BPoint* ptList,
133 												bool filled) const;
134 
135 								// rects
136 			BRect				StrokeRect(		const BRect& r) const;
137 
138 			// strokes a one pixel wide solid rect, no blending
139 			void				StrokeRect(		const BRect& r,
140 												const rgb_color& c) const;
141 
142 			BRect				FillRect(		const BRect& r) const;
143 
144 			// fills a solid rect with color c, no blending
145 			void				FillRect(		const BRect& r,
146 												const rgb_color& c) const;
147 
148 								// round rects
149 			BRect				StrokeRoundRect(const BRect& r,
150 												float xRadius,
151 												float yRadius) const;
152 
153 			BRect				FillRoundRect(	const BRect& r,
154 												float xRadius,
155 												float yRadius) const;
156 
157 								// ellipses
158 			BRect				StrokeEllipse(	BPoint center,
159 												float xRadius,
160 												float yRadius) const;
161 
162 			BRect				FillEllipse(	BPoint center,
163 												float xRadius,
164 												float yRadius) const;
165 
166 								// arcs
167 			BRect				StrokeArc(		BPoint center,
168 												float xRadius,
169 												float yRadius,
170 												float angle,
171 												float span) const;
172 
173 			BRect				FillArc(		BPoint center,
174 												float xRadius,
175 												float yRadius,
176 												float angle,
177 												float span) const;
178 
179 								// strings
180 			BRect				DrawChar(		char aChar);
181 
182 			BRect				DrawChar(		char aChar,
183 												BPoint baseLine);
184 
185 			BRect				DrawString(		const char* utf8String,
186 												uint32 length,
187 												const escapement_delta* delta = NULL);
188 
189 			BRect				DrawString(		const char* utf8String,
190 												uint32 length,
191 												BPoint baseLine,
192 												const escapement_delta* delta = NULL);
193 
194 			BRect				DrawString(		const char* utf8String,
195 												const escapement_delta* delta = NULL);
196 
197 			BRect				DrawString(		const char* utf8String,
198 												BPoint baseLine,
199 												const escapement_delta* delta = NULL);
200 
201 								// bitmaps
202 			BRect				DrawBitmap(		const ServerBitmap* bitmap,
203 												BRect bitmapRect,
204 												BRect viewRect) const;
205 
206 								// some convenience stuff
207 			BRect				FillRegion(		const BRegion* region) const;
208 
209 			BRect				InvertRect(		const BRect& r) const;
210 
211 			BRect				BoundingBox(	const char* utf8String,
212 												uint32 length,
213 												const BPoint& baseLine,
214 												BPoint* penLocation,
215 												const escapement_delta* delta = NULL) const;
216 
217 			float				StringWidth(	const char* utf8String,
218 												uint32 length) const;
219 
220 	inline	BRect				ClipRect(const BRect& rect) const
221 									{ return _Clipped(rect); }
222 
223  private:
224 			void				_MakeEmpty();
225 
226 			void				_Transform(BPoint* point,
227 										   bool centerOffset = true) const;
228 			BPoint				_Transform(const BPoint& point,
229 										   bool centerOffset = true) const;
230 			BRect				_Clipped(const BRect& rect) const;
231 
232 			void				_UpdateFont();
233 			void				_UpdateLineWidth();
234 			void				_UpdateDrawingMode();
235 			void				_SetRendererColor(const rgb_color& color) const;
236 
237 								// drawing functions stroke/fill
238 			BRect				_DrawTriangle(	BPoint pt1,
239 												BPoint pt2,
240 												BPoint pt3,
241 												bool fill) const;
242 			BRect				_DrawEllipse(	BPoint center,
243 												float xRadius,
244 												float yRadius,
245 												bool fill) const;
246 			BRect				_DrawPolygon(	const BPoint* ptArray,
247 												int32 numPts,
248 											    bool  closed,
249 												bool fill) const;
250 
251 			void				_DrawBitmap(	const agg::rendering_buffer& srcBuffer,
252 												color_space format,
253 												BRect actualBitmapRect,
254 												BRect bitmapRect,
255 												BRect viewRect) const;
256 			void				_DrawBitmap32(	const agg::rendering_buffer& srcBuffer,
257 												BRect actualBitmapRect,
258 												BRect bitmapRect,
259 												BRect viewRect) const;
260 
261 			void				_InvertRect32(BRect r) const;
262 
263 
264 			template<class VertexSource>
265 			BRect				_BoundingBox(VertexSource& path) const;
266 
267 			template<class VertexSource>
268 			BRect				_StrokePath(VertexSource& path) const;
269 			template<class VertexSource>
270 			BRect				_FillPath(VertexSource& path) const;
271 
272 	agg::rendering_buffer*		fBuffer;
273 
274 	// AGG rendering and rasterization classes
275 	pixfmt*						fPixelFormat;
276 	renderer_base*				fBaseRenderer;
277 
278 	outline_renderer_type*		fOutlineRenderer;
279 	outline_rasterizer_type*	fOutlineRasterizer;
280 
281 	scanline_type*				fScanline;
282 	rasterizer_type*			fRasterizer;
283 	renderer_type*				fRenderer;
284 
285 	font_renderer_solid_type*	fFontRendererSolid;
286 	font_renderer_bin_type*		fFontRendererBin;
287 
288 	agg::line_profile_aa		fLineProfile;
289 
290 	// for internal coordinate rounding/transformation
291 	bool						fSubpixelPrecise;
292 
293 	float						fPenSize;
294 	BRegion*					fClippingRegion;
295 	bool						fValidClipping;
296 	drawing_mode				fDrawingMode;
297 	source_alpha				fAlphaSrcMode;
298 	alpha_function				fAlphaFncMode;
299 	BPoint						fPenLocation;
300 	cap_mode					fLineCapMode;
301 	join_mode					fLineJoinMode;
302 	float						fMiterLimit;
303 
304 	DrawingModeFactory*			fDrawingModeFactory;
305 	PatternHandler*				fPatternHandler;
306 
307 	ServerFont					fFont;
308 	// a class handling rendering and caching of glyphs
309 	// it is setup to load from a specific Freetype supported
310 	// font file which it gets from ServerFont
311 	AGGTextRenderer*			fTextRenderer;
312 };
313 
314 // SetHighColor
315 inline void
316 Painter::SetHighColor(uint8 r, uint8 g, uint8 b, uint8 a)
317 {
318 	rgb_color color;
319 	color.red = r;
320 	color.green = g;
321 	color.blue = b;
322 	color.alpha = a;
323 	SetHighColor(color);
324 }
325 
326 // SetLowColor
327 inline void
328 Painter::SetLowColor(uint8 r, uint8 g, uint8 b, uint8 a)
329 {
330 	rgb_color color;
331 	color.red = r;
332 	color.green = g;
333 	color.blue = b;
334 	color.alpha = a;
335 	SetLowColor(color);
336 }
337 
338 
339 #endif // PAINTER_H
340 
341 
342