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