[LGMP] start of c-host conversion to use LGMP

This commit is contained in:
Geoffrey McRae 2020-01-09 15:42:32 +11:00
parent 8caa220ad5
commit 0b8f1a18b2
8 changed files with 231 additions and 300 deletions

3
.gitmodules vendored
View File

@ -0,0 +1,3 @@
[submodule "LGMP"]
path = LGMP
url = https://github.com/gnif/LGMP.git

1
LGMP Submodule

@ -0,0 +1 @@
Subproject commit 7e02dfd8ec53385cab76c819ab33a90911c6a271

View File

@ -1 +1 @@
B1-56-gb8203bec53+1 B1-57-g8caa220ad5+1

View File

@ -53,6 +53,7 @@ set(SOURCES
) )
add_subdirectory("${PROJECT_TOP}/common" "${CMAKE_BINARY_DIR}/common") add_subdirectory("${PROJECT_TOP}/common" "${CMAKE_BINARY_DIR}/common")
add_subdirectory("${PROJECT_TOP}/LGMP/lgmp" "${CMAKE_BINARY_DIR}/lgmp" )
add_subdirectory(platform) add_subdirectory(platform)
if(WIN32) if(WIN32)
@ -63,6 +64,7 @@ endif()
target_link_libraries(looking-glass-host target_link_libraries(looking-glass-host
lg_common lg_common
platform platform
lgmp
) )
set_target_properties(looking-glass-host PROPERTIES LINK_FLAGS "-Wl,--gc-sections") set_target_properties(looking-glass-host PROPERTIES LINK_FLAGS "-Wl,--gc-sections")
install(PROGRAMS ${CMAKE_BINARY_DIR}/looking-glass-host DESTINATION bin/ COMPONENT binary) install(PROGRAMS ${CMAKE_BINARY_DIR}/looking-glass-host DESTINATION bin/ COMPONENT binary)

View File

@ -71,21 +71,27 @@ typedef struct CapturePointer
} }
CapturePointer; CapturePointer;
typedef bool (*CaptureGetPointerBuffer )(void ** data, uint32_t * size);
typedef void (*CapturePostPointerBuffer)(CapturePointer pointer);
typedef struct CaptureInterface typedef struct CaptureInterface
{ {
const char * (*getName )(); const char * (*getName )();
void (*initOptions )(); void (*initOptions )();
bool (*create )(); bool(*create)(
bool (*init )(void * pointerShape, const unsigned int pointerSize); CaptureGetPointerBuffer getPointerBufferFn,
CapturePostPointerBuffer postPointerBufferFn
);
bool (*init )();
void (*stop )(); void (*stop )();
bool (*deinit )(); bool (*deinit )();
void (*free )(); void (*free )();
unsigned int (*getMaxFrameSize)(); unsigned int (*getMaxFrameSize)();
CaptureResult (*capture )(); CaptureResult (*capture )();
CaptureResult (*waitFrame )(CaptureFrame * frame ); CaptureResult (*waitFrame )(CaptureFrame * frame);
CaptureResult (*getFrame )(FrameBuffer frame ); CaptureResult (*getFrame )(FrameBuffer frame);
CaptureResult (*getPointer)(CapturePointer * pointer);
} }
CaptureInterface; CaptureInterface;

View File

@ -49,18 +49,6 @@ typedef struct Texture
} }
Texture; Texture;
typedef struct Pointer
{
unsigned int version;
unsigned int x, y;
unsigned int w, h;
bool visible;
unsigned int pitch;
CaptureFormat format;
}
Pointer;
// locals // locals
struct iface struct iface
{ {
@ -83,7 +71,9 @@ struct iface
int texWIndex; int texWIndex;
volatile int texReady; volatile int texReady;
bool needsRelease; bool needsRelease;
LGEvent * pointerEvent;
CaptureGetPointerBuffer getPointerBufferFn;
CapturePostPointerBuffer postPointerBufferFn;
LGEvent * frameEvent; LGEvent * frameEvent;
unsigned int width; unsigned int width;
@ -92,14 +82,8 @@ struct iface
unsigned int stride; unsigned int stride;
CaptureFormat format; CaptureFormat format;
// pointer state int lastPointerX, lastPointerY;
Pointer lastPointer; bool lastPointerVisible;
Pointer pointer;
// pointer shape
void * pointerShape;
unsigned int pointerSize;
unsigned int pointerUsed;
}; };
static bool dpiDone = false; static bool dpiDone = false;
@ -155,7 +139,7 @@ static void dxgi_initOptions()
option_register(options); option_register(options);
} }
static bool dxgi_create() static bool dxgi_create(CaptureGetPointerBuffer getPointerBufferFn, CapturePostPointerBuffer postPointerBufferFn)
{ {
assert(!this); assert(!this);
this = calloc(sizeof(struct iface), 1); this = calloc(sizeof(struct iface), 1);
@ -165,14 +149,6 @@ static bool dxgi_create()
return false; return false;
} }
this->pointerEvent = lgCreateEvent(true, 10);
if (!this->pointerEvent)
{
DEBUG_ERROR("failed to create the pointer event");
free(this);
return false;
}
this->frameEvent = lgCreateEvent(true, 17); // 60Hz = 16.7ms this->frameEvent = lgCreateEvent(true, 17); // 60Hz = 16.7ms
if (!this->frameEvent) if (!this->frameEvent)
{ {
@ -187,10 +163,12 @@ static bool dxgi_create()
this->useAcquireLock = option_get_bool("dxgi", "useAcquireLock"); this->useAcquireLock = option_get_bool("dxgi", "useAcquireLock");
this->texture = calloc(sizeof(struct Texture), this->maxTextures); this->texture = calloc(sizeof(struct Texture), this->maxTextures);
this->getPointerBufferFn = getPointerBufferFn;
this->postPointerBufferFn = postPointerBufferFn;
return true; return true;
} }
static bool dxgi_init(void * pointerShape, const unsigned int pointerSize) static bool dxgi_init()
{ {
assert(this); assert(this);
@ -213,17 +191,12 @@ static bool dxgi_init(void * pointerShape, const unsigned int pointerSize)
HRESULT status; HRESULT status;
DXGI_OUTPUT_DESC outputDesc; DXGI_OUTPUT_DESC outputDesc;
this->pointerShape = pointerShape;
this->pointerSize = pointerSize;
this->pointerUsed = 0;
this->stop = false; this->stop = false;
this->texRIndex = 0; this->texRIndex = 0;
this->texWIndex = 0; this->texWIndex = 0;
this->texReady = 0; this->texReady = 0;
lgResetEvent(this->frameEvent ); lgResetEvent(this->frameEvent );
lgResetEvent(this->pointerEvent);
status = CreateDXGIFactory1(&IID_IDXGIFactory1, (void **)&this->factory); status = CreateDXGIFactory1(&IID_IDXGIFactory1, (void **)&this->factory);
if (FAILED(status)) if (FAILED(status))
@ -549,7 +522,6 @@ fail:
static void dxgi_stop() static void dxgi_stop()
{ {
this->stop = true; this->stop = true;
lgSignalEvent(this->pointerEvent);
} }
static bool dxgi_deinit() static bool dxgi_deinit()
@ -627,7 +599,6 @@ static void dxgi_free()
if (this->initialized) if (this->initialized)
dxgi_deinit(); dxgi_deinit();
lgFreeEvent(this->pointerEvent);
free(this->texture); free(this->texture);
free(this); free(this);
@ -726,32 +697,37 @@ static CaptureResult dxgi_capture()
IDXGIResource_Release(res); IDXGIResource_Release(res);
// if the pointer has moved or changed state // if the pointer has moved or changed state
bool signalPointer = false; bool postPointer = false;
CapturePointer pointer = { 0 };
void * pointerShape = NULL;
UINT pointerShapeSize = 0;
if (frameInfo.LastMouseUpdateTime.QuadPart) if (frameInfo.LastMouseUpdateTime.QuadPart)
{ {
if ( if (
frameInfo.PointerPosition.Position.x != this->lastPointer.x || frameInfo.PointerPosition.Position.x != this->lastPointerX ||
frameInfo.PointerPosition.Position.y != this->lastPointer.y || frameInfo.PointerPosition.Position.y != this->lastPointerY ||
frameInfo.PointerPosition.Visible != this->lastPointer.visible frameInfo.PointerPosition.Visible != this->lastPointerVisible
) )
{ {
this->pointer.x = frameInfo.PointerPosition.Position.x; pointer.x = frameInfo.PointerPosition.Position.x;
this->pointer.y = frameInfo.PointerPosition.Position.y; pointer.y = frameInfo.PointerPosition.Position.y;
this->pointer.visible = frameInfo.PointerPosition.Visible; pointer.visible = frameInfo.PointerPosition.Visible;
signalPointer = true; postPointer = true;
} }
} }
// if the pointer shape has changed // if the pointer shape has changed
if (frameInfo.PointerShapeBufferSize > 0) if (frameInfo.PointerShapeBufferSize > 0)
{ {
// update the buffer uint32_t bufferSize;
if (frameInfo.PointerShapeBufferSize > this->pointerSize) if(!this->getPointerBufferFn(&pointerShape, &bufferSize))
DEBUG_WARN("The pointer shape is too large to fit in the buffer, ignoring the shape"); DEBUG_WARN("Failed to obtain a buffer for the pointer shape");
else else
{ {
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo; DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
LOCKED({status = IDXGIOutputDuplication_GetFramePointerShape(this->dup, this->pointerSize, this->pointerShape, &this->pointerUsed, &shapeInfo);});
LOCKED({status = IDXGIOutputDuplication_GetFramePointerShape(this->dup, bufferSize, pointerShape, &pointerShapeSize, &shapeInfo);});
if (FAILED(status)) if (FAILED(status))
{ {
DEBUG_WINERROR("Failed to get the new pointer shape", status); DEBUG_WINERROR("Failed to get the new pointer shape", status);
@ -760,25 +736,24 @@ static CaptureResult dxgi_capture()
switch(shapeInfo.Type) switch(shapeInfo.Type)
{ {
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR : this->pointer.format = CAPTURE_FMT_COLOR ; break; case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR : pointer.format = CAPTURE_FMT_COLOR ; break;
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR: this->pointer.format = CAPTURE_FMT_MASKED; break; case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR: pointer.format = CAPTURE_FMT_MASKED; break;
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME : this->pointer.format = CAPTURE_FMT_MONO ; break; case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME : pointer.format = CAPTURE_FMT_MONO ; break;
default: default:
DEBUG_ERROR("Unsupported cursor format"); DEBUG_ERROR("Unsupported cursor format");
return CAPTURE_RESULT_ERROR; return CAPTURE_RESULT_ERROR;
} }
this->pointer.w = shapeInfo.Width; pointer.width = shapeInfo.Width;
this->pointer.h = shapeInfo.Height; pointer.height = shapeInfo.Height;
this->pointer.pitch = shapeInfo.Pitch; pointer.pitch = shapeInfo.Pitch;
++this->pointer.version; postPointer = true;
signalPointer = true;
} }
} }
// signal about the pointer update // post back the pointer information
if (signalPointer) if (postPointer)
lgSignalEvent(this->pointerEvent); this->postPointerBufferFn(pointer);
return CAPTURE_RESULT_OK; return CAPTURE_RESULT_OK;
} }
@ -841,34 +816,6 @@ static CaptureResult dxgi_getFrame(FrameBuffer frame)
return CAPTURE_RESULT_OK; return CAPTURE_RESULT_OK;
} }
static CaptureResult dxgi_getPointer(CapturePointer * pointer)
{
assert(this);
assert(this->initialized);
if (!lgWaitEvent(this->pointerEvent, 1000))
return CAPTURE_RESULT_TIMEOUT;
if (this->stop)
return CAPTURE_RESULT_REINIT;
Pointer p;
memcpy(&p, &this->pointer, sizeof(Pointer));
pointer->x = p.x;
pointer->y = p.y;
pointer->width = p.w;
pointer->height = p.h;
pointer->pitch = p.pitch;
pointer->visible = p.visible;
pointer->format = p.format;
pointer->shapeUpdate = p.version > this->lastPointer.version;
memcpy(&this->lastPointer, &p, sizeof(Pointer));
return CAPTURE_RESULT_OK;
}
static CaptureResult dxgi_releaseFrame() static CaptureResult dxgi_releaseFrame()
{ {
assert(this); assert(this);
@ -914,6 +861,5 @@ struct CaptureInterface Capture_DXGI =
.getMaxFrameSize = dxgi_getMaxFrameSize, .getMaxFrameSize = dxgi_getMaxFrameSize,
.capture = dxgi_capture, .capture = dxgi_capture,
.waitFrame = dxgi_waitFrame, .waitFrame = dxgi_waitFrame,
.getFrame = dxgi_getFrame, .getFrame = dxgi_getFrame
.getPointer = dxgi_getPointer
}; };

View File

@ -28,6 +28,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "common/thread.h" #include "common/thread.h"
#include "common/ivshmem.h" #include "common/ivshmem.h"
#include <lgmp/host.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <unistd.h> #include <unistd.h>
@ -36,124 +38,51 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F) #define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F)
#define ALIGN_UP(x) ALIGN_DN(x + 0x7F) #define ALIGN_UP(x) ALIGN_DN(x + 0x7F)
#define MAX_FRAMES 2
#define LGMP_Q_POINTER 1
#define LGMP_Q_POINTER_LEN 10
#define LGMP_Q_FRAME 2
#define LGMP_Q_FRAME_LEN 2
#define MAX_POINTER_SIZE (sizeof(KVMFRCursor) + (128 * 128 * 4))
struct app struct app
{ {
unsigned int clientInstance; PLGMPHost lgmp;
KVMFRHeader * shmHeader; PLGMPHQueue pointerQueue;
uint8_t * pointerData; PLGMPMemory pointerMemory[LGMP_Q_POINTER_LEN];
unsigned int pointerDataSize; unsigned int pointerIndex;
unsigned int pointerOffset;
size_t maxFrameSize;
PLGMPHQueue frameQueue;
PLGMPMemory frameMemory[LGMP_Q_FRAME_LEN];
unsigned int frameIndex;
CaptureInterface * iface; CaptureInterface * iface;
uint8_t * frames;
unsigned int frameSize;
FrameBuffer frame[MAX_FRAMES];
unsigned int frameOffset[MAX_FRAMES];
bool running; bool running;
bool reinit; bool reinit;
LGThread * pointerThread; LGThread * lgmpThread;
LGThread * frameThread; LGThread * frameThread;
}; };
static struct app app; static struct app app;
static int pointerThread(void * opaque) static int lgmpThread(void * opaque)
{ {
DEBUG_INFO("Pointer thread started"); LGMP_STATUS status;
volatile KVMFRCursor * ci = &(app.shmHeader->cursor);
uint8_t flags;
bool pointerValid = false;
bool shapeValid = false;
unsigned int clientInstance = 0;
CapturePointer pointer = { 0 };
while(app.running) while(app.running)
{ {
bool resend = false; if ((status = lgmpHostProcess(app.lgmp)) != LGMP_OK)
pointer.shapeUpdate = false;
switch(app.iface->getPointer(&pointer))
{ {
case CAPTURE_RESULT_OK: DEBUG_ERROR("lgmpHostProcess Failed: %s", lgmpStatusString(status));
{
pointerValid = true;
break; break;
} }
case CAPTURE_RESULT_REINIT:
{
app.reinit = true;
DEBUG_INFO("Pointer thread reinit");
return 0;
}
case CAPTURE_RESULT_ERROR:
{
DEBUG_ERROR("Failed to get the pointer");
return 0;
}
case CAPTURE_RESULT_TIMEOUT:
{
// if the pointer is valid and the client has restarted, send it
if (pointerValid && clientInstance != app.clientInstance)
{
resend = true;
break;
}
continue;
}
}
clientInstance = app.clientInstance;
// wait for the client to finish with the previous update
while((ci->flags & ~KVMFR_CURSOR_FLAG_UPDATE) != 0 && app.running)
usleep(1000); usleep(1000);
flags = KVMFR_CURSOR_FLAG_UPDATE;
ci->x = pointer.x;
ci->y = pointer.y;
flags |= KVMFR_CURSOR_FLAG_POS;
if (pointer.visible)
flags |= KVMFR_CURSOR_FLAG_VISIBLE;
// if we have shape data
if (pointer.shapeUpdate || (shapeValid && resend))
{
switch(pointer.format)
{
case CAPTURE_FMT_COLOR : ci->type = CURSOR_TYPE_COLOR ; break;
case CAPTURE_FMT_MONO : ci->type = CURSOR_TYPE_MONOCHROME ; break;
case CAPTURE_FMT_MASKED: ci->type = CURSOR_TYPE_MASKED_COLOR; break;
default:
DEBUG_ERROR("Invalid pointer format: %d", pointer.format);
continue;
} }
ci->width = pointer.width; app.running = false;
ci->height = pointer.height;
ci->pitch = pointer.pitch;
ci->dataPos = app.pointerOffset;
++ci->version;
shapeValid = true;
flags |= KVMFR_CURSOR_FLAG_SHAPE;
}
// update the flags for the client
ci->flags = flags;
}
DEBUG_INFO("Pointer thread stopped");
return 0; return 0;
} }
@ -161,16 +90,23 @@ static int frameThread(void * opaque)
{ {
DEBUG_INFO("Frame thread started"); DEBUG_INFO("Frame thread started");
volatile KVMFRFrame * fi = &(app.shmHeader->frame);
bool frameValid = false; bool frameValid = false;
bool repeatFrame = false; bool repeatFrame = false;
int frameIndex = 0; int frameIndex = 0;
unsigned int clientInstance = 0;
CaptureFrame frame = { 0 }; CaptureFrame frame = { 0 };
(void)frameIndex;
(void)repeatFrame;
while(app.running) while(app.running)
{ {
//wait until there is room in the queue
if (lgmpHostQueuePending(app.frameQueue) == LGMP_Q_FRAME_LEN)
{
usleep(1);
continue;
}
switch(app.iface->waitFrame(&frame)) switch(app.iface->waitFrame(&frame))
{ {
case CAPTURE_RESULT_OK: case CAPTURE_RESULT_OK:
@ -192,7 +128,7 @@ static int frameThread(void * opaque)
case CAPTURE_RESULT_TIMEOUT: case CAPTURE_RESULT_TIMEOUT:
{ {
if (frameValid && clientInstance != app.clientInstance) if (frameValid && lgmpHostNewSubCount(app.frameQueue) > 0)
{ {
// resend the last frame // resend the last frame
repeatFrame = true; repeatFrame = true;
@ -203,16 +139,18 @@ static int frameThread(void * opaque)
} }
} }
clientInstance = app.clientInstance;
// wait for the client to finish with the previous frame
while(fi->flags & KVMFR_FRAME_FLAG_UPDATE && app.running)
usleep(1000);
if (repeatFrame) if (repeatFrame)
INTERLOCKED_OR8(&fi->flags, KVMFR_FRAME_FLAG_UPDATE);
else
{ {
lgmpHostPost(app.frameQueue, 0, app.frameMemory[app.frameIndex]);
continue;
}
// we increment the index first so that if we need to repeat a frame
// the index still points to the latest valid frame
if (frameIndex++ == LGMP_Q_FRAME_LEN)
frameIndex = 0;
KVMFRFrame * fi = lgmpHostMemPtr(app.frameMemory[app.frameIndex]);
switch(frame.format) switch(frame.format)
{ {
case CAPTURE_FMT_BGRA : fi->type = FRAME_TYPE_BGRA ; break; case CAPTURE_FMT_BGRA : fi->type = FRAME_TYPE_BGRA ; break;
@ -228,16 +166,13 @@ static int frameThread(void * opaque)
fi->height = frame.height; fi->height = frame.height;
fi->stride = frame.stride; fi->stride = frame.stride;
fi->pitch = frame.pitch; fi->pitch = frame.pitch;
fi->dataPos = app.frameOffset[frameIndex];
frameValid = true; frameValid = true;
framebuffer_prepare(app.frame[frameIndex]); FrameBuffer fb = (FrameBuffer)(fi + 1);
INTERLOCKED_OR8(&fi->flags, KVMFR_FRAME_FLAG_UPDATE); framebuffer_prepare(fb);
app.iface->getFrame(app.frame[frameIndex]); app.iface->getFrame(fb);
}
if (++frameIndex == MAX_FRAMES) lgmpHostPost(app.frameQueue, 0, app.frameMemory[app.frameIndex]);
frameIndex = 0;
} }
DEBUG_INFO("Frame thread stopped"); DEBUG_INFO("Frame thread stopped");
return 0; return 0;
@ -246,9 +181,9 @@ static int frameThread(void * opaque)
bool startThreads() bool startThreads()
{ {
app.running = true; app.running = true;
if (!lgCreateThread("CursorThread", pointerThread, NULL, &app.pointerThread)) if (!lgCreateThread("LGMPThread", lgmpThread, NULL, &app.lgmpThread))
{ {
DEBUG_ERROR("Failed to create the pointer thread"); DEBUG_ERROR("Failed to create the LGMP thread");
return false; return false;
} }
@ -275,12 +210,12 @@ bool stopThreads()
} }
app.frameThread = NULL; app.frameThread = NULL;
if (app.pointerThread && !lgJoinThread(app.pointerThread, NULL)) if (app.lgmpThread && !lgJoinThread(app.lgmpThread, NULL))
{ {
DEBUG_WARN("Failed to join the pointer thread"); DEBUG_WARN("Failed to join the LGMP thread");
ok = false; ok = false;
} }
app.pointerThread = NULL; app.lgmpThread = NULL;
return ok; return ok;
} }
@ -290,7 +225,7 @@ static bool captureStart()
DEBUG_INFO("Using : %s", app.iface->getName()); DEBUG_INFO("Using : %s", app.iface->getName());
const unsigned int maxFrameSize = app.iface->getMaxFrameSize(); const unsigned int maxFrameSize = app.iface->getMaxFrameSize();
if (maxFrameSize > app.frameSize) if (maxFrameSize > app.maxFrameSize)
{ {
DEBUG_ERROR("Maximum frame size of %d bytes excceds maximum space available", maxFrameSize); DEBUG_ERROR("Maximum frame size of %d bytes excceds maximum space available", maxFrameSize);
return false; return false;
@ -307,7 +242,7 @@ static bool captureRestart()
if (!stopThreads()) if (!stopThreads())
return false; return false;
if (!app.iface->deinit() || !app.iface->init(app.pointerData, app.pointerDataSize)) if (!app.iface->deinit() || !app.iface->init())
{ {
DEBUG_ERROR("Failed to reinitialize the capture device"); DEBUG_ERROR("Failed to reinitialize the capture device");
return false; return false;
@ -319,6 +254,57 @@ static bool captureRestart()
return true; return true;
} }
bool captureGetPointerBuffer(void ** data, uint32_t * size)
{
if (lgmpHostQueuePending(app.pointerQueue) == LGMP_Q_POINTER_LEN)
return false;
PLGMPMemory mem = app.pointerMemory[app.pointerIndex];
*data = ((uint8_t*)lgmpHostMemPtr(mem)) + sizeof(KVMFRCursor);
*size = MAX_POINTER_SIZE - sizeof(KVMFRCursor);
return true;
}
void capturePostPointerBuffer(CapturePointer pointer)
{
if (lgmpHostQueuePending(app.pointerQueue) == LGMP_Q_POINTER_LEN)
return;
PLGMPMemory mem = app.pointerMemory[app.pointerIndex];
KVMFRCursor *cursor = lgmpHostMemPtr(mem);
cursor->x = pointer.x;
cursor->y = pointer.y;
cursor->visible = pointer.visible;
if (pointer.shapeUpdate)
{
cursor->width = pointer.width;
cursor->height = pointer.height;
cursor->pitch = pointer.pitch;
switch(pointer.format)
{
case CAPTURE_FMT_COLOR : cursor->type = CURSOR_TYPE_COLOR ; break;
case CAPTURE_FMT_MONO : cursor->type = CURSOR_TYPE_MONOCHROME ; break;
case CAPTURE_FMT_MASKED: cursor->type = CURSOR_TYPE_MASKED_COLOR; break;
default:
DEBUG_ERROR("Invalid pointer type");
return;
}
}
LGMP_STATUS status;
if ((status = lgmpHostPost(app.pointerQueue, pointer.shapeUpdate ? 1 : 0, mem)) != LGMP_OK)
{
DEBUG_ERROR("lgmpHostPost Failed (Pointer): %s", lgmpStatusString(status));
return;
}
if (app.pointerIndex++ == LGMP_Q_POINTER_LEN)
app.pointerIndex = 0;
}
// this is called from the platform specific startup routine // this is called from the platform specific startup routine
int app_main(int argc, char * argv[]) int app_main(int argc, char * argv[])
{ {
@ -366,37 +352,60 @@ int app_main(int argc, char * argv[])
DEBUG_INFO("IVSHMEM Size : %u MiB", shmDev.size / 1048576); DEBUG_INFO("IVSHMEM Size : %u MiB", shmDev.size / 1048576);
DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmDev.mem); DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmDev.mem);
app.shmHeader = (KVMFRHeader *)shmDev.mem; LGMP_STATUS status;
app.pointerData = (uint8_t *)ALIGN_UP(shmDev.mem + sizeof(KVMFRHeader)); if ((status = lgmpHostInit(shmDev.mem, shmDev.size, &app.lgmp)) != LGMP_OK)
app.pointerDataSize = 1048576; // 1MB fixed for pointer size, should be more then enough
app.pointerOffset = app.pointerData - (uint8_t*)shmDev.mem;
app.frames = (uint8_t *)ALIGN_UP(app.pointerData + app.pointerDataSize);
app.frameSize = ALIGN_DN((shmDev.size - (app.frames - (uint8_t*)shmDev.mem)) / MAX_FRAMES);
DEBUG_INFO("Max Cursor Size : %u MiB", app.pointerDataSize / 1048576);
DEBUG_INFO("Max Frame Size : %u MiB", app.frameSize / 1048576);
DEBUG_INFO("Cursor : 0x%" PRIXPTR " (0x%08x)", (uintptr_t)app.pointerData, app.pointerOffset);
for (int i = 0; i < MAX_FRAMES; ++i)
{ {
app.frame [i] = (FrameBuffer)(app.frames + i * app.frameSize); DEBUG_ERROR("lgmpHostInit Failed: %s", lgmpStatusString(status));
app.frameOffset[i] = (uint8_t *)app.frame[i] - (uint8_t*)shmDev.mem; goto fail;
DEBUG_INFO("Frame %d : 0x%" PRIXPTR " (0x%08x)", i, (uintptr_t)app.frame[i], app.frameOffset[i]);
} }
if ((status = lgmpHostAddQueue(app.lgmp, LGMP_Q_POINTER, LGMP_Q_POINTER_LEN, &app.pointerQueue)) != LGMP_OK)
{
DEBUG_ERROR("lgmpHostQueueCreate Failed (Pointer): %s", lgmpStatusString(status));
goto fail;
}
for(int i = 0; i < LGMP_Q_POINTER_LEN; ++i)
{
if ((status = lgmpHostMemAlloc(app.lgmp, MAX_POINTER_SIZE, &app.pointerMemory[i])) != LGMP_OK)
{
DEBUG_ERROR("lgmpHostMemAlloc Failed (Pointer): %s", lgmpStatusString(status));
goto fail;
}
}
if ((status = lgmpHostAddQueue(app.lgmp, LGMP_Q_FRAME, LGMP_Q_FRAME_LEN, &app.frameQueue)) != LGMP_OK)
{
DEBUG_ERROR("lgmpHostQueueCreate Failed (Frame): %s", lgmpStatusString(status));
goto fail;
}
app.maxFrameSize = ALIGN_DN(lgmpHostMemAvail(app.lgmp) / LGMP_Q_FRAME_LEN);
for(int i = 0; i < LGMP_Q_FRAME_LEN; ++i)
{
if ((status = lgmpHostMemAlloc(app.lgmp, app.maxFrameSize, &app.frameMemory[i])) != LGMP_OK)
{
DEBUG_ERROR("lgmpHostMemAlloc Failed (Frame): %s", lgmpStatusString(status));
goto fail;
}
}
DEBUG_INFO("Max Pointer Size : %u KiB", (unsigned int)MAX_POINTER_SIZE);
DEBUG_INFO("Max Frame Size : %u MiB", (unsigned int)(app.maxFrameSize / 1048576LL));
CaptureInterface * iface = NULL; CaptureInterface * iface = NULL;
for(int i = 0; CaptureInterfaces[i]; ++i) for(int i = 0; CaptureInterfaces[i]; ++i)
{ {
iface = CaptureInterfaces[i]; iface = CaptureInterfaces[i];
DEBUG_INFO("Trying : %s", iface->getName()); DEBUG_INFO("Trying : %s", iface->getName());
if (!iface->create()) if (!iface->create(captureGetPointerBuffer, capturePostPointerBuffer))
{ {
iface = NULL; iface = NULL;
continue; continue;
} }
if (iface->init(app.pointerData, app.pointerDataSize)) if (iface->init())
break; break;
iface->free(); iface->free();
@ -412,31 +421,14 @@ int app_main(int argc, char * argv[])
app.iface = iface; app.iface = iface;
// initialize the shared memory headers
memcpy(app.shmHeader->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC));
app.shmHeader->version = KVMFR_HEADER_VERSION;
// zero and notify the client we are starting
memset(&(app.shmHeader->frame ), 0, sizeof(KVMFRFrame ));
memset(&(app.shmHeader->cursor), 0, sizeof(KVMFRCursor));
app.shmHeader->flags &= ~KVMFR_HEADER_FLAG_RESTART;
if (!captureStart()) if (!captureStart())
{ {
exitcode = -1; exitcode = -1;
goto exit; goto exit;
} }
volatile char * flags = (volatile char *)&(app.shmHeader->flags);
while(app.running) while(app.running)
{ {
if (INTERLOCKED_AND8(flags, ~(KVMFR_HEADER_FLAG_RESTART)) & KVMFR_HEADER_FLAG_RESTART)
{
DEBUG_INFO("Client restarted");
++app.clientInstance;
}
if (app.reinit && !captureRestart()) if (app.reinit && !captureRestart())
{ {
exitcode = -1; exitcode = -1;
@ -475,6 +467,13 @@ exit:
iface->deinit(); iface->deinit();
iface->free(); iface->free();
fail: fail:
for(int i = 0; i < LGMP_Q_POINTER_LEN; ++i)
lgmpHostMemFree(&app.pointerMemory[i]);
for(int i = 0; i < LGMP_Q_FRAME_LEN; ++i)
lgmpHostMemFree(&app.frameMemory[i]);
lgmpHostFree(&app.lgmp);
ivshmemClose(&shmDev); ivshmemClose(&shmDev);
return exitcode; return exitcode;
} }

View File

@ -42,49 +42,23 @@ typedef enum CursorType
} }
CursorType; CursorType;
#define KVMFR_CURSOR_FLAG_UPDATE 1 // cursor update available
#define KVMFR_CURSOR_FLAG_VISIBLE 2 // cursor is visible
#define KVMFR_CURSOR_FLAG_SHAPE 4 // shape updated
#define KVMFR_CURSOR_FLAG_POS 8 // position updated
typedef struct KVMFRCursor typedef struct KVMFRCursor
{ {
volatile uint8_t flags; // KVMFR_CURSOR_FLAGS
int16_t x, y; // cursor x & y position int16_t x, y; // cursor x & y position
bool visible; // cursor visible
uint32_t version; // shape version
CursorType type; // shape buffer data type CursorType type; // shape buffer data type
uint32_t width; // width of the shape uint32_t width; // width of the shape
uint32_t height; // height of the shape uint32_t height; // height of the shape
uint32_t pitch; // row length in bytes of the shape uint32_t pitch; // row length in bytes of the shape
uint64_t dataPos; // offset to the shape data
} }
KVMFRCursor; KVMFRCursor;
#define KVMFR_FRAME_FLAG_UPDATE 1 // frame update available
typedef struct KVMFRFrame typedef struct KVMFRFrame
{ {
volatile uint8_t flags; // KVMFR_FRAME_FLAGS
FrameType type; // the frame data type FrameType type; // the frame data type
uint32_t width; // the width uint32_t width; // the width
uint32_t height; // the height uint32_t height; // the height
uint32_t stride; // the row stride (zero if compressed data) uint32_t stride; // the row stride (zero if compressed data)
uint32_t pitch; // the row pitch (stride in bytes or the compressed frame size) uint32_t pitch; // the row pitch (stride in bytes or the compressed frame size)
uint64_t dataPos; // offset to the frame
} }
KVMFRFrame; KVMFRFrame;
#define KVMFR_HEADER_FLAG_RESTART 1 // restart signal from client
#define KVMFR_HEADER_FLAG_READY 2 // ready signal from client
#define KVMFR_HEADER_FLAG_PAUSED 4 // capture has been paused by the host
typedef struct KVMFRHeader
{
char magic[sizeof(KVMFR_HEADER_MAGIC)];
uint32_t version; // version of this structure
volatile uint8_t flags; // KVMFR_HEADER_FLAGS
KVMFRFrame frame; // the frame information
KVMFRCursor cursor; // the cursor information
}
KVMFRHeader;