xref: /haiku/src/servers/app/drawing/HWInterface.h (revision ba499cdc3336fb89429027418871bf263f1f5e14)
1 /*
2  * Copyright 2005-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 #ifndef HW_INTERFACE_H
9 #define HW_INTERFACE_H
10 
11 
12 #include "MultiLocker.h"
13 
14 #include <video_overlay.h>
15 
16 #include <Accelerant.h>
17 #include <GraphicsCard.h>
18 #include <List.h>
19 #include <Locker.h>
20 #include <OS.h>
21 #include <Region.h>
22 
23 
24 class Overlay;
25 class RenderingBuffer;
26 class RGBColor;
27 class ServerBitmap;
28 class ServerCursor;
29 class UpdateQueue;
30 class BString;
31 
32 enum {
33 	HW_ACC_COPY_REGION					= 0x00000001,
34 	HW_ACC_FILL_REGION					= 0x00000002,
35 	HW_ACC_INVERT_REGION				= 0x00000004,
36 };
37 
38 class HWInterfaceListener {
39  public:
40 								HWInterfaceListener();
41 	virtual						~HWInterfaceListener();
42 
43 	virtual	void				FrameBufferChanged() = 0;
44 };
45 
46 class HWInterface : protected MultiLocker {
47  public:
48 								HWInterface(bool doubleBuffered = false);
49 	virtual						~HWInterface();
50 
51 	// locking
52 			bool				LockParallelAccess() { return ReadLock(); }
53 			bool				IsParallelAccessLocked() { return IsReadLocked(); }
54 			void				UnlockParallelAccess() { ReadUnlock(); }
55 
56 			bool				LockExclusiveAccess() { return WriteLock(); }
57 			bool				IsExclusiveAccessLocked() { return IsWriteLocked(); }
58 			void				UnlockExclusiveAccess() { WriteUnlock(); }
59 
60 	// You need to WriteLock
61 	virtual	status_t			Initialize();
62 	virtual	status_t			Shutdown() = 0;
63 
64 	// screen mode stuff
65 	virtual	status_t			SetMode(const display_mode &mode) = 0;
66 	virtual	void				GetMode(display_mode *mode) = 0;
67 
68 	virtual status_t			GetDeviceInfo(accelerant_device_info *info) = 0;
69 	virtual status_t			GetFrameBufferConfig(frame_buffer_config& config) = 0;
70 	virtual status_t			GetModeList(display_mode **mode_list,
71 											uint32 *count) = 0;
72 	virtual status_t			GetPixelClockLimits(display_mode *mode,
73 													uint32 *low,
74 													uint32 *high) = 0;
75 	virtual status_t			GetTimingConstraints(display_timing_constraints *dtc) = 0;
76 	virtual status_t			ProposeMode(display_mode *candidate,
77 											const display_mode *low,
78 											const display_mode *high) = 0;
79 
80 	virtual sem_id				RetraceSemaphore() = 0;
81 	virtual status_t			WaitForRetrace(bigtime_t timeout = B_INFINITE_TIMEOUT) = 0;
82 
83 	virtual status_t			SetDPMSMode(const uint32 &state) = 0;
84 	virtual uint32				DPMSMode() = 0;
85 	virtual uint32				DPMSCapabilities() = 0;
86 
87 	virtual status_t			GetAccelerantPath(BString &path);
88 	virtual status_t			GetDriverPath(BString &path);
89 
90 	// query for available hardware accleration and perform it
91 	// (Initialize() must have been called already)
92 	virtual	uint32				AvailableHWAcceleration() const
93 									{ return 0; }
94 
95 	virtual	void				CopyRegion(const clipping_rect* sortedRectList,
96 										   uint32 count,
97 										   int32 xOffset, int32 yOffset) {}
98 	virtual	void				FillRegion(/*const*/ BRegion& region,
99 										   const RGBColor& color,
100 										   bool autoSync) {}
101 	virtual	void				InvertRegion(/*const*/ BRegion& region) {}
102 
103 	virtual	void				Sync() {}
104 
105 	// cursor handling (these do their own Read/Write locking)
106 			ServerCursor*		Cursor() const { return fCursor; }
107 	virtual	void				SetCursor(ServerCursor* cursor);
108 	virtual	void				SetCursorVisible(bool visible);
109 			bool				IsCursorVisible();
110 	virtual	void				ObscureCursor();
111 	virtual	void				MoveCursorTo(const float& x,
112 											 const float& y);
113 			BPoint				CursorPosition();
114 
115 			void				SetDragBitmap(const ServerBitmap* bitmap,
116 											  const BPoint& offsetFromCursor);
117 
118 	// overlay support
119 	virtual overlay_token		AcquireOverlayChannel();
120 	virtual void				ReleaseOverlayChannel(overlay_token token);
121 
122 	virtual bool				CheckOverlayRestrictions(int32 width, int32 height,
123 									color_space colorSpace);
124 	virtual const overlay_buffer* AllocateOverlayBuffer(int32 width, int32 height,
125 									color_space space);
126 	virtual void				FreeOverlayBuffer(const overlay_buffer* buffer);
127 
128 	virtual void				ConfigureOverlay(Overlay* overlay);
129 	virtual void				HideOverlay(Overlay* overlay);
130 
131 	// frame buffer access (you need to ReadLock!)
132 			RenderingBuffer*	DrawingBuffer() const;
133 	virtual	RenderingBuffer*	FrontBuffer() const = 0;
134 	virtual	RenderingBuffer*	BackBuffer() const = 0;
135 	virtual	bool				IsDoubleBuffered() const;
136 
137 	// Invalidate is planned to be used for scheduling an area for updating
138 	// you need to WriteLock!
139 	virtual	status_t			Invalidate(const BRect& frame);
140 	// while as CopyBackToFront() actually performs the operation
141 			status_t			CopyBackToFront(const BRect& frame);
142 
143 	// TODO: Just a quick and primitive way to get single buffered mode working.
144 	// Later, the implementation should be smarter, right now, it will
145 	// draw the cursor for almost every drawing operation.
146 	// It seems to me BeOS hides the cursor (in laymans words) before
147 	// BView::Draw() is called (if the cursor is within that views clipping region),
148 	// then, after all drawing commands that triggered have been caried out,
149 	// it shows the cursor again. This approach would have the adventage of
150 	// the code not cluttering/slowing down DrawingEngine.
151 	// For now, we hide the cursor for any drawing operation that has
152 	// a bounding box containing the cursor (in DrawingEngine) so
153 	// the cursor hiding is completely transparent from code using DrawingEngine.
154 	// ---
155 	// NOTE: Investigate locking for these! The client code should already hold a
156 	// ReadLock, but maybe these functions should acquire a WriteLock!
157 			bool				HideSoftwareCursor(const BRect& area);
158 			void				HideSoftwareCursor();
159 			void				ShowSoftwareCursor();
160 
161 	// Listener support
162 			bool				AddListener(HWInterfaceListener* listener);
163 			void				RemoveListener(HWInterfaceListener* listener);
164 
165  protected:
166 	// implement this in derived classes
167 	virtual	void				_DrawCursor(BRect area) const;
168 
169 	// does the actual transfer and handles color space conversion
170 			void				_CopyToFront(uint8* src, uint32 srcBPR,
171 											 int32 x, int32 y,
172 											 int32 right, int32 bottom) const;
173 
174 			BRect				_CursorFrame() const;
175 			void				_RestoreCursorArea() const;
176 			void				_AdoptDragBitmap(const ServerBitmap* bitmap,
177 												 const BPoint& offset);
178 
179 			void				_NotifyFrameBufferChanged();
180 
181 			// If we draw the cursor somewhere in the drawing buffer,
182 			// we need to backup its contents before drawing, so that
183 			// we can restore that area when the cursor needs to be
184 			// drawn somewhere else.
185 			struct buffer_clip {
186 								buffer_clip(int32 width, int32 height)
187 								{
188 									bpr = width * 4;
189 									if (bpr > 0 && height > 0)
190 										buffer = new uint8[bpr * height];
191 									else
192 										buffer = NULL;
193 									left = 0;
194 									top = 0;
195 									right = -1;
196 									bottom = -1;
197 									cursor_hidden = true;
198 								}
199 								~buffer_clip()
200 								{
201 									delete[] buffer;
202 								}
203 				uint8*			buffer;
204 				int32			left;
205 				int32			top;
206 				int32			right;
207 				int32			bottom;
208 				int32			bpr;
209 				bool			cursor_hidden;
210 			};
211 
212 			buffer_clip*		fCursorAreaBackup;
213 	mutable	BLocker				fSoftwareCursorLock;
214 
215 			ServerCursor*		fCursor;
216 			const ServerBitmap*	fDragBitmap;
217 			BPoint				fDragBitmapOffset;
218 			ServerCursor*		fCursorAndDragBitmap;
219 			bool				fCursorVisible;
220 			bool				fCursorObscured;
221 			BPoint				fCursorLocation;
222 			bool				fDoubleBuffered;
223 			int					fVGADevice;
224 
225  private:
226 			UpdateQueue*		fUpdateExecutor;
227 
228 			BList				fListeners;
229 };
230 
231 #endif // HW_INTERFACE_H
232