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.

381 lines
12 KiB

  1. namespace RGB_RAST_LIB_NAMESPACE
  2. {
  3. void MemFill( UINT32 uiData, void* pData, UINT32 uiBytes) throw();
  4. void MemMask( UINT32 uiData, UINT32 uiMask, void* pData, UINT32 uiBytes) throw();
  5. class IRGBSurface: public IVidMemSurface
  6. {
  7. protected:
  8. LONG m_lPitch;
  9. WORD m_wWidth;
  10. WORD m_wHeight;
  11. unsigned char m_ucBPP;
  12. IRGBSurface( DWORD dwHandle, LONG P, WORD W, WORD H, unsigned char BPP)
  13. throw(): IVidMemSurface( dwHandle), m_lPitch( P), m_wWidth( W),
  14. m_wHeight( H), m_ucBPP( BPP)
  15. { }
  16. public:
  17. LONG GetGBLlPitch( void) const throw()
  18. { return m_lPitch; }
  19. WORD GetGBLwWidth( void) const throw()
  20. { return m_wWidth; }
  21. WORD GetGBLwHeight( void) const throw()
  22. { return m_wHeight; }
  23. unsigned char GetBytesPerPixel( void) const throw()
  24. { return m_ucBPP; }
  25. virtual ~IRGBSurface() throw()
  26. { }
  27. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()= 0;
  28. };
  29. class CRGBSurfAllocator
  30. {
  31. public: // Types
  32. typedef IRGBSurface TSurface;
  33. typedef TSurface* (*TCreateSurfFn)( const DDSURFACEDESC&,
  34. PORTABLE_DDRAWSURFACE_LCL&);
  35. protected: // Types
  36. typedef vector< std::pair< DDSURFACEDESC, TCreateSurfFn> > TCreateSurfFns;
  37. TCreateSurfFns m_CreateSurfFns;
  38. struct SAdaptedMatchFn: public SMatchSDesc
  39. {
  40. typedef TCreateSurfFns::value_type argument_type;
  41. using SMatchSDesc::result_type;
  42. SAdaptedMatchFn( const DDSURFACEDESC& SDesc) throw(): SMatchSDesc( SDesc) {}
  43. result_type operator()( argument_type Arg) const throw()
  44. { return (*static_cast< const SMatchSDesc*>(this))( Arg.first); }
  45. };
  46. public:
  47. template< class TIter>
  48. CRGBSurfAllocator( TIter itStart, const TIter itEnd) throw(bad_alloc)
  49. {
  50. while( itStart!= itEnd)
  51. {
  52. m_CreateSurfFns.push_back(
  53. TCreateSurfFns::value_type( itStart->GetMatch(), *itStart));
  54. ++itStart;
  55. }
  56. }
  57. TSurface* CreateSurf( const DDSURFACEDESC& SDesc,
  58. PORTABLE_DDRAWSURFACE_LCL& Surf) const
  59. {
  60. TCreateSurfFns::const_iterator itFound(
  61. find_if( m_CreateSurfFns.begin(), m_CreateSurfFns.end(),
  62. SAdaptedMatchFn( SDesc) ) );
  63. // Hey, if we don't support a VM of this surface type,
  64. // but how did we get asked to allocate one, then?
  65. if( itFound== m_CreateSurfFns.end())
  66. throw HRESULT( DDERR_UNSUPPORTED);
  67. return (itFound->second)( SDesc, Surf);
  68. }
  69. };
  70. class CRGBSurface: public IRGBSurface
  71. {
  72. public: // Types
  73. typedef unsigned int TLocks;
  74. protected: // Variables
  75. void* m_pData;
  76. size_t m_uiBytes;
  77. TLocks m_uiLocks;
  78. public: // Functions
  79. CRGBSurface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf)
  80. throw(bad_alloc): IRGBSurface( DDSurf.lpSurfMore()->dwSurfaceHandle(),
  81. 0, DDSurf.lpGbl()->wWidth, DDSurf.lpGbl()->wHeight, 0),
  82. m_pData( NULL), m_uiBytes( 0), m_uiLocks( 0)
  83. {
  84. // We must allocate this surface. Since we are specified as a SW driver,
  85. // DDraw will not allocate for us.
  86. assert((SDesc.dwFlags& DDSD_PIXELFORMAT)!= 0);
  87. m_ucBPP= static_cast< unsigned char>(
  88. SDesc.ddpfPixelFormat.dwRGBBitCount>> 3);
  89. // TODO: Align pitch to 128-bit bit boundary, instead?
  90. DDSurf.lpGbl()->lPitch= m_lPitch= ((m_ucBPP* m_wWidth+ 7)& ~7);
  91. m_uiBytes= m_lPitch* m_wHeight;
  92. // It would've been nice to have the initial proctection NOACCESS, but
  93. // it seems the HAL needs to read to the region, initially.
  94. m_pData= VirtualAlloc( NULL, m_uiBytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  95. if( m_pData== NULL)
  96. throw bad_alloc( "Not enough memory to allocate Surface data");
  97. DDSurf.lpGbl()->fpVidMem= reinterpret_cast<FLATPTR>( m_pData);
  98. }
  99. virtual ~CRGBSurface() throw()
  100. {
  101. // Warning: m_uiLocks doesn't have to be 0. The run-time will destroy
  102. // a surface without un-locking it.
  103. assert( m_pData!= NULL);
  104. VirtualFree( m_pData, 0, MEM_DECOMMIT| MEM_RELEASE);
  105. }
  106. virtual void* Lock( DWORD dwFlags, const RECTL* pRect) throw()
  107. {
  108. numeric_limits< TLocks> Dummy;
  109. assert( Dummy.max()!= m_uiLocks);
  110. #if 0 // defined(DBG) || defined(_DEBUG)
  111. // This code can't be enabled. Currently, run-time knows that this is
  112. // really a system memory surface, and thus doesn't tell us to lock it
  113. // into memory before using the bits. Known areas that could be fixed:
  114. // (surface creation needs valid pointer & Present uses pointer).
  115. if( 0== m_uiLocks)
  116. {
  117. DWORD dwProtect( PAGE_EXECUTE_READWRITE);
  118. if( dwFlags& DDLOCK_READONLY)
  119. dwProtect= PAGE_READONLY;
  120. else if( dwFlags& DDLOCK_WRITEONLY)
  121. dwProtect= PAGE_READWRITE;
  122. DWORD dwOldP;
  123. VirtualProtect( m_pData, m_uiBytes, dwProtect, &dwOldP);
  124. }
  125. #endif
  126. ++m_uiLocks;
  127. if( pRect!= NULL)
  128. {
  129. return static_cast<void*>( reinterpret_cast<UINT8*>(
  130. m_pData)+ pRect->top* m_lPitch+ pRect->left* m_ucBPP);
  131. }
  132. else
  133. return m_pData;
  134. }
  135. virtual void Unlock( void) throw()
  136. {
  137. assert( 0!= m_uiLocks);
  138. #if 0 // defined(DBG) || defined(_DEBUG)
  139. if( 0== --m_uiLocks)
  140. {
  141. DWORD dwOldP;
  142. VirtualProtect( m_pData, m_uiBytes, PAGE_NOACCESS, &dwOldP);
  143. }
  144. #else
  145. --m_uiLocks;
  146. #endif
  147. }
  148. };
  149. class CR5G6B5Surface: public CRGBSurface
  150. {
  151. public:
  152. CR5G6B5Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  153. CRGBSurface( SDesc, DDSurf) { }
  154. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw();
  155. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  156. { return D3DI_SPTFMT_B5G6R5; }
  157. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  158. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  159. {
  160. return new CR5G6B5Surface( SDesc, DDSurf);
  161. }
  162. };
  163. class CA8R8G8B8Surface: public CRGBSurface
  164. {
  165. public:
  166. CA8R8G8B8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  167. CRGBSurface( SDesc, DDSurf) { }
  168. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw();
  169. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  170. { return D3DI_SPTFMT_B8G8R8A8; }
  171. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  172. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  173. {
  174. return new CA8R8G8B8Surface( SDesc, DDSurf);
  175. }
  176. };
  177. class CX8R8G8B8Surface: public CRGBSurface
  178. {
  179. public:
  180. CX8R8G8B8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  181. CRGBSurface( SDesc, DDSurf) { }
  182. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw();
  183. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  184. { return D3DI_SPTFMT_B8G8R8X8; }
  185. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  186. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  187. {
  188. return new CX8R8G8B8Surface( SDesc, DDSurf);
  189. }
  190. };
  191. class CD16Surface: public CRGBSurface
  192. {
  193. public:
  194. CD16Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  195. CRGBSurface( SDesc, DDSurf) { }
  196. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw();
  197. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  198. { return D3DI_SPTFMT_Z16S0; }
  199. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  200. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  201. {
  202. return new CD16Surface( SDesc, DDSurf);
  203. }
  204. };
  205. class CD24S8Surface: public CRGBSurface
  206. {
  207. public:
  208. CD24S8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  209. CRGBSurface( SDesc, DDSurf) { }
  210. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw();
  211. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  212. { return D3DI_SPTFMT_Z24S8; }
  213. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  214. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  215. {
  216. return new CD24S8Surface( SDesc, DDSurf);
  217. }
  218. };
  219. class CX1R5G5B5Surface: public CRGBSurface
  220. {
  221. public:
  222. CX1R5G5B5Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  223. CRGBSurface( SDesc, DDSurf) { }
  224. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  225. {
  226. const bool CX1R5G5B5Surface_being_asked_to_Clear( false);
  227. assert( CX1R5G5B5Surface_being_asked_to_Clear);
  228. }
  229. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  230. { return D3DI_SPTFMT_B5G5R5; }
  231. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  232. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  233. {
  234. return new CX1R5G5B5Surface( SDesc, DDSurf);
  235. }
  236. };
  237. class CA1R5G5B5Surface: public CRGBSurface
  238. {
  239. public:
  240. CA1R5G5B5Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  241. CRGBSurface( SDesc, DDSurf) { }
  242. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  243. {
  244. const bool CA1R5G5B5Surface_being_asked_to_Clear( false);
  245. assert( CA1R5G5B5Surface_being_asked_to_Clear);
  246. }
  247. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  248. { return D3DI_SPTFMT_B5G5R5A1; }
  249. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  250. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  251. {
  252. return new CA1R5G5B5Surface( SDesc, DDSurf);
  253. }
  254. };
  255. class CP8Surface: public CRGBSurface
  256. {
  257. public:
  258. CP8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  259. CRGBSurface( SDesc, DDSurf) { }
  260. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  261. {
  262. const bool CP8Surface_being_asked_to_Clear( false);
  263. assert( CP8Surface_being_asked_to_Clear);
  264. }
  265. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  266. { return D3DI_SPTFMT_PALETTE8; }
  267. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  268. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  269. {
  270. return new CP8Surface( SDesc, DDSurf);
  271. }
  272. };
  273. class CA8L8Surface: public CRGBSurface
  274. {
  275. public:
  276. CA8L8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  277. CRGBSurface( SDesc, DDSurf) { }
  278. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  279. {
  280. const bool CA8L8Surface_being_asked_to_Clear( false);
  281. assert( CA8L8Surface_being_asked_to_Clear);
  282. }
  283. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  284. { return D3DI_SPTFMT_L8A8; }
  285. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  286. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  287. {
  288. return new CA8L8Surface( SDesc, DDSurf);
  289. }
  290. };
  291. class CL8Surface: public CRGBSurface
  292. {
  293. public:
  294. CL8Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  295. CRGBSurface( SDesc, DDSurf) { }
  296. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  297. {
  298. const bool CL8Surface_being_asked_to_Clear( false);
  299. assert( CL8Surface_being_asked_to_Clear);
  300. }
  301. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  302. { return D3DI_SPTFMT_L8; }
  303. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  304. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  305. {
  306. return new CL8Surface( SDesc, DDSurf);
  307. }
  308. };
  309. class CA4R4G4B4Surface: public CRGBSurface
  310. {
  311. public:
  312. CA4R4G4B4Surface( const DDSURFACEDESC& SDesc, PORTABLE_DDRAWSURFACE_LCL& DDSurf):
  313. CRGBSurface( SDesc, DDSurf) { }
  314. virtual void Clear( const D3DHAL_DP2CLEAR& DP2Clear, const RECT& RC) throw()
  315. {
  316. const bool CA4R4G4B4Surface_being_asked_to_Clear( false);
  317. assert( CA4R4G4B4Surface_being_asked_to_Clear);
  318. }
  319. virtual D3DI_SPANTEX_FORMAT GetSpanTexFormat( void) const throw()
  320. { return D3DI_SPTFMT_B4G4R4A4; }
  321. static IRGBSurface* Create( const DDSURFACEDESC& SDesc,
  322. PORTABLE_DDRAWSURFACE_LCL& DDSurf) throw(bad_alloc)
  323. {
  324. return new CA4R4G4B4Surface( SDesc, DDSurf);
  325. }
  326. };
  327. }