wfreerdp-server: code style cleanup

This commit is contained in:
Marc-André Moreau 2012-09-19 18:16:49 -04:00
parent 1a2e71067a
commit 34b59653dd
2 changed files with 145 additions and 155 deletions

View File

@ -16,6 +16,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
@ -32,7 +33,7 @@
#include <tchar.h> #include <tchar.h>
#include "wf_dxgi.h" #include "wf_dxgi.h"
// Driver types supported /* Driver types supported */
D3D_DRIVER_TYPE DriverTypes[] = D3D_DRIVER_TYPE DriverTypes[] =
{ {
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_HARDWARE,
@ -41,7 +42,7 @@ D3D_DRIVER_TYPE DriverTypes[] =
}; };
UINT NumDriverTypes = ARRAYSIZE(DriverTypes); UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
// Feature levels supported /* Feature levels supported */
D3D_FEATURE_LEVEL FeatureLevels[] = D3D_FEATURE_LEVEL FeatureLevels[] =
{ {
D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_11_0,
@ -49,91 +50,77 @@ D3D_FEATURE_LEVEL FeatureLevels[] =
D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1 D3D_FEATURE_LEVEL_9_1
}; };
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels); UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
D3D_FEATURE_LEVEL FeatureLevel; D3D_FEATURE_LEVEL FeatureLevel;
ID3D11Device* MeinDevice = NULL; ID3D11Device* gDevice = NULL;
ID3D11DeviceContext* MeinContext = NULL; ID3D11DeviceContext* gContext = NULL;
IDXGIOutputDuplication* MeinDeskDupl = NULL; IDXGIOutputDuplication* gOutputDuplication = NULL;
ID3D11Texture2D* MeinAcquiredDesktopImage = NULL; ID3D11Texture2D* gAcquiredDesktopImage = NULL;
IDXGISurface* surf; IDXGISurface* surf;
ID3D11Texture2D * sStage; ID3D11Texture2D* sStage;
DXGI_OUTDUPL_FRAME_INFO FrameInfo; DXGI_OUTDUPL_FRAME_INFO FrameInfo;
int wf_dxgi_init(wfInfo* context) int wf_dxgi_init(wfInfo* context)
{ {
HRESULT hr; HRESULT status;
UINT dTop, i = 0;
DXGI_OUTPUT_DESC desc;
IDXGIOutput * pOutput;
UINT DriverTypeIndex; UINT DriverTypeIndex;
IDXGIDevice* DxgiDevice = NULL; IDXGIDevice* DxgiDevice = NULL;
IDXGIAdapter* DxgiAdapter = NULL; IDXGIAdapter* DxgiAdapter = NULL;
DXGI_OUTPUT_DESC desc;
UINT dTop, i = 0;
IDXGIOutput * pOutput;
IDXGIOutput* DxgiOutput = NULL; IDXGIOutput* DxgiOutput = NULL;
IDXGIOutput1* DxgiOutput1 = NULL; IDXGIOutput1* DxgiOutput1 = NULL;
gAcquiredDesktopImage = NULL;
/////////////////////////////////////////////////////////
//guessing this must be null
MeinAcquiredDesktopImage = NULL;
// Create device
for (DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex) for (DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
{ {
hr = D3D11CreateDevice(NULL, DriverTypes[DriverTypeIndex], NULL, D3D11_CREATE_DEVICE_DEBUG, FeatureLevels, NumFeatureLevels, status = D3D11CreateDevice(NULL, DriverTypes[DriverTypeIndex], NULL, D3D11_CREATE_DEVICE_DEBUG, FeatureLevels, NumFeatureLevels,
D3D11_SDK_VERSION, &MeinDevice, &FeatureLevel, &MeinContext); D3D11_SDK_VERSION, &gDevice, &FeatureLevel, &gContext);
if (SUCCEEDED(hr)) if (SUCCEEDED(status))
{
// Device creation success, no need to loop anymore
break; break;
}
} }
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to create device in InitializeDx\n")); _tprintf(_T("Failed to create device in InitializeDx\n"));
return 1; return 1;
} }
///////////////////////////////////////////////////////
hr = MeinDevice->lpVtbl->QueryInterface(MeinDevice, &IID_IDXGIDevice, (void**) &DxgiDevice); status = gDevice->lpVtbl->QueryInterface(gDevice, &IID_IDXGIDevice, (void**) &DxgiDevice);
if (FAILED(hr))
{ if (FAILED(status))
{
_tprintf(_T("Failed to get QI for DXGI Device\n")); _tprintf(_T("Failed to get QI for DXGI Device\n"));
return 1;
}
//////////////////////////////////////////////////////////
hr = DxgiDevice->lpVtbl->GetParent(DxgiDevice, &IID_IDXGIAdapter, (void**) &DxgiAdapter);
DxgiDevice->lpVtbl->Release(DxgiDevice);
DxgiDevice = NULL;
if (FAILED(hr))
{
_tprintf(_T("Failed to get parent DXGI Adapter\n"));
return 1; return 1;
} }
//////////////////////////////////////////////////////////// status = DxgiDevice->lpVtbl->GetParent(DxgiDevice, &IID_IDXGIAdapter, (void**) &DxgiAdapter);
DxgiDevice->lpVtbl->Release(DxgiDevice);
DxgiDevice = NULL;
memset(&desc, 0, sizeof(desc)); if (FAILED(status))
{
_tprintf(_T("Failed to get parent DXGI Adapter\n"));
return 1;
}
ZeroMemory(&desc, sizeof(desc));
pOutput = NULL; pOutput = NULL;
while(DxgiAdapter->lpVtbl->EnumOutputs(DxgiAdapter, i, &pOutput) != DXGI_ERROR_NOT_FOUND)
while (DxgiAdapter->lpVtbl->EnumOutputs(DxgiAdapter, i, &pOutput) != DXGI_ERROR_NOT_FOUND)
{ {
DXGI_OUTPUT_DESC* pDesc = &desc; DXGI_OUTPUT_DESC* pDesc = &desc;
hr = pOutput->lpVtbl->GetDesc(pOutput, pDesc); status = pOutput->lpVtbl->GetDesc(pOutput, pDesc);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to get description\n")); _tprintf(_T("Failed to get description\n"));
return 1; return 1;
@ -141,85 +128,83 @@ int wf_dxgi_init(wfInfo* context)
_tprintf(_T("Output %d: [%s] [%d]\n"), i, pDesc->DeviceName, pDesc->AttachedToDesktop); _tprintf(_T("Output %d: [%s] [%d]\n"), i, pDesc->DeviceName, pDesc->AttachedToDesktop);
if(pDesc->AttachedToDesktop) if (pDesc->AttachedToDesktop)
dTop = i; dTop = i;
pOutput->lpVtbl->Release(pOutput); pOutput->lpVtbl->Release(pOutput);
++i; ++i;
} }
//for now stick to the first one -- need to change this for multimon
dTop = 0; dTop = 0;
status = DxgiAdapter->lpVtbl->EnumOutputs(DxgiAdapter, dTop, &DxgiOutput);
hr = DxgiAdapter->lpVtbl->EnumOutputs(DxgiAdapter, dTop, &DxgiOutput); DxgiAdapter->lpVtbl->Release(DxgiAdapter);
DxgiAdapter->lpVtbl->Release(DxgiAdapter); DxgiAdapter = NULL;
DxgiAdapter = NULL;
if (FAILED(hr)) if (FAILED(status))
{ {
_tprintf(_T("Failed to get output\n")); _tprintf(_T("Failed to get output\n"));
return 1; return 1;
} }
//////////////////////////////////////////////
hr = DxgiOutput->lpVtbl->QueryInterface(DxgiOutput, &IID_IDXGIOutput1, (void**) &DxgiOutput1); status = DxgiOutput->lpVtbl->QueryInterface(DxgiOutput, &IID_IDXGIOutput1, (void**) &DxgiOutput1);
DxgiOutput->lpVtbl->Release(DxgiOutput); DxgiOutput->lpVtbl->Release(DxgiOutput);
DxgiOutput = NULL; DxgiOutput = NULL;
if (FAILED(hr))
{
_tprintf(_T("Failed to get IDXGIOutput1\n"));
return 1;
}
////////////////////////////////////////////// if (FAILED(status))
{
_tprintf(_T("Failed to get IDXGIOutput1\n"));
return 1;
}
hr = DxgiOutput1->lpVtbl->DuplicateOutput(DxgiOutput1, (IUnknown*)MeinDevice, &MeinDeskDupl); status = DxgiOutput1->lpVtbl->DuplicateOutput(DxgiOutput1, (IUnknown*)gDevice, &gOutputDuplication);
DxgiOutput1->lpVtbl->Release(DxgiOutput1); DxgiOutput1->lpVtbl->Release(DxgiOutput1);
DxgiOutput1 = NULL; DxgiOutput1 = NULL;
if (FAILED(hr))
{ if (FAILED(status))
if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) {
{ if (status == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
_tprintf(_T("There is already the maximum number of applications using the Desktop Duplication API running, please close one of those applications and then try again.\n")); {
_tprintf(_T("There is already the maximum number of applications using the Desktop Duplication API running, please close one of those applications and then try again.\n"));
return 1; return 1;
} }
_tprintf(_T("Failed to get duplicate output\n")); _tprintf(_T("Failed to get duplicate output\n"));
return 1; return 1;
} }
return 0; return 0;
} }
int wf_dxgi_cleanup(wfInfo* wfi) int wf_dxgi_cleanup(wfInfo* wfi)
{ {
if(wfi->framesWaiting > 0) if (wfi->framesWaiting > 0)
{ {
wf_dxgi_releasePixelData(wfi); wf_dxgi_releasePixelData(wfi);
} }
if(MeinAcquiredDesktopImage) if (gAcquiredDesktopImage)
{ {
MeinAcquiredDesktopImage->lpVtbl->Release(MeinAcquiredDesktopImage); gAcquiredDesktopImage->lpVtbl->Release(gAcquiredDesktopImage);
MeinAcquiredDesktopImage = NULL; gAcquiredDesktopImage = NULL;
} }
if(MeinDeskDupl) if (gOutputDuplication)
{ {
MeinDeskDupl->lpVtbl->Release(MeinDeskDupl); gOutputDuplication->lpVtbl->Release(gOutputDuplication);
MeinDeskDupl = NULL; gOutputDuplication = NULL;
} }
if(MeinContext) if(gContext)
{ {
MeinContext->lpVtbl->Release(MeinContext); gContext->lpVtbl->Release(gContext);
MeinContext = NULL; gContext = NULL;
} }
if(MeinDevice) if(gDevice)
{ {
MeinDevice->lpVtbl->Release(MeinDevice); gDevice->lpVtbl->Release(gDevice);
MeinDevice = NULL; gDevice = NULL;
} }
return 0; return 0;
@ -227,47 +212,49 @@ int wf_dxgi_cleanup(wfInfo* wfi)
int wf_dxgi_nextFrame(wfInfo* wfi, UINT timeout) int wf_dxgi_nextFrame(wfInfo* wfi, UINT timeout)
{ {
HRESULT hr; HRESULT status;
IDXGIResource* DesktopResource = NULL;
BYTE* MeinMetaDataBuffer = NULL;
UINT MeinMetaDataSize = 0;
UINT i = 0; UINT i = 0;
UINT DataBufferSize = 0;
BYTE* DataBuffer = NULL;
IDXGIResource* DesktopResource = NULL;
if(wfi->framesWaiting > 0) if (wfi->framesWaiting > 0)
{ {
wf_dxgi_releasePixelData(wfi); wf_dxgi_releasePixelData(wfi);
} }
if(MeinAcquiredDesktopImage) if (gAcquiredDesktopImage)
{ {
MeinAcquiredDesktopImage->lpVtbl->Release(MeinAcquiredDesktopImage); gAcquiredDesktopImage->lpVtbl->Release(gAcquiredDesktopImage);
MeinAcquiredDesktopImage = NULL; gAcquiredDesktopImage = NULL;
} }
hr = MeinDeskDupl->lpVtbl->AcquireNextFrame(MeinDeskDupl, timeout, &FrameInfo, &DesktopResource); status = gOutputDuplication->lpVtbl->AcquireNextFrame(gOutputDuplication, timeout, &FrameInfo, &DesktopResource);
if (hr == DXGI_ERROR_WAIT_TIMEOUT)
if (status == DXGI_ERROR_WAIT_TIMEOUT)
{ {
return 1; return 1;
} }
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to acquire next frame\n")); _tprintf(_T("Failed to acquire next frame\n"));
hr = MeinDeskDupl->lpVtbl->ReleaseFrame(MeinDeskDupl); status = gOutputDuplication->lpVtbl->ReleaseFrame(gOutputDuplication);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to release frame\n")); _tprintf(_T("Failed to release frame\n"));
} }
return 1; return 1;
} }
///////////////////////////////////////////////
hr = DesktopResource->lpVtbl->QueryInterface(DesktopResource, &IID_ID3D11Texture2D, (void**) &MeinAcquiredDesktopImage); status = DesktopResource->lpVtbl->QueryInterface(DesktopResource, &IID_ID3D11Texture2D, (void**) &gAcquiredDesktopImage);
DesktopResource->lpVtbl->Release(DesktopResource); DesktopResource->lpVtbl->Release(DesktopResource);
DesktopResource = NULL; DesktopResource = NULL;
if (FAILED(hr))
if (FAILED(status))
{ {
return 1; return 1;
} }
@ -279,10 +266,10 @@ int wf_dxgi_nextFrame(wfInfo* wfi, UINT timeout)
int wf_dxgi_getPixelData(wfInfo* context, BYTE** data, int* pitch, RECT* invalid) int wf_dxgi_getPixelData(wfInfo* context, BYTE** data, int* pitch, RECT* invalid)
{ {
HRESULT hr; HRESULT status;
DXGI_MAPPED_RECT MeinData;
D3D11_TEXTURE2D_DESC tDesc;
D3D11_BOX Box; D3D11_BOX Box;
DXGI_MAPPED_RECT mappedRect;
D3D11_TEXTURE2D_DESC tDesc;
tDesc.Width = (invalid->right - invalid->left); tDesc.Width = (invalid->right - invalid->left);
tDesc.Height = (invalid->bottom - invalid->top); tDesc.Height = (invalid->bottom - invalid->top);
@ -293,7 +280,7 @@ int wf_dxgi_getPixelData(wfInfo* context, BYTE** data, int* pitch, RECT* invalid
tDesc.SampleDesc.Quality = 0; tDesc.SampleDesc.Quality = 0;
tDesc.Usage = D3D11_USAGE_STAGING; tDesc.Usage = D3D11_USAGE_STAGING;
tDesc.BindFlags = 0; tDesc.BindFlags = 0;
tDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;// | D3D11_CPU_ACCESS_WRITE; tDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
tDesc.MiscFlags = 0; tDesc.MiscFlags = 0;
Box.top = invalid->top; Box.top = invalid->top;
@ -303,41 +290,44 @@ int wf_dxgi_getPixelData(wfInfo* context, BYTE** data, int* pitch, RECT* invalid
Box.front = 0; Box.front = 0;
Box.back = 1; Box.back = 1;
hr = MeinDevice->lpVtbl->CreateTexture2D(MeinDevice, &tDesc, NULL, &sStage); status = gDevice->lpVtbl->CreateTexture2D(gDevice, &tDesc, NULL, &sStage);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to create staging surface\n")); _tprintf(_T("Failed to create staging surface\n"));
exit(1); exit(1);
return 1; return 1;
} }
MeinContext->lpVtbl->CopySubresourceRegion(MeinContext, (ID3D11Resource*)sStage, 0,0,0,0, (ID3D11Resource*)MeinAcquiredDesktopImage, 0, &Box); gContext->lpVtbl->CopySubresourceRegion(gContext, (ID3D11Resource*) sStage, 0,0,0,0, (ID3D11Resource*) gAcquiredDesktopImage, 0, &Box);
hr = sStage->lpVtbl->QueryInterface(sStage, &IID_IDXGISurface, (void**)&surf); status = sStage->lpVtbl->QueryInterface(sStage, &IID_IDXGISurface, (void**) &surf);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to QI staging surface\n")); _tprintf(_T("Failed to QI staging surface\n"));
exit(1); exit(1);
return 1; return 1;
} }
surf->lpVtbl->Map(surf, &MeinData, DXGI_MAP_READ); surf->lpVtbl->Map(surf, &mappedRect, DXGI_MAP_READ);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to map staging surface\n")); _tprintf(_T("Failed to map staging surface\n"));
exit(1); exit(1);
return 1; return 1;
} }
*data = MeinData.pBits; *data = mappedRect.pBits;
*pitch = MeinData.Pitch; *pitch = mappedRect.Pitch;
return 0; return 0;
} }
int wf_dxgi_releasePixelData(wfInfo* wfi) int wf_dxgi_releasePixelData(wfInfo* wfi)
{ {
HRESULT hr; HRESULT status;
surf->lpVtbl->Unmap(surf); surf->lpVtbl->Unmap(surf);
surf->lpVtbl->Release(surf); surf->lpVtbl->Release(surf);
@ -345,73 +335,74 @@ int wf_dxgi_releasePixelData(wfInfo* wfi)
sStage->lpVtbl->Release(sStage); sStage->lpVtbl->Release(sStage);
sStage = NULL; sStage = NULL;
hr = MeinDeskDupl->lpVtbl->ReleaseFrame(MeinDeskDupl); status = gOutputDuplication->lpVtbl->ReleaseFrame(gOutputDuplication);
if (FAILED(hr))
if (FAILED(status))
{ {
_tprintf(_T("Failed to release frame\n")); _tprintf(_T("Failed to release frame\n"));
return 1; return 1;
} }
wfi->framesWaiting = 0; wfi->framesWaiting = 0;
return 0; return 0;
} }
int wf_dxgi_getInvalidRegion(RECT* invalid) int wf_dxgi_getInvalidRegion(RECT* invalid)
{ {
HRESULT hr;
UINT MeinMetaDataSize = 0;
UINT BufSize;
UINT i; UINT i;
BYTE* DirtyRects; HRESULT status;
UINT dirty; UINT dirty;
UINT BufSize;
RECT* pRect; RECT* pRect;
BYTE* DirtyRects;
UINT DataBufferSize = 0;
BYTE* DataBuffer = NULL;
//optimization note: make this buffer global and allocate only once (or grow only when needed) if (FrameInfo.AccumulatedFrames == 0)
BYTE* MeinMetaDataBuffer = NULL;
if(FrameInfo.AccumulatedFrames == 0)
{ {
//we dont care
return 1; return 1;
} }
if (FrameInfo.TotalMetadataBufferSize)
if(FrameInfo.TotalMetadataBufferSize)
{ {
if (FrameInfo.TotalMetadataBufferSize > MeinMetaDataSize) if (FrameInfo.TotalMetadataBufferSize > DataBufferSize)
{ {
if (MeinMetaDataBuffer) if (DataBuffer)
{ {
free(MeinMetaDataBuffer); free(DataBuffer);
MeinMetaDataBuffer = NULL; DataBuffer = NULL;
} }
MeinMetaDataBuffer = (BYTE*) malloc(FrameInfo.TotalMetadataBufferSize);
if (!MeinMetaDataBuffer) DataBuffer = (BYTE*) malloc(FrameInfo.TotalMetadataBufferSize);
if (!DataBuffer)
{ {
MeinMetaDataSize = 0; DataBufferSize = 0;
_tprintf(_T("Failed to allocate memory for metadata\n")); _tprintf(_T("Failed to allocate memory for metadata\n"));
exit(1); exit(1);
} }
MeinMetaDataSize = FrameInfo.TotalMetadataBufferSize;
DataBufferSize = FrameInfo.TotalMetadataBufferSize;
} }
BufSize = FrameInfo.TotalMetadataBufferSize; BufSize = FrameInfo.TotalMetadataBufferSize;
// Get move rectangles status = gOutputDuplication->lpVtbl->GetFrameMoveRects(gOutputDuplication, BufSize, (DXGI_OUTDUPL_MOVE_RECT*) DataBuffer, &BufSize);
hr = MeinDeskDupl->lpVtbl->GetFrameMoveRects(MeinDeskDupl, BufSize, (DXGI_OUTDUPL_MOVE_RECT*) MeinMetaDataBuffer, &BufSize);
if (FAILED(hr)) if (FAILED(status))
{ {
_tprintf(_T("Failed to get frame move rects\n")); _tprintf(_T("Failed to get frame move rects\n"));
return 1; return 1;
} }
DirtyRects = MeinMetaDataBuffer + BufSize; DirtyRects = DataBuffer + BufSize;
BufSize = FrameInfo.TotalMetadataBufferSize - BufSize; BufSize = FrameInfo.TotalMetadataBufferSize - BufSize;
// Get dirty rectangles status = gOutputDuplication->lpVtbl->GetFrameDirtyRects(gOutputDuplication, BufSize, (RECT*) DirtyRects, &BufSize);
hr = MeinDeskDupl->lpVtbl->GetFrameDirtyRects(MeinDeskDupl, BufSize, (RECT*) DirtyRects, &BufSize);
if (FAILED(hr)) if (FAILED(status))
{ {
_tprintf(_T("Failed to get frame dirty rects\n")); _tprintf(_T("Failed to get frame dirty rects\n"));
return 1; return 1;
@ -419,6 +410,7 @@ int wf_dxgi_getInvalidRegion(RECT* invalid)
dirty = BufSize / sizeof(RECT); dirty = BufSize / sizeof(RECT);
pRect = (RECT*) DirtyRects; pRect = (RECT*) DirtyRects;
for(i = 0; i<dirty; ++i) for(i = 0; i<dirty; ++i)
{ {
UnionRect(invalid, invalid, pRect); UnionRect(invalid, invalid, pRect);

View File

@ -309,9 +309,7 @@ void wf_info_getScreenData(wfInfo* wfi, long* width, long* height, uint8** pBits
*height = (wfi->invalid.bottom - wfi->invalid.top); *height = (wfi->invalid.bottom - wfi->invalid.top);
#ifdef WITH_WIN8 #ifdef WITH_WIN8
wf_dxgi_getPixelData(wfi, pBits, pitch, &wfi->invalid); wf_dxgi_getPixelData(wfi, pBits, pitch, &wfi->invalid);
#else #else
{ {
long offset; long offset;