convert line ends
[canaan.git] / prj / tech / libsrc / dispdev / wdisp.h
blobd7a7bec1ed5f8bcaa30678f5eafc8fcd75d74ef3
1 ///////////////////////////////////////////////////////////////////////////////
2 // $Source: x:/prj/tech/libsrc/dispdev/RCS/wdisp.h $
3 // $Author: KEVIN $
4 // $Date: 1998/01/12 15:44:26 $
5 // $Revision: 1.28 $
6 //
8 #ifndef __WDISP_H
9 #define __WDISP_H
11 #include <dispbase.h>
12 #include <wdispapi.h>
14 #include <thrdtool.h>
15 #include <relocptr.h>
17 ///////////////////////////////////////////////////////////////////////////////
19 F_DECLARE_INTERFACE(IWinApp);
21 class cWinDisplayProvider;
22 class cWinDisplayModeOperations;
24 ///////////////////////////////////////////////////////////////////////////////
26 // STRUCT: sGdiBitmapInfo
28 // Initializes and stores information required to create a GDI bitmap
31 struct sGdiBitmapInfo : public BITMAPINFO
33 sGdiBitmapInfo(int width, int height, int significantDepth);
34 RGBQUAD bmiColors2[255];
37 // Typedef Task Switch callback function
39 ///////////////////////////////////////////////////////////////////////////////
41 // CLASS: cWinDisplayDevice
43 // Implements IDisplayDevice. Acts primarily as an adaptor for instances
44 // of cWinDisplayProvider and cWinDisplayModeOperations
47 class cWinDisplayDevice : public cDisplayDevice
49 public:
50 cWinDisplayDevice(IUnknown * pOuterUnknown, eDisplayDeviceKind kind, int flags);
51 virtual ~cWinDisplayDevice();
53 ///////////////////////////////////
55 // Query functions for client Providers and Operations
59 // Query if the device has been opened
61 BOOL IsOpen() const;
64 // Query if the device is useful (i.e., is in some mode, any mode)
66 BOOL IsModeSet() const;
69 // Query the lock count
71 int GetLockCount() const;
74 // Query kind
76 STDMETHOD_(eDisplayDeviceKind, GetKind)();
79 // Query the creation flags
81 int GetCreateFlags() const;
84 // Temporarily break any lock on surfaces, returning lock count
86 STDMETHOD_(int, BreakLock)();
89 // Restore broken lock
91 STDMETHOD_(void, RestoreLock)(int iLockCount);
94 // Try to gain exclusive use of display object, without locking video memory.
96 STDMETHOD_(BOOL, IsLocked)();
99 // Threading hooks
101 void WaitForMutex();
102 void ReleaseMutex();
103 int BreakMutex();
104 void RestoreMutex(int);
107 // Get the properties of the current mode
109 const sGrModeInfo & GetCurrentModeInfo() const;
112 // Palette tools
114 struct sPalette256 : public LOGPALETTE
116 sPalette256()
118 palVersion = 0x300;
119 palNumEntries = 256;
121 PALETTEENTRY AddEntries[255];
124 LOGPALETTE & GetLogPalette();
125 PALETTEENTRY * GetPaletteEntries();
127 // Set kind (must be outside Open/Close pair)
128 STDMETHOD_(void, SetKind)(eDisplayDeviceKind, int flags);
130 // Get information about the device
131 STDMETHOD_(void, GetInfo)(sGrDeviceInfo *, sGrModeInfo *);
133 // Open/Close the device
134 STDMETHOD (Open)(sGrModeCap *, int fFlags);
135 STDMETHOD (Close)();
137 // Set/get the mode
138 STDMETHOD (SetMode)(eGrDispMode, int fFlags);
139 STDMETHOD_(eGrDispMode, GetMode)(sGrModeInfoEx * pOptionalModeInfo);
141 // Return TRUE of retrace is in progress
142 STDMETHOD_(BOOL, StatHTrace)();
143 STDMETHOD_(BOOL, StatVTrace)();
145 // Set/Get the palette (if 8-bit palletized)
146 STDMETHOD_(BOOL, SetPalette)(unsigned uStart, unsigned n, const unsigned char * pRGBs);
147 STDMETHOD_(BOOL, GetPalette)(unsigned uStart, unsigned n, unsigned char * pRGBs);
149 // Flip the visible surface. No-op if no back buffer.
150 STDMETHOD (PageFlip)();
152 // Try to lock video memory. Locked pointers will be placed in
153 // structure provided in SetMode(). Returns lock count.
154 STDMETHOD_(int, Lock)();
156 // Release a lock on memory
157 STDMETHOD_(int, Unlock)();
159 // Verify display reflects what is expected.
160 STDMETHOD_(void, Flush)();
162 // Flush a specific region
163 STDMETHOD_(void, FlushRect)(int x0, int y0, int x1, int y1);
165 // Add/Remove a pointer to the monitor set.
166 STDMETHOD_(BOOL, SetMonitor)(tGrDispBits **, eDisplayDeviceMonitorFlag);
168 // Create a device based bitmap
169 STDMETHOD (CreateBitmap)(sGrDevBitmapDesc *, int flags, IDeviceBitmap **);
171 // Locate an IDeviceBitmap for the given sGrBitmap, if any exists
172 STDMETHOD (FindBitmap)(const sGrBitmap *, IDeviceBitmap **);
175 STDMETHOD (GetRenderTargets)(sGrRenderTargets *);
177 // Set kind (must be outside Open/Close pair)
178 STDMETHOD_(void, SetKind2)(eDisplayDeviceKind, int flags, GUID *pDDrawGuid);
180 // Query kind, flags, and ddraw guid
181 STDMETHOD_(void, GetKind2)(eDisplayDeviceKind *, int *, GUID **);
183 // Create a GDI bitmap
184 HBITMAP CreateGDIBitmap(sGdiBitmapInfo * pBitmapInfo);
186 private:
188 ///////////////////////////////////
190 // Fixes-up monitored pointers
191 void Sync2DPointers();
192 void InvalidateBasePointers();
194 // Manipulation of m_LogicalPalette
195 static void SetStoredPaletteEntries(unsigned uStart, unsigned n, const unsigned char * pRGBs, int paletteFlags);
196 static void GetStoredPaletteEntries(unsigned uStart, unsigned n, unsigned char * pRGBs);
198 ///////////////////////////////////
200 void DoCursorBltFromGDIScreen(sGrBitmap *, int x, int y);
201 void DoCursorBltToGDIScreen(sGrBitmap *, int x, int y);
203 ///////////////////////////////////
205 // Enums and constants
208 enum
210 kTotallyBogusBase = 0xffeeffee,
211 kPrimaryBogusBase = 0xfefefefe,
212 kSecondaryBogusBase = 0xfdfdfdfd
215 enum eWinDispFlags
217 // Start higher than base flags!
218 kStrictMonitors = 0x10,
219 kSettingMode = 0x20 // because setting the mode often
220 // requires breaking the display
221 // device mutex, an additional
222 // bit ensures the message thread
223 // doesn't do anything bad
226 ///////////////////////////////////
228 // CLASS: cIWinDisplayDeviceProxy
230 // Simply passes requests to outer object
233 class cIWinDisplayDeviceProxy : public IWinDisplayDevice
235 public:
236 cIWinDisplayDeviceProxy(cWinDisplayDevice * pOuter, IUnknown * pOuterUnknown)
237 : m_pOuter(pOuter), m_pTaskSwitchCallback(NULL)
239 INIT_DELEGATION(pOuterUnknown);
242 private:
243 DECLARE_DELEGATION();
245 STDMETHOD_(BOOL, ProcessMessage)(UINT msg, WPARAM wParam, LPARAM lParam, long * pRetVal);
246 STDMETHOD_(void, GetWidthHeight)(unsigned * pWidth, unsigned * pHeight);
247 STDMETHOD_(void, OnTaskSwitch) (BOOL);
248 STDMETHOD_(BOOL, GetDirectDraw) (IDirectDraw **);
249 STDMETHOD_(void, WaitForMutex)();
250 STDMETHOD_(void, ReleaseMutex)();
251 STDMETHOD_(void, CursorBltFromGDIScreen)(sGrBitmap *, int x, int y);
252 STDMETHOD_(void, CursorBltToGDIScreen)(sGrBitmap *, int x, int y);
253 STDMETHOD_(BOOL, GetBitmapSurface)(sGrBitmap *, IDirectDrawSurface **);
254 STDMETHOD_(void, AddTaskSwitchCallback)(WinDispDevCallbackFunc *callback);
255 STDMETHOD_(void, RemoveTaskSwitchCallback)(int id);
257 cWinDisplayDevice * m_pOuter;
258 WinDispDevCallbackFunc *m_pTaskSwitchCallback;
261 friend class cIWinDisplayDeviceProxy;
263 ///////////////////////////////////
265 // Instance Data
268 public:
269 GUID * m_pDDrawGuid;
271 private:
272 int m_iLock;
273 cWinDisplayModeOperations * m_pDisplayModeOperations;
275 ///////////////////////////////////
277 sGrModeCap * m_pModeInfo;
278 eGrDispMode m_CurrentDispMode;
280 ///////////////////////////////////
282 eDisplayDeviceKind m_DisplayDeviceKind;
283 int m_fOptions;
284 cWinDisplayProvider * m_pDisplayProvider;
285 cIWinDisplayDeviceProxy m_IWinDisplayDeviceProxy;
287 ///////////////////////////////////
289 cThreadLock m_Mutex;
290 int m_MutexCount;
292 ///////////////////////////////////
294 cRelocationSet<tGrDispBits> m_PrimarySurfaceRelocationSet;
295 cRelocationSet<tGrDispBits> m_SecondarySurfaceRelocationSet;
297 ///////////////////////////////////
299 struct sCursorScratch
301 sCursorScratch()
303 hBitmap = NULL;
304 memset(&bitmap, 0, sizeof(BITMAP));
307 ~sCursorScratch()
309 if (hBitmap)
310 DeleteObject(hBitmap);
313 HBITMAP hBitmap;
314 BITMAP bitmap;
317 sCursorScratch m_CursorScratch;
319 ///////////////////////////////////
321 // Static data
324 static sPalette256 gm_LogicalPalette;
328 ///////////////////////////////////////////////////////////////////////////////
330 // Utility classes for display device mutex
333 class cAutoDisplayMutex
335 public:
336 cAutoDisplayMutex(cWinDisplayDevice * pWinDisplayDevice)
338 m_pWinDisplayDevice = pWinDisplayDevice;
339 m_pWinDisplayDevice->WaitForMutex();
342 ~cAutoDisplayMutex()
344 m_pWinDisplayDevice->ReleaseMutex();
347 private:
348 cWinDisplayDevice * m_pWinDisplayDevice;
353 // Yield the display. Sorry, only 1 per scope
355 #define YieldDisplay(pDisplayDevice) \
356 int _YieldDisplay_iPreviousLock = pDisplayDevice->BreakLock(); \
357 int _YieldDisplay_iPreviousMutexLevel = pDisplayDevice->BreakMutex()
359 #define RegainDisplay(pDisplayDevice) \
360 m_pDisplayDevice->RestoreMutex(_YieldDisplay_iPreviousMutexLevel);\
361 m_pDisplayDevice->RestoreLock(_YieldDisplay_iPreviousLock)
364 ///////////////////////////////////////////////////////////////////////////////
366 // Inline members of cWinDisplayDevice
369 inline BOOL cWinDisplayDevice::IsOpen() const
371 return !!m_pModeInfo;
374 ///////////////////////////////////////
376 inline BOOL cWinDisplayDevice::IsModeSet() const
378 return (m_pDisplayModeOperations && !(m_flags & kSettingMode));
381 ///////////////////////////////////////
383 inline int cWinDisplayDevice::GetLockCount() const
385 return m_iLock;
388 ///////////////////////////////////////
390 inline int cWinDisplayDevice::GetCreateFlags() const
392 return m_fOptions;
395 ///////////////////////////////////////
397 inline void cWinDisplayDevice::WaitForMutex()
399 m_Mutex.Lock();
400 m_MutexCount++;
403 ///////////////////////////////////////
405 inline void cWinDisplayDevice::ReleaseMutex()
407 m_MutexCount--;
408 m_Mutex.Unlock();
411 ///////////////////////////////////////
413 inline int cWinDisplayDevice::BreakMutex()
415 m_Mutex.Lock();
416 const int retVal = m_MutexCount;
417 while (m_MutexCount)
419 m_Mutex.Unlock();
420 m_MutexCount--;
422 m_Mutex.Unlock();
423 return retVal;
426 ///////////////////////////////////////
428 inline void cWinDisplayDevice::RestoreMutex(int iMutexCount)
430 m_Mutex.Lock();
431 m_MutexCount += iMutexCount;
432 while (iMutexCount)
434 m_Mutex.Lock();
435 iMutexCount--;
437 m_Mutex.Unlock();
440 ///////////////////////////////////////
442 inline const sGrModeInfo & cWinDisplayDevice::GetCurrentModeInfo() const
444 return EnumModeToModeInfo(m_CurrentDispMode);
447 ///////////////////////////////////////
449 inline LOGPALETTE & cWinDisplayDevice::GetLogPalette()
451 return gm_LogicalPalette;
454 ///////////////////////////////////////
456 inline PALETTEENTRY * cWinDisplayDevice::GetPaletteEntries()
458 return gm_LogicalPalette.palPalEntry;
461 ///////////////////////////////////////
463 inline HBITMAP cWinDisplayDevice::CreateGDIBitmap(sGdiBitmapInfo * pBitmapInfo)
465 HBITMAP hBitmap;
466 UINT fUsage = (pBitmapInfo->bmiHeader.biCompression == BI_RGB) ? DIB_RGB_COLORS : 0;
467 void * pDummy; // @Note (toml 04-12-96): In winG, the bits argument could be NULL. We have to get it here, even though we retreive it again with the GetObject() below. This section should be cleaned-up
469 HDC hdcScreen = GetDC(NULL);
470 hBitmap = CreateDIBSection(hdcScreen, pBitmapInfo, fUsage, &pDummy, NULL, 0);
471 ReleaseDC(NULL, hdcScreen);
472 return hBitmap;
475 ///////////////////////////////////////////////////////////////////////////////
477 #endif /* !__WDISP_H */