qemu_ram_free: Implement it
[qemu/kraxel.git] / vnc-encoding-tight.c
blobfaba4834cd36b4fbe678938d19f654f1516c3e1b
1 /*
2 * QEMU VNC display driver: tight encoding
4 * From libvncserver/libvncserver/tight.c
5 * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
6 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
8 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include "qdict.h"
30 #include "qint.h"
31 #include "vnc.h"
32 #include "vnc-encoding-tight.h"
34 /* Compression level stuff. The following array contains various
35 encoder parameters for each of 10 compression levels (0..9).
36 Last three parameters correspond to JPEG quality levels (0..9). */
38 static const struct {
39 int max_rect_size, max_rect_width;
40 int mono_min_rect_size, gradient_min_rect_size;
41 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
42 int gradient_threshold, gradient_threshold24;
43 int idx_max_colors_divisor;
44 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
45 } tight_conf[] = {
46 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
47 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
48 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
49 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
50 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
51 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
52 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
53 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
54 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
55 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
59 * Code to determine how many different colors used in rectangle.
62 static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6])
64 memset(buf, 0, 6);
66 if (bpp == 32) {
67 buf[0] = ((rgb >> 24) & 0xFF);
68 buf[1] = ((rgb >> 16) & 0xFF);
69 buf[2] = ((rgb >> 8) & 0xFF);
70 buf[3] = ((rgb >> 0) & 0xFF);
71 buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2;
72 buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0;
73 buf[0] |= 1;
74 buf[1] |= 1;
75 buf[2] |= 1;
76 buf[3] |= 1;
78 if (bpp == 16) {
79 buf[0] = ((rgb >> 8) & 0xFF);
80 buf[1] = ((rgb >> 0) & 0xFF);
81 buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0;
82 buf[0] |= 1;
83 buf[1] |= 1;
87 static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf)
89 uint32_t rgb = 0;
91 if (bpp == 32) {
92 rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24;
93 rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16;
94 rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8;
95 rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0;
97 if (bpp == 16) {
98 rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8;
99 rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0;
101 return rgb;
105 static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max)
107 uint8_t key[6];
108 int idx = qdict_size(palette);
109 bool present;
111 tight_palette_rgb2buf(rgb, bpp, key);
112 present = qdict_haskey(palette, (char *)key);
113 if (idx >= max && !present) {
114 return 0;
116 if (!present) {
117 qdict_put(palette, (char *)key, qint_from_int(idx));
119 return qdict_size(palette);
122 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
124 static int \
125 tight_fill_palette##bpp(VncState *vs, int x, int y, \
126 int max, size_t count, \
127 uint32_t *bg, uint32_t *fg, \
128 struct QDict **palette) { \
129 uint##bpp##_t *data; \
130 uint##bpp##_t c0, c1, ci; \
131 int i, n0, n1; \
133 data = (uint##bpp##_t *)vs->tight.buffer; \
135 c0 = data[0]; \
136 i = 1; \
137 while (i < count && data[i] == c0) \
138 i++; \
139 if (i >= count) { \
140 *bg = *fg = c0; \
141 return 1; \
144 if (max < 2) { \
145 return 0; \
148 n0 = i; \
149 c1 = data[i]; \
150 n1 = 0; \
151 for (i++; i < count; i++) { \
152 ci = data[i]; \
153 if (ci == c0) { \
154 n0++; \
155 } else if (ci == c1) { \
156 n1++; \
157 } else \
158 break; \
160 if (i >= count) { \
161 if (n0 > n1) { \
162 *bg = (uint32_t)c0; \
163 *fg = (uint32_t)c1; \
164 } else { \
165 *bg = (uint32_t)c1; \
166 *fg = (uint32_t)c0; \
168 return 2; \
171 if (max == 2) { \
172 return 0; \
175 *palette = qdict_new(); \
176 tight_palette_insert(*palette, c0, bpp, max); \
177 tight_palette_insert(*palette, c1, bpp, max); \
178 tight_palette_insert(*palette, ci, bpp, max); \
180 for (i++; i < count; i++) { \
181 if (data[i] == ci) { \
182 continue; \
183 } else { \
184 if (!tight_palette_insert(*palette, (uint32_t)ci, \
185 bpp, max)) { \
186 return 0; \
188 ci = data[i]; \
192 return qdict_size(*palette); \
195 DEFINE_FILL_PALETTE_FUNCTION(8)
196 DEFINE_FILL_PALETTE_FUNCTION(16)
197 DEFINE_FILL_PALETTE_FUNCTION(32)
199 static int tight_fill_palette(VncState *vs, int x, int y,
200 size_t count, uint32_t *bg, uint32_t *fg,
201 struct QDict **palette)
203 int max;
205 max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor;
206 if (max < 2 &&
207 count >= tight_conf[vs->tight_compression].mono_min_rect_size) {
208 max = 2;
210 if (max >= 256) {
211 max = 256;
214 switch(vs->clientds.pf.bytes_per_pixel) {
215 case 4:
216 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
217 case 2:
218 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
219 default:
220 max = 2;
221 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
223 return 0;
226 /* Callback to dump a palette with qdict_iter
227 static void print_palette(const char *key, QObject *obj, void *opaque)
229 uint8_t idx = qint_get_int(qobject_to_qint(obj));
230 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
232 fprintf(stderr, "%.2x ", (unsigned char)*key);
233 while (*key++)
234 fprintf(stderr, "%.2x ", (unsigned char)*key);
236 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
241 * Converting truecolor samples into palette indices.
243 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
245 static void \
246 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
247 struct QDict *palette) { \
248 uint##bpp##_t *src; \
249 uint##bpp##_t rgb; \
250 uint8_t key[6]; \
251 int i, rep; \
252 uint8_t idx; \
254 src = (uint##bpp##_t *) buf; \
256 for (i = 0; i < count; i++) { \
257 rgb = *src++; \
258 rep = 0; \
259 while (i < count && *src == rgb) { \
260 rep++, src++, i++; \
262 tight_palette_rgb2buf(rgb, bpp, key); \
263 if (!qdict_haskey(palette, (char *)key)) { \
264 /* \
265 * Should never happen, but don't break everything \
266 * if it does, use the first color instead \
267 */ \
268 idx = 0; \
269 } else { \
270 idx = qdict_get_int(palette, (char *)key); \
272 while (rep >= 0) { \
273 *buf++ = idx; \
274 rep--; \
279 DEFINE_IDX_ENCODE_FUNCTION(16)
280 DEFINE_IDX_ENCODE_FUNCTION(32)
282 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
284 static void \
285 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
286 uint##bpp##_t bg, uint##bpp##_t fg) { \
287 uint##bpp##_t *ptr; \
288 unsigned int value, mask; \
289 int aligned_width; \
290 int x, y, bg_bits; \
292 ptr = (uint##bpp##_t *) buf; \
293 aligned_width = w - w % 8; \
295 for (y = 0; y < h; y++) { \
296 for (x = 0; x < aligned_width; x += 8) { \
297 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
298 if (*ptr++ != bg) { \
299 break; \
302 if (bg_bits == 8) { \
303 *buf++ = 0; \
304 continue; \
306 mask = 0x80 >> bg_bits; \
307 value = mask; \
308 for (bg_bits++; bg_bits < 8; bg_bits++) { \
309 mask >>= 1; \
310 if (*ptr++ != bg) { \
311 value |= mask; \
314 *buf++ = (uint8_t)value; \
317 mask = 0x80; \
318 value = 0; \
319 if (x >= w) { \
320 continue; \
323 for (; x < w; x++) { \
324 if (*ptr++ != bg) { \
325 value |= mask; \
327 mask >>= 1; \
329 *buf++ = (uint8_t)value; \
333 DEFINE_MONO_ENCODE_FUNCTION(8)
334 DEFINE_MONO_ENCODE_FUNCTION(16)
335 DEFINE_MONO_ENCODE_FUNCTION(32)
338 * Check if a rectangle is all of the same color. If needSameColor is
339 * set to non-zero, then also check that its color equals to the
340 * *colorPtr value. The result is 1 if the test is successfull, and in
341 * that case new color will be stored in *colorPtr.
344 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
346 static bool \
347 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
348 uint32_t* color, bool samecolor) \
350 VncDisplay *vd = vs->vd; \
351 uint##bpp##_t *fbptr; \
352 uint##bpp##_t c; \
353 int dx, dy; \
355 fbptr = (uint##bpp##_t *) \
356 (vd->server->data + y * ds_get_linesize(vs->ds) + \
357 x * ds_get_bytes_per_pixel(vs->ds)); \
359 c = *fbptr; \
360 if (samecolor && (uint32_t)c != *color) { \
361 return false; \
364 for (dy = 0; dy < h; dy++) { \
365 for (dx = 0; dx < w; dx++) { \
366 if (c != fbptr[dx]) { \
367 return false; \
370 fbptr = (uint##bpp##_t *) \
371 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
374 *color = (uint32_t)c; \
375 return true; \
378 DEFINE_CHECK_SOLID_FUNCTION(32)
379 DEFINE_CHECK_SOLID_FUNCTION(16)
380 DEFINE_CHECK_SOLID_FUNCTION(8)
382 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
383 uint32_t* color, bool samecolor)
385 VncDisplay *vd = vs->vd;
387 switch(vd->server->pf.bytes_per_pixel) {
388 case 4:
389 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
390 case 2:
391 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
392 default:
393 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
397 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
398 uint32_t color, int *w_ptr, int *h_ptr)
400 int dx, dy, dw, dh;
401 int w_prev;
402 int w_best = 0, h_best = 0;
404 w_prev = w;
406 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
408 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
409 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
411 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
412 break;
415 for (dx = x + dw; dx < x + w_prev;) {
416 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
418 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
419 break;
421 dx += dw;
424 w_prev = dx - x;
425 if (w_prev * (dy + dh - y) > w_best * h_best) {
426 w_best = w_prev;
427 h_best = dy + dh - y;
431 *w_ptr = w_best;
432 *h_ptr = h_best;
435 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
436 uint32_t color, int *x_ptr, int *y_ptr,
437 int *w_ptr, int *h_ptr)
439 int cx, cy;
441 /* Try to extend the area upwards. */
442 for ( cy = *y_ptr - 1;
443 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
444 cy-- );
445 *h_ptr += *y_ptr - (cy + 1);
446 *y_ptr = cy + 1;
448 /* ... downwards. */
449 for ( cy = *y_ptr + *h_ptr;
450 cy < y + h &&
451 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
452 cy++ );
453 *h_ptr += cy - (*y_ptr + *h_ptr);
455 /* ... to the left. */
456 for ( cx = *x_ptr - 1;
457 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
458 cx-- );
459 *w_ptr += *x_ptr - (cx + 1);
460 *x_ptr = cx + 1;
462 /* ... to the right. */
463 for ( cx = *x_ptr + *w_ptr;
464 cx < x + w &&
465 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
466 cx++ );
467 *w_ptr += cx - (*x_ptr + *w_ptr);
470 static int tight_init_stream(VncState *vs, int stream_id,
471 int level, int strategy)
473 z_streamp zstream = &vs->tight_stream[stream_id];
475 if (zstream->opaque == NULL) {
476 int err;
478 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
479 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
480 zstream->zalloc = vnc_zlib_zalloc;
481 zstream->zfree = vnc_zlib_zfree;
483 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
484 MAX_MEM_LEVEL, strategy);
486 if (err != Z_OK) {
487 fprintf(stderr, "VNC: error initializing zlib\n");
488 return -1;
491 vs->tight_levels[stream_id] = level;
492 zstream->opaque = vs;
495 if (vs->tight_levels[stream_id] != level) {
496 if (deflateParams(zstream, level, strategy) != Z_OK) {
497 return -1;
499 vs->tight_levels[stream_id] = level;
501 return 0;
504 static void tight_send_compact_size(VncState *vs, size_t len)
506 int lpc = 0;
507 int bytes = 0;
508 char buf[3] = {0, 0, 0};
510 buf[bytes++] = len & 0x7F;
511 if (len > 0x7F) {
512 buf[bytes-1] |= 0x80;
513 buf[bytes++] = (len >> 7) & 0x7F;
514 if (len > 0x3FFF) {
515 buf[bytes-1] |= 0x80;
516 buf[bytes++] = (len >> 14) & 0xFF;
519 for (lpc = 0; lpc < bytes; lpc++) {
520 vnc_write_u8(vs, buf[lpc]);
524 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
525 int level, int strategy)
527 z_streamp zstream = &vs->tight_stream[stream_id];
528 int previous_out;
530 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
531 vnc_write(vs, vs->tight.buffer, vs->tight.offset);
532 return bytes;
535 if (tight_init_stream(vs, stream_id, level, strategy)) {
536 return -1;
539 /* reserve memory in output buffer */
540 buffer_reserve(&vs->tight_zlib, bytes + 64);
542 /* set pointers */
543 zstream->next_in = vs->tight.buffer;
544 zstream->avail_in = vs->tight.offset;
545 zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset;
546 zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset;
547 zstream->data_type = Z_BINARY;
548 previous_out = zstream->total_out;
550 /* start encoding */
551 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
552 fprintf(stderr, "VNC: error during tight compression\n");
553 return -1;
556 vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out;
557 bytes = zstream->total_out - previous_out;
559 tight_send_compact_size(vs, bytes);
560 vnc_write(vs, vs->tight_zlib.buffer, bytes);
562 buffer_reset(&vs->tight_zlib);
564 return bytes;
568 * Subencoding implementations.
570 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
572 uint32_t *buf32;
573 uint32_t pix;
574 int rshift, gshift, bshift;
576 buf32 = (uint32_t *)buf;
578 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
579 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
580 rshift = vs->clientds.pf.rshift;
581 gshift = vs->clientds.pf.gshift;
582 bshift = vs->clientds.pf.bshift;
583 } else {
584 rshift = 24 - vs->clientds.pf.rshift;
585 gshift = 24 - vs->clientds.pf.gshift;
586 bshift = 24 - vs->clientds.pf.bshift;
589 if (ret) {
590 *ret = count * 3;
593 while (count--) {
594 pix = *buf32++;
595 *buf++ = (char)(pix >> rshift);
596 *buf++ = (char)(pix >> gshift);
597 *buf++ = (char)(pix >> bshift);
601 static int send_full_color_rect(VncState *vs, int w, int h)
603 int stream = 0;
604 size_t bytes;
606 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
608 if (vs->tight_pixel24) {
609 tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset);
610 bytes = 3;
611 } else {
612 bytes = vs->clientds.pf.bytes_per_pixel;
615 bytes = tight_compress_data(vs, stream, w * h * bytes,
616 tight_conf[vs->tight_compression].raw_zlib_level,
617 Z_DEFAULT_STRATEGY);
619 return (bytes >= 0);
622 static int send_solid_rect(VncState *vs)
624 size_t bytes;
626 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
628 if (vs->tight_pixel24) {
629 tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
630 bytes = 3;
631 } else {
632 bytes = vs->clientds.pf.bytes_per_pixel;
635 vnc_write(vs, vs->tight.buffer, bytes);
636 return 1;
639 static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg)
641 size_t bytes;
642 int stream = 1;
643 int level = tight_conf[vs->tight_compression].mono_zlib_level;
645 bytes = ((w + 7) / 8) * h;
647 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
648 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
649 vnc_write_u8(vs, 1);
651 switch(vs->clientds.pf.bytes_per_pixel) {
652 case 4:
654 uint32_t buf[2] = {bg, fg};
655 size_t ret = sizeof (buf);
657 if (vs->tight_pixel24) {
658 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
660 vnc_write(vs, buf, ret);
662 tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg);
663 break;
665 case 2:
666 vnc_write(vs, &bg, 2);
667 vnc_write(vs, &fg, 2);
668 tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg);
669 break;
670 default:
671 vnc_write_u8(vs, bg);
672 vnc_write_u8(vs, fg);
673 tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg);
674 break;
676 vs->tight.offset = bytes;
678 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
679 return (bytes >= 0);
682 struct palette_cb_priv {
683 VncState *vs;
684 uint8_t *header;
687 static void write_palette(const char *key, QObject *obj, void *opaque)
689 struct palette_cb_priv *priv = opaque;
690 VncState *vs = priv->vs;
691 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
692 uint8_t idx = qint_get_int(qobject_to_qint(obj));
694 if (bytes == 4) {
695 uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
697 ((uint32_t*)priv->header)[idx] = color;
698 } else {
699 uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
701 ((uint16_t*)priv->header)[idx] = color;
705 static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette)
707 int stream = 2;
708 int level = tight_conf[vs->tight_compression].idx_zlib_level;
709 int colors;
710 size_t bytes;
712 colors = qdict_size(palette);
714 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
715 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
716 vnc_write_u8(vs, colors - 1);
718 switch(vs->clientds.pf.bytes_per_pixel) {
719 case 4:
721 size_t old_offset, offset;
722 uint32_t header[qdict_size(palette)];
723 struct palette_cb_priv priv = { vs, (uint8_t *)header };
725 old_offset = vs->output.offset;
726 qdict_iter(palette, write_palette, &priv);
727 vnc_write(vs, header, sizeof(header));
729 if (vs->tight_pixel24) {
730 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
731 vs->output.offset = old_offset + offset;
734 tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette);
735 break;
737 case 2:
739 uint16_t header[qdict_size(palette)];
740 struct palette_cb_priv priv = { vs, (uint8_t *)header };
742 qdict_iter(palette, write_palette, &priv);
743 vnc_write(vs, header, sizeof(header));
744 tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette);
745 break;
747 default:
748 return -1; /* No palette for 8bits colors */
749 break;
751 bytes = w * h;
752 vs->tight.offset = bytes;
754 bytes = tight_compress_data(vs, stream, bytes,
755 level, Z_DEFAULT_STRATEGY);
756 return (bytes >= 0);
759 static void vnc_tight_start(VncState *vs)
761 buffer_reset(&vs->tight);
763 // make the output buffer be the zlib buffer, so we can compress it later
764 vs->tight_tmp = vs->output;
765 vs->output = vs->tight;
768 static void vnc_tight_stop(VncState *vs)
770 // switch back to normal output/zlib buffers
771 vs->tight = vs->output;
772 vs->output = vs->tight_tmp;
775 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
777 struct QDict *palette = NULL;
778 uint32_t bg = 0, fg = 0;
779 int colors;
780 int ret = 0;
782 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
784 vnc_tight_start(vs);
785 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
786 vnc_tight_stop(vs);
788 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
790 if (colors == 0) {
791 ret = send_full_color_rect(vs, w, h);
792 } else if (colors == 1) {
793 ret = send_solid_rect(vs);
794 } else if (colors == 2) {
795 ret = send_mono_rect(vs, w, h, bg, fg);
796 } else if (colors <= 256) {
797 ret = send_palette_rect(vs, w, h, palette);
799 QDECREF(palette);
800 return ret;
803 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
805 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
807 vnc_tight_start(vs);
808 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
809 vnc_tight_stop(vs);
811 return send_solid_rect(vs);
814 static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
816 int max_size, max_width;
817 int max_sub_width, max_sub_height;
818 int dx, dy;
819 int rw, rh;
820 int n = 0;
822 max_size = tight_conf[vs->tight_compression].max_rect_size;
823 max_width = tight_conf[vs->tight_compression].max_rect_width;
825 if (w > max_width || w * h > max_size) {
826 max_sub_width = (w > max_width) ? max_width : w;
827 max_sub_height = max_size / max_sub_width;
829 for (dy = 0; dy < h; dy += max_sub_height) {
830 for (dx = 0; dx < w; dx += max_width) {
831 rw = MIN(max_sub_width, w - dx);
832 rh = MIN(max_sub_height, h - dy);
833 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
836 } else {
837 n += send_sub_rect(vs, x, y, w, h);
840 return n;
843 static int find_large_solid_color_rect(VncState *vs, int x, int y,
844 int w, int h, int max_rows)
846 int dx, dy, dw, dh;
847 int n = 0;
849 /* Try to find large solid-color areas and send them separately. */
851 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
853 /* If a rectangle becomes too large, send its upper part now. */
855 if (dy - y >= max_rows) {
856 n += send_rect_simple(vs, x, y, w, max_rows);
857 y += max_rows;
858 h -= max_rows;
861 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
863 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
864 uint32_t color_value;
865 int x_best, y_best, w_best, h_best;
867 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
869 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
870 continue ;
873 /* Get dimensions of solid-color area. */
875 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
876 color_value, &w_best, &h_best);
878 /* Make sure a solid rectangle is large enough
879 (or the whole rectangle is of the same color). */
881 if (w_best * h_best != w * h &&
882 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
883 continue;
886 /* Try to extend solid rectangle to maximum size. */
888 x_best = dx; y_best = dy;
889 extend_solid_area(vs, x, y, w, h, color_value,
890 &x_best, &y_best, &w_best, &h_best);
892 /* Send rectangles at top and left to solid-color area. */
894 if (y_best != y) {
895 n += send_rect_simple(vs, x, y, w, y_best-y);
897 if (x_best != x) {
898 n += vnc_tight_send_framebuffer_update(vs, x, y_best,
899 x_best-x, h_best);
902 /* Send solid-color rectangle. */
903 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
905 /* Send remaining rectangles (at right and bottom). */
907 if (x_best + w_best != x + w) {
908 n += vnc_tight_send_framebuffer_update(vs, x_best+w_best,
909 y_best,
910 w-(x_best-x)-w_best,
911 h_best);
913 if (y_best + h_best != y + h) {
914 n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best,
915 w, h-(y_best-y)-h_best);
918 /* Return after all recursive calls are done. */
919 return n;
922 return n + send_rect_simple(vs, x, y, w, h);
925 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
926 int w, int h)
928 int max_rows;
930 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
931 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
932 vs->tight_pixel24 = true;
933 } else {
934 vs->tight_pixel24 = false;
937 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
938 return send_rect_simple(vs, x, y, w, h);
940 /* Calculate maximum number of rows in one non-solid rectangle. */
942 max_rows = tight_conf[vs->tight_compression].max_rect_size;
943 max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w);
945 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
948 void vnc_tight_clear(VncState *vs)
950 int i;
951 for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) {
952 if (vs->tight_stream[i].opaque) {
953 deflateEnd(&vs->tight_stream[i]);
957 buffer_free(&vs->tight);
958 buffer_free(&vs->tight_zlib);