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
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). */
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
;
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])
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;
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;
87 static uint32_t tight_palette_buf2rgb(int bpp
, const uint8_t *buf
)
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;
98 rgb
|= ((buf
[0] & ~1) | !((buf
[2] >> 1) & 1)) << 8;
99 rgb
|= ((buf
[1] & ~1) | !((buf
[2] >> 0) & 1)) << 0;
105 static int tight_palette_insert(QDict
*palette
, uint32_t rgb
, int bpp
, int max
)
108 int idx
= qdict_size(palette
);
111 tight_palette_rgb2buf(rgb
, bpp
, key
);
112 present
= qdict_haskey(palette
, (char *)key
);
113 if (idx
>= max
&& !present
) {
117 qdict_put(palette
, (char *)key
, qint_from_int(idx
));
119 return qdict_size(palette
);
122 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
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; \
133 data = (uint##bpp##_t *)vs->tight.buffer; \
137 while (i < count && data[i] == c0) \
151 for (i++; i < count; i++) { \
155 } else if (ci == c1) { \
162 *bg = (uint32_t)c0; \
163 *fg = (uint32_t)c1; \
165 *bg = (uint32_t)c1; \
166 *fg = (uint32_t)c0; \
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) { \
184 if (!tight_palette_insert(*palette, (uint32_t)ci, \
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
)
205 max
= count
/ tight_conf
[vs
->tight_compression
].idx_max_colors_divisor
;
207 count
>= tight_conf
[vs
->tight_compression
].mono_min_rect_size
) {
214 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
216 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
218 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
221 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
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);
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) \
246 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
247 struct QDict *palette) { \
248 uint##bpp##_t *src; \
254 src = (uint##bpp##_t *) buf; \
256 for (i = 0; i < count; i++) { \
259 while (i < count && *src == rgb) { \
262 tight_palette_rgb2buf(rgb, bpp, key); \
263 if (!qdict_haskey(palette, (char *)key)) { \
265 * Should never happen, but don't break everything \
266 * if it does, use the first color instead \
270 idx = qdict_get_int(palette, (char *)key); \
279 DEFINE_IDX_ENCODE_FUNCTION(16)
280 DEFINE_IDX_ENCODE_FUNCTION(32)
282 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
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; \
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) { \
302 if (bg_bits == 8) { \
306 mask = 0x80 >> bg_bits; \
308 for (bg_bits++; bg_bits < 8; bg_bits++) { \
310 if (*ptr++ != bg) { \
314 *buf++ = (uint8_t)value; \
323 for (; x < w; x++) { \
324 if (*ptr++ != bg) { \
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) \
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; \
355 fbptr = (uint##bpp##_t *) \
356 (vd->server->data + y * ds_get_linesize(vs->ds) + \
357 x * ds_get_bytes_per_pixel(vs->ds)); \
360 if (samecolor && (uint32_t)c != *color) { \
364 for (dy = 0; dy < h; dy++) { \
365 for (dx = 0; dx < w; dx++) { \
366 if (c != fbptr[dx]) { \
370 fbptr = (uint##bpp##_t *) \
371 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
374 *color = (uint32_t)c; \
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
) {
389 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
391 return check_solid_tile16(vs
, x
, y
, w
, h
, color
, samecolor
);
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
)
402 int w_best
= 0, h_best
= 0;
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)) {
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)) {
425 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
427 h_best
= dy
+ dh
- y
;
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
)
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);
445 *h_ptr
+= *y_ptr
- (cy
+ 1);
449 for ( cy
= *y_ptr
+ *h_ptr
;
451 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
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);
459 *w_ptr
+= *x_ptr
- (cx
+ 1);
462 /* ... to the right. */
463 for ( cx
= *x_ptr
+ *w_ptr
;
465 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
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
) {
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
);
487 fprintf(stderr
, "VNC: error initializing zlib\n");
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
) {
499 vs
->tight_levels
[stream_id
] = level
;
504 static void tight_send_compact_size(VncState
*vs
, size_t len
)
508 char buf
[3] = {0, 0, 0};
510 buf
[bytes
++] = len
& 0x7F;
512 buf
[bytes
-1] |= 0x80;
513 buf
[bytes
++] = (len
>> 7) & 0x7F;
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
];
530 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
531 vnc_write(vs
, vs
->tight
.buffer
, vs
->tight
.offset
);
535 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
539 /* reserve memory in output buffer */
540 buffer_reserve(&vs
->tight_zlib
, bytes
+ 64);
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
;
551 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
552 fprintf(stderr
, "VNC: error during tight compression\n");
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
);
568 * Subencoding implementations.
570 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
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
;
584 rshift
= 24 - vs
->clientds
.pf
.rshift
;
585 gshift
= 24 - vs
->clientds
.pf
.gshift
;
586 bshift
= 24 - vs
->clientds
.pf
.bshift
;
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
)
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
);
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
,
622 static int send_solid_rect(VncState
*vs
)
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
);
632 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
635 vnc_write(vs
, vs
->tight
.buffer
, bytes
);
639 static int send_mono_rect(VncState
*vs
, int w
, int h
, uint32_t bg
, uint32_t fg
)
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
);
651 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
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
);
666 vnc_write(vs
, &bg
, 2);
667 vnc_write(vs
, &fg
, 2);
668 tight_encode_mono_rect16(vs
->tight
.buffer
, w
, h
, bg
, fg
);
671 vnc_write_u8(vs
, bg
);
672 vnc_write_u8(vs
, fg
);
673 tight_encode_mono_rect8(vs
->tight
.buffer
, w
, h
, bg
, fg
);
676 vs
->tight
.offset
= bytes
;
678 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
682 struct palette_cb_priv
{
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
));
695 uint32_t color
= tight_palette_buf2rgb(32, (uint8_t *)key
);
697 ((uint32_t*)priv
->header
)[idx
] = color
;
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
)
708 int level
= tight_conf
[vs
->tight_compression
].idx_zlib_level
;
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
) {
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
);
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
);
748 return -1; /* No palette for 8bits colors */
752 vs
->tight
.offset
= bytes
;
754 bytes
= tight_compress_data(vs
, stream
, bytes
,
755 level
, Z_DEFAULT_STRATEGY
);
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;
782 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
785 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
788 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
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
);
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
);
808 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
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
;
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
);
837 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
843 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
844 int w
, int h
, int max_rows
)
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
);
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)) {
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
) {
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. */
895 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
);
898 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_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
,
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. */
922 return n
+ send_rect_simple(vs
, x
, y
, w
, h
);
925 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
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;
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
)
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
);