Have example/imageviewer avoid big xcb_image_put's

This commit is contained in:
Nigel Tao committed 2021-10-07 12:35:12 +11:00
1 parent ea4c85bfa2
commit 2945ef2b29
1 file changed
+51 -26
+51 -26
View File
@@ -87,8 +87,8 @@ The Escape key quits.
// program to generate a stand-alone C file. // program to generate a stand-alone C file.
#include "../../release/c/wuffs-unsupported-snapshot.c" #include "../../release/c/wuffs-unsupported-snapshot.c"
// X11 limits its image dimensions to uint16_t. // X11 limits its image dimensions to uint16_t and some coordinates to int16_t.
#define MAX_INCL_DIMENSION 65535 #define MAX_INCL_DIMENSION 32767
#define NUM_BACKGROUND_COLORS 3 #define NUM_BACKGROUND_COLORS 3
#define NUM_ZOOMS 8 #define NUM_ZOOMS 8
@@ -186,6 +186,7 @@ load_image(const char* filename) {
#define XK_Escape 0xFF1B #define XK_Escape 0xFF1B
#define XK_Return 0xFF0D #define XK_Return 0xFF0D
uint32_t g_maximum_request_length = 0; // Measured in 4-byte units.
xcb_atom_t g_atom_net_wm_name = XCB_NONE; xcb_atom_t g_atom_net_wm_name = XCB_NONE;
xcb_atom_t g_atom_utf8_string = XCB_NONE; xcb_atom_t g_atom_utf8_string = XCB_NONE;
xcb_atom_t g_atom_wm_protocols = XCB_NONE; xcb_atom_t g_atom_wm_protocols = XCB_NONE;
@@ -288,36 +289,63 @@ load(xcb_connection_t* c, xcb_window_t w, const char* filename) {
0, NULL); 0, NULL);
apply_zoom_and_filter(c); apply_zoom_and_filter(c);
// Make libxcb-image interpret WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL as // Copy the pixels from the X11 client process (this process) to the X11
// XCB_PICT_STANDARD_ARGB_32 with byte_order XCB_IMAGE_ORDER_LSB_FIRST. // server process. For large images, this may involve multiple xcb_image_put
xcb_image_t* unconverted = // calls, each copying part of the pixels (a strip that has the same width
xcb_image_create(g_width, // width // but smaller height), to avoid XCB_CONN_CLOSED_REQ_LEN_EXCEED.
g_height, // height if (g_width > 0) {
XCB_IMAGE_FORMAT_Z_PIXMAP, // format uint32_t max_strip_height = g_maximum_request_length / g_width;
32, // xpad for (uint32_t y = 0; y < g_height;) {
g_pictforminfo->depth, // depth uint32_t h = g_height - y;
32, // bpp if (h > max_strip_height) {
32, // unit h = max_strip_height;
XCB_IMAGE_ORDER_LSB_FIRST, // byte_order }
XCB_IMAGE_ORDER_MSB_FIRST, // bit_order
NULL, // base
tab.height * tab.stride, // bytes
tab.ptr); // data
xcb_image_t* converted = // Make libxcb-image interpret WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL as
xcb_image_native(c, unconverted, true); // true means to convert. // XCB_PICT_STANDARD_ARGB_32 with byte_order XCB_IMAGE_ORDER_LSB_FIRST.
if (converted != unconverted) { xcb_image_t* unconverted =
xcb_image_destroy(unconverted); xcb_image_create(g_width, // width
h, // height
XCB_IMAGE_FORMAT_Z_PIXMAP, // format
32, // xpad
g_pictforminfo->depth, // depth
32, // bpp
32, // unit
XCB_IMAGE_ORDER_LSB_FIRST, // byte_order
XCB_IMAGE_ORDER_MSB_FIRST, // bit_order
NULL, // base
h * tab.stride, // bytes
tab.ptr + (y * tab.stride)); // data
xcb_image_t* converted =
xcb_image_native(c, unconverted, true); // true means to convert.
if (converted != unconverted) {
xcb_image_destroy(unconverted);
}
xcb_image_put(c, g_pixmap, g_pixmap_gc, converted, 0, y, 0);
xcb_image_destroy(converted);
y += h;
}
} }
xcb_image_put(c, g_pixmap, g_pixmap_gc, converted, 0, 0, 0);
xcb_image_destroy(converted);
return true; return true;
} }
int // int //
main(int argc, char** argv) { main(int argc, char** argv) {
xcb_connection_t* c = xcb_connect(NULL, NULL); xcb_connection_t* c = xcb_connect(NULL, NULL);
g_maximum_request_length = xcb_get_maximum_request_length(c);
// Our X11 requests (especially xcb_image_put) also need a header, in terms
// of wire format. 256 4-byte units should be big enough.
const uint32_t max_req_len_adjustment = 256;
if (g_maximum_request_length < max_req_len_adjustment) {
printf("XCB failure (maximum request length is too short)\n");
exit(EXIT_FAILURE);
}
g_maximum_request_length -= max_req_len_adjustment;
const xcb_setup_t* z = xcb_get_setup(c); const xcb_setup_t* z = xcb_get_setup(c);
xcb_screen_t* s = xcb_setup_roots_iterator(z).data; xcb_screen_t* s = xcb_setup_roots_iterator(z).data;
@@ -366,9 +394,6 @@ main(int argc, char** argv) {
while (true) { while (true) {
xcb_generic_event_t* event = xcb_wait_for_event(c); xcb_generic_event_t* event = xcb_wait_for_event(c);
// XXX: Large pictures may cause XCB_CONN_CLOSED_REQ_LEN_EXCEED because,
// e.g., 4_194_303 * 4 = 16_777_212 = not nearly enough,
// requiring some trickery to be uploaded to the server.
if (!event) { if (!event) {
printf("XCB failure (error code %d)\n", xcb_connection_has_error(c)); printf("XCB failure (error code %d)\n", xcb_connection_has_error(c));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);