Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
10 KiB

  1. /***************************************************************************\
  2. *
  3. * File: Buffer.h
  4. *
  5. * Description:
  6. * Buffer.h contains definitions of objects used in buffering operations,
  7. * including double buffering, DX-Transforms, etc. These objects are
  8. * maintained by a central BufferManager that is available process-wide.
  9. *
  10. *
  11. * History:
  12. * 1/18/2000: JStall: Created
  13. *
  14. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  15. *
  16. \***************************************************************************/
  17. #if !defined(SERVICES__Buffer_h__INCLUDED)
  18. #define SERVICES__Buffer_h__INCLUDED
  19. #pragma once
  20. #include "DxManager.h"
  21. #include "GdiCache.h"
  22. #include "Surface.h"
  23. #define ENABLE_USEFASTDIB 1
  24. class DuSurface;
  25. /***************************************************************************\
  26. *
  27. * class BmpBuffer
  28. *
  29. * BmpBuffer abstracts out drawing using a double buffer by ensuring the
  30. * buffer is properly setup and is used internally inside the BufferManager
  31. * to manage resources. This class provides a foundation for all bitmap
  32. * buffers.
  33. *
  34. \***************************************************************************/
  35. class BmpBuffer
  36. {
  37. // Construction
  38. public:
  39. virtual ~BmpBuffer() { };
  40. enum EDrawCmd
  41. {
  42. dcNone = 0, // No special processing
  43. dcCopyBkgnd = 1, // Copy the destination as a background
  44. };
  45. // Operations
  46. public:
  47. virtual HRESULT BeginDraw(DuSurface * psrfDraw, const RECT * prcInvalid, UINT nCmd, DuSurface ** ppsrfBuffer) PURE;
  48. virtual void Fill(COLORREF cr) PURE;
  49. virtual void PreEndDraw(BOOL fCommit) PURE;
  50. virtual void EndDraw(BOOL fCommit, BYTE bAlphaLevel = BLEND_OPAQUE, BYTE bAlphaFormat = 0) PURE;
  51. virtual void PostEndDraw() PURE;
  52. virtual void SetupClipRgn() PURE;
  53. virtual BOOL InUse() const PURE;
  54. virtual DuSurface::EType
  55. GetType() const PURE;
  56. // Data
  57. protected:
  58. SIZE m_sizeBmp;
  59. POINT m_ptDraw;
  60. SIZE m_sizeDraw;
  61. UINT m_nCmd;
  62. BOOL m_fChangeOrg:1;
  63. BOOL m_fChangeXF:1;
  64. BOOL m_fClip:1;
  65. };
  66. /***************************************************************************\
  67. *
  68. * class DCBmpBuffer
  69. *
  70. * DCBmpBuffer implements a double-buffer for GDI.
  71. *
  72. \***************************************************************************/
  73. class DCBmpBuffer : public BmpBuffer
  74. {
  75. // Construction
  76. public:
  77. DCBmpBuffer();
  78. virtual ~DCBmpBuffer();
  79. // Operations
  80. public:
  81. virtual HRESULT BeginDraw(DuSurface * psrfDraw, const RECT * prcInvalid, UINT nCmd, DuSurface ** ppsrfBuffer);
  82. virtual void Fill(COLORREF cr);
  83. virtual void PreEndDraw(BOOL fCommit);
  84. virtual void EndDraw(BOOL fCommit, BYTE bAlphaLevel = BLEND_OPAQUE, BYTE bAlphaFormat = 0);
  85. virtual void PostEndDraw();
  86. virtual void SetupClipRgn();
  87. virtual BOOL InUse() const;
  88. virtual DuSurface::EType
  89. GetType() const { return DuSurface::stDC; }
  90. // Implementation
  91. protected:
  92. BOOL AllocBitmap(HDC hdcDraw, int cx, int cy);
  93. void FreeBitmap();
  94. // Data
  95. protected:
  96. HBITMAP m_hbmpBuffer;
  97. HBITMAP m_hbmpOld;
  98. HPALETTE m_hpalOld;
  99. HDC m_hdcDraw;
  100. HDC m_hdcBitmap;
  101. HRGN m_hrgnDrawClip;
  102. HRGN m_hrgnDrawOld;
  103. POINT m_ptOldBrushOrg;
  104. int m_nOldGfxMode;
  105. XFORM m_xfOldDraw;
  106. XFORM m_xfOldBitmap;
  107. };
  108. /***************************************************************************\
  109. *
  110. * class GpBmpBuffer
  111. *
  112. * GpBmpBuffer implements a double-buffer for GDI.
  113. *
  114. \***************************************************************************/
  115. class GpBmpBuffer : public BmpBuffer
  116. {
  117. // Construction
  118. public:
  119. GpBmpBuffer();
  120. virtual ~GpBmpBuffer();
  121. // Operations
  122. public:
  123. virtual HRESULT BeginDraw(DuSurface * psrfDraw, const RECT * prcInvalid, UINT nCmd, DuSurface ** ppsrfBuffer);
  124. virtual void Fill(COLORREF cr);
  125. virtual void PreEndDraw(BOOL fCommit);
  126. virtual void EndDraw(BOOL fCommit, BYTE bAlphaLevel = BLEND_OPAQUE, BYTE bAlphaFormat = 0);
  127. virtual void PostEndDraw();
  128. virtual void SetupClipRgn();
  129. virtual BOOL InUse() const;
  130. virtual DuSurface::EType
  131. GetType() const { return DuSurface::stGdiPlus; }
  132. // Implementation
  133. protected:
  134. BOOL AllocBitmap(Gdiplus::Graphics * pgpgr, int cx, int cy);
  135. void FreeBitmap();
  136. // Data
  137. protected:
  138. #if ENABLE_USEFASTDIB
  139. HBITMAP m_hbmpBuffer;
  140. HBITMAP m_hbmpOld;
  141. HDC m_hdcBitmap;
  142. BITMAPINFOHEADER m_bmih;
  143. void * m_pvBits;
  144. #else
  145. Gdiplus::Bitmap * m_pgpbmpBuffer;
  146. #endif
  147. Gdiplus::Graphics * m_pgpgrBitmap;
  148. Gdiplus::Graphics * m_pgpgrDraw;
  149. Gdiplus::Region * m_pgprgnDrawClip;
  150. Gdiplus::Region * m_pgprgnDrawOld;
  151. Gdiplus::Matrix m_gpmatOldDraw;
  152. Gdiplus::Matrix m_gpmatOldBitmap;
  153. };
  154. /***************************************************************************\
  155. *****************************************************************************
  156. *
  157. * class DCBmpBufferCache
  158. *
  159. * DCBmpBufferCache implements a DCBmpBuffer cache.
  160. *
  161. *****************************************************************************
  162. \***************************************************************************/
  163. class DCBmpBufferCache : public ObjectCache
  164. {
  165. public:
  166. inline DCBmpBuffer* Get();
  167. inline void Release(DCBmpBuffer * pbufBmp);
  168. protected:
  169. virtual void * Build();
  170. virtual void DestroyObject(void * pObj);
  171. };
  172. /***************************************************************************\
  173. *****************************************************************************
  174. *
  175. * class GpBmpBufferCache
  176. *
  177. * GpBmpBufferCache implements a GpBmpBuffer cache.
  178. *
  179. *****************************************************************************
  180. \***************************************************************************/
  181. class GpBmpBufferCache : public ObjectCache
  182. {
  183. public:
  184. inline GpBmpBuffer* Get();
  185. inline void Release(GpBmpBuffer * pbufBmp);
  186. protected:
  187. virtual void * Build();
  188. virtual void DestroyObject(void * pObj);
  189. };
  190. /***************************************************************************\
  191. *
  192. * class TrxBuffer
  193. *
  194. * TrxBuffer maintains a set of DxSurfaces that are used by Transitions. The
  195. * BufferManager will internally build these objects, as needed, for
  196. * Transitions. All of the surfaces in the buffer will be the same size, as
  197. * this is standard for Transitions.
  198. *
  199. \***************************************************************************/
  200. class TrxBuffer
  201. {
  202. // Construction
  203. public:
  204. TrxBuffer();
  205. ~TrxBuffer();
  206. static HRESULT Build(SIZE sizePxl, int cSurfaces, TrxBuffer ** ppbufNew);
  207. // Operations
  208. public:
  209. inline DxSurface * GetSurface(int idxSurface) const;
  210. inline SIZE GetSize() const;
  211. inline BOOL GetInUse() const;
  212. inline void SetInUse(BOOL fInUse);
  213. // Implementation
  214. protected:
  215. HRESULT BuildSurface(int idxSurface);
  216. void RemoveAllSurfaces();
  217. // Data
  218. protected:
  219. enum {
  220. MAX_Surfaces = 3 // All DxTx only use 2 In and 1 Out at most
  221. };
  222. SIZE m_sizePxl; // Size (in pixels) of each surface
  223. int m_cSurfaces; // Number of surfaces
  224. DxSurface * m_rgpsur[MAX_Surfaces]; // Collection of DX surfaces
  225. BOOL m_fInUse; // Buffer is being used
  226. };
  227. /***************************************************************************\
  228. *
  229. * class BufferManager
  230. *
  231. * BufferManager maintains a collection of buffers of various types across
  232. * the entire process (including multiple threads).
  233. *
  234. \***************************************************************************/
  235. class BufferManager
  236. {
  237. // Construction
  238. public:
  239. BufferManager();
  240. ~BufferManager();
  241. void Destroy();
  242. // Operations
  243. public:
  244. //
  245. // TODO: Change the implementation of these functions so that they are
  246. // reentrant (multi-threaded friendly).
  247. //
  248. inline HRESULT GetSharedBuffer(const RECT * prcInvalid, DCBmpBuffer ** ppbuf);
  249. inline HRESULT GetSharedBuffer(const RECT * prcInvalid, GpBmpBuffer ** ppbuf);
  250. inline void ReleaseSharedBuffer(BmpBuffer * pbuf);
  251. HRESULT GetCachedBuffer(DuSurface::EType type, BmpBuffer ** ppbuf);
  252. void ReleaseCachedBuffer(BmpBuffer * pbuf);
  253. HRESULT BeginTransition(SIZE sizePxl, int cSurfaces, BOOL fExactSize, TrxBuffer ** ppbuf);
  254. void EndTransition(TrxBuffer * pbufTrx, BOOL fCache);
  255. void FlushTrxBuffers();
  256. // Implementation
  257. protected:
  258. void RemoveAllTrxBuffers();
  259. // Data
  260. protected:
  261. //
  262. // TODO: Change these to be dynamically allocated and maintained across
  263. // multiple threads, automatically freeing resources after not used
  264. // for a specified timeout (perhaps 10 minutes).
  265. //
  266. // Bitmaps used by double-buffering
  267. DCBmpBuffer m_bufDCBmpShared;
  268. GpBmpBuffer * m_pbufGpBmpShared;
  269. DCBmpBufferCache m_cacheDCBmpCached; // Cached buffers (long ownership)
  270. GpBmpBufferCache m_cacheGpBmpCached; // Cached buffers (long ownership)
  271. // Surfaces used by Transitions
  272. TrxBuffer * m_pbufTrx;
  273. };
  274. #include "Buffer.inl"
  275. #endif // SERVICES__Buffer_h__INCLUDED