[dxgi] cleaned up retry logic

This commit is contained in:
Geoffrey McRae 2018-10-04 17:05:08 +10:00
parent 9613127162
commit fb37174e5f

View File

@ -361,22 +361,33 @@ GrabStatus Capture::DXGI::Capture()
IDXGIResourcePtr res; IDXGIResourcePtr res;
HRESULT status; HRESULT status;
for (int i = 0; i < 2; ++i) for (int retryCount = 0; retryCount < 2; ++retryCount)
{
while (true)
{ {
GrabStatus ret = ReleaseFrame(); GrabStatus ret = ReleaseFrame();
if (ret != GRAB_STATUS_OK) if (ret != GRAB_STATUS_OK)
return ret; return ret;
status = m_dup->AcquireNextFrame(1000, &frameInfo, &res); status = m_dup->AcquireNextFrame(1000, &frameInfo, &res);
if (status == DXGI_ERROR_WAIT_TIMEOUT)
return GRAB_STATUS_TIMEOUT;
if (FAILED(status)) switch (status)
{
case S_OK:
m_releaseFrame = true;
break; break;
m_releaseFrame = true; case DXGI_ERROR_WAIT_TIMEOUT:
return GRAB_STATUS_TIMEOUT;
// desktop switch, mode change, switch DWM on or off or Secure Desktop
case DXGI_ERROR_ACCESS_LOST:
case WAIT_ABANDONED:
return GRAB_STATUS_REINIT;
default:
// unknown failure
DEBUG_WINERROR("AcquireNextFrame failed", status);
return GRAB_STATUS_ERROR;
}
// if we have a mouse update // if we have a mouse update
if (frameInfo.LastMouseUpdateTime.QuadPart) if (frameInfo.LastMouseUpdateTime.QuadPart)
@ -464,45 +475,33 @@ GrabStatus Capture::DXGI::Capture()
LeaveCriticalSection(&m_cursorCS); LeaveCriticalSection(&m_cursorCS);
} }
// if we also have frame data, break out to process it // if we don't have frame data
if (frameInfo.LastPresentTime.QuadPart != 0) if (frameInfo.LastPresentTime.QuadPart == 0)
break; {
// if there is nothing to update, just start again
if (!cursorUpdated)
{
--retryCount;
continue;
}
res = NULL; res = NULL;
// if the cursor has been updated
if (cursorUpdated)
return GRAB_STATUS_CURSOR; return GRAB_STATUS_CURSOR;
// otherwise just try again
} }
if (SUCCEEDED(status)) // success, break out of the retry loop
break; break;
switch (status)
{
// desktop switch, mode change, switch DWM on or off or Secure Desktop
case DXGI_ERROR_ACCESS_LOST:
case WAIT_ABANDONED:
return GRAB_STATUS_REINIT;
default:
// unknown failure
DEBUG_WINERROR("AcquireNextFrame failed", status);
return GRAB_STATUS_ERROR;
}
} }
// retry count exceeded // ensure we have a frame
if (FAILED(status)) if (!m_releaseFrame)
{ {
DEBUG_WINERROR("Failed to acquire next frame", status); DEBUG_WINERROR("Failed to acquire next frame", status);
return GRAB_STATUS_ERROR; return GRAB_STATUS_ERROR;
} }
// get the texture
res.QueryInterface(IID_PPV_ARGS(&m_ftexture)); res.QueryInterface(IID_PPV_ARGS(&m_ftexture));
if (!m_ftexture) if (!m_ftexture)
{ {
DEBUG_ERROR("Failed to get src ID3D11Texture2D"); DEBUG_ERROR("Failed to get src ID3D11Texture2D");
@ -565,11 +564,14 @@ GrabStatus Capture::DXGI::GrabFrameRaw(FrameInfo & frame)
frame.pitch = m_width * 4; frame.pitch = m_width * 4;
frame.stride = m_width; frame.stride = m_width;
if (frame.pitch == mapping.RowPitch)
memcpySSE(frame.buffer, mapping.pData, frame.pitch * m_height);
else
for(unsigned int y = 0; y < m_height; ++y) for(unsigned int y = 0; y < m_height; ++y)
memcpySSE( memcpySSE(
(uint32_t*)frame.buffer + (m_width * y), (uint8_t *)frame.buffer + (frame.pitch * y),
(uint8_t *)mapping.pData + (mapping.RowPitch * y), (uint8_t *)mapping.pData + (mapping.RowPitch * y),
m_width * 4 frame.pitch
); );
m_deviceContext->Unmap(m_texture[0], 0); m_deviceContext->Unmap(m_texture[0], 0);