xrdp: speed up bitmap cache lookup using hash table

ulab-next
Jay Sorg 11 years ago
parent c34ba69ad8
commit f94f5bec1c

@ -213,9 +213,6 @@ int APP_CC
xrdp_bitmap_compare(struct xrdp_bitmap* self,
struct xrdp_bitmap* b);
int APP_CC
xrdp_bitmap_compare_with_crc(struct xrdp_bitmap* self,
struct xrdp_bitmap* b);
int APP_CC
xrdp_bitmap_invalidate(struct xrdp_bitmap* self, struct xrdp_rect* rect);
int APP_CC
xrdp_bitmap_def_proc(struct xrdp_bitmap* self, int msg,

@ -23,9 +23,22 @@
#include "xrdp.h"
#include "log.h"
static int g_crc_seed = 0xffffffff;
static int g_crc_table[256] =
#include "crc16.h"
#define LLOG_LEVEL 1
#define LLOGLN(_level, _args) \
do \
{ \
if (_level < LLOG_LEVEL) \
{ \
g_write("xrdp:xrdp_bitmap [%10.10u]: ", g_time3()); \
g_writeln _args ; \
} \
} \
while (0)
static const int g_crc_table[256] =
{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
@ -72,10 +85,10 @@ static int g_crc_table[256] =
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
#define CRC_START(in_crc) (in_crc) = g_crc_seed
#define CRC_START(in_crc) (in_crc) = 0xFFFFFFFF
#define CRC_PASS(in_pixel, in_crc) \
(in_crc) = g_crc_table[((in_crc) ^ (in_pixel)) & 0xff] ^ ((in_crc) >> 8)
#define CRC_END(in_crc) (in_crc) = ((in_crc) ^ g_crc_seed)
#define CRC_END(in_crc) (in_crc) = ((in_crc) ^ 0xFFFFFFFF)
/*****************************************************************************/
struct xrdp_bitmap *APP_CC
@ -809,20 +822,20 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
struct xrdp_bitmap *dest,
int x, int y, int cx, int cy)
{
int i = 0;
int j = 0;
int destx = 0;
int desty = 0;
int pixel = 0;
int crc = 0;
int incs = 0;
int incd = 0;
unsigned char *s8 = (unsigned char *)NULL;
unsigned char *d8 = (unsigned char *)NULL;
unsigned short *s16 = (unsigned short *)NULL;
unsigned short *d16 = (unsigned short *)NULL;
unsigned int *s32;
unsigned int *d32;
int i;
int j;
int destx;
int desty;
int pixel;
int crc;
int incs;
int incd;
tui8 *s8;
tui8 *d8;
tui16 *s16;
tui16 *d16;
tui32 *s32;
tui32 *d32;
if (self == 0)
{
@ -864,47 +877,54 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
CRC_START(crc);
CRC_PASS(self->width, crc);
CRC_PASS(self->width >> 8, crc);
CRC_PASS(self->height, crc);
CRC_PASS(self->height >> 8, crc);
if (self->bpp == 24)
{
s32 = ((unsigned int *)(self->data)) + (self->width * y + x);
d32 = ((unsigned int *)(dest->data)) + (dest->width * desty + destx);
s32 = ((tui32 *)(self->data)) + (self->width * y + x);
d32 = ((tui32 *)(dest->data)) + (dest->width * desty + destx);
incs = self->width - cx;
incd = dest->width - cx;
for (i = 0; i < cy; i++)
{
j = 0;
while (j < cx - 4)
{
pixel = *s32;
*d32 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
CRC_PASS(pixel >> 16, crc);
*d32 = pixel;
s32++;
d32++;
pixel = *s32;
*d32 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
CRC_PASS(pixel >> 16, crc);
*d32 = pixel;
s32++;
d32++;
pixel = *s32;
*d32 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
CRC_PASS(pixel >> 16, crc);
*d32 = pixel;
s32++;
d32++;
pixel = *s32;
*d32 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
CRC_PASS(pixel >> 16, crc);
*d32 = pixel;
s32++;
d32++;
@ -913,10 +933,10 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
while (j < cx)
{
pixel = *s32;
*d32 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
CRC_PASS(pixel >> 16, crc);
*d32 = pixel;
s32++;
d32++;
@ -929,8 +949,8 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
}
else if (self->bpp == 15 || self->bpp == 16)
{
s16 = ((unsigned short *)(self->data)) + (self->width * y + x);
d16 = ((unsigned short *)(dest->data)) + (dest->width * desty + destx);
s16 = ((tui16 *)(self->data)) + (self->width * y + x);
d16 = ((tui16 *)(dest->data)) + (dest->width * desty + destx);
incs = self->width - cx;
incd = dest->width - cx;
@ -939,9 +959,9 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
for (j = 0; j < cx; j++)
{
pixel = *s16;
*d16 = pixel;
CRC_PASS(pixel, crc);
CRC_PASS(pixel >> 8, crc);
*d16 = pixel;
s16++;
d16++;
}
@ -952,8 +972,8 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
}
else if (self->bpp == 8)
{
s8 = ((unsigned char *)(self->data)) + (self->width * y + x);
d8 = ((unsigned char *)(dest->data)) + (dest->width * desty + destx);
s8 = ((tui8 *)(self->data)) + (self->width * y + x);
d8 = ((tui8 *)(dest->data)) + (dest->width * desty + destx);
incs = self->width - cx;
incd = dest->width - cx;
@ -962,8 +982,8 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
for (j = 0; j < cx; j++)
{
pixel = *s8;
CRC_PASS(pixel, crc);
*d8 = pixel;
CRC_PASS(pixel, crc);
s8++;
d8++;
}
@ -978,7 +998,14 @@ xrdp_bitmap_copy_box_with_crc(struct xrdp_bitmap *self,
}
CRC_END(crc);
dest->crc = crc;
dest->crc32 = crc;
dest->crc16 = dest->crc32 & 0xffff;
LLOGLN(10, ("xrdp_bitmap_copy_box_with_crc: crc16 0x%4.4x",
dest->crc16));
LLOGLN(10, ("xrdp_bitmap_copy_box_with_crc: width %d height %d",
dest->width, dest->height));
return 0;
}
@ -988,45 +1015,8 @@ int APP_CC
xrdp_bitmap_compare(struct xrdp_bitmap *self,
struct xrdp_bitmap *b)
{
if (self == 0)
{
return 0;
}
LLOGLN(10, ("xrdp_bitmap_compare:"));
if (b == 0)
{
return 0;
}
if (self->bpp != b->bpp)
{
return 0;
}
if (self->width != b->width)
{
return 0;
}
if (self->height != b->height)
{
return 0;
}
if (g_memcmp(self->data, b->data, b->height * b->line_size) == 0)
{
return 1;
}
return 0;
}
/*****************************************************************************/
/* returns true if they are the same, else returns false */
int APP_CC
xrdp_bitmap_compare_with_crc(struct xrdp_bitmap *self,
struct xrdp_bitmap *b)
{
if (self == 0)
{
return 0;
@ -1052,7 +1042,7 @@ xrdp_bitmap_compare_with_crc(struct xrdp_bitmap *self,
return 0;
}
if (self->crc == b->crc)
if (g_memcmp(self->data, b->data, b->height * b->line_size) == 0)
{
return 1;
}

@ -20,6 +20,7 @@
#include "xrdp.h"
#include "log.h"
#include "crc16.h"
#define LLOG_LEVEL 1
#define LLOGLN(_level, _args) \
@ -33,6 +34,60 @@
} \
while (0)
/*****************************************************************************/
static int APP_CC
xrdp_cache_reset_lru(struct xrdp_cache *self)
{
int index;
int jndex;
struct xrdp_lru_item *lru;
for (index = 0; index < XRDP_MAX_BITMAP_CACHE_ID; index++)
{
/* fist item */
lru = &(self->bitmap_lrus[index][0]);
lru->next = 1;
lru->prev = -1;
lru->cacheid = -1;
/* middle items */
for (jndex = 1; jndex < XRDP_MAX_BITMAP_CACHE_IDX - 1; jndex++)
{
lru = &(self->bitmap_lrus[index][jndex]);
lru->next = jndex + 1;
lru->prev = jndex - 1;
lru->cacheid = -1;
}
/* last item */
lru = &(self->bitmap_lrus[index][XRDP_MAX_BITMAP_CACHE_IDX - 1]);
lru->next = -1;
lru->prev = XRDP_MAX_BITMAP_CACHE_IDX - 2;
lru->cacheid = -1;
self->lru_head[index] = 0;
self->lru_tail[index] = XRDP_MAX_BITMAP_CACHE_IDX - 1;
}
return 0;
}
/*****************************************************************************/
static int APP_CC
xrdp_cache_reset_crc(struct xrdp_cache *self)
{
int index;
int jndex;
for (index = 0; index < XRDP_MAX_BITMAP_CACHE_ID; index++)
{
for (jndex = 0; jndex < 64 * 1024; jndex++)
{
list_delete(self->crc16[index][jndex]);
self->crc16[index][jndex] = list_create();
}
}
return 0;
}
/*****************************************************************************/
struct xrdp_cache *APP_CC
xrdp_cache_create(struct xrdp_wm *owner,
@ -65,6 +120,8 @@ xrdp_cache_create(struct xrdp_wm *owner,
self->bitmap_cache_version = client_info->bitmap_cache_version;
self->pointer_cache_entries = client_info->pointer_cache_entries;
self->xrdp_os_del_list = list_create();
xrdp_cache_reset_lru(self);
xrdp_cache_reset_crc(self);
LLOGLN(10, ("xrdp_cache_create: 0 %d 1 %d 2 %d",
self->cache1_entries, self->cache2_entries, self->cache3_entries));
return self;
@ -157,11 +214,13 @@ xrdp_cache_reset(struct xrdp_cache *self,
self->bitmap_cache_persist_enable = client_info->bitmap_cache_persist_enable;
self->bitmap_cache_version = client_info->bitmap_cache_version;
self->pointer_cache_entries = client_info->pointer_cache_entries;
xrdp_cache_reset_lru(self);
xrdp_cache_reset_crc(self);
return 0;
}
#define COMPARE_WITH_CRC(_b1, _b2) \
((_b1 != 0) && (_b2 != 0) && (_b1->crc == _b2->crc) && \
((_b1 != 0) && (_b2 != 0) && (_b1->crc32 == _b2->crc32) && \
(_b1->bpp == _b2->bpp) && \
(_b1->width == _b2->width) && (_b1->height == _b2->height))
@ -171,87 +230,71 @@ int APP_CC
xrdp_cache_add_bitmap(struct xrdp_cache *self, struct xrdp_bitmap *bitmap,
int hints)
{
int i = 0;
int j = 0;
int oldest = 0;
int cache_id = 0;
int cache_idx = 0;
int bmp_size = 0;
int e = 0;
int Bpp = 0;
e = bitmap->width % 4;
if (e != 0)
{
e = 4 - e;
}
int i;
int j;
int oldest;
int cache_id;
int cache_idx;
int bmp_size;
int e;
int Bpp;
int crc16;
int iig;
int found;
int cacheidx;
struct list *ll;
struct xrdp_bitmap *lbm;
LLOGLN(10, ("xrdp_cache_add_bitmap:"));
LLOGLN(10, ("xrdp_cache_add_bitmap: crc16 0x%4.4x",
bitmap->crc16));
e = (4 - (bitmap->width % 4)) & 3;
found = 0;
i = 0;
/* client Bpp, bmp_size */
Bpp = (bitmap->bpp + 7) / 8;
bmp_size = (bitmap->width + e) * bitmap->height * Bpp;
self->bitmap_stamp++;
/* look for match */
if (bmp_size <= self->cache1_size)
{
i = 0;
for (j = 0; j < self->cache1_entries; j++)
{
#ifdef USE_CRC
if (COMPARE_WITH_CRC(self->bitmap_items[i][j].bitmap, bitmap))
#else
if (xrdp_bitmap_compare(self->bitmap_items[i][j].bitmap, bitmap))
#endif
{
self->bitmap_items[i][j].stamp = self->bitmap_stamp;
LLOGLN(10, ("found bitmap at %d %d", i, j));
xrdp_bitmap_delete(bitmap);
return MAKELONG(j, i);
}
}
}
else if (bmp_size <= self->cache2_size)
{
i = 1;
for (j = 0; j < self->cache2_entries; j++)
{
#ifdef USE_CRC
if (COMPARE_WITH_CRC(self->bitmap_items[i][j].bitmap, bitmap))
#else
if (xrdp_bitmap_compare(self->bitmap_items[i][j].bitmap, bitmap))
#endif
{
self->bitmap_items[i][j].stamp = self->bitmap_stamp;
LLOGLN(10, ("found bitmap at %d %d", i, j));
xrdp_bitmap_delete(bitmap);
return MAKELONG(j, i);
}
}
}
else if (bmp_size <= self->cache3_size)
{
i = 2;
}
else
{
log_message(LOG_LEVEL_ERROR, "error in xrdp_cache_add_bitmap, "
"too big(%d) bpp %d", bmp_size, bitmap->bpp);
return 0;
}
for (j = 0; j < self->cache3_entries; j++)
crc16 = bitmap->crc16;
ll = self->crc16[i][crc16];
for (j = 0; j < ll->count; j++)
{
cacheidx = list_get_item(ll, j);
if (COMPARE_WITH_CRC(self->bitmap_items[i][cacheidx].bitmap, bitmap))
{
#ifdef USE_CRC
if (COMPARE_WITH_CRC(self->bitmap_items[i][j].bitmap, bitmap))
#else
if (xrdp_bitmap_compare(self->bitmap_items[i][j].bitmap, bitmap))
#endif
{
self->bitmap_items[i][j].stamp = self->bitmap_stamp;
LLOGLN(10, ("found bitmap at %d %d", i, j));
xrdp_bitmap_delete(bitmap);
return MAKELONG(j, i);
}
j = cacheidx;
LLOGLN(10, ("found bitmap at %d %d", i, j));
found = 1;
break;
}
}
else
if (found)
{
log_message(LOG_LEVEL_ERROR,"error in xrdp_cache_add_bitmap, too big(%d) bpp %d", bmp_size, bitmap->bpp);
self->bitmap_items[i][j].stamp = self->bitmap_stamp;
xrdp_bitmap_delete(bitmap);
return MAKELONG(j, i);
}
/* look for oldest */
@ -301,14 +344,45 @@ xrdp_cache_add_bitmap(struct xrdp_cache *self, struct xrdp_bitmap *bitmap,
}
}
}
LLOGLN(10, ("xrdp_cache_add_bitmap: oldest %d %d", cache_id, cache_idx));
LLOGLN(10, ("adding bitmap at %d %d old ptr %p new ptr %p",
cache_id, cache_idx,
self->bitmap_items[cache_id][cache_idx].bitmap,
bitmap));
/* remove old, about to be deleted, from crc16 list */
lbm = self->bitmap_items[cache_id][cache_idx].bitmap;
if (lbm != 0)
{
crc16 = lbm->crc16;
ll = self->crc16[cache_id][crc16];
iig = list_index_of(ll, cache_idx);
if (iig == -1)
{
LLOGLN(0, ("xrdp_cache_add_bitmap: error removing cache_idx"));
}
LLOGLN(10, ("xrdp_cache_add_bitmap: removing index %d from crc16 %d",
iig, crc16));
list_remove_item(ll, iig);
xrdp_bitmap_delete(lbm);
}
LLOGLN(10, ("adding bitmap at %d %d ptr %p", cache_id, cache_idx,
self->bitmap_items[cache_id][cache_idx].bitmap));
/* set, send bitmap and return */
xrdp_bitmap_delete(self->bitmap_items[cache_id][cache_idx].bitmap);
self->bitmap_items[cache_id][cache_idx].bitmap = bitmap;
self->bitmap_items[cache_id][cache_idx].stamp = self->bitmap_stamp;
/* add to crc16 list */
crc16 = bitmap->crc16;
ll = self->crc16[cache_id][crc16];
list_add_item(ll, cache_idx);
if (ll->count > 1)
{
LLOGLN(10, ("xrdp_cache_add_bitmap: count %d", ll->count));
}
if (self->use_bitmap_comp)
{
if (self->bitmap_cache_version & 4)

@ -177,9 +177,18 @@ struct xrdp_palette_item
struct xrdp_bitmap_item
{
int stamp;
int lru_index;
struct xrdp_bitmap* bitmap;
};
struct xrdp_lru_item
{
int next;
int prev;
int cacheid;
int pad0;
};
struct xrdp_os_bitmap_item
{
int id;
@ -225,6 +234,16 @@ struct xrdp_cache
int bitmap_stamp;
struct xrdp_bitmap_item bitmap_items[XRDP_MAX_BITMAP_CACHE_ID]
[XRDP_MAX_BITMAP_CACHE_IDX];
/* lru optimize */
struct xrdp_lru_item bitmap_lrus[XRDP_MAX_BITMAP_CACHE_ID]
[XRDP_MAX_BITMAP_CACHE_IDX];
int lru_head[XRDP_MAX_BITMAP_CACHE_ID];
int lru_tail[XRDP_MAX_BITMAP_CACHE_ID];
/* crc optimize */
struct list *crc16[XRDP_MAX_BITMAP_CACHE_ID][64 * 1024];
int use_bitmap_comp;
int cache1_entries;
int cache1_size;
@ -453,7 +472,8 @@ struct xrdp_bitmap
struct xrdp_bitmap* popped_from;
int item_height;
/* crc */
int crc;
int crc32;
int crc16;
};
#define NUM_FONTS 0x4e00

Loading…
Cancel
Save