home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / winutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  14.7 KB  |  375 lines

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