Skip to content

Commit

Permalink
Allow command buffer to be expanded (lite-xl#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guldoman authored Jan 2, 2023
1 parent 4e272c3 commit 2638e96
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/rencache.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
#define CELLS_X 80
#define CELLS_Y 50
#define CELL_SIZE 96
#define COMMAND_BUF_SIZE (1024 * 512)
#define CMD_BUF_RESIZE_RATE 1.2
#define CMD_BUF_INIT_SIZE (1024 * 512)
#define COMMAND_BARE_SIZE offsetof(Command, text)

enum { SET_CLIP, DRAW_TEXT, DRAW_RECT };
Expand All @@ -48,7 +49,9 @@ static unsigned cells_buf2[CELLS_X * CELLS_Y];
static unsigned *cells_prev = cells_buf1;
static unsigned *cells = cells_buf2;
static RenRect rect_buf[CELLS_X * CELLS_Y / 2];
static char command_buf[COMMAND_BUF_SIZE];
size_t command_buf_size = 0;
uint8_t *command_buf = NULL;
static bool resize_issue;
static int command_buf_idx;
static RenRect screen_rect;
static bool show_debug;
Expand Down Expand Up @@ -96,16 +99,38 @@ static RenRect merge_rects(RenRect a, RenRect b) {
return (RenRect) { x1, y1, x2 - x1, y2 - y1 };
}

static bool expand_command_buffer() {
size_t new_size = command_buf_size * CMD_BUF_RESIZE_RATE;
if (new_size == 0) {
new_size = CMD_BUF_INIT_SIZE;
}
uint8_t *new_command_buf = realloc(command_buf, new_size);
if (!new_command_buf) {
return false;
}
command_buf_size = new_size;
command_buf = new_command_buf;
return true;
}

static Command* push_command(int type, int size) {
if (resize_issue) {
// Don't push new commands as we had problems resizing the command buffer.
// Let's wait for the next frame.
return NULL;
}
size_t alignment = alignof(max_align_t) - 1;
size = (size + alignment) & ~alignment;
Command *cmd = (Command*) (command_buf + command_buf_idx);
int n = command_buf_idx + size;
if (n > COMMAND_BUF_SIZE) {
fprintf(stderr, "Warning: (" __FILE__ "): exhausted command buffer\n");
return NULL;
while (n > command_buf_size) {
if (!expand_command_buffer()) {
fprintf(stderr, "Warning: (" __FILE__ "): unable to resize command buffer (%ld)\n",
(size_t)(command_buf_size * CMD_BUF_RESIZE_RATE));
resize_issue = true;
return NULL;
}
}
Command *cmd = (Command*) (command_buf + command_buf_idx);
command_buf_idx = n;
memset(cmd, 0, size);
cmd->type = type;
Expand Down Expand Up @@ -175,6 +200,7 @@ void rencache_invalidate(void) {
void rencache_begin_frame() {
/* reset all cells if the screen width/height has changed */
int w, h;
resize_issue = false;
ren_get_size(&w, &h);
if (screen_rect.width != w || h != screen_rect.height) {
screen_rect.width = w;
Expand Down
5 changes: 5 additions & 0 deletions src/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,13 @@ void ren_draw_rect(RenRect rect, RenColor color) {

/*************** Window Management ****************/
void ren_free_window_resources() {
extern uint8_t *command_buf;
extern size_t command_buf_size;
renwin_free(&window_renderer);
SDL_FreeSurface(draw_rect_surface);
free(command_buf);
command_buf = NULL;
command_buf_size = 0;
}

void ren_init(SDL_Window *win) {
Expand Down

0 comments on commit 2638e96

Please sign in to comment.