home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / directshow / baseclasses / winutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  15.7 KB  |  404 lines

  1. //------------------------------------------------------------------------------
  2. // File: WinUtil.h
  3. //
  4. // Desc: DirectShow base classes - defines generic handler classes.
  5. //
  6. // Copyright (c) 1992 - 2000, Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. // Make sure that you call PrepareWindow to initialise the window after
  11. // the object has been constructed. It is a separate method so that
  12. // derived classes can override useful methods like MessageLoop. Also
  13. // any derived class must call DoneWithWindow in its destructor. If it
  14. // doesn't a message may be retrieved and call a derived class member
  15. // function while a thread is executing the base class destructor code
  16.  
  17. #ifndef __WINUTIL__
  18. #define __WINUTIL__
  19.  
  20. const int DEFWIDTH = 320;                    // Initial window width
  21. const int DEFHEIGHT = 240;                   // Initial window height
  22. const int CAPTION = 256;                     // Maximum length of caption
  23. const int TIMELENGTH = 50;                   // Maximum length of times
  24. const int PROFILESTR = 128;                  // Normal profile string
  25. const WORD PALVERSION = 0x300;               // GDI palette version
  26. const LONG PALETTE_VERSION = (LONG) 1;       // Initial palette version
  27. const COLORREF VIDEO_COLOUR = 0;             // Defaults to black background
  28. const HANDLE hMEMORY = (HANDLE) (-1);        // Says to open as memory file
  29.  
  30. #define WIDTH(x) ((*(x)).right - (*(x)).left)
  31. #define HEIGHT(x) ((*(x)).bottom - (*(x)).top)
  32. #define SHOWSTAGE TEXT("WM_SHOWSTAGE")
  33. #define SHOWSTAGETOP TEXT("WM_SHOWSTAGETOP")
  34. #define REALIZEPALETTE TEXT("WM_REALIZEPALETTE")
  35.  
  36. class AM_NOVTABLE CBaseWindow
  37. {
  38. protected:
  39.  
  40.     HINSTANCE m_hInstance;          // Global module instance handle
  41.     HWND m_hwnd;                    // Handle for our window
  42.     HDC m_hdc;                      // Device context for the window
  43.     LONG m_Width;                   // Client window width
  44.     LONG m_Height;                  // Client window height
  45.     BOOL m_bActivated;              // Has the window been activated
  46.     LPTSTR m_pClassName;            // Static string holding class name
  47.     DWORD m_ClassStyles;            // Passed in to our constructor
  48.     DWORD m_WindowStyles;           // Likewise the initial window styles
  49.     DWORD m_WindowStylesEx;         // And the extended window styles
  50.     UINT m_ShowStageMessage;        // Have the window shown with focus
  51.     UINT m_ShowStageTop;            // Makes the window WS_EX_TOPMOST
  52.     UINT m_RealizePalette;          // Makes us realize our new palette
  53.     HDC m_MemoryDC;                 // Used for fast BitBlt operations
  54.     HPALETTE m_hPalette;            // Handle to any palette we may have
  55.     BYTE m_bNoRealize;              // Don't realize palette now
  56.     BYTE m_bBackground;             // Should we realise in background
  57.     BYTE m_bRealizing;              // already realizing the palette
  58.     CCritSec m_WindowLock;          // Serialise window object access
  59.     BOOL m_bDoGetDC;                // Should this window get a DC
  60.     bool m_bDoPostToDestroy;        // Use PostMessage to destroy
  61.  
  62.  
  63.     // Maps windows message procedure into C++ methods
  64.     friend LRESULT CALLBACK WndProc(HWND hwnd,      // Window handle
  65.                                     UINT uMsg,      // Message ID
  66.                                     WPARAM wParam,  // First parameter
  67.                                     LPARAM lParam); // Other parameter
  68.  
  69.     virtual LRESULT OnPaletteChange(HWND hwnd, UINT Message);
  70.  
  71. public:
  72.  
  73.     CBaseWindow(BOOL bDoGetDC = TRUE, bool bPostToDestroy = false);
  74.  
  75. #ifdef DEBUG
  76.     virtual ~CBaseWindow();
  77. #endif
  78.  
  79.     virtual HRESULT DoneWithWindow();
  80.     virtual HRESULT PrepareWindow();
  81.     virtual HRESULT InactivateWindow();
  82.     virtual HRESULT ActivateWindow();
  83.     virtual BOOL OnSize(LONG Width, LONG Height);
  84.     virtual BOOL OnClose();
  85.     virtual RECT GetDefaultRect();
  86.     virtual HRESULT UninitialiseWindow();
  87.     virtual HRESULT InitialiseWindow(HWND hwnd);
  88.  
  89.     HRESULT CompleteConnect();
  90.     HRESULT DoCreateWindow();
  91.  
  92.     HRESULT PerformanceAlignWindow();
  93.     HRESULT DoShowWindow(LONG ShowCmd);
  94.     void PaintWindow(BOOL bErase);
  95.     void DoSetWindowForeground(BOOL bFocus);
  96.     virtual HRESULT SetPalette(HPALETTE hPalette);
  97.     void SetRealize(BOOL bRealize)
  98.     {
  99.         m_bNoRealize = !bRealize;
  100.     }
  101.  
  102.     //  Jump over to the window thread to set the current palette
  103.     HRESULT SetPalette();
  104.  
  105.     virtual HRESULT DoRealisePalette(BOOL bForceBackground = FALSE);
  106.  
  107.     virtual BOOL PossiblyEatMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  108.         { return FALSE; };
  109.  
  110.     // Access our window information
  111.  
  112.     LONG GetWindowWidth();
  113.     LONG GetWindowHeight();
  114.     HWND GetWindowHWND();
  115.     HDC GetMemoryHDC();
  116.     HDC GetWindowHDC();
  117.  
  118.     // This is the window procedure the derived object should override
  119.  
  120.     virtual LRESULT OnReceiveMessage(HWND hwnd,          // Window handle
  121.                                      UINT uMsg,          // Message ID
  122.                                      WPARAM wParam,      // First parameter
  123.                                      LPARAM lParam);     // Other parameter
  124.  
  125.     // Must be overriden to return class and window styles
  126.  
  127.     virtual LPTSTR GetClassWindowStyles(
  128.                             DWORD *pClassStyles,          // Class styles
  129.                             DWORD *pWindowStyles,         // Window styles
  130.                             DWORD *pWindowStylesEx) PURE; // Extended styles
  131. };
  132.  
  133.  
  134. // This helper class is entirely subservient to the owning CBaseWindow object
  135. // All this object does is to split out the actual drawing operation from the
  136. // main object (because it was becoming too large). We have a number of entry
  137. // points to set things like the draw device contexts, to implement the actual
  138. // drawing and to set the destination rectangle in the client window. We have
  139. // no critical section locking in this class because we are used exclusively
  140. // by the owning window object which looks after serialising calls into us
  141.  
  142. // If you want to use this class make sure you call NotifyAllocator once the
  143. // allocate has been agreed, also call NotifyMediaType with a pointer to a
  144. // NON stack based CMediaType once that has been set (we keep a pointer to
  145. // the original rather than taking a copy). When the palette changes call
  146. // IncrementPaletteVersion (easiest thing to do is to also call this method
  147. // in the SetMediaType method most filters implement). Finally before you
  148. // start rendering anything call SetDrawContext so that we can get the HDCs
  149. // for drawing from the CBaseWindow object we are given during construction
  150.  
  151. class CDrawImage
  152. {
  153. protected:
  154.  
  155.     CBaseWindow *m_pBaseWindow;     // Owning video window object
  156.     CRefTime m_StartSample;         // Start time for the current sample
  157.     CRefTime m_EndSample;           // And likewise it's end sample time
  158.     HDC m_hdc;                      // Main window device context
  159.     HDC m_MemoryDC;                 // Offscreen draw device context
  160.     RECT m_TargetRect;              // Target destination rectangle
  161.     RECT m_SourceRect;              // Source image rectangle
  162.     BOOL m_bStretch;                // Do we have to stretch the images
  163.     BOOL m_bUsingImageAllocator;    // Are the samples shared DIBSECTIONs
  164.     CMediaType *m_pMediaType;       // Pointer to the current format
  165.     int m_perfidRenderTime;         // Time taken to render an image
  166.     LONG m_PaletteVersion;          // Current palette version cookie
  167.  
  168.     // Draw the video images in the window
  169.  
  170.     void SlowRender(IMediaSample *pMediaSample);
  171.     void FastRender(IMediaSample *pMediaSample);
  172.     void DisplaySampleTimes(IMediaSample *pSample);
  173.     void UpdateColourTable(HDC hdc,BITMAPINFOHEADER *pbmi);
  174.     void SetStretchMode();
  175.  
  176. public:
  177.  
  178.     // Used to control the image drawing
  179.  
  180.     CDrawImage(CBaseWindow *pBaseWindow);
  181.     BOOL DrawImage(IMediaSample *pMediaSample);
  182.     BOOL DrawVideoImageHere(HDC hdc, IMediaSample *pMediaSample,
  183.                             LPRECT lprcSrc, LPRECT lprcDst);
  184.     void SetDrawContext();
  185.     void SetTargetRect(RECT *pTargetRect);
  186.     void SetSourceRect(RECT *pSourceRect);
  187.     void GetTargetRect(RECT *pTargetRect);
  188.     void GetSourceRect(RECT *pSourceRect);
  189.     virtual RECT ScaleSourceRect(const RECT *pSource);
  190.  
  191.     // Handle updating palettes as they change
  192.  
  193.     LONG GetPaletteVersion();
  194.     void ResetPaletteVersion();
  195.     void IncrementPaletteVersion();
  196.  
  197.     // Tell us media types and allocator assignments
  198.  
  199.     void NotifyAllocator(BOOL bUsingImageAllocator);
  200.     void NotifyMediaType(CMediaType *pMediaType);
  201.     BOOL UsingImageAllocator();
  202.  
  203.     // Called when we are about to draw an image
  204.  
  205.     void NotifyStartDraw() {
  206.         MSR_START(m_perfidRenderTime);
  207.     };
  208.  
  209.     // Called when we complete an image rendering
  210.  
  211.     void NotifyEndDraw() {
  212.         MSR_STOP(m_perfidRenderTime);
  213.     };
  214. };
  215.  
  216.  
  217. // This is the structure used to keep information about each GDI DIB. All the
  218. // samples we create from our allocator will have a DIBSECTION allocated to
  219. // them. When we receive the sample we know we can BitBlt straight to an HDC
  220.  
  221. typedef struct tagDIBDATA {
  222.  
  223.     LONG        PaletteVersion;     // Current palette version in use
  224.     DIBSECTION  DibSection;         // Details of DIB section allocated
  225.     HBITMAP     hBitmap;            // Handle to bitmap for drawing
  226.     HANDLE      hMapping;           // Handle to shared memory block
  227.     BYTE        *pBase;             // Pointer to base memory address
  228.  
  229. } DIBDATA;
  230.  
  231.  
  232. // This class inherits from CMediaSample and uses all of it's methods but it
  233. // overrides the constructor to initialise itself with the DIBDATA structure
  234. // When we come to render an IMediaSample we will know if we are using our own
  235. // allocator, and if we are, we can cast the IMediaSample to a pointer to one
  236. // of these are retrieve the DIB section information and hence the HBITMAP
  237.  
  238. class CImageSample : public CMediaSample
  239. {
  240. protected:
  241.  
  242.     DIBDATA m_DibData;      // Information about the DIBSECTION
  243.     BOOL m_bInit;           // Is the DIB information setup
  244.  
  245. public:
  246.  
  247.     // Constructor
  248.  
  249.     CImageSample(CBaseAllocator *pAllocator,
  250.                  TCHAR *pName,
  251.                  HRESULT *phr,
  252.                  LPBYTE pBuffer,
  253.                  LONG length);
  254.  
  255.     // Maintain the DIB/DirectDraw state
  256.  
  257.     void SetDIBData(DIBDATA *pDibData);
  258.     DIBDATA *GetDIBData();
  259. };
  260.  
  261.  
  262. // This is an allocator based on the abstract CBaseAllocator base class that
  263. // allocates sample buffers in shared memory. The number and size of these
  264. // are determined when the output pin calls Prepare on us. The shared memory
  265. // blocks are used in subsequent calls to GDI CreateDIBSection, once that
  266. // has been done the output pin can fill the buffers with data which will
  267. // then be handed to GDI through BitBlt calls and thereby remove one copy
  268.  
  269. class CImageAllocator : public CBaseAllocator
  270. {
  271. protected:
  272.  
  273.     CBaseFilter *m_pFilter;   // Delegate reference counts to
  274.     CMediaType *m_pMediaType;           // Pointer to the current format
  275.  
  276.     // Used to create and delete samples
  277.  
  278.     HRESULT Alloc();
  279.     void Free();
  280.  
  281.     // Manage the shared DIBSECTION and DCI/DirectDraw buffers
  282.  
  283.     HRESULT CreateDIB(LONG InSize,DIBDATA &DibData);
  284.     STDMETHODIMP CheckSizes(ALLOCATOR_PROPERTIES *pRequest);
  285.     virtual CImageSample *CreateImageSample(LPBYTE pData,LONG Length);
  286.  
  287. public:
  288.  
  289.     // Constructor and destructor
  290.  
  291.     CImageAllocator(CBaseFilter *pFilter,TCHAR *pName,HRESULT *phr);
  292. #ifdef DEBUG
  293.     ~CImageAllocator();
  294. #endif
  295.  
  296.     STDMETHODIMP_(ULONG) NonDelegatingAddRef();
  297.     STDMETHODIMP_(ULONG) NonDelegatingRelease();
  298.     void NotifyMediaType(CMediaType *pMediaType);
  299.  
  300.     // Agree the number of buffers to be used and their size
  301.  
  302.     STDMETHODIMP SetProperties(
  303.         ALLOCATOR_PROPERTIES *pRequest,
  304.         ALLOCATOR_PROPERTIES *pActual);
  305. };
  306.  
  307.  
  308. // This class is a fairly specialised helper class for image renderers that
  309. // have to create and manage palettes. The CBaseWindow class looks after
  310. // realising palettes once they have been installed. This class can be used
  311. // to create the palette handles from a media format (which must contain a
  312. // VIDEOINFO structure in the format block). We try to make the palette an
  313. // identity palette to maximise performance and also only change palettes
  314. // if actually required to (we compare palette colours before updating).
  315. // All the methods are virtual so that they can be overriden if so required
  316.  
  317. class CImagePalette
  318. {
  319. protected:
  320.  
  321.     CBaseWindow *m_pBaseWindow;             // Window to realise palette in
  322.     CBaseFilter *m_pFilter;       // Media filter to send events
  323.     CDrawImage *m_pDrawImage;               // Object who will be drawing
  324.     HPALETTE m_hPalette;                    // The palette handle we own
  325.  
  326. public:
  327.  
  328.     CImagePalette(CBaseFilter *pBaseFilter,
  329.                   CBaseWindow *pBaseWindow,
  330.                   CDrawImage *pDrawImage);
  331.  
  332. #ifdef DEBUG
  333.     virtual ~CImagePalette();
  334. #endif
  335.  
  336.     static HPALETTE MakePalette(const VIDEOINFOHEADER *pVideoInfo, LPSTR szDevice);
  337.     HRESULT RemovePalette();
  338.     static HRESULT MakeIdentityPalette(PALETTEENTRY *pEntry,INT iColours, LPSTR szDevice);
  339.     HRESULT CopyPalette(const CMediaType *pSrc,CMediaType *pDest);
  340.     BOOL ShouldUpdate(const VIDEOINFOHEADER *pNewInfo,const VIDEOINFOHEADER *pOldInfo);
  341.     HRESULT PreparePalette(const CMediaType *pmtNew,const CMediaType *pmtOld,LPSTR szDevice);
  342.  
  343.     BOOL DrawVideoImageHere(HDC hdc, IMediaSample *pMediaSample, LPRECT lprcSrc, LPRECT lprcDst)
  344.     {
  345.         return m_pDrawImage->DrawVideoImageHere(hdc, pMediaSample, lprcSrc,lprcDst);
  346.     }
  347. };
  348.  
  349.  
  350. // Another helper class really for video based renderers. Most such renderers
  351. // need to know what the display format is to some degree or another. This
  352. // class initialises itself with the display format. The format can be asked
  353. // for through GetDisplayFormat and various other accessor functions. If a
  354. // filter detects a display format change (perhaps it gets a WM_DEVMODECHANGE
  355. // message then it can call RefreshDisplayType to reset that format). Also
  356. // many video renderers will want to check formats as they are proposed by
  357. // source filters. This class provides methods to check formats and only
  358. // accept those video formats that can be efficiently drawn using GDI calls
  359.  
  360. class CImageDisplay : public CCritSec
  361. {
  362. protected:
  363.  
  364.     // This holds the display format; biSize should not be too big, so we can
  365.     // safely use the VIDEOINFO structure
  366.     VIDEOINFO m_Display;
  367.  
  368.     static DWORD CountSetBits(const DWORD Field);
  369.     static DWORD CountPrefixBits(const DWORD Field);
  370.     static BOOL CheckBitFields(const VIDEOINFO *pInput);
  371.  
  372. public:
  373.  
  374.     // Constructor and destructor
  375.  
  376.     CImageDisplay();
  377.  
  378.     // Used to manage BITMAPINFOHEADERs and the display format
  379.  
  380.     const VIDEOINFO *GetDisplayFormat();
  381.     HRESULT RefreshDisplayType(LPSTR szDeviceName);
  382.     static BOOL CheckHeaderValidity(const VIDEOINFO *pInput);
  383.     static BOOL CheckPaletteHeader(const VIDEOINFO *pInput);
  384.     BOOL IsPalettised();
  385.     WORD GetDisplayDepth();
  386.  
  387.     // Provide simple video format type checking
  388.  
  389.     HRESULT CheckMediaType(const CMediaType *pmtIn);
  390.     HRESULT CheckVideoType(const VIDEOINFO *pInput);
  391.     HRESULT UpdateFormat(VIDEOINFO *pVideoInfo);
  392.     const DWORD *GetBitMasks(const VIDEOINFO *pVideoInfo);
  393.  
  394.     BOOL GetColourMask(DWORD *pMaskRed,
  395.                        DWORD *pMaskGreen,
  396.                        DWORD *pMaskBlue);
  397. };
  398.  
  399. //  Convert a FORMAT_VideoInfo to FORMAT_VideoInfo2
  400. STDAPI ConvertVideoInfoToVideoInfo2(AM_MEDIA_TYPE *pmt);
  401.  
  402. #endif // __WINUTIL__
  403.  
  404.