xref: /haiku/src/servers/app/drawing/DrawingEngine.h (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
1 /*
2  * Copyright 2001-2008, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm <bpmagic@columbus.rr.com>
7  *		Gabe Yoder <gyoder@stny.rr.com>
8  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 #ifndef DRAWING_ENGINE_H_
11 #define DRAWING_ENGINE_H_
12 
13 
14 #include <Accelerant.h>
15 #include <Font.h>
16 #include <Locker.h>
17 #include <Point.h>
18 #include <Gradient.h>
19 
20 #include "HWInterface.h"
21 
22 class BPoint;
23 class BRect;
24 class BRegion;
25 
26 class DrawState;
27 class Painter;
28 class ServerBitmap;
29 class ServerCursor;
30 class ServerFont;
31 
32 typedef struct {
33 	BPoint pt1;
34 	BPoint pt2;
35 	rgb_color color;
36 } LineArrayData;
37 
38 class DrawingEngine : public HWInterfaceListener {
39 public:
40 							DrawingEngine(HWInterface* interface = NULL);
41 	virtual					~DrawingEngine();
42 
43 	// HWInterfaceListener interface
44 	virtual	void			FrameBufferChanged();
45 
46 			// for "changing" hardware
47 			void			SetHWInterface(HWInterface* interface);
48 
49 			void			SetCopyToFrontEnabled(bool enable);
50 			bool			CopyToFrontEnabled() const
51 								{ return fCopyToFront; }
52 			void			CopyToFront(/*const*/ BRegion& region);
53 
54 			// locking
55 			bool			LockParallelAccess();
56 			bool			IsParallelAccessLocked();
57 			void			UnlockParallelAccess();
58 
59 			bool			LockExclusiveAccess();
60 			bool			IsExclusiveAccessLocked();
61 			void			UnlockExclusiveAccess();
62 
63 			// for screen shots
64 			ServerBitmap*	DumpToBitmap();
65 			status_t		ReadBitmap(ServerBitmap *bitmap, bool drawCursor,
66 								BRect bounds);
67 
68 			// clipping for all drawing functions, passing a NULL region
69 			// will remove any clipping (drawing allowed everywhere)
70 			void			ConstrainClippingRegion(const BRegion* region);
71 
72 			void			SetDrawState(const DrawState* state,
73 								int32 xOffset = 0, int32 yOffset = 0);
74 
75 			void			SetHighColor(const rgb_color& color);
76 			void			SetLowColor(const rgb_color& color);
77 			void			SetPenSize(float size);
78 			void			SetStrokeMode(cap_mode lineCap, join_mode joinMode,
79 								float miterLimit);
80 			void			SetPattern(const struct pattern& pattern);
81 			void			SetDrawingMode(drawing_mode mode);
82 			void			SetDrawingMode(drawing_mode mode,
83 								drawing_mode& oldMode);
84 			void			SetBlendingMode(source_alpha srcAlpha,
85 								alpha_function alphaFunc);
86 			void			SetFont(const ServerFont& font);
87 			void			SetFont(const DrawState* state);
88 
89 			void			SuspendAutoSync();
90 			void			Sync();
91 
92 			// drawing functions
93 			void			CopyRegion(/*const*/ BRegion* region,
94 								int32 xOffset, int32 yOffset);
95 
96 			void			InvertRect(BRect r);
97 
98 			void			DrawBitmap(ServerBitmap* bitmap,
99 								const BRect& bitmapRect, const BRect& viewRect,
100 								uint32 options = 0);
101 			// drawing primitives
102 
103 			void			DrawArc(BRect r, const float& angle,
104 								const float& span, bool filled);
105 			void			FillArc(BRect r, const float& angle,
106 								const float& span, const BGradient& gradient);
107 
108 			void			DrawBezier(BPoint* pts, bool filled);
109 			void			FillBezier(BPoint* pts, const BGradient& gradient);
110 
111 			void			DrawEllipse(BRect r, bool filled);
112 			void			FillEllipse(BRect r, const BGradient& gradient);
113 
114 			void			DrawPolygon(BPoint* ptlist, int32 numpts,
115 								BRect bounds, bool filled, bool closed);
116 			void			FillPolygon(BPoint* ptlist, int32 numpts,
117 								BRect bounds, const BGradient& gradient,
118 								bool closed);
119 
120 			// these rgb_color versions are used internally by the server
121 			void			StrokePoint(const BPoint& pt,
122 								const rgb_color& color);
123 			void			StrokeRect(BRect r, const rgb_color &color);
124 			void			FillRect(BRect r, const rgb_color &color);
125 			void			FillRegion(BRegion& r, const rgb_color& color);
126 
127 			void			StrokeRect(BRect r);
128 			void			FillRect(BRect r);
129 			void			FillRect(BRect r, const BGradient& gradient);
130 
131 			void			FillRegion(BRegion& r);
132 			void			FillRegion(BRegion& r, const BGradient& gradient);
133 
134 			void			DrawRoundRect(BRect r, float xrad,
135 								float yrad, bool filled);
136 			void			FillRoundRect(BRect r, float xrad,
137 								float yrad, const BGradient& gradient);
138 
139 			void			DrawShape(const BRect& bounds,
140 								int32 opcount, const uint32* oplist,
141 								int32 ptcount, const BPoint* ptlist,
142 								bool filled);
143 			void			FillShape(const BRect& bounds,
144 							  int32 opcount, const uint32* oplist,
145 							  int32 ptcount, const BPoint* ptlist,
146 							  const BGradient& gradient);
147 
148 			void			DrawTriangle(BPoint* pts, const BRect& bounds,
149 								bool filled);
150 			void			FillTriangle(BPoint* pts,
151 								const BRect& bounds, const BGradient& gradient);
152 
153 			// this version used by Decorator
154 			void			StrokeLine(const BPoint& start,
155 								const BPoint& end, const rgb_color& color);
156 
157 			void			StrokeLine(const BPoint& start,
158 								const BPoint& end);
159 
160 			void			StrokeLineArray(int32 numlines,
161 								const LineArrayData* data);
162 
163 			// -------- text related calls
164 
165 			// returns the pen position behind the (virtually) drawn
166 			// string
167 			BPoint			DrawString(const char* string, int32 length,
168 								const BPoint& pt,
169 								escapement_delta* delta = NULL);
170 
171 			float			StringWidth(const char* string, int32 length,
172 								escapement_delta* delta = NULL);
173 
174 			// convenience function which is independent of graphics
175 			// state (to be used by Decorator or ServerApp etc)
176 			float			StringWidth(const char* string,
177 								int32 length, const ServerFont& font,
178 								escapement_delta* delta = NULL);
179 
180  private:
181 			BRect			_CopyRect(BRect r, int32 xOffset,
182 								int32 yOffset) const;
183 
184 			void			_CopyRect(uint8* bits, uint32 width,
185 								uint32 height, uint32 bytesPerRow,
186 								int32 xOffset, int32 yOffset) const;
187 
188 	inline	void			_CopyToFront(const BRect& frame);
189 
190 			Painter*		fPainter;
191 			HWInterface*	fGraphicsCard;
192 			uint32			fAvailableHWAccleration;
193 			int32			fSuspendSyncLevel;
194 			bool			fCopyToFront;
195 };
196 
197 #endif // DRAWING_ENGINE_H_
198