nuttx/graphics/vnc/server/vnc_updater.c

570 lines
16 KiB
C
Raw Normal View History

/****************************************************************************
* graphics/vnc/vnc_updater.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name NuttX 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 AND CONTRIBUTORS
* "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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
2016-04-18 00:48:30 +02:00
#include <semaphore.h>
#include <sched.h>
#include <pthread.h>
2016-04-18 00:48:30 +02:00
#include <queue.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/semaphore.h>
#include "vnc_server.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#undef VNCSERVER_SEM_DEBUG
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef VNCSERVER_SEM_DEBUG
static sem_t g_dbgsem = SEM_INITIALIZER(1);
#endif
/* A rectangle represent the entire local framebuffer */
static const struct nxgl_rect_s g_wholescreen =
{
{
0,
0
},
{
CONFIG_VNCSERVER_SCREENWIDTH - 1,
CONFIG_VNCSERVER_SCREENHEIGHT - 1
}
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: vnc_sem_debug
*
* Description:
* Dump information about the freesem to verify that it is sync.
*
* Input Parameters:
* session - A reference to the VNC session structure.
*
* Returned Value:
* A non-NULL structure pointer should always be returned. This function
* will wait if no structure is available.
*
****************************************************************************/
#ifdef VNCSERVER_SEM_DEBUG
static void vnc_sem_debug(FAR struct vnc_session_s *session,
FAR const char *msg, unsigned int unattached)
{
FAR struct vnc_fbupdate_s *update;
unsigned int nqueued;
unsigned int nfree;
while (sem_wait(&g_dbgsem) < 0)
{
DEBUGASSERT(get_errno() == EINTR);
}
/* Count structures in the list */
for (nqueued = 0, update = (FAR struct vnc_fbupdate_s *)session->updqueue.head;
update != NULL;
nqueued++, update = update->flink);
for (nfree = 0, update = (FAR struct vnc_fbupdate_s *)session->updfree.head;
update != NULL;
nfree++, update = update->flink);
syslog(LOG_INFO, "FREESEM DEBUG: %s\n", msg);
syslog(LOG_INFO, " freesem: %d\n", session->freesem.semcount);
syslog(LOG_INFO, " queued: %u\n", nqueued);
syslog(LOG_INFO, " free: %u\n", nfree);
syslog(LOG_INFO, " unattached: %u\n", unattached);
sem_post(&g_dbgsem);
}
#else
# define vnc_sem_debug(s,m,u)
#endif
2016-04-18 00:48:30 +02:00
/****************************************************************************
* Name: vnc_alloc_update
*
* Description:
* Allocate one update structure by taking it from the freelist.
2016-04-18 00:48:30 +02:00
*
* Input Parameters:
* session - A reference to the VNC session structure.
*
* Returned Value:
* A non-NULL structure pointer should always be returned. This function
* will wait if no structure is available.
*
****************************************************************************/
static FAR struct vnc_fbupdate_s *
vnc_alloc_update(FAR struct vnc_session_s *session)
{
FAR struct vnc_fbupdate_s *update;
/* Reserve one element from the free list. Lock the scheduler to assure
2016-04-18 02:09:04 +02:00
* that the sq_remfirst() and the successful return from sem_wait are
2016-04-18 00:48:30 +02:00
* atomic. Of course, the scheduler will be unlocked while we wait.
*/
sched_lock();
vnc_sem_debug(session, "Before alloc", 0);
2016-04-18 02:09:04 +02:00
while (sem_wait(&session->freesem) < 0)
2016-04-18 00:48:30 +02:00
{
DEBUGASSERT(get_errno() == EINTR);
}
/* It is reserved.. go get it */
update = (FAR struct vnc_fbupdate_s *)sq_remfirst(&session->updfree);
vnc_sem_debug(session, "After alloc", 1);
2016-04-18 00:48:30 +02:00
sched_unlock();
DEBUGASSERT(update != NULL);
return update;
}
/****************************************************************************
* Name: vnc_free_update
*
* Description:
* Free one update structure by returning it from the freelist.
*
* Input Parameters:
2016-04-18 02:09:04 +02:00
* session - A reference to the VNC session structure.
2016-04-18 00:48:30 +02:00
*
* Returned Value:
2016-04-18 02:09:04 +02:00
* None
2016-04-18 00:48:30 +02:00
*
****************************************************************************/
static void vnc_free_update(FAR struct vnc_session_s *session,
FAR struct vnc_fbupdate_s *update)
{
/* Reserve one element from the free list. Lock the scheduler to assure
* that the sq_addlast() and the sem_post() are atomic.
*/
sched_lock();
vnc_sem_debug(session, "Before free", 1);
2016-04-18 00:48:30 +02:00
/* Put the entry into the free list */
sq_addlast((FAR sq_entry_t *)update, &session->updfree);
/* Post the semaphore to indicate the availability of one more update */
2016-04-18 02:09:04 +02:00
sem_post(&session->freesem);
vnc_sem_debug(session, "After free", 0);
2016-04-18 02:09:04 +02:00
DEBUGASSERT(session->freesem.semcount <= CONFIG_VNCSERVER_NUPDATES);
sched_unlock();
}
/****************************************************************************
* Name: vnc_remove_queue
*
* Description:
* Remove one entry from the list of queued rectangles, waiting if
* necessary if the queue is empty.
*
* Input Parameters:
* session - A reference to the VNC session structure.
*
* Returned Value:
* A non-NULL structure pointer should always be returned. This function
* will wait if no structure is available.
*
****************************************************************************/
static FAR struct vnc_fbupdate_s *
vnc_remove_queue(FAR struct vnc_session_s *session)
{
FAR struct vnc_fbupdate_s *rect;
/* Reserve one element from the list of queued rectangle. Lock the
* scheduler to assure that the sq_remfirst() and the successful return
* from sem_wait are atomic. Of course, the scheduler will be unlocked
* while we wait.
*/
sched_lock();
vnc_sem_debug(session, "Before remove", 0);
2016-04-18 02:09:04 +02:00
while (sem_wait(&session->queuesem) < 0)
{
DEBUGASSERT(get_errno() == EINTR);
}
/* It is reserved.. go get it */
rect = (FAR struct vnc_fbupdate_s *)sq_remfirst(&session->updqueue);
DEBUGASSERT(rect != NULL);
/* Check if we just removed the whole screen update from the queue */
if (session->nwhupd > 0 && rect->whupd)
{
session->nwhupd--;
updvdbg("Whole screen update: nwhupd=%d\n", session->nwhupd);
}
vnc_sem_debug(session, "After remove", 0);
2016-04-18 02:09:04 +02:00
sched_unlock();
return rect;
}
/****************************************************************************
* Name: vnc_add_queue
*
* Description:
* Add one rectangle entry to the list of queued rectangles to be updated.
*
* Input Parameters:
2016-04-18 18:42:44 +02:00
* session - A reference to the VNC session structure.
* rect - The rectangle to be added to the queue.
2016-04-18 02:09:04 +02:00
*
* Returned Value:
* None
*
****************************************************************************/
static void vnc_add_queue(FAR struct vnc_session_s *session,
2016-04-18 18:42:44 +02:00
FAR struct vnc_fbupdate_s *rect)
2016-04-18 02:09:04 +02:00
{
/* Lock the scheduler to assure that the sq_addlast() and the sem_post()
* are atomic.
*/
sched_lock();
vnc_sem_debug(session, "Before add", 1);
2016-04-18 02:09:04 +02:00
/* Put the entry into the list of queued rectangles. */
sq_addlast((FAR sq_entry_t *)rect, &session->updqueue);
/* Post the semaphore to indicate the availability of one more rectangle
* in the queue. This may wakeup the updater.
*/
sem_post(&session->queuesem);
vnc_sem_debug(session, "After add", 0);
2016-04-18 02:09:04 +02:00
DEBUGASSERT(session->queuesem.semcount <= CONFIG_VNCSERVER_NUPDATES);
2016-04-18 00:48:30 +02:00
sched_unlock();
}
/****************************************************************************
* Name: vnc_updater
*
* Description:
* This is the "updater" thread. It is the sender of all Server-to-Client
* messages
*
* Input Parameters:
* Standard pthread arguments.
*
* Returned Value:
* NULL is always returned.
*
****************************************************************************/
static FAR void *vnc_updater(FAR void *arg)
{
FAR struct vnc_session_s *session = (FAR struct vnc_session_s *)arg;
2016-04-18 02:09:04 +02:00
FAR struct vnc_fbupdate_s *srcrect;
int ret;
2016-04-18 18:42:44 +02:00
DEBUGASSERT(session != NULL);
gvdbg("Updater running for Display %d\n", session->display);
/* Loop, processing updates until we are asked to stop.
2016-04-18 18:42:44 +02:00
* REVISIT: Probably need some kind of signal mechanism to wake up
* vnc_remove_queue() in order to stop. Or perhaps a special STOP
* message in the queue?
*/
while (session->state == VNCSERVER_RUNNING)
{
2016-04-18 02:09:04 +02:00
/* Get the next queued rectangle update. This call will block until an
* upate is available for the case where the update queue is empty.
*/
srcrect = vnc_remove_queue(session);
DEBUGASSERT(srcrect != NULL);
updvdbg("Dequeued {(%d, %d),(%d, %d)}\n",
srcrect->rect.pt1.x, srcrect->rect.pt1.y,
srcrect->rect.pt2.x, srcrect->rect.pt2.y);
2016-04-21 01:01:48 +02:00
/* Attempt to use RRE encoding */
2016-04-18 02:09:04 +02:00
2016-04-21 01:01:48 +02:00
ret = vnc_rre(session, &srcrect->rect);
if (ret == 0)
{
/* Perform the framebuffer update using the default RAW encoding */
ret = vnc_raw(session, &srcrect->rect);
}
2016-04-18 02:09:04 +02:00
/* Release the update structure */
2016-04-18 02:09:04 +02:00
vnc_free_update(session, srcrect);
2016-04-18 02:09:04 +02:00
/* Break out and terminate the server if the encoding failed */
2016-04-18 02:09:04 +02:00
if (ret < 0)
{
gdbg("ERROR: Encoding failed: %d\n", ret);
break;
2016-04-18 02:09:04 +02:00
}
}
2016-04-18 18:42:44 +02:00
session->state = VNCSERVER_STOPPED;
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: vnc_start_updater
*
* Description:
* Start the updater thread
*
* Input Parameters:
* session - An instance of the session structure.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int vnc_start_updater(FAR struct vnc_session_s *session)
{
pthread_attr_t attr;
struct sched_param param;
int status;
gvdbg("Starting updater for Display %d\n", session->display);
/* Create thread that is gonna send rectangles to the client */
session->state = VNCSERVER_RUNNING;
DEBUGVERIFY(pthread_attr_init(&attr));
DEBUGVERIFY(pthread_attr_setstacksize(&attr, CONFIG_VNCSERVER_UPDATER_STACKSIZE));
param.sched_priority = CONFIG_VNCSERVER_UPDATER_PRIO;
DEBUGVERIFY(pthread_attr_setschedparam(&attr, &param));
status = pthread_create(&session->updater, &attr, vnc_updater,
(pthread_addr_t)session);
if (status != 0)
{
return -status;
}
return OK;
}
/****************************************************************************
* Name: vnc_stop_updater
*
* Description:
* Stop the updater thread
*
* Input Parameters:
* session - An instance of the session structure.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int vnc_stop_updater(FAR struct vnc_session_s *session)
{
pthread_addr_t result;
int status;
/* Is the update thread running? */
if (session->state == VNCSERVER_RUNNING)
{
/* Yes.. ask it to please stop */
session->state = VNCSERVER_STOPPING;
/* Wait for the thread to comply with our request */
status = pthread_join(session->updater, &result);
if (status != 0)
{
return -status;
}
/* Return what the thread returned */
return (int)((intptr_t)result);
}
/* Not running? Just say we stopped the thread successfully. */
return OK;
}
/****************************************************************************
* Name: vnc_update_rectangle
*
* Description:
* Queue an update of the specified rectangular region on the display.
*
* Input Parameters:
* session - An instance of the session structure.
* rect - The rectanglular region to be updated.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int vnc_update_rectangle(FAR struct vnc_session_s *session,
FAR const struct nxgl_rect_s *rect)
{
2016-04-18 00:48:30 +02:00
FAR struct vnc_fbupdate_s *update;
struct nxgl_rect_s intersection;
bool whupd;
/* Clip rectangle to the screen dimensions */
nxgl_rectintersect(&intersection, rect, &g_wholescreen);
2016-04-18 00:48:30 +02:00
/* Make sure that the clipped rectangle has a area */
if (!nxgl_nullrect(&intersection))
2016-04-18 00:48:30 +02:00
{
/* Check for a whole screen update. The RealVNC client sends a lot
* of these (especially when it is confused)
*/
whupd = (memcmp(&intersection, &g_wholescreen,
sizeof(struct nxgl_rect_s)) == 0);
/* Ignore all updates if there is a queue whole screen update */
sched_lock();
if (session->nwhupd == 0)
{
/* Is this a new whole screen update */
if (whupd)
{
FAR struct vnc_fbupdate_s *curr;
FAR struct vnc_fbupdate_s *next;
/* Yes.. discard all of the previously queued updates */
updvdbg("New whole screen update...\n");
curr = (FAR struct vnc_fbupdate_s *)session->updqueue.head;
sq_init(&session->updqueue);
sem_reset(&session->queuesem, 0);
for (; curr != NULL; curr = next)
{
next = curr->flink;
vnc_free_update(session, curr);
}
session->nwhupd = 1;
}
/* Allocate an update structure... waiting if necessary */
2016-04-18 00:48:30 +02:00
update = vnc_alloc_update(session);
DEBUGASSERT(update != NULL);
2016-04-18 00:48:30 +02:00
/* Copy the clipped rectangle into the update structure */
2016-04-18 00:48:30 +02:00
update->whupd = whupd;
nxgl_rectcopy(&update->rect, &intersection);
2016-04-18 00:48:30 +02:00
/* Add the upate to the end of the update queue. */
2016-04-21 01:01:48 +02:00
vnc_add_queue(session, update);
updvdbg("Queued {(%d, %d),(%d, %d)}\n",
intersection.pt1.x, intersection.pt1.y,
intersection.pt2.x, intersection.pt2.y);
}
2016-04-18 00:48:30 +02:00
sched_unlock();
2016-04-18 00:48:30 +02:00
}
/* Since we ignore bad rectangles and wait for updata structures, there is
* really no way a failure can occur.
*/
return OK;
}