0
0
mirror of https://github.com/rkd77/elinks.git synced 2025-06-30 22:19:29 -04:00

[kitty] Try to use reference counting

It is a bit suspicious, but worked for a few seconds while testing.
This commit is contained in:
Witold Filipczyk 2025-06-26 12:29:58 +02:00
parent 0fa0e40c32
commit bf6ebe2687
7 changed files with 72 additions and 35 deletions

9
src/cache/cache.c vendored
View File

@ -26,6 +26,9 @@
#ifdef CONFIG_SCRIPTING_SPIDERMONKEY
# include "scripting/smjs/smjs.h"
#endif
#ifdef CONFIG_KITTY
#include "terminal/kitty.h"
#endif
#include "util/base64.h"
#include "util/error.h"
#include "util/memory.h"
@ -703,6 +706,12 @@ done_cache_entry(struct cache_entry *cached)
mem_free_if(cached->encoding_info);
mem_free_if(cached->etag);
#ifdef CONFIG_KITTY
if (cached->pixels && --(cached->pixels->refcnt) <= 0) {
mem_free(cached->pixels->data);
mem_free(cached->pixels);
}
#endif
mem_free(cached);
}

2
src/cache/cache.h vendored
View File

@ -9,6 +9,7 @@
extern "C" {
#endif
struct el_string;
struct listbox_item;
struct uri;
@ -61,6 +62,7 @@ struct cache_entry {
#endif
#ifdef CONFIG_KITTY
struct el_string *pixels;
int width;
int height;
int number;

View File

@ -250,8 +250,7 @@ html_img_kitty(struct html_context *html_context, char *a,
if (!html_context->document) {
return;
}
char *data = NULL;
int datalen = 0;
struct el_string *pixels = NULL;
int width = 0;
int height = 0;
int im_number = 0;
@ -270,23 +269,29 @@ html_img_kitty(struct html_context *html_context, char *a,
struct cache_entry *cached = get_redirected_cache_entry(uri);
if (cached && !cached->incomplete) {
struct fragment *fragment = get_cache_fragment(cached);
if (cached->pixels) {
pixels = cached->pixels;
width = cached->width;
height = cached->height;
im_number = cached->number;
compressed = cached->compressed;
} else {
struct fragment *fragment = get_cache_fragment(cached);
if (fragment) {
data = (char *)el_kitty_get_image(fragment->data, fragment->length, &datalen, &width, &height, &compressed);
if (fragment) {
pixels = el_kitty_get_image(fragment->data, fragment->length, &width, &height, &compressed);
if (data) {
cached->width = width;
cached->height = height;
if (!cached->number) {
if (pixels) {
cached->pixels = pixels;
cached->width = width;
cached->height = height;
cached->number = im_number = ++kitty_image_number;
cached->compressed = compressed;
}
cached->compressed = compressed;
}
}
}
if (!data) {
if (!pixels) {
html_context->special_f(html_context, SP_IMAGE, uri);
}
done_uri(uri);
@ -295,7 +300,7 @@ html_img_kitty(struct html_context *html_context, char *a,
}
mem_free(url);
if (!data) {
if (!pixels) {
return;
}
struct document *document = html_context->document;
@ -304,11 +309,8 @@ html_img_kitty(struct html_context *html_context, char *a,
ln_break(html_context, 1);
int lineno = html_context->part->cy + html_context->part->box.y;
struct k_image *im = NULL;
/* data will be freed later */
int how_many = add_kitty_image_to_document(document, data, datalen, lineno, &im, width, height);
int how_many = add_kitty_image_to_document(document, pixels, lineno, &im, width, height);
if (!im) {
return;

View File

@ -21,6 +21,9 @@
#include "elinks.h"
#include "terminal/image.h"
#ifdef CONFIG_KITTY
#include "terminal/kitty.h"
#endif
#include "util/base64.h"
#include "util/memory.h"
#include "util/string.h"
@ -31,17 +34,25 @@
#endif
#ifdef CONFIG_KITTY
char *
el_kitty_get_image(char *data, int length, int *outlen, int *width, int *height, int *compressed)
struct el_string *
el_kitty_get_image(char *data, int length, int *width, int *height, int *compressed)
{
ELOG
int comp;
int outlen = 0;
unsigned char *pixels = stbi_load_from_memory((unsigned char *)data, length, width, height, &comp, KITTY_BYTES_PER_PIXEL);
unsigned char *b64;
if (!pixels) {
return NULL;
}
struct el_string *ret = mem_calloc(1, sizeof(*ret));
if (!ret) {
stbi_image_free(pixels);
return NULL;
}
int size = *width * *height * KITTY_BYTES_PER_PIXEL;
*compressed = 0;
@ -54,18 +65,36 @@ el_kitty_get_image(char *data, int length, int *outlen, int *width, int *height,
if (res == Z_OK) {
*compressed = 1;
b64 = base64_encode_bin(complace, compsize, outlen);
b64 = base64_encode_bin(complace, compsize, &outlen);
stbi_image_free(pixels);
mem_free(complace);
return (char *)b64;
if (b64) {
ret->data = (char *)b64;
ret->length = outlen;
ret->refcnt = 1;
return ret;
} else {
mem_free(ret);
return NULL;
}
}
mem_free(complace);
}
#endif
b64 = base64_encode_bin(pixels, size, outlen);
b64 = base64_encode_bin(pixels, size, &outlen);
stbi_image_free(pixels);
return (char *)b64;
if (b64) {
ret->data = (char *)b64;
ret->length = outlen;
ret->refcnt = 1;
return ret;
} else {
mem_free(ret);
return NULL;
}
}
#endif

View File

@ -5,8 +5,10 @@
extern "C" {
#endif
struct el_string;
#ifdef CONFIG_KITTY
char *el_kitty_get_image(char *data, int len, int *outlen, int *width, int *height, int *compressed);
struct el_string *el_kitty_get_image(char *data, int len, int *width, int *height, int *compressed);
#endif
#ifdef CONFIG_LIBSIXEL

View File

@ -23,7 +23,7 @@
#include "util/memory.h"
int
add_kitty_image_to_document(struct document *doc, char *data, int datalen, int lineno, struct k_image **imagine, int width, int height)
add_kitty_image_to_document(struct document *doc, struct el_string *pixels, int lineno, struct k_image **imagine, int width, int height)
{
ELOG
struct k_image *im = mem_calloc(1, sizeof(*im));
@ -35,19 +35,12 @@ add_kitty_image_to_document(struct document *doc, char *data, int datalen, int l
if (!im) {
return 0;
}
im->pixels = (struct el_string *)mem_calloc(1, sizeof(struct el_string));
if (!im->pixels) {
mem_free(im);
return 0;
}
im->pixels = pixels;
im->cy = lineno;
im->cx = 0;
im->width = width;
im->height = height;
im->pixels->data = data;
im->pixels->length = datalen;
im->pixels->refcnt = 1;
im->pixels->refcnt++;
int ile = (height + doc->options.cell_height - 1) / doc->options.cell_height;
add_to_list(doc->k_images, im);

View File

@ -42,7 +42,7 @@ void delete_k_image(struct k_image *im);
void try_to_draw_k_images(struct terminal *term, struct string *text);
/* return height of image in terminal lines */
int add_kitty_image_to_document(struct document *doc, char *data, int datalen, int lineno, struct k_image **imagine, int width, int height);
int add_kitty_image_to_document(struct document *doc, struct el_string *pixels, int lineno, struct k_image **imagine, int width, int height);
struct k_image *copy_k_frame(struct k_image *src, struct el_box *box, int cell_width, int cell_height, int dx, int dy);