2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2020-08-09 02:29:52 +00:00
|
|
|
#define _GNU_SOURCE //needed for pthread_setname_np
|
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
#include <obs/obs-config.h>
|
2020-01-10 07:14:08 +00:00
|
|
|
#include <obs/obs-module.h>
|
2020-04-24 11:30:28 +00:00
|
|
|
#include <obs/util/threading.h>
|
2021-08-13 09:41:48 +00:00
|
|
|
#include <obs/graphics/graphics.h>
|
2020-10-10 13:39:31 +00:00
|
|
|
#include <obs/graphics/matrix4.h>
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
#include <common/array.h>
|
2020-01-10 07:14:08 +00:00
|
|
|
#include <common/ivshmem.h>
|
|
|
|
#include <common/KVMFR.h>
|
|
|
|
#include <common/framebuffer.h>
|
|
|
|
#include <lgmp/client.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-04-24 11:30:28 +00:00
|
|
|
#include <unistd.h>
|
2020-10-10 13:39:31 +00:00
|
|
|
#include <stdatomic.h>
|
2020-10-11 01:04:10 +00:00
|
|
|
#include <GL/gl.h>
|
2020-04-24 11:30:28 +00:00
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
/**
|
|
|
|
* the following comes from drm_fourcc.h and is included here to avoid the
|
|
|
|
* external dependency for the few simple defines we need
|
|
|
|
*/
|
|
|
|
#define fourcc_code(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | \
|
|
|
|
((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
|
|
|
|
#define DRM_FORMAT_ARGB8888 fourcc_code('A', 'R', '2', '4')
|
|
|
|
#define DRM_FORMAT_ABGR8888 fourcc_code('A', 'B', '2', '4')
|
|
|
|
#define DRM_FORMAT_BGRA1010102 fourcc_code('B', 'A', '3', '0')
|
|
|
|
#define DRM_FORMAT_ABGR16161616F fourcc_code('A', 'B', '4', 'H')
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
STATE_STOPPED,
|
|
|
|
STATE_OPEN,
|
|
|
|
STATE_STARTING,
|
|
|
|
STATE_RUNNING,
|
2022-08-08 23:11:33 +00:00
|
|
|
STATE_STOPPING,
|
|
|
|
STATE_RESTARTING
|
2020-04-24 11:30:28 +00:00
|
|
|
}
|
|
|
|
LGState;
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
KVMFRFrame * frame;
|
|
|
|
size_t dataSize;
|
|
|
|
int fd;
|
|
|
|
}
|
|
|
|
DMAFrameInfo;
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
obs_source_t * context;
|
2020-04-24 11:30:28 +00:00
|
|
|
LGState state;
|
2020-01-10 07:14:08 +00:00
|
|
|
char * shmFile;
|
2020-10-12 07:52:37 +00:00
|
|
|
uint32_t formatVer;
|
2022-05-01 09:41:46 +00:00
|
|
|
uint32_t screenWidth, screenHeight;
|
|
|
|
uint32_t frameWidth, frameHeight;
|
|
|
|
struct vec2 screenScale;
|
2020-01-10 07:14:08 +00:00
|
|
|
FrameType type;
|
2020-10-11 09:42:09 +00:00
|
|
|
int bpp;
|
2020-01-10 07:14:08 +00:00
|
|
|
struct IVSHMEM shmDev;
|
|
|
|
PLGMPClient lgmp;
|
2020-10-10 13:39:31 +00:00
|
|
|
PLGMPClientQueue frameQueue, pointerQueue;
|
2020-01-10 07:14:08 +00:00
|
|
|
gs_texture_t * texture;
|
2020-05-20 21:31:12 +00:00
|
|
|
uint8_t * texData;
|
|
|
|
uint32_t linesize;
|
2020-04-24 11:30:28 +00:00
|
|
|
|
2022-06-29 21:47:00 +00:00
|
|
|
bool hideMouse;
|
2021-08-13 09:41:48 +00:00
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
bool dmabuf;
|
|
|
|
DMAFrameInfo dmaInfo[LGMP_Q_FRAME_LEN];
|
|
|
|
#endif
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
pthread_t frameThread, pointerThread;
|
2020-04-24 11:30:28 +00:00
|
|
|
os_sem_t * frameSem;
|
2020-10-10 13:39:31 +00:00
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
bool cursorMono;
|
2020-10-10 13:39:31 +00:00
|
|
|
gs_texture_t * cursorTex;
|
2020-10-11 01:04:10 +00:00
|
|
|
struct gs_rect cursorRect;
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
bool cursorVisible;
|
2020-10-11 01:04:10 +00:00
|
|
|
KVMFRCursor cursor;
|
2020-10-10 13:39:31 +00:00
|
|
|
os_sem_t * cursorSem;
|
|
|
|
atomic_uint cursorVer;
|
|
|
|
unsigned int cursorCurVer;
|
|
|
|
uint32_t cursorSize;
|
2020-10-11 01:04:10 +00:00
|
|
|
uint32_t * cursorData;
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
LGPlugin;
|
|
|
|
|
2022-08-08 23:11:33 +00:00
|
|
|
static void * frameThread(void * data);
|
|
|
|
static void * pointerThread(void * data);
|
2020-01-10 07:14:08 +00:00
|
|
|
static void lgUpdate(void * data, obs_data_t * settings);
|
|
|
|
|
|
|
|
static const char * lgGetName(void * unused)
|
|
|
|
{
|
|
|
|
return obs_module_text("Looking Glass Client");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * lgCreate(obs_data_t * settings, obs_source_t * context)
|
|
|
|
{
|
|
|
|
LGPlugin * this = bzalloc(sizeof(LGPlugin));
|
|
|
|
this->context = context;
|
2020-10-10 13:39:31 +00:00
|
|
|
os_sem_init (&this->frameSem , 0);
|
|
|
|
os_sem_init (&this->cursorSem, 1);
|
|
|
|
atomic_store(&this->cursorVer, 0);
|
2020-01-10 07:14:08 +00:00
|
|
|
lgUpdate(this, settings);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-08-08 23:11:33 +00:00
|
|
|
static void createThreads(LGPlugin * this)
|
|
|
|
{
|
|
|
|
pthread_create(&this->frameThread, NULL, frameThread, this);
|
|
|
|
pthread_setname_np(this->frameThread, "LGFrameThread");
|
|
|
|
pthread_create(&this->pointerThread, NULL, pointerThread, this);
|
|
|
|
pthread_setname_np(this->pointerThread, "LGPointerThread");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void waitThreads(LGPlugin * this)
|
|
|
|
{
|
|
|
|
pthread_join(this->frameThread, NULL);
|
|
|
|
pthread_join(this->pointerThread, NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
static void deinit(LGPlugin * this)
|
|
|
|
{
|
2020-04-24 11:30:28 +00:00
|
|
|
switch(this->state)
|
|
|
|
{
|
|
|
|
case STATE_STARTING:
|
|
|
|
/* wait for startup to finish */
|
|
|
|
while(this->state == STATE_STARTING)
|
|
|
|
usleep(1);
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case STATE_RUNNING:
|
|
|
|
case STATE_STOPPING:
|
2022-08-08 23:11:33 +00:00
|
|
|
case STATE_RESTARTING:
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_STOPPING;
|
2022-10-21 03:01:27 +00:00
|
|
|
waitThreads(this);
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_STOPPED;
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case STATE_OPEN:
|
2021-08-13 09:41:48 +00:00
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
for (int i = 0 ; i < ARRAY_LENGTH(this->dmaInfo); ++i)
|
|
|
|
if (this->dmaInfo[i].fd >= 0)
|
|
|
|
{
|
|
|
|
close(this->dmaInfo[i].fd);
|
|
|
|
this->dmaInfo[i].fd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
lgmpClientFree(&this->lgmp);
|
|
|
|
ivshmemClose(&this->shmDev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_STOPPED:
|
|
|
|
break;
|
|
|
|
}
|
2020-01-10 07:14:08 +00:00
|
|
|
|
|
|
|
if (this->shmFile)
|
|
|
|
{
|
|
|
|
bfree(this->shmFile);
|
|
|
|
this->shmFile = NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
if (this->texture)
|
|
|
|
{
|
|
|
|
obs_enter_graphics();
|
|
|
|
gs_texture_destroy(this->texture);
|
2020-05-20 21:31:12 +00:00
|
|
|
gs_texture_unmap(this->texture);
|
2020-04-24 11:30:28 +00:00
|
|
|
obs_leave_graphics();
|
2020-05-20 21:31:12 +00:00
|
|
|
this->texture = NULL;
|
2020-04-24 11:30:28 +00:00
|
|
|
}
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
if (this->cursorTex)
|
|
|
|
{
|
|
|
|
obs_enter_graphics();
|
|
|
|
gs_texture_destroy(this->cursorTex);
|
|
|
|
gs_texture_unmap(this->cursorTex);
|
|
|
|
obs_leave_graphics();
|
|
|
|
this->cursorTex = NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_STOPPED;
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lgDestroy(void * data)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
deinit(this);
|
2020-10-10 13:39:31 +00:00
|
|
|
os_sem_destroy(this->frameSem );
|
|
|
|
os_sem_destroy(this->cursorSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
bfree(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lgGetDefaults(obs_data_t * defaults)
|
|
|
|
{
|
|
|
|
obs_data_set_default_string(defaults, "shmFile", "/dev/shm/looking-glass");
|
|
|
|
}
|
|
|
|
|
|
|
|
static obs_properties_t * lgGetProperties(void * data)
|
|
|
|
{
|
|
|
|
obs_properties_t * props = obs_properties_create();
|
|
|
|
|
|
|
|
obs_properties_add_text(props, "shmFile", obs_module_text("SHM File"), OBS_TEXT_DEFAULT);
|
2022-06-29 21:47:00 +00:00
|
|
|
obs_properties_add_bool(props, "hideMouse", obs_module_text("Hide mouse cursor"));
|
2021-08-13 09:41:48 +00:00
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
obs_properties_add_bool(props, "dmabuf", obs_module_text("Use DMABUF import (requires kvmfr device)"));
|
2021-08-15 06:02:02 +00:00
|
|
|
#else
|
|
|
|
obs_property_t * dmabuf = obs_properties_add_bool(props, "dmabuf",
|
|
|
|
obs_module_text("Use DMABUF import (requires OBS 27+ and kvmfr device)"));
|
|
|
|
obs_property_set_enabled(dmabuf, false);
|
2021-08-13 09:41:48 +00:00
|
|
|
#endif
|
2020-01-10 07:14:08 +00:00
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
static void * frameThread(void * data)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
|
|
|
|
if (lgmpClientSubscribe(this->lgmp, LGMP_Q_FRAME, &this->frameQueue) != LGMP_OK)
|
|
|
|
{
|
|
|
|
this->state = STATE_STOPPING;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->state = STATE_RUNNING;
|
|
|
|
os_sem_post(this->frameSem);
|
|
|
|
|
|
|
|
while(this->state == STATE_RUNNING)
|
|
|
|
{
|
|
|
|
LGMP_STATUS status;
|
|
|
|
|
|
|
|
os_sem_wait(this->frameSem);
|
|
|
|
if ((status = lgmpClientAdvanceToLast(this->frameQueue)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
if (status != LGMP_ERR_QUEUE_EMPTY)
|
|
|
|
{
|
|
|
|
os_sem_post(this->frameSem);
|
2020-05-20 21:31:12 +00:00
|
|
|
printf("lgmpClientAdvanceToLast: %s\n", lgmpStatusString(status));
|
2020-04-24 11:30:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os_sem_post(this->frameSem);
|
2020-05-20 21:31:12 +00:00
|
|
|
usleep(1000);
|
2020-04-24 11:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lgmpClientUnsubscribe(&this->frameQueue);
|
2022-08-08 23:11:33 +00:00
|
|
|
this->state = STATE_RESTARTING;
|
2020-04-24 11:30:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
inline static void allocCursorData(LGPlugin * this, const unsigned int size)
|
|
|
|
{
|
|
|
|
if (this->cursorSize >= size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bfree(this->cursorData);
|
|
|
|
this->cursorSize = size;
|
|
|
|
this->cursorData = bmalloc(size);
|
|
|
|
}
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
static void * pointerThread(void * data)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
|
|
|
|
if (lgmpClientSubscribe(this->lgmp, LGMP_Q_POINTER, &this->pointerQueue) != LGMP_OK)
|
|
|
|
{
|
|
|
|
this->state = STATE_STOPPING;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(this->state == STATE_RUNNING)
|
|
|
|
{
|
|
|
|
LGMP_STATUS status;
|
|
|
|
LGMPMessage msg;
|
|
|
|
|
|
|
|
if ((status = lgmpClientProcess(this->pointerQueue, &msg)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
if (status != LGMP_ERR_QUEUE_EMPTY)
|
|
|
|
{
|
|
|
|
printf("lgmpClientProcess: %s\n", lgmpStatusString(status));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
const KVMFRCursor * const cursor = (const KVMFRCursor * const)msg.mem;
|
2022-06-29 21:47:00 +00:00
|
|
|
this->cursorVisible = this->hideMouse ?
|
|
|
|
0 : msg.udata & CURSOR_FLAG_VISIBLE;
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
if (msg.udata & CURSOR_FLAG_SHAPE)
|
|
|
|
{
|
|
|
|
os_sem_wait(this->cursorSem);
|
2020-10-11 01:04:10 +00:00
|
|
|
const uint8_t * const data = (const uint8_t * const)(cursor + 1);
|
2020-10-10 13:39:31 +00:00
|
|
|
unsigned int dataSize = 0;
|
|
|
|
|
|
|
|
switch(cursor->type)
|
|
|
|
{
|
|
|
|
case CURSOR_TYPE_MASKED_COLOR:
|
2020-10-11 01:04:10 +00:00
|
|
|
{
|
|
|
|
dataSize = cursor->height * cursor->pitch;
|
|
|
|
allocCursorData(this, dataSize);
|
|
|
|
|
|
|
|
const uint32_t * s = (const uint32_t *)data;
|
|
|
|
uint32_t * d = this->cursorData;
|
2021-05-31 23:13:36 +00:00
|
|
|
for(int i = 0; i < dataSize / sizeof(uint32_t); ++i, ++s, ++d)
|
2020-10-11 01:04:10 +00:00
|
|
|
*d = (*s & ~0xFF000000) | (*s & 0xFF000000 ? 0x0 : 0xFF000000);
|
|
|
|
break;
|
|
|
|
}
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
case CURSOR_TYPE_COLOR:
|
2020-10-11 01:04:10 +00:00
|
|
|
{
|
2020-10-10 13:39:31 +00:00
|
|
|
dataSize = cursor->height * cursor->pitch;
|
2020-10-11 01:04:10 +00:00
|
|
|
allocCursorData(this, dataSize);
|
|
|
|
memcpy(this->cursorData, data, dataSize);
|
2020-10-10 13:39:31 +00:00
|
|
|
break;
|
2020-10-11 01:04:10 +00:00
|
|
|
}
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
case CURSOR_TYPE_MONOCHROME:
|
2020-10-11 01:04:10 +00:00
|
|
|
{
|
2021-05-22 23:28:40 +00:00
|
|
|
dataSize = cursor->height * cursor->width * sizeof(uint32_t);
|
2020-10-11 01:04:10 +00:00
|
|
|
allocCursorData(this, dataSize);
|
|
|
|
|
|
|
|
const int hheight = cursor->height / 2;
|
|
|
|
uint32_t * d = this->cursorData;
|
|
|
|
for(int y = 0; y < hheight; ++y)
|
|
|
|
for(int x = 0; x < cursor->width; ++x)
|
|
|
|
{
|
|
|
|
const uint8_t * srcAnd = data + (cursor->pitch * y) + (x / 8);
|
|
|
|
const uint8_t * srcXor = srcAnd + cursor->pitch * hheight;
|
|
|
|
const uint8_t mask = 0x80 >> (x % 8);
|
|
|
|
const uint32_t andMask = (*srcAnd & mask) ? 0xFFFFFFFF : 0xFF000000;
|
|
|
|
const uint32_t xorMask = (*srcXor & mask) ? 0x00FFFFFF : 0x00000000;
|
|
|
|
|
|
|
|
d[y * cursor->width + x ] = andMask;
|
|
|
|
d[y * cursor->width + x + cursor->width * hheight] = xorMask;
|
|
|
|
}
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
break;
|
2020-10-11 01:04:10 +00:00
|
|
|
}
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Invalid cursor type\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
this->cursor.type = cursor->type;
|
2020-10-10 13:39:31 +00:00
|
|
|
this->cursor.width = cursor->width;
|
|
|
|
this->cursor.height = cursor->height;
|
|
|
|
|
|
|
|
atomic_fetch_add_explicit(&this->cursorVer, 1, memory_order_relaxed);
|
|
|
|
os_sem_post(this->cursorSem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.udata & CURSOR_FLAG_POSITION)
|
|
|
|
{
|
|
|
|
this->cursor.x = cursor->x;
|
|
|
|
this->cursor.y = cursor->y;
|
|
|
|
}
|
|
|
|
|
|
|
|
lgmpClientMessageDone(this->pointerQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
lgmpClientUnsubscribe(&this->pointerQueue);
|
|
|
|
|
|
|
|
bfree(this->cursorData);
|
|
|
|
this->cursorData = NULL;
|
|
|
|
this->cursorSize = 0;
|
|
|
|
|
2022-08-08 23:11:33 +00:00
|
|
|
this->state = STATE_RESTARTING;
|
2020-10-10 13:39:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
static void lgUpdate(void * data, obs_data_t * settings)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
|
|
|
|
deinit(this);
|
|
|
|
this->shmFile = bstrdup(obs_data_get_string(settings, "shmFile"));
|
|
|
|
if (!ivshmemOpenDev(&this->shmDev, this->shmFile))
|
|
|
|
return;
|
|
|
|
|
2022-06-29 21:47:00 +00:00
|
|
|
this->hideMouse = obs_data_get_bool(settings, "hideMouse") ? 1 : 0;
|
2021-08-13 09:41:48 +00:00
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
this->dmabuf = obs_data_get_bool(settings, "dmabuf") && ivshmemHasDMA(&this->shmDev);
|
|
|
|
#endif
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_OPEN;
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2020-05-17 02:04:41 +00:00
|
|
|
uint32_t udataSize;
|
|
|
|
KVMFR * udata;
|
|
|
|
|
2020-05-20 23:28:41 +00:00
|
|
|
if (lgmpClientInit(this->shmDev.mem, this->shmDev.size, &this->lgmp)
|
|
|
|
!= LGMP_OK)
|
|
|
|
return;
|
|
|
|
|
2020-05-20 23:37:20 +00:00
|
|
|
usleep(200000);
|
|
|
|
|
2022-11-07 13:00:11 +00:00
|
|
|
if (lgmpClientSessionInit(this->lgmp, &udataSize, (uint8_t **)&udata, NULL)
|
2020-05-20 23:37:20 +00:00
|
|
|
!= LGMP_OK)
|
2020-01-10 07:14:08 +00:00
|
|
|
return;
|
|
|
|
|
2022-01-05 10:57:49 +00:00
|
|
|
if (udataSize < sizeof(KVMFR) ||
|
2020-05-17 02:04:41 +00:00
|
|
|
memcmp(udata->magic, KVMFR_MAGIC, sizeof(udata->magic)) != 0 ||
|
|
|
|
udata->version != KVMFR_VERSION)
|
|
|
|
{
|
|
|
|
printf("The host application is not compatible with this client\n");
|
|
|
|
printf("Expected KVMFR version %d\n", KVMFR_VERSION);
|
|
|
|
printf("This is not a Looking Glass error, do not report this\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_STARTING;
|
2022-08-08 23:11:33 +00:00
|
|
|
createThreads(this);
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
static int dmabufGetFd(LGPlugin * this, LGMPMessage * msg, KVMFRFrame * frame, size_t dataSize)
|
|
|
|
{
|
|
|
|
DMAFrameInfo * dma = NULL;
|
|
|
|
|
|
|
|
/* find the existing dma buffer if it exists */
|
|
|
|
for (int i = 0; i < ARRAY_LENGTH(this->dmaInfo); ++i)
|
|
|
|
if (this->dmaInfo[i].frame == frame)
|
|
|
|
{
|
|
|
|
dma = this->dmaInfo + i;
|
|
|
|
/* if it's too small close it */
|
|
|
|
if (dma->dataSize < dataSize)
|
|
|
|
{
|
|
|
|
close(dma->fd);
|
|
|
|
dma->fd = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise find a free buffer for use */
|
|
|
|
if (!dma)
|
|
|
|
for (int i = 0; i < ARRAY_LENGTH(this->dmaInfo); ++i)
|
|
|
|
if (!this->dmaInfo[i].frame)
|
|
|
|
{
|
|
|
|
dma = this->dmaInfo + i;
|
|
|
|
dma->frame = frame;
|
|
|
|
dma->fd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(dma);
|
|
|
|
|
|
|
|
/* open the buffer */
|
|
|
|
if (dma->fd == -1)
|
|
|
|
{
|
|
|
|
const uintptr_t pos = (uintptr_t) msg->mem - (uintptr_t) this->shmDev.mem;
|
2022-05-16 09:32:45 +00:00
|
|
|
const uintptr_t offset = (uintptr_t) frame->offset + sizeof(FrameBuffer);
|
2021-08-13 09:41:48 +00:00
|
|
|
|
|
|
|
dma->dataSize = dataSize;
|
|
|
|
dma->fd = ivshmemGetDMABuf(&this->shmDev, pos + offset, dataSize);
|
|
|
|
if (dma->fd < 0)
|
|
|
|
{
|
|
|
|
puts("Failed to get the DMA buffer for the frame");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dma->fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
static void lgVideoTick(void * data, float seconds)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
|
2022-08-08 23:11:33 +00:00
|
|
|
if (this->state == STATE_RESTARTING) {
|
|
|
|
waitThreads(this);
|
|
|
|
|
|
|
|
this->state = STATE_STARTING;
|
|
|
|
createThreads(this);
|
|
|
|
}
|
2020-04-24 11:30:28 +00:00
|
|
|
if (this->state != STATE_RUNNING)
|
2020-01-10 07:14:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
LGMP_STATUS status;
|
|
|
|
LGMPMessage msg;
|
2021-08-13 09:41:48 +00:00
|
|
|
bool framebuffer = true;
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2020-04-24 11:30:28 +00:00
|
|
|
os_sem_wait(this->frameSem);
|
|
|
|
if (this->state != STATE_RUNNING)
|
2020-01-10 07:14:08 +00:00
|
|
|
{
|
2020-04-24 11:30:28 +00:00
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
this->cursorRect.x = this->cursor.x;
|
|
|
|
this->cursorRect.y = this->cursor.y;
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
/* update the cursor texture */
|
|
|
|
unsigned int cursorVer = atomic_load(&this->cursorVer);
|
|
|
|
if (cursorVer != this->cursorCurVer)
|
|
|
|
{
|
|
|
|
os_sem_wait(this->cursorSem);
|
|
|
|
obs_enter_graphics();
|
|
|
|
|
|
|
|
if (this->cursorTex)
|
|
|
|
{
|
|
|
|
gs_texture_destroy(this->cursorTex);
|
|
|
|
this->cursorTex = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(this->cursor.type)
|
|
|
|
{
|
2020-10-10 14:07:20 +00:00
|
|
|
case CURSOR_TYPE_MASKED_COLOR:
|
|
|
|
/* fallthrough */
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
case CURSOR_TYPE_COLOR:
|
2020-10-11 01:04:10 +00:00
|
|
|
this->cursorMono = false;
|
|
|
|
this->cursorTex =
|
2020-10-10 13:39:31 +00:00
|
|
|
gs_texture_create(
|
|
|
|
this->cursor.width,
|
|
|
|
this->cursor.height,
|
|
|
|
GS_BGRA,
|
|
|
|
1,
|
2020-10-11 01:04:10 +00:00
|
|
|
(const uint8_t **)&this->cursorData,
|
|
|
|
GS_DYNAMIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURSOR_TYPE_MONOCHROME:
|
|
|
|
this->cursorMono = true;
|
|
|
|
this->cursorTex =
|
|
|
|
gs_texture_create(
|
|
|
|
this->cursor.width,
|
|
|
|
this->cursor.height,
|
2021-05-31 23:12:14 +00:00
|
|
|
GS_BGRA,
|
2020-10-11 01:04:10 +00:00
|
|
|
1,
|
|
|
|
(const uint8_t **)&this->cursorData,
|
2020-10-10 13:39:31 +00:00
|
|
|
GS_DYNAMIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_leave_graphics();
|
2020-10-11 01:04:10 +00:00
|
|
|
|
|
|
|
this->cursorCurVer = cursorVer;
|
|
|
|
this->cursorRect.cx = this->cursor.width;
|
|
|
|
this->cursorRect.cy = this->cursor.height;
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
os_sem_post(this->cursorSem);
|
|
|
|
}
|
|
|
|
|
2020-08-09 02:29:52 +00:00
|
|
|
if ((status = lgmpClientAdvanceToLast(this->frameQueue)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
if (status != LGMP_ERR_QUEUE_EMPTY)
|
|
|
|
{
|
|
|
|
os_sem_post(this->frameSem);
|
|
|
|
printf("lgmpClientAdvanceToLast: %s\n", lgmpStatusString(status));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
if ((status = lgmpClientProcess(this->frameQueue, &msg)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
if (status == LGMP_ERR_QUEUE_EMPTY)
|
2020-04-24 11:30:28 +00:00
|
|
|
{
|
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
return;
|
2020-04-24 11:30:28 +00:00
|
|
|
}
|
2020-01-10 07:14:08 +00:00
|
|
|
|
|
|
|
printf("lgmpClientProcess: %s\n", lgmpStatusString(status));
|
2020-04-24 11:30:28 +00:00
|
|
|
this->state = STATE_STOPPING;
|
2020-04-24 16:26:34 +00:00
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KVMFRFrame * frame = (KVMFRFrame *)msg.mem;
|
2020-10-12 07:52:37 +00:00
|
|
|
if (!this->texture || this->formatVer != frame->formatVer)
|
2020-01-10 07:14:08 +00:00
|
|
|
{
|
2022-05-01 09:41:46 +00:00
|
|
|
this->formatVer = frame->formatVer;
|
|
|
|
this->screenWidth = frame->screenWidth;
|
|
|
|
this->screenHeight = frame->screenHeight;
|
|
|
|
this->frameWidth = frame->frameWidth;
|
|
|
|
this->frameHeight = frame->frameHeight;
|
|
|
|
this->type = frame->type;
|
|
|
|
|
|
|
|
this->screenScale.x = this->screenWidth / this->frameWidth ;
|
|
|
|
this->screenScale.y = this->screenHeight / this->frameHeight;
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2020-05-20 21:31:12 +00:00
|
|
|
obs_enter_graphics();
|
|
|
|
if (this->texture)
|
|
|
|
{
|
|
|
|
gs_texture_unmap(this->texture);
|
|
|
|
gs_texture_destroy(this->texture);
|
2020-10-10 13:39:31 +00:00
|
|
|
this->texture = NULL;
|
2020-05-20 21:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
enum gs_color_format format;
|
2021-08-13 09:41:48 +00:00
|
|
|
uint32_t drm_format;
|
|
|
|
|
2020-10-11 09:42:09 +00:00
|
|
|
this->bpp = 4;
|
2020-01-10 07:14:08 +00:00
|
|
|
switch(this->type)
|
|
|
|
{
|
2021-08-13 09:41:48 +00:00
|
|
|
case FRAME_TYPE_BGRA:
|
|
|
|
format = GS_BGRA;
|
|
|
|
drm_format = DRM_FORMAT_ARGB8888;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA:
|
|
|
|
format = GS_RGBA;
|
|
|
|
drm_format = DRM_FORMAT_ARGB8888;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA10:
|
|
|
|
format = GS_R10G10B10A2;
|
|
|
|
drm_format = DRM_FORMAT_BGRA1010102;
|
|
|
|
break;
|
2020-10-11 09:42:09 +00:00
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA16F:
|
2021-08-13 09:41:48 +00:00
|
|
|
this->bpp = 8;
|
|
|
|
format = GS_RGBA16F;
|
|
|
|
drm_format = DRM_FORMAT_ABGR16161616F;
|
2020-10-11 09:42:09 +00:00
|
|
|
break;
|
|
|
|
|
2020-01-10 07:14:08 +00:00
|
|
|
default:
|
|
|
|
printf("invalid type %d\n", this->type);
|
2023-11-09 23:00:47 +00:00
|
|
|
lgmpClientMessageDone(this->frameQueue);
|
2020-04-24 13:03:40 +00:00
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
this->texture = NULL;
|
|
|
|
|
|
|
|
#if LIBOBS_API_MAJOR_VER >= 27
|
|
|
|
if (this->dmabuf)
|
|
|
|
{
|
2022-05-01 09:41:46 +00:00
|
|
|
int fd = dmabufGetFd(this, &msg, frame, frame->frameHeight * frame->pitch);
|
2021-08-13 09:41:48 +00:00
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
goto dmabuf_fail;
|
|
|
|
|
2022-05-01 09:41:46 +00:00
|
|
|
this->texture = gs_texture_create_from_dmabuf(
|
|
|
|
frame->frameWidth,
|
|
|
|
frame->frameHeight,
|
2021-08-13 09:41:48 +00:00
|
|
|
drm_format, format, 1, &fd, &(uint32_t) { frame->pitch },
|
|
|
|
&(uint32_t) { 0 }, &(uint64_t) { 0 });
|
|
|
|
|
|
|
|
if (!this->texture)
|
|
|
|
{
|
|
|
|
puts("Failed to create dmabuf texture");
|
|
|
|
this->dmabuf = false;
|
|
|
|
goto dmabuf_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
framebuffer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmabuf_fail:
|
|
|
|
#else
|
|
|
|
(void) drm_format;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!this->texture)
|
|
|
|
this->texture = gs_texture_create(
|
2022-05-01 09:41:46 +00:00
|
|
|
this->frameWidth, this->frameHeight, format, 1, NULL, GS_DYNAMIC);
|
2020-01-10 07:14:08 +00:00
|
|
|
|
|
|
|
if (!this->texture)
|
|
|
|
{
|
|
|
|
printf("create texture failed\n");
|
2020-04-24 13:03:40 +00:00
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-20 21:31:12 +00:00
|
|
|
|
|
|
|
gs_texture_map(this->texture, &this->texData, &this->linesize);
|
|
|
|
obs_leave_graphics();
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 09:41:48 +00:00
|
|
|
if (framebuffer && this->texture)
|
2020-10-12 07:52:37 +00:00
|
|
|
{
|
|
|
|
FrameBuffer * fb = (FrameBuffer *)(((uint8_t*)frame) + frame->offset);
|
|
|
|
framebuffer_read(
|
|
|
|
fb,
|
2022-05-01 09:41:46 +00:00
|
|
|
this->texData, // dst
|
|
|
|
this->linesize, // dstpitch
|
|
|
|
frame->frameHeight, // height
|
|
|
|
frame->frameWidth, // width
|
|
|
|
this->bpp, // bpp
|
|
|
|
frame->pitch // linepitch
|
2020-10-12 07:52:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
lgmpClientMessageDone(this->frameQueue);
|
|
|
|
os_sem_post(this->frameSem);
|
2020-01-10 07:14:08 +00:00
|
|
|
|
2020-10-12 07:52:37 +00:00
|
|
|
obs_enter_graphics();
|
|
|
|
gs_texture_unmap(this->texture);
|
|
|
|
gs_texture_map(this->texture, &this->texData, &this->linesize);
|
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lgmpClientMessageDone(this->frameQueue);
|
|
|
|
os_sem_post(this->frameSem);
|
|
|
|
}
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lgVideoRender(void * data, gs_effect_t * effect)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
|
|
|
|
|
|
|
if (!this->texture)
|
|
|
|
return;
|
|
|
|
|
|
|
|
effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
|
|
|
|
gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
|
|
|
|
gs_effect_set_texture(image, this->texture);
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
while (gs_effect_loop(effect, "Draw"))
|
2020-01-10 07:14:08 +00:00
|
|
|
gs_draw_sprite(this->texture, 0, 0, 0);
|
2020-10-10 13:39:31 +00:00
|
|
|
|
|
|
|
if (this->cursorVisible && this->cursorTex)
|
|
|
|
{
|
2020-10-11 01:04:10 +00:00
|
|
|
struct matrix4 m4;
|
|
|
|
gs_matrix_get(&m4);
|
|
|
|
struct gs_rect r =
|
|
|
|
{
|
|
|
|
.x = m4.t.x,
|
|
|
|
.y = m4.t.y,
|
2022-05-01 09:41:46 +00:00
|
|
|
.cx = (double)this->frameWidth * m4.x.x,
|
|
|
|
.cy = (double)this->frameHeight * m4.y.y
|
2020-10-11 01:04:10 +00:00
|
|
|
};
|
|
|
|
gs_set_scissor_rect(&r);
|
|
|
|
|
2020-10-10 13:39:31 +00:00
|
|
|
effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
|
|
|
|
image = gs_effect_get_param_by_name(effect, "image");
|
|
|
|
gs_effect_set_texture(image, this->cursorTex);
|
2020-10-11 01:04:10 +00:00
|
|
|
|
|
|
|
gs_matrix_push();
|
2022-05-01 09:41:46 +00:00
|
|
|
gs_matrix_translate3f(
|
|
|
|
this->cursorRect.x / this->screenScale.x,
|
|
|
|
this->cursorRect.y / this->screenScale.y,
|
|
|
|
0.0f);
|
2020-10-11 01:04:10 +00:00
|
|
|
|
|
|
|
if (!this->cursorMono)
|
2020-10-10 13:39:31 +00:00
|
|
|
{
|
2021-05-31 23:12:14 +00:00
|
|
|
gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
|
2020-10-11 01:04:10 +00:00
|
|
|
while (gs_effect_loop(effect, "Draw"))
|
|
|
|
gs_draw_sprite(this->cursorTex, 0, 0, 0);
|
2021-05-31 23:12:14 +00:00
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
2020-10-10 13:39:31 +00:00
|
|
|
}
|
2020-10-11 01:04:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (gs_effect_loop(effect, "Draw"))
|
|
|
|
{
|
|
|
|
glEnable(GL_COLOR_LOGIC_OP);
|
2020-10-10 13:39:31 +00:00
|
|
|
|
2020-10-11 01:04:10 +00:00
|
|
|
glLogicOp(GL_AND);
|
|
|
|
gs_draw_sprite_subregion(
|
|
|
|
this->cursorTex , 0,
|
|
|
|
0 , 0,
|
2021-05-31 22:56:37 +00:00
|
|
|
this->cursorRect.cx, this->cursorRect.cy / 2);
|
2020-10-11 01:04:10 +00:00
|
|
|
|
|
|
|
glLogicOp(GL_XOR);
|
|
|
|
gs_draw_sprite_subregion(
|
|
|
|
this->cursorTex , 0,
|
2021-05-31 22:56:37 +00:00
|
|
|
0 , this->cursorRect.cy / 2,
|
2020-10-11 01:04:10 +00:00
|
|
|
this->cursorRect.cx, this->cursorRect.cy / 2);
|
|
|
|
|
|
|
|
glDisable(GL_COLOR_LOGIC_OP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_matrix_pop();
|
|
|
|
gs_set_scissor_rect(NULL);
|
|
|
|
}
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t lgGetWidth(void * data)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
2022-05-01 09:41:46 +00:00
|
|
|
return this->frameWidth;
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t lgGetHeight(void * data)
|
|
|
|
{
|
|
|
|
LGPlugin * this = (LGPlugin *)data;
|
2022-05-01 09:41:46 +00:00
|
|
|
return this->frameHeight;
|
2020-01-10 07:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_source_info lg_source =
|
|
|
|
{
|
|
|
|
.id = "looking-glass-obs",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
|
|
|
|
OBS_SOURCE_DO_NOT_DUPLICATE,
|
|
|
|
.get_name = lgGetName,
|
|
|
|
.create = lgCreate,
|
|
|
|
.destroy = lgDestroy,
|
|
|
|
.update = lgUpdate,
|
|
|
|
.get_defaults = lgGetDefaults,
|
|
|
|
.get_properties = lgGetProperties,
|
|
|
|
.video_tick = lgVideoTick,
|
|
|
|
.video_render = lgVideoRender,
|
|
|
|
.get_width = lgGetWidth,
|
|
|
|
.get_height = lgGetHeight,
|
|
|
|
// .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE
|
2020-04-14 03:27:07 +00:00
|
|
|
};
|