Skip to content

Commit

Permalink
dynamic resize working with accel_assist
Browse files Browse the repository at this point in the history
  • Loading branch information
jsorg71 committed Nov 7, 2024
1 parent d8da4bc commit 4efd280
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 66 deletions.
89 changes: 35 additions & 54 deletions xrdp_accel_assist/xrdp_accel_assist.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ struct xorgxrdp_info
struct trans *xorg_trans;
struct trans *xrdp_trans;
struct source_info si;
int resizing;
int shmem_fd_ret;
int shmem_bytes_ret;
int idr_count;
int pad0;
};

static int g_display_num = 0;
int xrdp_invalidate = 0;

/*****************************************************************************/
static int
Expand All @@ -71,6 +70,7 @@ gfx_wiretosurface1(struct xorgxrdp_info *xi, struct stream *s)
int height;
int cdata_bytes;
int rv;
int encoder_flags;
char *flags_pointer;
char *final_pointer;

Expand Down Expand Up @@ -166,10 +166,17 @@ gfx_wiretosurface1(struct xorgxrdp_info *xi, struct stream *s)
(void)top;

cdata_bytes = GFX_MAP_SIZE;
encoder_flags = 0;
if (xi->idr_count > 0)
{
encoder_flags = XH_ENC_FLAGS_FORCEIDR;
xi->idr_count--;
}
rv = xrdp_accel_assist_x11_encode_pixmap(0, 0,
width, height, surface_id,
num_rects_c, crects,
addr, &cdata_bytes);
addr, &cdata_bytes,
encoder_flags);
LOG_DEVEL(LOG_LEVEL_INFO, "gfx_wiretosurface1: rv %d cdata_bytes %d",
rv, cdata_bytes);

Expand Down Expand Up @@ -248,7 +255,6 @@ xorg_process_message_62(struct xorgxrdp_info *xi, struct stream *s)
}
break;
case 0x000E: /* XR_RDPGFX_CMDID_RESETGRAPHICS */
xrdp_invalidate = 1;
LOG(LOG_LEVEL_INFO, "xorg_process_message_62: "
"XR_RDPGFX_CMDID_RESETGRAPHICS detected");
break;
Expand Down Expand Up @@ -351,6 +357,7 @@ xorg_process_message_64(struct xorgxrdp_info *xi, struct stream *s)
int cdata_bytes;
int index;
int recv_bytes;
int encoder_flags;
enum encoder_result rv;
struct xh_rect *crects;
char *bmpdata;
Expand All @@ -360,6 +367,7 @@ xorg_process_message_64(struct xorgxrdp_info *xi, struct stream *s)

(void)twidth;
(void)theight;
(void)frame_id;

/* dirty pixels */
in_uint16_le(s, num_drects);
Expand Down Expand Up @@ -389,26 +397,6 @@ xorg_process_message_64(struct xorgxrdp_info *xi, struct stream *s)
in_uint16_le(s, theight);
char *final_pointer = s->p;

if (xi->resizing == 3)
{
if (xrdp_invalidate > 0 && frame_id == 1)
{
// Let it through. We are no longer resizing.
LOG(LOG_LEVEL_DEBUG, "Invalidate received and processing frame ID 1. Unblocking encoder. Invalidate is %d.", xrdp_invalidate);
xi->resizing = 0;
}
else
{
LOG(LOG_LEVEL_DEBUG, "Blocked Incoming Frame ID %d. Invalidate is %d", frame_id, xrdp_invalidate);
return 0;
}
}

if (xi->resizing > 0)
{
return 0;
}

if (xi->shmem_fd_ret != -1)
{
LOG(LOG_LEVEL_ERROR, "xorg_process_message_64: xi->shmem_fd_ret "
Expand Down Expand Up @@ -441,12 +429,18 @@ xorg_process_message_64(struct xorgxrdp_info *xi, struct stream *s)
if ((bmpdata != NULL) && (flags & 1))
{
cdata_bytes = 16 * 1024 * 1024;
encoder_flags = 0;
if (xi->idr_count > 0)
{
encoder_flags = XH_ENC_FLAGS_FORCEIDR;
xi->idr_count--;
}
rv = xrdp_accel_assist_x11_encode_pixmap(left, top,
width, height,
(flags >> 28) & 0xF,
num_crects, crects,
bmpdata + 4,
&cdata_bytes);
&cdata_bytes, encoder_flags);
if (rv == ENCODER_ERROR)
{
LOG(LOG_LEVEL_ERROR, "error %d", rv);
Expand Down Expand Up @@ -550,10 +544,6 @@ xorg_process_message(struct xorgxrdp_info *xi, struct stream *s)
s->p = phold + size;
s->end = endhold;
}
if (xi->resizing > 0)
{
return 0;
}
}
else if (type == 100)
{
Expand All @@ -568,10 +558,6 @@ xorg_process_message(struct xorgxrdp_info *xi, struct stream *s)
case 1:
LOG(LOG_LEVEL_DEBUG, "calling xrdp_accel_assist_x11_delete_all_pixmaps");
xrdp_accel_assist_x11_delete_all_pixmaps();
if (xi->resizing == 1)
{
xi->resizing = 2;
}
break;
case 2:
in_uint16_le(s, width);
Expand All @@ -582,24 +568,22 @@ xorg_process_message(struct xorgxrdp_info *xi, struct stream *s)
LOG(LOG_LEVEL_DEBUG, "calling xrdp_accel_assist_x11_create_pixmap");
xrdp_accel_assist_x11_create_pixmap(width, height, magic,
con_id, mon_id);
if (xi->resizing == 2)
{
xi->resizing = 3;
}
break;
}
s->p = phold + size;
}
/* this will force I frames for 10 frames */
xi->idr_count = 10;
LOG(LOG_LEVEL_INFO, "setting idr_count to %d", xi->idr_count);
}
s->p = s->data;

if (xi->shmem_fd_ret == -1)
{
// Using system-v or no shared memory
/* no shared memory, ok */
ret = trans_write_copy_s(xi->xrdp_trans, s);
return ret;
}
// Using posix shared memory
/* using posix shared memory, got to pass fd on to xrdp */
ret = trans_force_write_s(xi->xrdp_trans, s);
if (ret)
{
Expand All @@ -611,7 +595,8 @@ xorg_process_message(struct xorgxrdp_info *xi, struct stream *s)
{
return 1;
}
return g_file_close(xi->shmem_fd_ret);
g_file_close(xi->shmem_fd_ret);
return 0;
}

/*****************************************************************************/
Expand Down Expand Up @@ -668,25 +653,23 @@ xrdp_process_message(struct xorgxrdp_info *xi, struct stream *s)

in_uint32_le(s, len);
in_uint16_le(s, msg_type1);
if (msg_type1 == 103) // client message
if (msg_type1 == 103) /* client message */
{
in_uint32_le(s, msg_type2);
if (msg_type2 == 200) // invalidate
if (msg_type2 == 200) /* invalidate */
{
LOG(LOG_LEVEL_DEBUG, "Invalidate found (len: %d, msg1: %d, msg2: %d)", len, msg_type1, msg_type2);
/*
10 is an arbitrary number. Anecdotally, you need the first 10 frames to be key frames to make sure the client
receives at least one of them.
*/
xrdp_invalidate += 10;
}
else if (msg_type2 == 300) // resize
else if (msg_type2 == 300) /* resize */
{
LOG(LOG_LEVEL_DEBUG, "Resize found (len: %d, msg1: %d, msg2: %d)", len, msg_type1, msg_type2);
xi->resizing = 1;
LOG(LOG_LEVEL_DEBUG, "Resize 300 found (len: %d, msg1: %d, msg2: %d)", len, msg_type1, msg_type2);
}
else if (msg_type2 == 302) /* resize */
{
LOG(LOG_LEVEL_DEBUG, "Resize 302 found (len: %d, msg1: %d, msg2: %d)", len, msg_type1, msg_type2);
}
}
//Reset read pointer
/* Reset read pointer */
s->p = s->data;
return trans_write_copy_s(xi->xorg_trans, s);
}
Expand Down Expand Up @@ -971,8 +954,6 @@ main(int argc, char **argv)
xrdp_fd = g_atoi(g_getenv("XORGXRDP_XRDP_FD"));
LOG(LOG_LEVEL_INFO, "xrdp_fd: %d", xrdp_fd);

xi.resizing = 0;

xi.xorg_trans = trans_create(TRANS_MODE_UNIX, 128 * 1024, 128 * 1024);
xi.xorg_trans->sck = xorg_fd;
xi.xorg_trans->status = TRANS_STATUS_UP;
Expand Down
2 changes: 2 additions & 0 deletions xrdp_accel_assist/xrdp_accel_assist.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct xh_rect
short h;
};

#define XH_ENC_FLAGS_FORCEIDR (1 << 0)

enum encoder_result
{
INCREMENTAL_FRAME_ENCODED, /* P frame */
Expand Down
12 changes: 5 additions & 7 deletions xrdp_accel_assist/xrdp_accel_assist_nvenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ struct enc_info
NV_ENC_REGISTERED_PTR registeredResource;
};

extern int xrdp_invalidate;

/*****************************************************************************/
int
xrdp_accel_assist_nvenc_init(void)
Expand Down Expand Up @@ -306,7 +304,8 @@ xrdp_accel_assist_nvenc_delete_encoder(struct enc_info *ei)
/*****************************************************************************/
enum encoder_result
xrdp_accel_assist_nvenc_encode(struct enc_info *ei, int tex,
void *cdata, int *cdata_bytes)
void *cdata, int *cdata_bytes,
int flags)
{
NV_ENC_PIC_PARAMS picParams;
NV_ENC_LOCK_BITSTREAM lockBitstream;
Expand All @@ -326,12 +325,11 @@ xrdp_accel_assist_nvenc_encode(struct enc_info *ei, int tex,
picParams.inputTimeStamp = ei->frameCount;
picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
picParams.encodePicFlags = NV_ENC_PIC_FLAG_OUTPUT_SPSPPS;
if (xrdp_invalidate > 0 || ei->frameCount == 0)
if ((flags & XH_ENC_FLAGS_FORCEIDR) || (ei->frameCount < 1))
{
picParams.encodePicFlags |= NV_ENC_PIC_FLAG_FORCEIDR;
LOG(LOG_LEVEL_INFO, "Forcing NVENC H264 IDR SPSPPS for frame id: %d,"
"invalidate is: %d", ei->frameCount, xrdp_invalidate);
xrdp_invalidate = MAX(0, xrdp_invalidate - 1);
LOG(LOG_LEVEL_INFO, "Forcing NVENC H264 IDR SPSPPS for frame id: %d",
ei->frameCount);
}
nv_error = g_enc_funcs.nvEncEncodePicture(ei->enc, &picParams);
rv = ENCODER_ERROR;
Expand Down
3 changes: 2 additions & 1 deletion xrdp_accel_assist/xrdp_accel_assist_nvenc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ int
xrdp_accel_assist_nvenc_delete_encoder(struct enc_info *ei);
enum encoder_result
xrdp_accel_assist_nvenc_encode(struct enc_info *ei, int tex,
void *cdata, int *cdata_bytes);
void *cdata, int *cdata_bytes,
int flags);

#endif
8 changes: 5 additions & 3 deletions xrdp_accel_assist/xrdp_accel_assist_x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ struct enc_funcs
struct enc_info **ei);
int (*destroy_enc)(struct enc_info *ei);
enum encoder_result (*encode)(struct enc_info *ei, int tex,
void *cdata, int *cdata_bytes);
void *cdata, int *cdata_bytes,
int flags);
};

static struct enc_funcs g_enc_funcs[] =
Expand Down Expand Up @@ -834,7 +835,8 @@ enum encoder_result
xrdp_accel_assist_x11_encode_pixmap(int left, int top, int width, int height,
int mon_id, int num_crects,
struct xh_rect *crects,
void *cdata, int *cdata_bytes)
void *cdata, int *cdata_bytes,
int flags)
{
struct mon_info *mi;
struct shader_info *si;
Expand Down Expand Up @@ -862,6 +864,6 @@ xrdp_accel_assist_x11_encode_pixmap(int left, int top, int width, int height,
XFlush(g_display);
/* encode */
rv = g_enc_funcs[g_enc].encode(mi->ei, mi->enc_texture,
cdata, cdata_bytes);
cdata, cdata_bytes, flags);
return rv;
}
3 changes: 2 additions & 1 deletion xrdp_accel_assist/xrdp_accel_assist_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum encoder_result
xrdp_accel_assist_x11_encode_pixmap(int left, int top, int width, int height,
int mon_id, int num_crects,
struct xh_rect *crects,
void *cdata, int *cdata_bytes);
void *cdata, int *cdata_bytes,
int flags);

#endif

0 comments on commit 4efd280

Please sign in to comment.