source: trunk/Utilities/StressEvr/MainDialog.h @ 56

Last change on this file since 56 was 56, checked in by roman, 11 years ago
  • Property svn:keywords set to Id
File size: 23.9 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: MainDialog.h 56 2012-03-03 14:43:35Z roman $
6
7#pragma once
8
9#include <dxgi.h>
10#include "AboutDialog.h"
11#include <dshow.h>
12#include <d3d9.h>
13#include <vmr9.h>
14#include <evr.h>
15#include <evr9.h>
16#include "rodshow.h"
17
18#pragma comment(lib, "dxgi.lib")
19
20////////////////////////////////////////////////////////////
21// CEvrWindow
22
23class CEvrWindow :
24        public CControlWindowT<CEvrWindow>
25{
26public:
27
28BEGIN_MSG_MAP_EX(CEvrWindow)
29        //CHAIN_MSG_MAP(CControlWindowT<CEvrWindow>)
30        MSG_WM_ERASEBKGND(OnEraseBkgnd)
31        MSG_WM_PAINT(OnPaint)
32        MSG_WM_SIZE(OnSize)
33        MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
34END_MSG_MAP()
35
36public:
37        CComPtr<IBaseFilter> m_pBaseFilter;
38        CComPtr<IMFVideoDisplayControl> m_pMfVideoDisplayControl;
39
40public:
41// CEvrWindow
42        static CLSID GetRendererClassIdentifier() throw()
43        {
44                return CLSID_EnhancedVideoRenderer;
45        }
46        VOID Initialize(IBaseFilter* pBaseFilter)
47        {
48                _A(pBaseFilter);
49                _A(!m_pBaseFilter && !m_pMfVideoDisplayControl);
50                m_pBaseFilter = pBaseFilter;
51                CComQIPtr<IMFGetService> pMfGetInterface = pBaseFilter;
52                CComPtr<IMFVideoDisplayControl> pMfVideoDisplayControl;
53                __C(pMfGetInterface->GetService(MR_VIDEO_RENDER_SERVICE, __uuidof(IMFVideoDisplayControl), (VOID**) &pMfVideoDisplayControl));
54                _A(pMfVideoDisplayControl);
55                m_pMfVideoDisplayControl = pMfVideoDisplayControl;
56                __C(pMfVideoDisplayControl->SetVideoWindow(m_hWnd));
57                CRect VideoPosition = GetVideoPosition();
58                _Z4(atlTraceGeneral, 4, _T("pMfVideoDisplayControl 0x%p, VideoPosition at (%d, %d) size (%d, %d)\n"), pMfVideoDisplayControl, VideoPosition.left, VideoPosition.top, VideoPosition.Width(), VideoPosition.Height());
59                __C(pMfVideoDisplayControl->SetVideoPosition(NULL, VideoPosition));
60                //__C(pMfVideoDisplayControl->SetAspectRatioMode(MFVideoARMode_None)); // As opposed to default MFVideoARMode_Picture - disable letterboxing
61        }
62        VOID Terminate() throw()
63        {
64                m_pBaseFilter = NULL;
65                m_pMfVideoDisplayControl = NULL;
66        }
67        CRect GetVideoPosition() const throw()
68        {
69                CRect Position;
70                _W(GetClientRect(Position));
71                return Position;
72        }
73
74// Window Message Handlers
75        LRESULT OnEraseBkgnd(CDCHandle Dc)
76        {
77                Dc;
78                if(m_pMfVideoDisplayControl)
79                {
80                        return TRUE;
81                } else
82                        SetMsgHandled(FALSE);
83                return 0;
84        }
85        LRESULT OnPaint(CDCHandle)
86        {
87                if(m_pMfVideoDisplayControl)
88                {
89                        CPaintDC Dc(m_hWnd);
90                        const HRESULT nRepaintVideoResult = m_pMfVideoDisplayControl->RepaintVideo();
91                        _Z4(atlTraceUI, SUCCEEDED(nRepaintVideoResult) ? 6 : 4, _T("nRepaintVideoResult 0x%08x\n"), nRepaintVideoResult);
92                } else
93                        SetMsgHandled(FALSE);
94                return 0;
95        }
96        LRESULT OnSize(UINT nType, CSize)
97        {
98                if(nType != SIZE_MINIMIZED)
99                        if(m_pMfVideoDisplayControl)
100                        {
101                                CRect VideoPosition = GetVideoPosition();
102                                const HRESULT nSetVideoPositionResult = m_pMfVideoDisplayControl->SetVideoPosition(NULL, &VideoPosition);
103                                _Z4(atlTraceUI, SUCCEEDED(nSetVideoPositionResult) ? 6 : 4, _T("nSetVideoPositionResult 0x%08x\n"), nSetVideoPositionResult);
104                        }
105                return 0;
106        }
107        LRESULT OnLButtonDblClk(UINT, CPoint Position)
108        {
109                COlePropertyFrameDialog Dialog;
110                Dialog.SetObject(m_pBaseFilter);
111                Dialog.SetObjectPages();
112                Dialog.DoModal(m_hWnd);
113                return 0;
114        }
115};
116
117////////////////////////////////////////////////////////////
118// CVideoDialog
119
120class CVideoDialog : 
121        public CDialogImpl<CVideoDialog>,
122        public CDialogResize<CVideoDialog>
123{
124public:
125        enum { IDD = IDD_VIDEO };
126
127BEGIN_MSG_MAP_EX(CVideoDialog)
128        //CHAIN_MSG_MAP(CDialogImpl<CVideoDialog>)
129        CHAIN_MSG_MAP(CDialogResize<CVideoDialog>)
130        MSG_WM_INITDIALOG(OnInitDialog)
131        MSG_WM_DESTROY(OnDestroy)
132        COMMAND_ID_HANDLER_EX(IDCANCEL, OnCommand)
133        COMMAND_ID_HANDLER_EX(IDC_VIDEO_RUN, OnRunButtonClicked)
134        COMMAND_ID_HANDLER_EX(IDC_VIDEO_PAUSE, OnPauseButtonClicked)
135        COMMAND_ID_HANDLER_EX(IDC_VIDEO_STOP, OnStopButtonClicked)
136        MESSAGE_HANDLER_EX(WM_FILTERGRAPHEVENT, OnFilterGraphEvent)
137END_MSG_MAP()
138
139BEGIN_DLGRESIZE_MAP(CVideoDialog)
140        DLGRESIZE_CONTROL(IDC_VIDEO_AREA, DLSZ_SIZE_X | DLSZ_SIZE_Y)
141        DLGRESIZE_CONTROL(IDC_VIDEO_RUN, DLSZ_MOVE_Y)
142        DLGRESIZE_CONTROL(IDC_VIDEO_PAUSE, DLSZ_MOVE_Y)
143        DLGRESIZE_CONTROL(IDC_VIDEO_STOP, DLSZ_MOVE_Y)
144        DLGRESIZE_CONTROL(IDCANCEL, DLSZ_MOVE_X | DLSZ_MOVE_Y)
145END_DLGRESIZE_MAP()
146
147public:
148
149        ////////////////////////////////////////////////////////
150        // Window Message Identifiers
151
152        enum
153        {
154                WM_FIRST = WM_APP,
155                WM_FILTERGRAPHEVENT,
156        };
157
158        ////////////////////////////////////////////////////////
159        // CSourceFilter
160
161        class ATL_NO_VTABLE CSourceFilter :
162                public CComObjectRootEx<CComMultiThreadModelNoCS>,
163                public CComCoClass<CSourceFilter>,
164                public CPushSourceFilterT<CSourceFilter>,
165                public CBasePersistT<CSourceFilter>,
166                public CAmFilterMiscFlagsT<CSourceFilter, AM_FILTER_MISC_FLAGS_IS_SOURCE>
167        {
168        public:
169
170        DECLARE_NO_REGISTRY()
171
172        DECLARE_PROTECT_FINAL_CONSTRUCT()
173
174        //DECLARE_QI_TRACE(CSourceFilter)
175
176        BEGIN_COM_MAP(CSourceFilter)
177                COM_INTERFACE_ENTRY(IBaseFilter)
178                COM_INTERFACE_ENTRY(IMediaFilter)
179                COM_INTERFACE_ENTRY_IID(__uuidof(IPersist), IBaseFilter)
180                COM_INTERFACE_ENTRY(IAMFilterMiscFlags)
181        END_COM_MAP()
182
183        public:
184
185                ////////////////////////////////////////////////////////
186                // COutputPin
187
188                class ATL_NO_VTABLE COutputPin :
189                        public CComObjectRootEx<CComMultiThreadModelNoCS>,
190                        public CPushSourceFilterT<CSourceFilter>::COutputPinT<COutputPin, CSourceFilter, CThreadContext>,
191                        public CAmPushSourceT<COutputPin, 0>
192                {
193                public:
194
195                //DECLARE_QI_TRACE(CSourceFilter::COutputPin)
196
197                BEGIN_COM_MAP(COutputPin)
198                        COM_INTERFACE_ENTRY(IPin)
199                        COM_INTERFACE_ENTRY(IAMPushSource)
200                        COM_INTERFACE_ENTRY(IAMLatency)
201                END_COM_MAP()
202
203                public:
204                // COutputPin
205                        COutputPin() throw()
206                        {
207                                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
208                        }
209                        ~COutputPin() throw()
210                        {
211                                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
212                        }
213                        VOID EnumerateMediaTypes(CAtlList<CMediaType>& MediaTypeList)
214                        {
215                                //CRoCriticalSectionLock DataLock(GetDataCriticalSection());
216                                _W(MediaTypeList.AddTail(GetFilter()->m_pRequestedMediaType));
217                        }
218                        BOOL CheckMediaType(const CMediaType& pMediaType) const throw()
219                        {
220                                _A(pMediaType);
221                                //CRoCriticalSectionLock DataLock(GetDataCriticalSection());
222                                _A(GetFilter()->m_pRequestedMediaType);
223                                return CVideoInfoHeader::Compare(&GetFilter()->m_pRequestedMediaType.GetCompatibleVideoInfoHeader(), &pMediaType.GetCompatibleVideoInfoHeader(), FALSE);
224                        }
225                        BOOL DecideMemAllocatorProperties(IMemAllocator* pMemAllocator, ALLOCATOR_PROPERTIES Properties) throw()
226                        {
227                                static const SIZE_T g_nMiminalBufferCount = 8;
228                                Properties.cBuffers = max(Properties.cBuffers, (LONG) g_nMiminalBufferCount);
229                                CRoCriticalSectionLock DataLock(GetDataCriticalSection());
230                                const CMediaType& pMediaType = GetMediaTypeReference();
231                                const CVideoInfoHeader VideoInfoHeader = pMediaType.GetCompatibleVideoInfoHeader();
232                                const CSize Extent = VideoInfoHeader.GetExtent();
233                                SIZE_T nBufferSize;
234                                if(Extent.cx >= 1080)
235                                {
236                                        // NOTE: At higher resolutions we are good to go with much smaller buffers
237                                        static const UINT g_nPixelBitCount = 7;
238                                        nBufferSize = Extent.cy * Extent.cx * g_nPixelBitCount / 8;
239                                } else
240                                {
241                                        static const UINT g_nPixelBitCount = 14;
242                                        nBufferSize = Extent.cy * Extent.cx * g_nPixelBitCount / 8;
243                                }
244                                return SetMemAllocatorBufferSize(pMemAllocator, Properties, nBufferSize);
245                        }
246                        BOOL ComposeMediaSample(CThreadContext& ThreadContext, IMediaSample* pMediaSample)
247                        {
248                                CMediaSampleProperties OutputProperties(pMediaSample);
249                                #pragma region Dynamic Type Change
250                                if(OutputProperties.dwSampleFlags & AM_SAMPLE_TYPECHANGED)
251                                {
252                                        _A(OutputProperties.pMediaType);
253                                        _A(CheckMediaType(OutputProperties.pMediaType));
254                                        SetMediaType(OutputProperties.pMediaType);
255                                } else
256                                        _A(!OutputProperties.pMediaType);
257                                #pragma endregion
258                                OutputProperties.dwTypeSpecificFlags = 0;
259                                OutputProperties.dwSampleFlags = 0; // AM_SAMPLE_DATADISCONTINUITY
260                                OutputProperties.lActual = GetMediaType().GetCompatibleVideoInfoHeader().GetDataSize();
261                                OutputProperties.dwStreamId = 0;
262                                FillMemory(OutputProperties.pbBuffer, OutputProperties.lActual, rand() & 0xFF);
263                                OutputProperties.Set();
264                                return TRUE;
265                        }
266                };
267
268        private:
269                CObjectPtr<COutputPin> m_pOutputPin;
270                CMediaType m_pRequestedMediaType;
271
272        public:
273        // CSourceFilter
274                CSourceFilter() throw() :
275                        CBasePersistT<CSourceFilter>(GetDataCriticalSection())
276                {
277                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
278                }
279                ~CSourceFilter() throw()
280                {
281                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
282                }
283                HRESULT FinalConstruct() throw()
284                {
285                        _ATLTRY
286                        {
287                                m_pOutputPin.Construct()->Initialize(this, L"Output");
288                                AddPin(m_pOutputPin);
289                        }
290                        _ATLCATCH(Exception)
291                        {
292                                _C(Exception);
293                        }
294                        return S_OK;
295                }
296                VOID FinalRelease() throw()
297                {
298                        m_pOutputPin = NULL;
299                }
300                VOID DeliverBeginFlush(IPin*)
301                {
302                        m_pOutputPin->DeliverBeginFlush();
303                }
304                VOID DeliverEndFlush(IPin*)
305                {
306                        m_pOutputPin->DeliverEndFlush();
307                }
308                VOID DeliverNewSegment(IPin*, REFERENCE_TIME nStartTime, REFERENCE_TIME nStopTime, DOUBLE fRate)
309                {
310                        m_pOutputPin->DeliverNewSegment(nStartTime, nStopTime, fRate);
311                }
312                static BOOL CanCue() throw()
313                {
314                        return FALSE;
315                }
316                VOID CueFilter()
317                {
318                        m_pOutputPin->CuePin();
319                }
320                VOID RunFilter(REFERENCE_TIME nStartTime)
321                {
322                        m_pOutputPin->RunPin(nStartTime);
323                }
324                VOID PauseFilter() throw()
325                {
326                        m_pOutputPin->PausePin();
327                }
328                VOID StopFilter() throw()
329                {
330                        m_pOutputPin->StopPin();
331                }
332                const CObjectPtr<COutputPin>& GetOutputPin() const throw()
333                {
334                        return m_pOutputPin;
335                }
336                VOID Initialize(CMediaType& pMediaType)
337                {
338                        _A(!m_pRequestedMediaType && pMediaType);
339                        m_pRequestedMediaType = pMediaType;
340                }
341        };
342
343private:
344        CMediaType m_pMediaType;
345        SIZE_T m_nLayout;
346        CStatic m_AreaStatic;
347        CGenericFilterGraph m_FilterGraph;
348        CRoArrayT<CEvrWindow> m_EvrWindowArray;
349        CButton m_RunButton;
350        CButton m_PauseButton;
351        CButton m_StopButton;
352
353        CRect GetVideoPosition(const CRect& Position, SIZE_T nIndex)
354        {
355                CRect VideoPosition;
356                VideoPosition.SetRectEmpty();
357                switch(m_nLayout)
358                {
359                case 0: // 1
360                        if(nIndex < 1)
361                                VideoPosition = Position;
362                        break;
363                case 1: // 4
364                case 2: // 9
365                case 3: // 16
366                case 4: // 25
367                case 5: // 36
368                        if(nIndex < (m_nLayout + 1) * (m_nLayout + 1))
369                        {
370                                const SIZE_T nZ = m_nLayout + 1;
371                                VideoPosition.left = (LONG) (Position.left + (Position.Width() - (nZ - 1) * Position.left) * (nIndex % nZ) / nZ + Position.left * (nIndex % nZ));
372                                VideoPosition.top = (LONG) (Position.top + (Position.Height() - (nZ - 1) * Position.top) * (nIndex / nZ) / nZ + Position.top * (nIndex / nZ));
373                                VideoPosition.right = (LONG) (VideoPosition.left + (Position.Width() - (nZ - 1) * Position.left) / nZ);
374                                VideoPosition.bottom = (LONG) (VideoPosition.top + (Position.Height() - (nZ - 1) * Position.top) / nZ);
375                        }
376                        break;
377                //default:
378                        //VideoPosition.SetRectEmpty();
379                }
380                _A(!VideoPosition.IsRectEmpty());
381                return VideoPosition;
382        }
383        CRect GetVideoPosition(SIZE_T nIndex)
384        {
385                CRect Position;
386                _W(m_AreaStatic.GetWindowRect(Position));
387                _W(ScreenToClient(Position));
388                return GetVideoPosition(Position, nIndex);
389        }
390        VOID UpdateControls()
391        {
392                if(m_FilterGraph.m_pMediaControl)
393                {
394                        OAFilterState State;
395                        if(SUCCEEDED(m_FilterGraph.m_pMediaControl->GetState(0, &State)))
396                        {
397                                m_RunButton.EnableWindow(State != State_Running);
398                                m_PauseButton.EnableWindow(State != State_Paused);
399                                m_StopButton.EnableWindow(State != State_Stopped);
400                                return;
401                        }
402                }
403                m_RunButton.EnableWindow(FALSE);
404                m_PauseButton.EnableWindow(FALSE);
405                m_StopButton.EnableWindow(FALSE);
406        }
407
408public:
409// CVideoDialog
410        CVideoDialog(const CMediaType& pMediaType, SIZE_T nLayout) :
411                m_pMediaType(pMediaType),
412                m_nLayout(nLayout)
413        {
414                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
415        }
416        ~CVideoDialog() throw()
417        {
418                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
419        }
420
421// CDialogResize
422        VOID DlgResize_UpdateLayout(INT nWidth, INT nHeight)
423        {
424                __super::DlgResize_UpdateLayout(nWidth, nHeight);
425                CRect Position;
426                _W(m_AreaStatic.GetWindowRect(Position));
427                _W(ScreenToClient(Position));
428                CDeferWindowPos DeferWindowPos;
429                _W(DeferWindowPos.Begin((INT) m_EvrWindowArray.GetCount()));
430                for(SIZE_T nIndex = 0; nIndex < m_EvrWindowArray.GetCount(); nIndex++)
431                        _W(DeferWindowPos.SetWindowPos(m_EvrWindowArray[nIndex], NULL, GetVideoPosition(Position, nIndex), SWP_NOZORDER | SWP_NOACTIVATE));
432                _W(DeferWindowPos.End());
433        }
434
435// Window Message Handelrs
436        LRESULT OnInitDialog(HWND, LPARAM)
437        {
438                m_AreaStatic = GetDlgItem(IDC_VIDEO_AREA);
439                m_AreaStatic.ShowWindow(SW_HIDE);
440                m_RunButton = GetDlgItem(IDC_VIDEO_RUN);
441                m_PauseButton = GetDlgItem(IDC_VIDEO_PAUSE);
442                m_StopButton = GetDlgItem(IDC_VIDEO_STOP);
443                DlgResize_Init(TRUE);
444                UpdateControls();
445                m_FilterGraph.CoCreateInstance();
446                const SIZE_T nCount = (m_nLayout + 1) * (m_nLayout + 1); // 1, 4, 9, 16, ...
447                _A(m_EvrWindowArray.IsEmpty());
448                _W(m_EvrWindowArray.SetCount(0, (INT) nCount));
449                for(SIZE_T nIndex = 0; nIndex < nCount; nIndex++)
450                {
451                        CEvrWindow& EvrWindow = m_EvrWindowArray[m_EvrWindowArray.Add()];
452                        CComPtr<IBaseFilter> pBaseFilter;
453                        __C(pBaseFilter.CoCreateInstance(CEvrWindow::GetRendererClassIdentifier()));
454                        __C(m_FilterGraph->AddFilter(pBaseFilter, CT2CW(AtlFormatString(_T("Renderer %02d"), nIndex + 1))));
455                        EvrWindow.Create(m_hWnd);
456                        EvrWindow.ShowWindow(SW_SHOWNORMAL);
457                        EvrWindow.Initialize(pBaseFilter);
458                        CObjectPtr<CSourceFilter> pSourceFilter;
459                        pSourceFilter.Construct()->Initialize(m_pMediaType);
460                        __C(m_FilterGraph->AddFilter(pSourceFilter, CT2CW(AtlFormatString(_T("Source %02d"), nIndex + 1))));
461                        m_FilterGraph->Connect(pSourceFilter->GetOutputPin(), _FilterGraphHelper::GetFilterPin(EvrWindow.m_pBaseFilter));
462                }
463                __C(m_FilterGraph.m_pMediaEventEx->SetNotifyWindow((OAHWND) m_hWnd, WM_FILTERGRAPHEVENT, (LONG_PTR) this));
464                UpdateControls();
465                CRect Position;
466                _W(GetClientRect(Position));
467                DlgResize_UpdateLayout(Position.Width(), Position.Height());
468                CString sCaption;
469                GetWindowText(sCaption);
470                const CSize Extent = m_pMediaType.GetCompatibleVideoInfoHeader().GetExtent();
471                SetWindowText(AtlFormatString(sCaption, nCount, Extent.cx, Extent.cy));
472                return 0;
473        }
474        LRESULT OnDestroy() throw()
475        {
476                if(m_FilterGraph.m_pMediaControl)
477                        _V(m_FilterGraph.m_pMediaControl->Stop());
478                for(SIZE_T nIndex = 0; nIndex < m_EvrWindowArray.GetCount(); nIndex++)
479                        m_EvrWindowArray[nIndex].Terminate();
480                m_FilterGraph.Release();
481                return 0;
482        }
483        LRESULT OnCommand(UINT, INT nIdentifier, HWND)
484        {
485                DestroyWindow();
486                return 0;
487        }
488        LRESULT OnRunButtonClicked(UINT, INT, HWND)
489        {
490                CWaitCursor WaitCursor;
491                __D(m_FilterGraph.m_pMediaControl, E_NOINTERFACE);
492                __C(m_FilterGraph.m_pMediaControl->Run());
493                UpdateControls();
494                return 0;
495        }
496        LRESULT OnPauseButtonClicked(UINT, INT, HWND)
497        {
498                CWaitCursor WaitCursor;
499                __D(m_FilterGraph.m_pMediaControl, E_NOINTERFACE);
500                __C(m_FilterGraph.m_pMediaControl->Pause());
501                UpdateControls();
502                return 0;
503        }
504        LRESULT OnStopButtonClicked(UINT, INT, HWND)
505        {
506                CWaitCursor WaitCursor;
507                if(m_FilterGraph.m_pMediaControl)
508                        _V(m_FilterGraph.m_pMediaControl->Stop());
509                UpdateControls();
510                return 0;
511        }
512        LRESULT OnFilterGraphEvent(UINT, WPARAM, LPARAM)
513        {
514                if(!m_FilterGraph.m_pMediaEventEx)
515                        return 0;
516                _ATLTRY
517                {
518                        for(; ; )
519                        {
520                                LONG nEventCode;
521                                LONG_PTR nParameter1, nParameter2;
522                                const HRESULT nGetEventResult = m_FilterGraph.m_pMediaEventEx->GetEvent(&nEventCode, &nParameter1, &nParameter2, 0);
523                                if(nGetEventResult == E_ABORT)
524                                        break;
525                                __C(nGetEventResult);
526                                _ATLTRY
527                                {
528                                        switch(nEventCode)
529                                        {
530                                        case EC_ERRORABORT:
531                                                _Z2(atlTraceGeneral, 2, _T("nEventCode EC_ERRORABORT 0x%02X, nParameter1 0x%08x, nParameter2 0x%08x\n"), nEventCode, nParameter1, nParameter2);
532                                                _A(FAILED(nParameter1));
533                                                AtlMessageBoxEx(m_hWnd, (LPCTSTR) AtlFormatString(_T("EC_ERRORABORT Event: %s."), AtlFormatSystemMessage((HRESULT) nParameter1).TrimRight(_T("\t\n\r ."))), IDS_ERROR, MB_ICONERROR | MB_OK);
534                                                break;
535                                        default:
536                                                _Z1(atlTraceGeneral, 1, _T("nEventCode 0x%02X, nParameter1 0x%08x, nParameter2 0x%08x\n"), nEventCode, nParameter1, nParameter2);
537                                        }
538                                }
539                                _ATLCATCHALL()
540                                {
541                                        _V(m_FilterGraph.m_pMediaEventEx->FreeEventParams(nEventCode, nParameter1, nParameter2));
542                                        _ATLRETHROW;
543                                }
544                                _V(m_FilterGraph.m_pMediaEventEx->FreeEventParams(nEventCode, nParameter1, nParameter2));
545                        }
546                }
547                _ATLCATCHALL()
548                {
549                        _Z_EXCEPTION();
550                }
551                return 0;
552        }
553};
554
555////////////////////////////////////////////////////////////
556// CMainDialog
557
558class CMainDialog : 
559        public CDialogImpl<CMainDialog>
560{
561public:
562        enum { IDD = IDD_MAIN };
563
564BEGIN_MSG_MAP_EX(CMainDialog)
565        //CHAIN_MSG_MAP(CDialogImpl<CMainDialog>)
566        MSG_WM_INITDIALOG(OnInitDialog)
567        MSG_WM_DESTROY(OnDestroy)
568        MSG_WM_TIMER(OnTimer)
569        MSG_WM_SYSCOMMAND(OnSysCommand)
570        COMMAND_ID_HANDLER_EX(IDCANCEL, OnCommand)
571        COMMAND_ID_HANDLER_EX(IDC_CREATE, OnCreateButtonClicked)
572        REFLECT_NOTIFICATIONS()
573END_MSG_MAP()
574
575public:
576
577        ////////////////////////////////////////////////////////
578        // Time Identifiers
579
580        enum
581        {
582                TIMER_FIRST = 0,
583                TIMER_UPDATEADAPTERS,
584        };
585
586private:
587        CRoEdit m_DxgiEdit;
588        CComPtr<IDXGIFactory> m_pDxgiFactory;
589        CRect m_DefaultPosition;
590        CRoListT<CAutoPtr<CVideoDialog>> m_VideoDialogList;
591
592public:
593// CMainDialog
594        CMainDialog()
595        {
596        }
597
598// Window Message Handelrs
599        LRESULT OnInitDialog(HWND, LPARAM)
600        {
601                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
602                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
603                CMenuHandle Menu = GetSystemMenu(FALSE);
604                _W(Menu.AppendMenu(MF_SEPARATOR));
605                _W(Menu.AppendMenu(MF_STRING, ID_APP_ABOUT, _T("&About...")));
606                {
607                        CString sCaption;
608                        GetWindowText(sCaption);
609                        #if defined(_WIN64)
610                        sCaption.Append(_T(" (64-bit)"));
611                        #else
612                        if(SafeIsWow64Process())
613                                sCaption.Append(_T(" (32-bit)"));
614                        #endif // defined(_WIN64)
615                        SetWindowText(sCaption);
616                }
617                _W(CenterWindow());
618                CButton(GetDlgItem(IDC_RESOLUTION_19201080)).SetCheck(TRUE);
619                CButton(GetDlgItem(IDC_PIXELFORMAT_NV12)).SetCheck(TRUE);
620                CButton(GetDlgItem(IDC_LAYOUT_9)).SetCheck(TRUE);
621                #pragma region DXGI
622                m_DxgiEdit = GetDlgItem(IDC_DXGI);
623                _ATLTRY
624                {
625                        __C(CreateDXGIFactory(__uuidof(IDXGIFactory), (VOID**) &m_pDxgiFactory));
626                        SetTimer(TIMER_UPDATEADAPTERS, 5 * 1000); // 5 seconds
627                        _W(PostMessage(WM_TIMER, TIMER_UPDATEADAPTERS));
628                }
629                _ATLCATCH(Exception)
630                {
631                        _Z_ATLEXCEPTION(Exception);
632                        m_DxgiEdit.SetValue(AtlFormatString(_T("Failed to initialize with DXGI: %s."), AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r ."))));
633                }
634                #pragma endregion
635                _W(GetWindowRect(m_DefaultPosition));
636                GetDlgItem(IDC_CREATE).SetFocus();
637                return FALSE;
638        }
639        LRESULT OnDestroy() throw()
640        {
641                for(POSITION Position = m_VideoDialogList.GetHeadPosition(); Position; m_VideoDialogList.GetNext(Position))
642                {
643                        CAutoPtr<CVideoDialog>& pVideoDialog = m_VideoDialogList.GetAt(Position);
644                        _A(pVideoDialog);
645                        if(pVideoDialog->IsWindow())
646                                _W(pVideoDialog->DestroyWindow());
647                }
648                m_VideoDialogList.RemoveAll();
649                return 0;
650        }
651        LRESULT OnTimer(UINT_PTR nEvent)
652        {
653                switch(nEvent)
654                {
655                case TIMER_UPDATEADAPTERS:
656                        _ATLTRY
657                        {
658                                _A(m_pDxgiFactory);
659                                CWaitCursor WaitCursor;
660                                CString sText;
661                                for(UINT nAdapter = 0; ; nAdapter++)
662                                {
663                                        CComPtr<IDXGIAdapter> pDxgiAdapter;
664                                        const HRESULT nEnumAdaptersResult = m_pDxgiFactory->EnumAdapters(nAdapter, &pDxgiAdapter);
665                                        _Z4(atlTraceGeneral, 4, _T("nAdapter %d, nEnumAdaptersResult 0x%08x\n"), nAdapter, nEnumAdaptersResult);
666                                        if(nEnumAdaptersResult == DXGI_ERROR_NOT_FOUND)
667                                                break;
668                                        _ATLTRY
669                                        {
670                                                DXGI_ADAPTER_DESC Description;
671                                                __C(pDxgiAdapter->GetDesc(&Description));
672                                                sText.AppendFormat(_T("Adatper %d: %ls, Vendor 0x%x, Device 0x%x, Subsystem 0x%x, Revision 0x%x\n"), nAdapter, Description.Description, Description.VendorId, Description.DeviceId, Description.SubSysId, Description.Revision);
673                                                sText.AppendFormat(
674                                                        _T("  ") _T("DedicatedVideoMemory %s") _T("\n")
675                                                        _T("  ") _T("DedicatedSystemMemory %s") _T("\n")
676                                                        _T("  ") _T("SharedSystemMemory %s") _T("\n")
677                                                        _T("  ") _T("AdapterLuid %d.%u") _T("\n")
678                                                        , 
679                                                        _StringHelper::FormatNumber((LONGLONG) Description.DedicatedVideoMemory), 
680                                                        _StringHelper::FormatNumber((LONGLONG) Description.DedicatedSystemMemory), 
681                                                        _StringHelper::FormatNumber((LONGLONG) Description.SharedSystemMemory), 
682                                                        Description.AdapterLuid.HighPart, Description.AdapterLuid.LowPart,
683                                                        0);
684                                        }
685                                        _ATLCATCH(Exception)
686                                        {
687                                                _Z_ATLEXCEPTION(Exception);
688                                                sText.AppendFormat(_T("Adatper %d Failure: %s\n"), nAdapter, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r .")));
689                                        }
690                                }
691                                sText.Replace(_T("\n"), _T("\r\n"));
692                                m_DxgiEdit.SetValue(sText);
693                        }
694                        _ATLCATCH(Exception)
695                        {
696                                _Z_ATLEXCEPTION(Exception);
697                                m_DxgiEdit.SetValue(AtlFormatString(_T("Failed to initialize with DXGI: %s."), AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r ."))));
698                        }
699                        break;
700                default:
701                        SetMsgHandled(FALSE);
702                }
703                return 0;
704        }
705        LRESULT OnSysCommand(UINT nCommand, CPoint)
706        {
707                switch(nCommand)
708                {
709                case ID_APP_ABOUT:
710                        {
711                                CAboutDialog Dialog;
712                                Dialog.DoModal(m_hWnd);
713                        }
714                        break;
715                default:
716                        SetMsgHandled(FALSE);
717                }
718                return 0;
719        }
720        LRESULT OnCommand(UINT, INT nIdentifier, HWND)
721        {
722                _W(EndDialog(nIdentifier));
723                return 0;
724        }
725        LRESULT OnCreateButtonClicked(UINT, INT, HWND)
726        {
727                CWaitCursor WaitCursor;
728                #pragma region Query Controls
729                SIZE_T nResolution;
730                for(INT nIdentifier = IDC_RESOLUTION_720576; nIdentifier <= IDC_RESOLUTION_19201080; nIdentifier++)
731                        if(IsDlgButtonChecked(nIdentifier))
732                        {
733                                nResolution = nIdentifier - IDC_RESOLUTION_720576;
734                                break;
735                        }
736                SIZE_T nPixelFormat;
737                for(INT nIdentifier = IDC_PIXELFORMAT_NV12; nIdentifier <= IDC_PIXELFORMAT_RGB32; nIdentifier++)
738                        if(IsDlgButtonChecked(nIdentifier))
739                        {
740                                nPixelFormat = nIdentifier - IDC_PIXELFORMAT_NV12;
741                                break;
742                        }
743                SIZE_T nLayout;
744                for(INT nIdentifier = IDC_LAYOUT_1; nIdentifier <= IDC_LAYOUT_36; nIdentifier++)
745                        if(IsDlgButtonChecked(nIdentifier))
746                        {
747                                nLayout = nIdentifier - IDC_LAYOUT_1;
748                                break;
749                        }
750                #pragma endregion
751                static const SIZE g_pExtents[] = { { 720, 576 }, { 1280, 720 }, { 1920, 1080} };
752                static const DWORD g_pnCompressions[] = { MAKEFOURCC('N', 'V', '1', '2'), MAKEFOURCC('Y', 'U', 'Y', '2'), BI_RGB, BI_RGB };
753                static const WORD g_pnBitCounts[] = { 12, 16, 24, 32 };
754                CMediaType pMediaType;
755                pMediaType.AllocateVideoInfo(g_pExtents[nResolution], g_pnBitCounts[nPixelFormat], g_pnCompressions[nPixelFormat]);
756                CVideoDialog* pVideoDialog = new CVideoDialog(pMediaType, nLayout);
757                _ATLTRY
758                {
759                        pVideoDialog->Create(m_hWnd);
760                        _ATLTRY
761                        {
762                                CRect Position;
763                                GetWindowRect(Position);
764                                if(Position == m_DefaultPosition && m_VideoDialogList.IsEmpty())
765                                {
766                                        Position.OffsetRect(-Position.Width() / 3, 0);
767                                        _W(MoveWindow(Position));
768                                }
769                                Position.left += Position.Width();
770                                const LONG nOffset = GetSystemMetrics(SM_CYCAPTION);
771                                Position.OffsetRect(nOffset * (INT) m_VideoDialogList.GetCount(), nOffset * (INT) m_VideoDialogList.GetCount());
772                                _W(pVideoDialog->MoveWindow(Position));
773                                pVideoDialog->ShowWindow(SW_SHOWNORMAL);
774                                m_VideoDialogList.GetAt(m_VideoDialogList.AddTail()).Attach(pVideoDialog);
775                        }
776                        _ATLCATCHALL()
777                        {
778                                _W(pVideoDialog->DestroyWindow());
779                                _ATLRETHROW;
780                        }
781                }
782                _ATLCATCHALL()
783                {
784                        delete pVideoDialog;
785                        _ATLRETHROW;
786                }
787                return 0;
788        }
789};
Note: See TracBrowser for help on using the repository browser.