-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDAImpl.cpp
202 lines (173 loc) · 6.61 KB
/
DDAImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Defs.h"
#include "DDAImpl.h"
#include <iomanip>
#include "dxgi1_2.h"
IDXGIOutput1* pOut1;
IDXGIDevice3* pDevice;
IDXGIOutput* pOutput;
IDXGIFactory3* pFactory;
IDXGIAdapter2* pAdapter;
/// Initialize DDA
HRESULT DDAImpl::Init()
{
pOutput = nullptr;
pDevice = nullptr;
pFactory = nullptr;
pAdapter = nullptr;
pOut1 = nullptr;
/// Release all temporary refs before exit
#define CLEAN_RETURN(x) \
SAFE_RELEASE(pDevice);\
SAFE_RELEASE(pFactory);\
SAFE_RELEASE(pOutput);\
SAFE_RELEASE(pOut1);\
SAFE_RELEASE(pAdapter);\
return x;
HRESULT hr = S_OK;
/// To create a DDA object given a D3D11 device, we must first get to the DXGI Adapter associated with that device
if (FAILED(hr = pD3DDev->QueryInterface(__uuidof(IDXGIDevice2), (void**)&pDevice)))
{
CLEAN_RETURN(hr);
}
if (FAILED(hr = pDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&pAdapter)))
{
CLEAN_RETURN(hr);
}
/// Once we have the DXGI Adapter, we enumerate the attached display outputs, and select which one we want to capture
/// This sample application always captures the primary display output, enumerated at index 0.
if (FAILED(hr = pAdapter->EnumOutputs(0, &pOutput)))
{
CLEAN_RETURN(hr);
}
if (FAILED(hr = pOutput->QueryInterface(__uuidof(IDXGIOutput1), (void**)&pOut1)))
{
CLEAN_RETURN(hr);
}
/// Ask DXGI to create an instance of IDXGIOutputDuplication for the selected output. We can now capture this display output
if (FAILED(hr = pOut1->DuplicateOutput(pDevice, &pDup)))
{
CLEAN_RETURN(hr);
}
DXGI_OUTDUPL_DESC outDesc;
ZeroMemory(&outDesc, sizeof(outDesc));
pDup->GetDesc(&outDesc);
height = outDesc.ModeDesc.Height;
width = outDesc.ModeDesc.Width;
CLEAN_RETURN(hr);
}
/// Acquire a new frame from DDA, and return it as a Texture2D object.
/// 'wait' specifies the time in milliseconds that DDA shoulo wait for a new screen update.
HRESULT DDAImpl::GetCapturedFrame(ID3D11Texture2D **ppTex2D, int wait)
{
HRESULT hr = S_OK;
DXGI_OUTDUPL_FRAME_INFO frameInfo;
ZeroMemory(&frameInfo, sizeof(frameInfo));
int acquired = 0;
#define RETURN_ERR(x) {/*printf(__FUNCTION__": %d : Line %d return 0x%x\n", frameno, __LINE__, x);*/return x;}
if (pResource)
{
pDup->ReleaseFrame();
pResource->Release();
pResource = nullptr;
}
try {
//pResource->Release();
pDup->ReleaseFrame();
hr = pDup->AcquireNextFrame(wait, &frameInfo, &pResource);
}
catch (...) {
hr = pDup->AcquireNextFrame(wait, &frameInfo, &pResource);
}
if (FAILED(hr))
{
if (hr == DXGI_ERROR_WAIT_TIMEOUT)
{
//printf(__FUNCTION__": %d : Wait for %d ms timed out\n", frameno, wait);
}
if (hr == DXGI_ERROR_INVALID_CALL)
{
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pDup);
pResource->Release();
pResource = nullptr; pResource->Release();
pResource = nullptr;
pOut1->DuplicateOutput(pDevice, &pDup);
hr = pDup->AcquireNextFrame(wait, &frameInfo, &pResource);
cout << "Invalid Call, previous frame not released ? \n";
}
if (hr == DXGI_ERROR_ACCESS_LOST)
{
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pFactory);
SAFE_RELEASE(pOutput);
SAFE_RELEASE(pOut1);
SAFE_RELEASE(pAdapter);
SAFE_RELEASE(pDup);
cout << "Access lost, frame needs to be released?\n";
}
RETURN_ERR(hr);
}
//if (frameInfo.AccumulatedFrames == 0 || frameInfo.LastPresentTime.QuadPart == 0)
//{
// // No image update, only cursor moved.
// //ofs << "frameNo: " << frameno << " | Accumulated: " << frameInfo.AccumulatedFrames << "MouseOnly?" << frameInfo.LastMouseUpdateTime.QuadPart << endl;
// RETURN_ERR(DXGI_ERROR_WAIT_TIMEOUT);
//}
if (!pResource)
{
printf(__FUNCTION__": %d : Null output resource. Return error.\n", frameno);
return E_UNEXPECTED;
}
if (FAILED(hr = pResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)ppTex2D)))
{
return hr;
}
LARGE_INTEGER pts = frameInfo.LastPresentTime; MICROSEC_TIME(pts, qpcFreq);
LONGLONG interval = pts.QuadPart - lastPTS.QuadPart;
//printf(__FUNCTION__": %d : Accumulated Frames %u PTS Interval %lld PTS %lld\n", frameno, frameInfo.AccumulatedFrames, interval * 1000, frameInfo.LastPresentTime.QuadPart);
//ofs << "frameNo: " << frameno << " | Accumulated: "<< frameInfo.AccumulatedFrames <<" | PTS: " << frameInfo.LastPresentTime.QuadPart << " | PTSInterval: "<< (interval)*1000<<endl;
lastPTS = pts; // store microsec value
frameno += frameInfo.AccumulatedFrames;
return hr;
}
/// Release all resources
int DDAImpl::Cleanup()
{
if (pResource)
{
pDup->ReleaseFrame();
SAFE_RELEASE(pResource);
}
width = height = frameno = 0;
SAFE_RELEASE(pDup);
SAFE_RELEASE(pCtx);
SAFE_RELEASE(pD3DDev);
return 0;
}