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