wfreerdp-server: get rid of wfInfoSingleton

This commit is contained in:
Marc-André Moreau 2012-09-02 17:09:36 -04:00
parent 7c25f133a8
commit 3a33d63902
7 changed files with 110 additions and 98 deletions

View File

@ -31,44 +31,45 @@
extern wfInfo * wfInfoSingleton;
int wf_info_lock(DWORD ms)
static wfInfo* wfInfoInstance = NULL;
int wf_info_lock(wfInfo* wfi, DWORD ms)
{
DWORD dRes;
dRes = WaitForSingleObject(wfInfoSingleton->mutex, ms);
dRes = WaitForSingleObject(wfi->mutex, ms);
switch(dRes)
switch (dRes)
{
case WAIT_ABANDONED:
//complain and proceed as normal
printf("Got ownership of abandoned mutex... resuming...\n");
case WAIT_ABANDONED:
printf("Got ownership of abandoned mutex... resuming...\n");
break;
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
return 1;
break;
case WAIT_FAILED:
printf("WAIT FAILED code %#X\n", GetLastError());
return -1;
break;
case WAIT_TIMEOUT:
return 1;
break;
case WAIT_FAILED:
printf("WAIT FAILED code %#X\n", GetLastError());
return -1;
break;
}
return 0;
}
int wf_info_unlock()
int wf_info_unlock(wfInfo* wfi)
{
if(ReleaseMutex(wfInfoSingleton->mutex) == 0)
if (ReleaseMutex(wfi->mutex) == 0)
return 0;
return 1;
}
wfInfo* wf_info_init(wfInfo * wfi)
wfInfo* wf_info_init(wfInfo* wfi)
{
if (!wfi)
{
@ -100,13 +101,21 @@ wfInfo* wf_info_init(wfInfo * wfi)
return wfi;
}
wfInfo* wf_info_get_instance()
{
if (wfInfoInstance == NULL)
wfInfoInstance = wf_info_init(NULL);
return wfInfoInstance;
}
void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context)
{
DWORD dRes;
dRes = WaitForSingleObject(wfi->mutex, INFINITE);
switch(dRes)
switch (dRes)
{
case WAIT_OBJECT_0:
@ -114,21 +123,21 @@ void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context)
{
/* only the first peer needs to call this. */
context->wfInfo = wfi;
wf_check_disp_devices(context->wfInfo);
wf_disp_device_set_attatch(context->wfInfo, 1);
wf_update_mirror_drv(context->wfInfo, 0);
wf_map_mirror_mem(context->wfInfo);
context->info = wfi;
wf_check_disp_devices(context->info);
wf_disp_device_set_attatch(context->info, 1);
wf_update_mirror_drv(context->info, 0);
wf_map_mirror_mem(context->info);
context->rfx_context = rfx_context_new();
context->rfx_context->mode = RLGR3;
context->rfx_context->width = context->wfInfo->width;
context->rfx_context->height = context->wfInfo->height;
context->rfx_context->width = context->info->width;
context->rfx_context->height = context->info->height;
rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
context->s = stream_new(65536);
context->wfInfo->roflbuffer = (BYTE*)malloc( (context->wfInfo->width) * (context->wfInfo->height) * 4);
context->info->roflbuffer = (BYTE*)malloc( (context->info->width) * (context->info->height) * 4);
printf("Start Encoder\n");
ReleaseMutex(wfi->encodeMutex);
@ -171,14 +180,14 @@ void wf_info_subscriber_release(wfInfo* wfi, wfPeerContext* context)
case WAIT_OBJECT_0:
--wfi->subscribers;
/* only the last peer needs to call this */
wf_mirror_cleanup(context->wfInfo);
wf_disp_device_set_attatch(context->wfInfo, 0);
wf_update_mirror_drv(context->wfInfo, 1);
wf_mirror_cleanup(context->info);
wf_disp_device_set_attatch(context->info, 0);
wf_update_mirror_drv(context->info, 1);
stream_free(context->s);
rfx_context_free(context->rfx_context);
free(context->wfInfo->roflbuffer);
free(context->info->roflbuffer);
break;
/**
@ -222,9 +231,7 @@ BOOL wf_info_have_updates(wfInfo* wfi)
void wf_info_updated(wfInfo* wfi)
{
wfi->lastUpdate = wfi->nextUpdate;
wfi->lastUpdate = wfi->nextUpdate;
}
void wf_info_update_changes(wfInfo* wfi)
@ -233,7 +240,6 @@ void wf_info_update_changes(wfInfo* wfi)
buf = (GETCHANGESBUF*) wfi->changeBuffer;
wfi->nextUpdate = buf->buffer->counter;
}
void wf_info_find_invalid_region(wfInfo* wfi)
@ -270,7 +276,6 @@ void wf_info_find_invalid_region(wfInfo* wfi)
if (wfi->invalid_y2 >= wfi->height)
wfi->invalid_y2 = wfi->height - 1;
}
void wf_info_clear_invalid_region(wfInfo* wfi)

View File

@ -53,9 +53,10 @@ struct wf_info
};
typedef struct wf_info wfInfo;
int wf_info_lock(DWORD ms);
int wf_info_unlock();
int wf_info_lock(wfInfo* wfi, DWORD ms);
int wf_info_unlock(wfInfo* wfi);
wfInfo* wf_info_get_instance();
wfInfo* wf_info_init(wfInfo* wfi);
void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context);
void wf_info_subscriber_release(wfInfo* wfi, wfPeerContext* context);

View File

@ -243,7 +243,6 @@ BOOL wf_map_mirror_mem(wfInfo* context)
{
int status;
GETCHANGESBUF* b;
_tprintf(_T("\n\nCreating a device context...\n"));
context->driverDC = CreateDC(context->deviceName, NULL, NULL, NULL);

View File

@ -36,50 +36,52 @@
void wf_peer_context_new(freerdp_peer* client, wfPeerContext* context)
{
#ifndef WITH_WIN8
wfInfoSingleton = wf_info_init(wfInfoSingleton);
wf_info_mirror_init(wfInfoSingleton, context);
context->info = wf_info_get_instance();
wf_info_mirror_init(context->info, context);
#endif
}
void wf_peer_context_free(freerdp_peer* client, wfPeerContext* context)
{
#ifndef WITH_WIN8
wf_info_subscriber_release(wfInfoSingleton, context);
wf_info_subscriber_release(context->info, context);
#endif
}
static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam)
{
DWORD fps;
wfInfo* wfi;
DWORD beg, end;
DWORD diff, rate;
freerdp_peer* client;
wfPeerContext* context;
fps = 24;
rate = 1000 / fps;
client = (freerdp_peer*) lpParam;
context = (wfPeerContext*) client->context;
wfi = context->info;
while (1)
{
beg = GetTickCount();
wf_info_lock(INFINITE);
wf_info_lock(wfi, INFINITE);
if (wf_info_has_subscribers(wfInfoSingleton))
if (wf_info_has_subscribers(wfi))
{
wf_info_update_changes(wfi);
wf_info_update_changes(wfInfoSingleton);
if (wf_info_have_updates(wfInfoSingleton))
{
if (wf_info_have_updates(wfi))
wf_rfx_encode(client);
}
}
else
{
wf_info_unlock();
wf_info_unlock(wfi);
}
wf_info_unlock();
wf_info_unlock(wfi);
end = GetTickCount();
diff = end - beg;
@ -92,7 +94,7 @@ static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam)
_tprintf(_T("monitor thread terminating...\n"));
wf_info_set_thread_count(wfInfoSingleton, wf_info_get_thread_count(wfInfoSingleton) - 1);
wf_info_set_thread_count(wfi, wf_info_get_thread_count(wfi) - 1);
return 0;
}
@ -118,36 +120,34 @@ void wf_rfx_encode(freerdp_peer* client)
BYTE* dstp;
#endif
if(client->activated == FALSE)
if (client->activated == FALSE)
return;
wfp = (wfPeerContext*) client->context;
wfi = wfp->info;
dRes = WaitForSingleObject(wfInfoSingleton->encodeMutex, INFINITE);
dRes = WaitForSingleObject(wfi->encodeMutex, INFINITE);
switch (dRes)
{
case WAIT_ABANDONED:
printf("\n\nwf_rfx_encode: Got ownership of abandoned mutex... resuming...\n");
//no break
printf("\n\nwf_rfx_encode: Got ownership of abandoned mutex... resuming...\n");
case WAIT_OBJECT_0:
wf_info_find_invalid_region(wfInfoSingleton);
wf_info_find_invalid_region(wfi);
if( (wfp->activated == false) ||
(wf_info_has_subscribers(wfInfoSingleton) == false) ||
!wf_info_have_invalid_region(wfInfoSingleton) ||
(wfInfoSingleton->enc_data == true) )
(wf_info_has_subscribers(wfi) == false) ||
!wf_info_have_invalid_region(wfi) ||
(wfi->enc_data == true) )
{
ReleaseMutex(wfInfoSingleton->encodeMutex);
ReleaseMutex(wfi->encodeMutex);
break;
}
update = client->update;
cmd = &update->surface_bits_command;
wfi = wfp->wfInfo;
buf = (GETCHANGESBUF*) wfi->changeBuffer;
width = (wfi->invalid_x2 - wfi->invalid_x1) + 1;
@ -216,12 +216,12 @@ void wf_rfx_encode(freerdp_peer* client)
cmd->bitmapData = stream_get_head(s);
wfi->enc_data = true;
ReleaseMutex(wfInfoSingleton->encodeMutex);
ReleaseMutex(wfi->encodeMutex);
break;
case WAIT_TIMEOUT:
ReleaseMutex(wfInfoSingleton->encodeMutex);
ReleaseMutex(wfi->encodeMutex);
break;
default:
@ -233,17 +233,22 @@ void wf_rfx_encode(freerdp_peer* client)
void wf_peer_init(freerdp_peer* client)
{
wfInfo* wfi;
client->context_size = sizeof(wfPeerContext);
client->ContextNew = (psPeerContextNew) wf_peer_context_new;
client->ContextFree = (psPeerContextFree) wf_peer_context_free;
freerdp_peer_context_new(client);
wfi = ((wfPeerContext*) client->context)->info;
#ifndef WITH_WIN8
if (!wf_info_get_thread_count(wfInfoSingleton))
if (!wf_info_get_thread_count(wfi))
{
if (CreateThread(NULL, 0, wf_peer_mirror_monitor, client, 0, NULL) != 0)
{
wf_info_set_thread_count(wfInfoSingleton, wf_info_get_thread_count(wfInfoSingleton) + 1);
wf_info_set_thread_count(wfi, wf_info_get_thread_count(wfi) + 1);
}
else
{
@ -255,8 +260,11 @@ void wf_peer_init(freerdp_peer* client)
boolean wf_peer_post_connect(freerdp_peer* client)
{
wfInfo* wfi;
wfPeerContext* context = (wfPeerContext*) client->context;
wfi = context->info;
/**
* This callback is called when the entire connection sequence is done, i.e. we've received the
* Font List PDU from the client and sent out the Font Map PDU.
@ -280,10 +288,10 @@ boolean wf_peer_post_connect(freerdp_peer* client)
printf("Client requested desktop: %dx%dx%d\n",
client->settings->width, client->settings->height, client->settings->color_depth);
printf("But we will try resizing to %dx%d\n", wfInfoSingleton->width, wfInfoSingleton->height);
printf("But we will try resizing to %dx%d\n", wfi->width, wfi->height);
client->settings->width = wfInfoSingleton->width;
client->settings->height = wfInfoSingleton->height;
client->settings->width = wfi->width;
client->settings->height = wfi->height;
client->update->DesktopResize(client->update->context);
@ -304,42 +312,46 @@ void wf_peer_synchronize_event(rdpInput* input, uint32 flags)
}
void wf_peer_send_changes(rdpUpdate* update)
void wf_peer_send_changes(freerdp_peer* client)
{
int dRes;
wfInfo* wfi;
wfPeerContext* context = (wfPeerContext*) client->context;
wfi = context->info;
/* are we currently encoding? */
dRes = WaitForSingleObject(wfInfoSingleton->encodeMutex, 0);
dRes = WaitForSingleObject(wfi->encodeMutex, 0);
switch(dRes)
{
case WAIT_ABANDONED:
printf("\n\nwf_peer_send_changes: Got ownership of abandoned mutex... resuming...\n");
//no break;
/* no break; */
case WAIT_OBJECT_0:
/* are there changes to send? */
if ( ((wf_info_lock(0) != 0)) ||
!wf_info_have_updates(wfInfoSingleton) ||
!wf_info_have_invalid_region(wfInfoSingleton) ||
(wfInfoSingleton->enc_data == FALSE))
if ( ((wf_info_lock(wfi, 0) != 0)) ||
!wf_info_have_updates(wfi) ||
!wf_info_have_invalid_region(wfi) ||
(wfi->enc_data == FALSE))
{
//we dont send
wf_info_unlock();
ReleaseMutex(wfInfoSingleton->encodeMutex);
/* we do not send */
wf_info_unlock(wfi);
ReleaseMutex(wfi->encodeMutex);
break;
}
wf_info_updated(wfInfoSingleton);
wf_info_updated(wfi);
update->SurfaceBits(update->context, &update->surface_bits_command);
client->update->SurfaceBits(client->update->context, &client->update->surface_bits_command);
wfInfoSingleton->enc_data = FALSE;
wf_info_unlock();
ReleaseMutex(wfInfoSingleton->encodeMutex);
wfi->enc_data = FALSE;
wf_info_unlock(wfi);
ReleaseMutex(wfi->encodeMutex);
break;
case WAIT_TIMEOUT:

View File

@ -35,8 +35,6 @@ boolean wf_peer_activate(freerdp_peer* client);
void wf_peer_synchronize_event(rdpInput* input, uint32 flags);
void wf_peer_send_changes(rdpUpdate* update);
wfInfo* wfInfoSingleton;
void wf_peer_send_changes(freerdp_peer* client);
#endif /* WF_PEER_H */

View File

@ -93,7 +93,7 @@ static DWORD WINAPI wf_peer_main_loop(LPVOID lpParam)
#ifndef WITH_WIN8
if(client->activated)
wf_peer_send_changes(client->update);
wf_peer_send_changes(client);
#endif
}

View File

@ -30,14 +30,11 @@ struct wf_peer_context
{
rdpContext _p;
wfInfo* wfInfo;
STREAM* s;
wfInfo* info;
boolean activated;
RFX_CONTEXT* rfx_context;
STREAM* s;
};
typedef struct wf_peer_context wfPeerContext;
extern wfInfo * wfInfoSingleton;
#endif /* WFREERDP_H */