├── Sample ├── stdafx.cpp ├── targetver.h ├── stdafx.h ├── DXGICaptureSample.vcxproj.filters ├── DXGICaptureSample.cpp └── DXGICaptureSample.vcxproj ├── README.md ├── .gitattributes ├── OpenCVTest ├── OpenCVTest.vcxproj.filters ├── OpenCVTest.cpp └── OpenCVTest.vcxproj ├── DXGICapture ├── DXGICapture.vcxproj.filters ├── DXGIManager.h ├── DXGICapture.vcxproj └── DXGIManager.cpp ├── LICENSE ├── DXGICaptureSample.sln └── .gitignore /Sample/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // DXGICaptureSample.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Sample/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DXGICapture 2 | DXGICapture is a library version of DXGICaptureSample. 3 | 4 | More info in my blog post: (In Chinese) 5 | 6 | https://kheresy.wordpress.com/2015/10/01/screen-capture-in-windows/ 7 | 8 | ----- 9 | 10 | # DXGICaptureSample 11 | DXGICaptureSample shows how to enumerate DXGI outputs and perform fast, non-GDI screen capturing. 12 | 13 | More info in my blog post: 14 | 15 | http://ps.pavelgurenko.com/2013/12/dxgi-outputs-enumeration-and-fast.html 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /Sample/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | #define LOG_FILENAME "logfile.log" 14 | #define BUF_SIZE 512 15 | #define __L_INFO(format, ...) { FILE* fp = NULL; fopen_s(&fp, LOG_FILENAME, "a+"); if(fp) { char buf[BUF_SIZE]; _snprintf_s(buf, BUF_SIZE, _TRUNCATE, format, __VA_ARGS__); fprintf(fp, buf); fprintf(fp, "\n"); fclose(fp); }} 16 | #define __L_ERROR(format, ...) { FILE* fp = NULL; fopen_s(&fp, LOG_FILENAME, "a+"); if(fp) { char buf[BUF_SIZE]; _snprintf_s(buf, BUF_SIZE, _TRUNCATE, format, __VA_ARGS__); fprintf(fp, buf); fprintf(fp, "\n"); fclose(fp); }} 17 | #define __L_FUNC __L(__FUNCTION__) 18 | #define __L_MTHD __L(__FUNCTION__" [%p]", (void*) this) -------------------------------------------------------------------------------- /OpenCVTest/OpenCVTest.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 原始程式檔 20 | 21 | 22 | -------------------------------------------------------------------------------- /OpenCVTest/OpenCVTest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "DXGIManager.h" 10 | 11 | int main(int argc, char** argv) 12 | { 13 | CoInitialize(NULL); 14 | 15 | DXGIManager mDXGIManager; 16 | mDXGIManager.SetCaptureSource(CSMonitor1); 17 | 18 | RECT rcDim; 19 | mDXGIManager.GetOutputRect(rcDim); 20 | 21 | DWORD dwWidth = rcDim.right - rcDim.left; 22 | DWORD dwHeight = rcDim.bottom - rcDim.top; 23 | 24 | printf("dwWidth=%d dwHeight=%d\n", dwWidth, dwHeight); 25 | 26 | cv::ocl::setUseOpenCL(true); 27 | cv::Mat mScreen(dwHeight, dwWidth, CV_8UC4); 28 | cv::UMat mShowImg; 29 | 30 | cv::namedWindow("Screen"); 31 | while (true) 32 | { 33 | if (mDXGIManager.GetOutputBits(mScreen.data, rcDim) == S_OK) 34 | { 35 | cv::resize(mScreen, mShowImg, cv::Size(mScreen.cols / 2, mScreen.rows / 2)); 36 | cv::imshow("Screen", mShowImg); 37 | } 38 | if (cv::waitKey(5) == 'q') 39 | { 40 | break; 41 | } 42 | } 43 | 44 | CoUninitialize(); 45 | } 46 | -------------------------------------------------------------------------------- /DXGICapture/DXGICapture.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 原始程式檔 20 | 21 | 22 | 23 | 24 | 標頭檔 25 | 26 | 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Pavel Gurenko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Sample/DXGICaptureSample.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | -------------------------------------------------------------------------------- /DXGICapture/DXGIManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | enum CaptureSource 14 | { 15 | CSUndefined, 16 | CSMonitor1, 17 | CSMonitor2, 18 | CSDesktop 19 | }; 20 | 21 | class DXGIPointerInfo 22 | { 23 | public: 24 | DXGIPointerInfo(BYTE* pPointerShape, UINT uiPointerShapeBufSize, DXGI_OUTDUPL_FRAME_INFO fi, DXGI_OUTDUPL_POINTER_SHAPE_INFO psi); 25 | ~DXGIPointerInfo(); 26 | BYTE* GetBuffer(); 27 | UINT GetBufferSize(); 28 | DXGI_OUTDUPL_FRAME_INFO& GetFrameInfo(); 29 | DXGI_OUTDUPL_POINTER_SHAPE_INFO& GetShapeInfo(); 30 | 31 | private: 32 | BYTE* m_pPointerShape; 33 | UINT m_uiPointerShapeBufSize; 34 | DXGI_OUTDUPL_POINTER_SHAPE_INFO m_PSI; 35 | DXGI_OUTDUPL_FRAME_INFO m_FI; 36 | }; 37 | 38 | class DXGIOutputDuplication 39 | { 40 | public: 41 | DXGIOutputDuplication(IDXGIAdapter1* pAdapter, 42 | ID3D11Device* pD3DDevice, 43 | ID3D11DeviceContext* pD3DDeviceContext, 44 | IDXGIOutput1* pDXGIOutput1, 45 | IDXGIOutputDuplication* pDXGIOutputDuplication); 46 | 47 | HRESULT GetDesc(DXGI_OUTPUT_DESC& desc); 48 | HRESULT AcquireNextFrame(IDXGISurface1** pD3D11Texture2D, DXGIPointerInfo*& pDXGIPointer); 49 | HRESULT ReleaseFrame(); 50 | 51 | bool IsPrimary(); 52 | 53 | private: 54 | CComPtr m_Adapter; 55 | CComPtr m_D3DDevice; 56 | CComPtr m_D3DDeviceContext; 57 | CComPtr m_DXGIOutput1; 58 | CComPtr m_DXGIOutputDuplication; 59 | }; 60 | 61 | class DXGIManager 62 | { 63 | public: 64 | DXGIManager(); 65 | ~DXGIManager(); 66 | HRESULT SetCaptureSource(CaptureSource type); 67 | CaptureSource GetCaptureSource(); 68 | 69 | HRESULT GetOutputRect(RECT& rc); 70 | HRESULT GetOutputBits(BYTE* pBits, RECT& rcDest); 71 | 72 | private: 73 | HRESULT Init(); 74 | int GetMonitorCount(); 75 | vector GetOutputDuplication(); 76 | void DrawMousePointer(BYTE* pDesktopBits, RECT rcDesktop, RECT rcDest); 77 | 78 | private: 79 | CComPtr m_spDXGIFactory1; 80 | vector m_vOutputs; 81 | bool m_bInitialized; 82 | CaptureSource m_CaptureSource; 83 | RECT m_rcCurrentOutput; 84 | BYTE* m_pBuf; 85 | 86 | CComPtr m_spWICFactory; 87 | ULONG_PTR m_gdiplusToken; 88 | DXGIPointerInfo* m_pDXGIPointer; 89 | }; 90 | -------------------------------------------------------------------------------- /Sample/DXGICaptureSample.cpp: -------------------------------------------------------------------------------- 1 | // DXGICaptureSample.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include "stdafx.h" 5 | #include "DXGIManager.h" 6 | 7 | DXGIManager g_DXGIManager; 8 | 9 | int _tmain(int argc, _TCHAR* argv[]) 10 | { 11 | printf("DXGICaptureSample. Fast windows screen capture\n"); 12 | printf("Capturing desktop to: capture.bmp\n"); 13 | printf("Log: logfile.log\n"); 14 | 15 | CoInitialize(NULL); 16 | 17 | g_DXGIManager.SetCaptureSource(CSDesktop); 18 | 19 | RECT rcDim; 20 | g_DXGIManager.GetOutputRect(rcDim); 21 | 22 | DWORD dwWidth = rcDim.right - rcDim.left; 23 | DWORD dwHeight = rcDim.bottom - rcDim.top; 24 | 25 | printf("dwWidth=%d dwHeight=%d\n", dwWidth, dwHeight); 26 | 27 | DWORD dwBufSize = dwWidth*dwHeight*4; 28 | 29 | BYTE* pBuf = new BYTE[dwBufSize]; 30 | 31 | CComPtr spWICFactory = NULL; 32 | HRESULT hr = spWICFactory.CoCreateInstance(CLSID_WICImagingFactory); 33 | if( FAILED(hr) ) 34 | return hr; 35 | 36 | int i=0; 37 | do 38 | { 39 | hr = g_DXGIManager.GetOutputBits(pBuf, rcDim); 40 | i++; 41 | } 42 | while (hr == DXGI_ERROR_WAIT_TIMEOUT || i < 2); 43 | 44 | if( FAILED(hr) ) 45 | { 46 | printf("GetOutputBits failed with hr=0x%08x\n", hr); 47 | return hr; 48 | } 49 | 50 | printf("Saving capture to file\n"); 51 | 52 | CComPtr spBitmap = NULL; 53 | hr = spWICFactory->CreateBitmapFromMemory(dwWidth, dwHeight, GUID_WICPixelFormat32bppBGRA, dwWidth*4, dwBufSize, (BYTE*)pBuf, &spBitmap); 54 | if( FAILED(hr) ) 55 | return hr; 56 | 57 | CComPtr spStream = NULL; 58 | 59 | hr = spWICFactory->CreateStream(&spStream); 60 | if (SUCCEEDED(hr)) { 61 | hr = spStream->InitializeFromFilename(L"capture.bmp", GENERIC_WRITE); 62 | } 63 | 64 | CComPtr spEncoder = NULL; 65 | if (SUCCEEDED(hr)) { 66 | hr = spWICFactory->CreateEncoder(GUID_ContainerFormatBmp, NULL, &spEncoder); 67 | } 68 | 69 | if (SUCCEEDED(hr)) { 70 | hr = spEncoder->Initialize(spStream,WICBitmapEncoderNoCache); 71 | } 72 | 73 | CComPtr spFrame = NULL; 74 | if (SUCCEEDED(hr)) { 75 | hr = spEncoder->CreateNewFrame(&spFrame, NULL); 76 | } 77 | 78 | if (SUCCEEDED(hr)) { 79 | hr = spFrame->Initialize(NULL); 80 | } 81 | 82 | if (SUCCEEDED(hr)) { 83 | hr = spFrame->SetSize(dwWidth, dwHeight); 84 | } 85 | 86 | WICPixelFormatGUID format; 87 | spBitmap->GetPixelFormat(&format); 88 | 89 | if (SUCCEEDED(hr)) { 90 | hr = spFrame->SetPixelFormat(&format); 91 | } 92 | 93 | if (SUCCEEDED(hr)) { 94 | hr = spFrame->WriteSource(spBitmap, NULL); 95 | } 96 | 97 | if (SUCCEEDED(hr)) { 98 | hr = spFrame->Commit(); 99 | } 100 | 101 | if (SUCCEEDED(hr)) { 102 | hr = spEncoder->Commit(); 103 | } 104 | 105 | delete[] pBuf; 106 | 107 | return 0; 108 | } 109 | 110 | -------------------------------------------------------------------------------- /DXGICaptureSample.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DXGICapture", "DXGICapture\DXGICapture.vcxproj", "{3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DXGICaptureSample", "Sample\DXGICaptureSample.vcxproj", "{7456BCB4-72FD-49AC-93DB-FE372F11DCDA}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenCVTest", "OpenCVTest\OpenCVTest.vcxproj", "{9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Win32 = Debug|Win32 15 | Debug|x64 = Debug|x64 16 | Release|Win32 = Release|Win32 17 | Release|x64 = Release|x64 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Debug|Win32.ActiveCfg = Debug|Win32 21 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Debug|Win32.Build.0 = Debug|Win32 22 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Debug|x64.ActiveCfg = Debug|x64 23 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Debug|x64.Build.0 = Debug|x64 24 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Release|Win32.ActiveCfg = Release|Win32 25 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Release|Win32.Build.0 = Release|Win32 26 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Release|x64.ActiveCfg = Release|x64 27 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF}.Release|x64.Build.0 = Release|x64 28 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Debug|Win32.ActiveCfg = Debug|Win32 29 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Debug|Win32.Build.0 = Debug|Win32 30 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Debug|x64.ActiveCfg = Debug|x64 31 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Debug|x64.Build.0 = Debug|x64 32 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Release|Win32.ActiveCfg = Release|Win32 33 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Release|Win32.Build.0 = Release|Win32 34 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Release|x64.ActiveCfg = Release|x64 35 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA}.Release|x64.Build.0 = Release|x64 36 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Debug|Win32.ActiveCfg = Debug|Win32 37 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Debug|Win32.Build.0 = Debug|Win32 38 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Debug|x64.ActiveCfg = Debug|x64 39 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Debug|x64.Build.0 = Debug|x64 40 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Release|Win32.ActiveCfg = Release|Win32 41 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Release|Win32.Build.0 = Release|Win32 42 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Release|x64.ActiveCfg = Release|x64 43 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1}.Release|x64.Build.0 = Release|x64 44 | EndGlobalSection 45 | GlobalSection(SolutionProperties) = preSolution 46 | HideSolutionNode = FALSE 47 | EndGlobalSection 48 | EndGlobal 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /DXGICapture/DXGICapture.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {3B9EF501-CD0D-462D-AC88-D26B4BB6F7EF} 23 | Win32Proj 24 | DXGICapture 25 | 26 | 27 | 28 | StaticLibrary 29 | true 30 | v120 31 | Unicode 32 | 33 | 34 | StaticLibrary 35 | true 36 | v120 37 | Unicode 38 | 39 | 40 | StaticLibrary 41 | false 42 | v120 43 | true 44 | Unicode 45 | 46 | 47 | StaticLibrary 48 | false 49 | v120 50 | true 51 | Unicode 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | true 71 | 72 | 73 | true 74 | 75 | 76 | false 77 | 78 | 79 | false 80 | 81 | 82 | 83 | 84 | 85 | Level3 86 | Disabled 87 | WIN32;_DEBUG;_WINDOWS;_USRDLL;DXGICAPTURE_EXPORTS;%(PreprocessorDefinitions) 88 | true 89 | 90 | 91 | Windows 92 | true 93 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 94 | 95 | 96 | 97 | 98 | 99 | 100 | Level3 101 | Disabled 102 | WIN32;_DEBUG;_WINDOWS;_USRDLL;DXGICAPTURE_EXPORTS;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Windows 107 | true 108 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 109 | 110 | 111 | 112 | 113 | Level3 114 | 115 | 116 | MaxSpeed 117 | true 118 | true 119 | WIN32;NDEBUG;_WINDOWS;_USRDLL;DXGICAPTURE_EXPORTS;%(PreprocessorDefinitions) 120 | true 121 | 122 | 123 | Windows 124 | true 125 | true 126 | true 127 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 128 | 129 | 130 | 131 | 132 | Level3 133 | 134 | 135 | MaxSpeed 136 | true 137 | true 138 | WIN32;NDEBUG;_WINDOWS;_USRDLL;DXGICAPTURE_EXPORTS;%(PreprocessorDefinitions) 139 | true 140 | 141 | 142 | Windows 143 | true 144 | true 145 | true 146 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /OpenCVTest/OpenCVTest.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {9BB9D604-7E92-4A5D-A879-7752C1D9EFC1} 23 | Win32Proj 24 | OpenCVTest 25 | 26 | 27 | 28 | Application 29 | true 30 | v120 31 | Unicode 32 | 33 | 34 | Application 35 | true 36 | v120 37 | Unicode 38 | 39 | 40 | Application 41 | false 42 | v120 43 | true 44 | Unicode 45 | 46 | 47 | Application 48 | false 49 | v120 50 | true 51 | Unicode 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | true 71 | 72 | 73 | true 74 | 75 | 76 | false 77 | 78 | 79 | false 80 | 81 | 82 | 83 | 84 | 85 | Level3 86 | Disabled 87 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 88 | true 89 | ..\DXGICapture;..\..\OpenCV\opencv3\build\include 90 | 91 | 92 | Console 93 | true 94 | D:\Heresy\OpenCV\opencv3\build\x64\vc14\lib 95 | D3D11.lib;dxgi.lib;gdiplus.lib;opencv_world310d.lib 96 | 97 | 98 | 99 | 100 | 101 | 102 | Level3 103 | Disabled 104 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 105 | true 106 | ..\DXGICapture;..\..\OpenCV\opencv3\build\include 107 | 108 | 109 | Console 110 | true 111 | D:\Heresy\OpenCV\opencv3\build\x64\vc12\lib 112 | D3D11.lib;dxgi.lib;gdiplus.lib;opencv_world310d.lib 113 | 114 | 115 | 116 | 117 | Level3 118 | 119 | 120 | MaxSpeed 121 | true 122 | true 123 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 124 | true 125 | ..\DXGICapture;..\..\OpenCV\opencv3\build\include 126 | 127 | 128 | Console 129 | true 130 | true 131 | true 132 | D:\Heresy\OpenCV\opencv3\build\win32\vc14\lib 133 | D3D11.lib;dxgi.lib;gdiplus.lib;opencv_world310.lib 134 | 135 | 136 | 137 | 138 | Level3 139 | 140 | 141 | MaxSpeed 142 | true 143 | true 144 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 145 | true 146 | ..\DXGICapture;..\..\OpenCV\opencv3\build\include 147 | 148 | 149 | Console 150 | true 151 | true 152 | true 153 | D:\Heresy\OpenCV\opencv3\build\x64\vc14\lib 154 | D3D11.lib;dxgi.lib;gdiplus.lib;opencv_world310.lib 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | {3b9ef501-cd0d-462d-ac88-d26b4bb6f7ef} 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /Sample/DXGICaptureSample.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {7456BCB4-72FD-49AC-93DB-FE372F11DCDA} 23 | Win32Proj 24 | DXGICaptureSample 25 | 26 | 27 | 28 | Application 29 | true 30 | Unicode 31 | v120 32 | 33 | 34 | Application 35 | true 36 | Unicode 37 | v120 38 | 39 | 40 | Application 41 | false 42 | true 43 | Unicode 44 | v120 45 | 46 | 47 | Application 48 | false 49 | true 50 | Unicode 51 | v120 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | true 71 | $(ExecutablePath) 72 | C:\Program Files (x86)\Windows Kits\8.0\Include\um;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;$(VCInstallDir)atlmfc\include;$(VCInstallDir)include;$(FrameworkSDKDir)\include 73 | C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86;$(LibraryPath) 74 | 75 | 76 | true 77 | $(ExecutablePath) 78 | C:\Program Files (x86)\Windows Kits\8.0\Include\um;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;$(VCInstallDir)atlmfc\include;$(VCInstallDir)include;$(FrameworkSDKDir)\include 79 | C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x64;$(LibraryPath) 80 | 81 | 82 | false 83 | C:\Program Files (x86)\Windows Kits\8.0\Include\um;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include; 84 | C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86;$(VCInstallDir)lib;$(VCInstallDir)atlmfc\lib;$(WindowsSdkDir)lib;$(FrameworkSDKDir)\lib 85 | 86 | 87 | false 88 | C:\Program Files (x86)\Windows Kits\8.0\Include\um;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include; 89 | C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x64;$(VCInstallDir)lib;$(VCInstallDir)atlmfc\lib;$(WindowsSdkDir)lib;$(FrameworkSDKDir)\lib 90 | 91 | 92 | 93 | Use 94 | Level3 95 | Disabled 96 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 97 | ..\DXGICapture 98 | 99 | 100 | Console 101 | true 102 | D3D11.lib;dxgi.lib;gdiplus.lib;%(AdditionalDependencies) 103 | 104 | 105 | 106 | 107 | Use 108 | Level3 109 | Disabled 110 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 111 | ..\DXGICapture 112 | 113 | 114 | Console 115 | true 116 | D3D11.lib;dxgi.lib;gdiplus.lib;%(AdditionalDependencies) 117 | 118 | 119 | 120 | 121 | Level3 122 | Use 123 | MaxSpeed 124 | true 125 | true 126 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 127 | ..\DXGICapture 128 | 129 | 130 | Console 131 | true 132 | true 133 | true 134 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 135 | 136 | 137 | 138 | 139 | Level3 140 | Use 141 | MaxSpeed 142 | true 143 | true 144 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 145 | ..\DXGICapture 146 | 147 | 148 | Console 149 | true 150 | true 151 | true 152 | D3D11.lib;dxgi.lib;gdiplus.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | Create 162 | Create 163 | Create 164 | Create 165 | 166 | 167 | 168 | 169 | 170 | {3b9ef501-cd0d-462d-ac88-d26b4bb6f7ef} 171 | 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /DXGICapture/DXGIManager.cpp: -------------------------------------------------------------------------------- 1 | #include "DXGIManager.h" 2 | #include 3 | #include 4 | 5 | using namespace Gdiplus; 6 | 7 | #pragma region Functions of DXGIPointerInfo 8 | DXGIPointerInfo::DXGIPointerInfo(BYTE* pPointerShape, UINT uiPointerShapeBufSize, DXGI_OUTDUPL_FRAME_INFO fi, DXGI_OUTDUPL_POINTER_SHAPE_INFO psi) 9 | : m_pPointerShape(pPointerShape), 10 | m_uiPointerShapeBufSize(uiPointerShapeBufSize), 11 | m_FI(fi), 12 | m_PSI(psi) 13 | { 14 | } 15 | 16 | DXGIPointerInfo::~DXGIPointerInfo() 17 | { 18 | if(m_pPointerShape) 19 | { 20 | delete [] m_pPointerShape; 21 | } 22 | } 23 | 24 | BYTE* DXGIPointerInfo::GetBuffer() 25 | { 26 | return m_pPointerShape; 27 | } 28 | 29 | UINT DXGIPointerInfo::GetBufferSize() 30 | { 31 | return m_uiPointerShapeBufSize; 32 | } 33 | 34 | DXGI_OUTDUPL_FRAME_INFO& DXGIPointerInfo::GetFrameInfo() 35 | { 36 | return m_FI; 37 | } 38 | 39 | DXGI_OUTDUPL_POINTER_SHAPE_INFO& DXGIPointerInfo::GetShapeInfo() 40 | { 41 | return m_PSI; 42 | } 43 | #pragma endregion 44 | 45 | #pragma region Functions of DXGIOutputDuplication 46 | DXGIOutputDuplication::DXGIOutputDuplication(IDXGIAdapter1* pAdapter, 47 | ID3D11Device* pD3DDevice, 48 | ID3D11DeviceContext* pD3DDeviceContext, 49 | IDXGIOutput1* pDXGIOutput1, 50 | IDXGIOutputDuplication* pDXGIOutputDuplication) 51 | : m_Adapter(pAdapter), 52 | m_D3DDevice(pD3DDevice), 53 | m_D3DDeviceContext(pD3DDeviceContext), 54 | m_DXGIOutput1(pDXGIOutput1), 55 | m_DXGIOutputDuplication(pDXGIOutputDuplication) 56 | { 57 | } 58 | 59 | HRESULT DXGIOutputDuplication::GetDesc(DXGI_OUTPUT_DESC& desc) 60 | { 61 | m_DXGIOutput1->GetDesc(&desc); 62 | return S_OK; 63 | } 64 | 65 | HRESULT DXGIOutputDuplication::AcquireNextFrame(IDXGISurface1** pDXGISurface, DXGIPointerInfo*& pDXGIPointer) 66 | { 67 | DXGI_OUTDUPL_FRAME_INFO fi; 68 | CComPtr spDXGIResource; 69 | HRESULT hr = m_DXGIOutputDuplication->AcquireNextFrame(20, &fi, &spDXGIResource); 70 | if(FAILED(hr)) 71 | { 72 | // TODO : Change log method 73 | // __L_INFO("m_DXGIOutputDuplication->AcquireNextFrame failed with hr=0x%08x", hr); 74 | return hr; 75 | } 76 | 77 | CComQIPtr spTextureResource = spDXGIResource; 78 | 79 | D3D11_TEXTURE2D_DESC desc; 80 | spTextureResource->GetDesc(&desc); 81 | 82 | D3D11_TEXTURE2D_DESC texDesc; 83 | ZeroMemory( &texDesc, sizeof(texDesc) ); 84 | texDesc.Width = desc.Width; 85 | texDesc.Height = desc.Height; 86 | texDesc.MipLevels = 1; 87 | texDesc.ArraySize = 1; 88 | texDesc.SampleDesc.Count = 1; 89 | texDesc.SampleDesc.Quality = 0; 90 | texDesc.Usage = D3D11_USAGE_STAGING; 91 | texDesc.Format = desc.Format; 92 | texDesc.BindFlags = 0; 93 | texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; 94 | texDesc.MiscFlags = 0; 95 | 96 | CComPtr spD3D11Texture2D = NULL; 97 | hr = m_D3DDevice->CreateTexture2D(&texDesc, NULL, &spD3D11Texture2D); 98 | if(FAILED(hr)) 99 | return hr; 100 | 101 | m_D3DDeviceContext->CopyResource(spD3D11Texture2D, spTextureResource); 102 | 103 | CComQIPtr spDXGISurface = spD3D11Texture2D; 104 | 105 | *pDXGISurface = spDXGISurface.Detach(); 106 | 107 | // Updating mouse pointer, if visible 108 | if(fi.PointerPosition.Visible) 109 | { 110 | BYTE* pPointerShape = new BYTE[fi.PointerShapeBufferSize]; 111 | 112 | DXGI_OUTDUPL_POINTER_SHAPE_INFO psi = {}; 113 | UINT uiPointerShapeBufSize = fi.PointerShapeBufferSize; 114 | hr = m_DXGIOutputDuplication->GetFramePointerShape(uiPointerShapeBufSize, pPointerShape, &uiPointerShapeBufSize, &psi); 115 | if(hr == DXGI_ERROR_MORE_DATA) 116 | { 117 | pPointerShape = new BYTE[uiPointerShapeBufSize]; 118 | 119 | hr = m_DXGIOutputDuplication->GetFramePointerShape(uiPointerShapeBufSize, pPointerShape, &uiPointerShapeBufSize, &psi); 120 | } 121 | 122 | if(hr == S_OK) 123 | { 124 | // TODO : Change log method 125 | // __L_INFO("PointerPosition Visible=%d x=%d y=%d w=%d h=%d type=%d\n", fi.PointerPosition.Visible, fi.PointerPosition.Position.x, fi.PointerPosition.Position.y, psi.Width, psi.Height, psi.Type); 126 | 127 | if((psi.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME || 128 | psi.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR || 129 | psi.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR) && 130 | psi.Width <= 128 && psi.Height <= 128) 131 | { 132 | // Here we can obtain pointer shape 133 | if(pDXGIPointer) 134 | { 135 | delete pDXGIPointer; 136 | } 137 | 138 | pDXGIPointer = new DXGIPointerInfo(pPointerShape, uiPointerShapeBufSize, fi, psi); 139 | 140 | pPointerShape = NULL; 141 | } 142 | 143 | DXGI_OUTPUT_DESC outDesc; 144 | GetDesc(outDesc); 145 | 146 | if(pDXGIPointer) 147 | { 148 | pDXGIPointer->GetFrameInfo().PointerPosition.Position.x = outDesc.DesktopCoordinates.left + fi.PointerPosition.Position.x; 149 | pDXGIPointer->GetFrameInfo().PointerPosition.Position.y = outDesc.DesktopCoordinates.top + fi.PointerPosition.Position.y; 150 | } 151 | } 152 | 153 | if(pPointerShape) 154 | { 155 | delete [] pPointerShape; 156 | } 157 | } 158 | 159 | return hr; 160 | } 161 | 162 | HRESULT DXGIOutputDuplication::ReleaseFrame() 163 | { 164 | m_DXGIOutputDuplication->ReleaseFrame(); 165 | return S_OK; 166 | } 167 | 168 | bool DXGIOutputDuplication::IsPrimary() 169 | { 170 | DXGI_OUTPUT_DESC outdesc; 171 | m_DXGIOutput1->GetDesc(&outdesc); 172 | 173 | MONITORINFO mi; 174 | mi.cbSize = sizeof(MONITORINFO); 175 | GetMonitorInfo(outdesc.Monitor, &mi); 176 | if(mi.dwFlags & MONITORINFOF_PRIMARY) 177 | { 178 | return true; 179 | } 180 | return false; 181 | } 182 | #pragma endregion 183 | 184 | #pragma region Functions of DXGIManager 185 | DXGIManager::DXGIManager() 186 | { 187 | m_CaptureSource = CSUndefined; 188 | SetRect(&m_rcCurrentOutput, 0, 0, 0, 0); 189 | m_pBuf = NULL; 190 | m_pDXGIPointer = NULL; 191 | m_bInitialized = false; 192 | } 193 | 194 | DXGIManager::~DXGIManager() 195 | { 196 | GdiplusShutdown(m_gdiplusToken); 197 | 198 | if(m_pBuf) 199 | { 200 | delete [] m_pBuf; 201 | m_pBuf = NULL; 202 | } 203 | 204 | if(m_pDXGIPointer) 205 | { 206 | delete m_pDXGIPointer; 207 | m_pDXGIPointer = NULL; 208 | } 209 | } 210 | 211 | HRESULT DXGIManager::SetCaptureSource(CaptureSource cs) 212 | { 213 | m_CaptureSource = cs; 214 | return S_OK; 215 | } 216 | 217 | CaptureSource DXGIManager::GetCaptureSource() 218 | { 219 | return m_CaptureSource; 220 | } 221 | 222 | HRESULT DXGIManager::Init() 223 | { 224 | if(m_bInitialized) 225 | return S_OK; 226 | 227 | GdiplusStartupInput gdiplusStartupInput; 228 | GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 229 | 230 | HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)(&m_spDXGIFactory1) ); 231 | if( FAILED(hr) ) 232 | { 233 | // TODO : Change log method 234 | // __L_ERROR("Failed to CreateDXGIFactory1 hr=%08x", hr); 235 | return hr; 236 | } 237 | 238 | // Getting all adapters 239 | vector> vAdapters; 240 | 241 | CComPtr spAdapter; 242 | for(int i=0; m_spDXGIFactory1->EnumAdapters1(i, &spAdapter) != DXGI_ERROR_NOT_FOUND; i++) 243 | { 244 | vAdapters.push_back(spAdapter); 245 | spAdapter.Release(); 246 | } 247 | 248 | // Iterating over all adapters to get all outputs 249 | for(vector>::iterator AdapterIter = vAdapters.begin(); 250 | AdapterIter != vAdapters.end(); 251 | AdapterIter++) 252 | { 253 | vector> vOutputs; 254 | 255 | CComPtr spDXGIOutput; 256 | for(int i=0; (*AdapterIter)->EnumOutputs(i, &spDXGIOutput) != DXGI_ERROR_NOT_FOUND; i++) 257 | { 258 | DXGI_OUTPUT_DESC outputDesc; 259 | spDXGIOutput->GetDesc(&outputDesc); 260 | 261 | // TODO : Change log method 262 | // __L_INFO("Display output found. DeviceName=%ls AttachedToDesktop=%d Rotation=%d DesktopCoordinates={(%d,%d),(%d,%d)}", 263 | // outputDesc.DeviceName, 264 | // outputDesc.AttachedToDesktop, 265 | // outputDesc.Rotation, 266 | // outputDesc.DesktopCoordinates.left, 267 | // outputDesc.DesktopCoordinates.top, 268 | // outputDesc.DesktopCoordinates.right, 269 | // outputDesc.DesktopCoordinates.bottom); 270 | 271 | if(outputDesc.AttachedToDesktop) 272 | { 273 | vOutputs.push_back(spDXGIOutput); 274 | } 275 | 276 | spDXGIOutput.Release(); 277 | } 278 | 279 | if(vOutputs.size() == 0) 280 | continue; 281 | 282 | // Creating device for each adapter that has the output 283 | CComPtr spD3D11Device; 284 | CComPtr spD3D11DeviceContext; 285 | D3D_FEATURE_LEVEL fl = D3D_FEATURE_LEVEL_9_1; 286 | hr = D3D11CreateDevice((*AdapterIter), D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &spD3D11Device, &fl, &spD3D11DeviceContext); 287 | if( FAILED(hr) ) 288 | { 289 | // TODO : Change log method 290 | // __L_ERROR("Failed to create D3D11CreateDevice hr=%08x", hr); 291 | return hr; 292 | } 293 | 294 | for(std::vector>::iterator OutputIter = vOutputs.begin(); 295 | OutputIter != vOutputs.end(); 296 | OutputIter++) 297 | { 298 | CComQIPtr spDXGIOutput1 = *OutputIter; 299 | CComQIPtr spDXGIDevice = spD3D11Device; 300 | if(!spDXGIOutput1 || !spDXGIDevice) 301 | continue; 302 | 303 | CComPtr spDXGIOutputDuplication; 304 | hr = spDXGIOutput1->DuplicateOutput(spDXGIDevice, &spDXGIOutputDuplication); 305 | if( FAILED(hr) ) 306 | continue; 307 | 308 | m_vOutputs.push_back( 309 | DXGIOutputDuplication((*AdapterIter), 310 | spD3D11Device, 311 | spD3D11DeviceContext, 312 | spDXGIOutput1, 313 | spDXGIOutputDuplication)); 314 | } 315 | } 316 | 317 | hr = m_spWICFactory.CoCreateInstance(CLSID_WICImagingFactory); 318 | if( FAILED(hr) ) 319 | { 320 | // TODO : Change log method 321 | // __L_ERROR("Failed to create WICImagingFactory hr=%08x", hr); 322 | return hr; 323 | } 324 | 325 | m_bInitialized = true; 326 | 327 | return S_OK; 328 | } 329 | 330 | HRESULT DXGIManager::GetOutputRect(RECT& rc) 331 | { 332 | // Nulling rc just in case... 333 | SetRect(&rc, 0, 0, 0, 0); 334 | 335 | HRESULT hr = Init(); 336 | if(hr != S_OK) 337 | return hr; 338 | 339 | vector vOutputs = GetOutputDuplication(); 340 | 341 | RECT rcShare; 342 | SetRect(&rcShare, 0, 0, 0, 0); 343 | 344 | for(vector::iterator iter = vOutputs.begin(); 345 | iter != vOutputs.end(); 346 | iter++) 347 | { 348 | DXGIOutputDuplication& out = *iter; 349 | 350 | DXGI_OUTPUT_DESC outDesc; 351 | out.GetDesc(outDesc); 352 | RECT rcOutCoords = outDesc.DesktopCoordinates; 353 | 354 | UnionRect(&rcShare, &rcShare, &rcOutCoords); 355 | } 356 | 357 | CopyRect(&rc, &rcShare); 358 | 359 | return S_OK; 360 | } 361 | 362 | HRESULT DXGIManager::GetOutputBits(BYTE* pBits, RECT& rcDest) 363 | { 364 | HRESULT hr = S_OK; 365 | 366 | DWORD dwDestWidth = rcDest.right - rcDest.left; 367 | DWORD dwDestHeight = rcDest.bottom - rcDest.top; 368 | 369 | RECT rcOutput; 370 | hr = GetOutputRect(rcOutput); 371 | if( FAILED(hr) ) 372 | return hr; 373 | 374 | DWORD dwOutputWidth = rcOutput.right - rcOutput.left; 375 | DWORD dwOutputHeight = rcOutput.bottom - rcOutput.top; 376 | 377 | BYTE* pBuf = NULL; 378 | if(rcOutput.right > (LONG)dwDestWidth || rcOutput.bottom > (LONG)dwDestHeight) 379 | { 380 | // Output is larger than pBits dimensions 381 | if(!m_pBuf || !EqualRect(&m_rcCurrentOutput, &rcOutput)) 382 | { 383 | DWORD dwBufSize = dwOutputWidth*dwOutputHeight*4; 384 | 385 | if(m_pBuf) 386 | { 387 | delete [] m_pBuf; 388 | m_pBuf = NULL; 389 | } 390 | 391 | m_pBuf = new BYTE[dwBufSize]; 392 | 393 | CopyRect(&m_rcCurrentOutput, &rcOutput); 394 | } 395 | 396 | pBuf = m_pBuf; 397 | } 398 | else 399 | { 400 | // Output is smaller than pBits dimensions 401 | pBuf = pBits; 402 | dwOutputWidth = dwDestWidth; 403 | dwOutputHeight = dwDestHeight; 404 | } 405 | 406 | vector vOutputs = GetOutputDuplication(); 407 | 408 | for(vector::iterator iter = vOutputs.begin(); 409 | iter != vOutputs.end(); 410 | iter++) 411 | { 412 | DXGIOutputDuplication& out = *iter; 413 | 414 | DXGI_OUTPUT_DESC outDesc; 415 | out.GetDesc(outDesc); 416 | RECT rcOutCoords = outDesc.DesktopCoordinates; 417 | 418 | CComPtr spDXGISurface1; 419 | hr = out.AcquireNextFrame(&spDXGISurface1, m_pDXGIPointer); 420 | if( FAILED(hr) ) 421 | break; 422 | 423 | DXGI_MAPPED_RECT map; 424 | spDXGISurface1->Map(&map, DXGI_MAP_READ); 425 | 426 | RECT rcDesktop = outDesc.DesktopCoordinates; 427 | DWORD dwWidth = rcDesktop.right - rcDesktop.left; 428 | DWORD dwHeight = rcDesktop.bottom - rcDesktop.top; 429 | 430 | OffsetRect(&rcDesktop, -rcOutput.left, -rcOutput.top); 431 | 432 | DWORD dwMapPitchPixels = map.Pitch/4; 433 | 434 | switch(outDesc.Rotation) 435 | { 436 | case DXGI_MODE_ROTATION_IDENTITY: 437 | { 438 | // Just copying 439 | DWORD dwStripe = dwWidth*4; 440 | for(unsigned int i=0; iUnmap(); 491 | 492 | out.ReleaseFrame(); 493 | } 494 | 495 | if(FAILED(hr)) 496 | return hr; 497 | 498 | // Now pBits have the desktop. Let's paint mouse pointer! 499 | if(pBuf != pBits) 500 | { 501 | DrawMousePointer(pBuf, rcOutput, rcOutput); 502 | } 503 | else 504 | { 505 | DrawMousePointer(pBuf, rcOutput, rcDest); 506 | } 507 | 508 | // We have the pBuf filled with current desktop/monitor image. 509 | if(pBuf != pBits) 510 | { 511 | // pBuf contains the image that should be resized 512 | CComPtr spBitmap = NULL; 513 | hr = m_spWICFactory->CreateBitmapFromMemory(dwOutputWidth, dwOutputHeight, GUID_WICPixelFormat32bppBGRA, dwOutputWidth*4, dwOutputWidth*dwOutputHeight*4, (BYTE*)pBuf, &spBitmap); 514 | if( FAILED(hr) ) 515 | return hr; 516 | 517 | CComPtr spBitmapScaler = NULL; 518 | hr = m_spWICFactory->CreateBitmapScaler(&spBitmapScaler); 519 | if( FAILED(hr) ) 520 | return hr; 521 | 522 | dwOutputWidth = rcOutput.right - rcOutput.left; 523 | dwOutputHeight = rcOutput.bottom - rcOutput.top; 524 | 525 | double aspect = (double)dwOutputWidth/(double)dwOutputHeight; 526 | 527 | DWORD scaledWidth = dwDestWidth; 528 | DWORD scaledHeight = dwDestHeight; 529 | 530 | if(aspect > 1) 531 | { 532 | scaledWidth = dwDestWidth; 533 | scaledHeight = (DWORD)(dwDestWidth/aspect); 534 | } 535 | else 536 | { 537 | scaledWidth = (DWORD)(aspect*dwDestHeight); 538 | scaledHeight = dwDestHeight; 539 | } 540 | 541 | spBitmapScaler->Initialize( 542 | spBitmap, scaledWidth, scaledHeight, WICBitmapInterpolationModeNearestNeighbor); 543 | 544 | spBitmapScaler->CopyPixels(NULL, scaledWidth*4, dwDestWidth*dwDestHeight*4, pBits); 545 | } 546 | return hr; 547 | } 548 | 549 | void DXGIManager::DrawMousePointer(BYTE* pDesktopBits, RECT rcDesktop, RECT rcDest) 550 | { 551 | if(!m_pDXGIPointer) 552 | return; 553 | 554 | DWORD dwDesktopWidth = rcDesktop.right - rcDesktop.left; 555 | DWORD dwDesktopHeight = rcDesktop.bottom - rcDesktop.top; 556 | 557 | DWORD dwDestWidth = rcDest.right - rcDest.left; 558 | DWORD dwDestHeight = rcDest.bottom - rcDest.top; 559 | 560 | int PtrX = m_pDXGIPointer->GetFrameInfo().PointerPosition.Position.x - rcDesktop.left; 561 | int PtrY = m_pDXGIPointer->GetFrameInfo().PointerPosition.Position.y - rcDesktop.top; 562 | switch(m_pDXGIPointer->GetShapeInfo().Type) 563 | { 564 | case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR: 565 | { 566 | unique_ptr bmpBitmap(new Bitmap(dwDestWidth, dwDestHeight, dwDestWidth*4, PixelFormat32bppARGB, pDesktopBits)); 567 | unique_ptr graphics(Graphics::FromImage(bmpBitmap.get())); 568 | unique_ptr bmpPointer(new Bitmap(m_pDXGIPointer->GetShapeInfo().Width, m_pDXGIPointer->GetShapeInfo().Height, m_pDXGIPointer->GetShapeInfo().Width*4, PixelFormat32bppARGB, m_pDXGIPointer->GetBuffer())); 569 | 570 | graphics->DrawImage(bmpPointer.get(), PtrX, PtrY); 571 | } 572 | break; 573 | case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME: 574 | case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR: 575 | { 576 | RECT rcPointer; 577 | 578 | if(m_pDXGIPointer->GetShapeInfo().Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME) 579 | { 580 | SetRect(&rcPointer, PtrX, PtrY, PtrX + m_pDXGIPointer->GetShapeInfo().Width, PtrY + m_pDXGIPointer->GetShapeInfo().Height/2); 581 | } 582 | else 583 | { 584 | SetRect(&rcPointer, PtrX, PtrY, PtrX + m_pDXGIPointer->GetShapeInfo().Width, PtrY + m_pDXGIPointer->GetShapeInfo().Height); 585 | } 586 | 587 | RECT rcDesktopPointer; 588 | IntersectRect(&rcDesktopPointer, &rcPointer, &rcDesktop); 589 | 590 | CopyRect(&rcPointer, &rcDesktopPointer); 591 | OffsetRect(&rcPointer, -PtrX, -PtrY); 592 | 593 | BYTE* pShapeBuffer = m_pDXGIPointer->GetBuffer(); 594 | UINT* pDesktopBits32 = (UINT*)pDesktopBits; 595 | 596 | if(m_pDXGIPointer->GetShapeInfo().Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME) 597 | { 598 | for(int j = rcPointer.top, jDP = rcDesktopPointer.top; 599 | j> (i % 8); 607 | BYTE AndMask = pShapeBuffer[i/8 + (m_pDXGIPointer->GetShapeInfo().Pitch)*j] & Mask; 608 | BYTE XorMask = pShapeBuffer[i/8 + (m_pDXGIPointer->GetShapeInfo().Pitch)*(j + m_pDXGIPointer->GetShapeInfo().Height / 2)] & Mask; 609 | 610 | UINT AndMask32 = (AndMask) ? 0xFFFFFFFF : 0xFF000000; 611 | UINT XorMask32 = (XorMask) ? 0x00FFFFFF : 0x00000000; 612 | 613 | pDesktopBits32[jDP*dwDestWidth + iDP] = (pDesktopBits32[jDP*dwDestWidth + iDP] & AndMask32) ^ XorMask32; 614 | } 615 | } 616 | } 617 | else 618 | { 619 | UINT* pShapeBuffer32 = (UINT*)pShapeBuffer; 620 | for(int j = rcPointer.top, jDP = rcDesktopPointer.top; 621 | jGetShapeInfo().Pitch/4)*j]; 630 | if (MaskVal) 631 | { 632 | // Mask was 0xFF 633 | pDesktopBits32[jDP*dwDestWidth + iDP] = (pDesktopBits32[jDP*dwDestWidth + iDP] ^ pShapeBuffer32[i + (m_pDXGIPointer->GetShapeInfo().Pitch/4)*j]) | 0xFF000000; 634 | } 635 | else 636 | { 637 | // Mask was 0x00 - replacing pixel 638 | pDesktopBits32[jDP*dwDestWidth + iDP] = pShapeBuffer32[i + (m_pDXGIPointer->GetShapeInfo().Pitch/4)*j]; 639 | } 640 | } 641 | } 642 | } 643 | } 644 | break; 645 | } 646 | } 647 | 648 | vector DXGIManager::GetOutputDuplication() 649 | { 650 | vector outputs; 651 | switch(m_CaptureSource) 652 | { 653 | case CSMonitor1: 654 | { 655 | // Return the one with IsPrimary 656 | for(vector::iterator iter = m_vOutputs.begin(); 657 | iter != m_vOutputs.end(); 658 | iter++) 659 | { 660 | DXGIOutputDuplication& out = *iter; 661 | if(out.IsPrimary()) 662 | { 663 | outputs.push_back(out); 664 | break; 665 | } 666 | } 667 | } 668 | break; 669 | 670 | case CSMonitor2: 671 | { 672 | // Return the first with !IsPrimary 673 | for(vector::iterator iter = m_vOutputs.begin(); 674 | iter != m_vOutputs.end(); 675 | iter++) 676 | { 677 | DXGIOutputDuplication& out = *iter; 678 | if(!out.IsPrimary()) 679 | { 680 | outputs.push_back(out); 681 | break; 682 | } 683 | } 684 | } 685 | break; 686 | 687 | case CSDesktop: 688 | { 689 | // Return all outputs 690 | for(vector::iterator iter = m_vOutputs.begin(); 691 | iter != m_vOutputs.end(); 692 | iter++) 693 | { 694 | DXGIOutputDuplication& out = *iter; 695 | outputs.push_back(out); 696 | } 697 | } 698 | break; 699 | } 700 | return outputs; 701 | } 702 | 703 | BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) 704 | { 705 | int *Count = (int*)dwData; 706 | (*Count)++; 707 | return TRUE; 708 | } 709 | 710 | int DXGIManager::GetMonitorCount() 711 | { 712 | int Count = 0; 713 | if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&Count)) 714 | return Count; 715 | return -1; 716 | } 717 | #pragma endregion 718 | --------------------------------------------------------------------------------