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
34 #include "vnc-encoding-tight.h"
36 /* Compression level stuff. The following array contains various
37 encoder parameters for each of 10 compression levels (0..9).
38 Last three parameters correspond to JPEG quality levels (0..9). */
41 int max_rect_size
, max_rect_width
;
42 int mono_min_rect_size
, gradient_min_rect_size
;
43 int idx_zlib_level
, mono_zlib_level
, raw_zlib_level
, gradient_zlib_level
;
44 int gradient_threshold
, gradient_threshold24
;
45 int idx_max_colors_divisor
;
46 int jpeg_quality
, jpeg_threshold
, jpeg_threshold24
;
48 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
49 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
50 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
51 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
52 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
53 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
54 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
55 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
56 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
57 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
61 * Code to determine how many different colors used in rectangle.
64 static void tight_palette_rgb2buf(uint32_t rgb
, int bpp
, uint8_t buf
[6])
69 buf
[0] = ((rgb
>> 24) & 0xFF);
70 buf
[1] = ((rgb
>> 16) & 0xFF);
71 buf
[2] = ((rgb
>> 8) & 0xFF);
72 buf
[3] = ((rgb
>> 0) & 0xFF);
73 buf
[4] = ((buf
[0] & 1) == 0) << 3 | ((buf
[1] & 1) == 0) << 2;
74 buf
[4]|= ((buf
[2] & 1) == 0) << 1 | ((buf
[3] & 1) == 0) << 0;
81 buf
[0] = ((rgb
>> 8) & 0xFF);
82 buf
[1] = ((rgb
>> 0) & 0xFF);
83 buf
[2] = ((buf
[0] & 1) == 0) << 1 | ((buf
[1] & 1) == 0) << 0;
89 static uint32_t tight_palette_buf2rgb(int bpp
, const uint8_t *buf
)
94 rgb
|= ((buf
[0] & ~1) | !((buf
[4] >> 3) & 1)) << 24;
95 rgb
|= ((buf
[1] & ~1) | !((buf
[4] >> 2) & 1)) << 16;
96 rgb
|= ((buf
[2] & ~1) | !((buf
[4] >> 1) & 1)) << 8;
97 rgb
|= ((buf
[3] & ~1) | !((buf
[4] >> 0) & 1)) << 0;
100 rgb
|= ((buf
[0] & ~1) | !((buf
[2] >> 1) & 1)) << 8;
101 rgb
|= ((buf
[1] & ~1) | !((buf
[2] >> 0) & 1)) << 0;
107 static int tight_palette_insert(QDict
*palette
, uint32_t rgb
, int bpp
, int max
)
110 int idx
= qdict_size(palette
);
113 tight_palette_rgb2buf(rgb
, bpp
, key
);
114 present
= qdict_haskey(palette
, (char *)key
);
115 if (idx
>= max
&& !present
) {
119 qdict_put(palette
, (char *)key
, qint_from_int(idx
));
121 return qdict_size(palette
);
124 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
127 tight_fill_palette##bpp(VncState *vs, int x, int y, \
128 int max, size_t count, \
129 uint32_t *bg, uint32_t *fg, \
130 struct QDict **palette) { \
131 uint##bpp##_t *data; \
132 uint##bpp##_t c0, c1, ci; \
135 data = (uint##bpp##_t *)vs->tight.buffer; \
139 while (i < count && data[i] == c0) \
153 for (i++; i < count; i++) { \
157 } else if (ci == c1) { \
164 *bg = (uint32_t)c0; \
165 *fg = (uint32_t)c1; \
167 *bg = (uint32_t)c1; \
168 *fg = (uint32_t)c0; \
177 *palette = qdict_new(); \
178 tight_palette_insert(*palette, c0, bpp, max); \
179 tight_palette_insert(*palette, c1, bpp, max); \
181 for (i++; i < count; i++) { \
182 if (data[i] == ci) { \
185 if (!tight_palette_insert(*palette, (uint32_t)ci, \
193 return qdict_size(*palette); \
196 DEFINE_FILL_PALETTE_FUNCTION(8)
197 DEFINE_FILL_PALETTE_FUNCTION(16)
198 DEFINE_FILL_PALETTE_FUNCTION(32)
200 static int tight_fill_palette(VncState
*vs
, int x
, int y
,
201 size_t count
, uint32_t *bg
, uint32_t *fg
,
202 struct QDict
**palette
)
206 max
= count
/ tight_conf
[vs
->tight_compression
].idx_max_colors_divisor
;
208 count
>= tight_conf
[vs
->tight_compression
].mono_min_rect_size
) {
215 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
217 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
219 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
222 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
227 /* Callback to dump a palette with qdict_iter
228 static void print_palette(const char *key, QObject *obj, void *opaque)
230 uint8_t idx = qint_get_int(qobject_to_qint(obj));
231 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
233 fprintf(stderr, "%.2x ", (unsigned char)*key);
235 fprintf(stderr, "%.2x ", (unsigned char)*key);
237 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
242 * Converting truecolor samples into palette indices.
244 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
247 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
248 struct QDict *palette) { \
249 uint##bpp##_t *src; \
255 src = (uint##bpp##_t *) buf; \
257 for (i = 0; i < count; i++) { \
260 while (i < count && *src == rgb) { \
263 tight_palette_rgb2buf(rgb, bpp, key); \
264 if (!qdict_haskey(palette, (char *)key)) { \
266 * Should never happen, but don't break everything \
267 * if it does, use the first color instead \
271 idx = qdict_get_int(palette, (char *)key); \
280 DEFINE_IDX_ENCODE_FUNCTION(16)
281 DEFINE_IDX_ENCODE_FUNCTION(32)
283 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
286 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
287 uint##bpp##_t bg, uint##bpp##_t fg) { \
288 uint##bpp##_t *ptr; \
289 unsigned int value, mask; \
293 ptr = (uint##bpp##_t *) buf; \
294 aligned_width = w - w % 8; \
296 for (y = 0; y < h; y++) { \
297 for (x = 0; x < aligned_width; x += 8) { \
298 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
299 if (*ptr++ != bg) { \
303 if (bg_bits == 8) { \
307 mask = 0x80 >> bg_bits; \
309 for (bg_bits++; bg_bits < 8; bg_bits++) { \
311 if (*ptr++ != bg) { \
315 *buf++ = (uint8_t)value; \
324 for (; x < w; x++) { \
325 if (*ptr++ != bg) { \
330 *buf++ = (uint8_t)value; \
334 DEFINE_MONO_ENCODE_FUNCTION(8)
335 DEFINE_MONO_ENCODE_FUNCTION(16)
336 DEFINE_MONO_ENCODE_FUNCTION(32)
339 * Check if a rectangle is all of the same color. If needSameColor is
340 * set to non-zero, then also check that its color equals to the
341 * *colorPtr value. The result is 1 if the test is successfull, and in
342 * that case new color will be stored in *colorPtr.
345 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
348 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
349 uint32_t* color, bool samecolor) \
351 VncDisplay *vd = vs->vd; \
352 uint##bpp##_t *fbptr; \
356 fbptr = (uint##bpp##_t *) \
357 (vd->server->data + y * ds_get_linesize(vs->ds) + \
358 x * ds_get_bytes_per_pixel(vs->ds)); \
361 if (samecolor && (uint32_t)c != *color) { \
365 for (dy = 0; dy < h; dy++) { \
366 for (dx = 0; dx < w; dx++) { \
367 if (c != fbptr[dx]) { \
371 fbptr = (uint##bpp##_t *) \
372 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
375 *color = (uint32_t)c; \
379 DEFINE_CHECK_SOLID_FUNCTION(32)
380 DEFINE_CHECK_SOLID_FUNCTION(16)
381 DEFINE_CHECK_SOLID_FUNCTION(8)
383 static bool check_solid_tile(VncState
*vs
, int x
, int y
, int w
, int h
,
384 uint32_t* color
, bool samecolor
)
386 VncDisplay
*vd
= vs
->vd
;
388 switch(vd
->server
->pf
.bytes_per_pixel
) {
390 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
392 return check_solid_tile16(vs
, x
, y
, w
, h
, color
, samecolor
);
394 return check_solid_tile8(vs
, x
, y
, w
, h
, color
, samecolor
);
398 static void find_best_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
399 uint32_t color
, int *w_ptr
, int *h_ptr
)
403 int w_best
= 0, h_best
= 0;
407 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
409 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, y
+ h
- dy
);
410 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, w_prev
);
412 if (!check_solid_tile(vs
, x
, dy
, dw
, dh
, &color
, true)) {
416 for (dx
= x
+ dw
; dx
< x
+ w_prev
;) {
417 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, x
+ w_prev
- dx
);
419 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color
, true)) {
426 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
428 h_best
= dy
+ dh
- y
;
436 static void extend_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
437 uint32_t color
, int *x_ptr
, int *y_ptr
,
438 int *w_ptr
, int *h_ptr
)
442 /* Try to extend the area upwards. */
443 for ( cy
= *y_ptr
- 1;
444 cy
>= y
&& check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
446 *h_ptr
+= *y_ptr
- (cy
+ 1);
450 for ( cy
= *y_ptr
+ *h_ptr
;
452 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
454 *h_ptr
+= cy
- (*y_ptr
+ *h_ptr
);
456 /* ... to the left. */
457 for ( cx
= *x_ptr
- 1;
458 cx
>= x
&& check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
460 *w_ptr
+= *x_ptr
- (cx
+ 1);
463 /* ... to the right. */
464 for ( cx
= *x_ptr
+ *w_ptr
;
466 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
468 *w_ptr
+= cx
- (*x_ptr
+ *w_ptr
);
471 static int tight_init_stream(VncState
*vs
, int stream_id
,
472 int level
, int strategy
)
474 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
476 if (zstream
->opaque
== NULL
) {
479 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id
);
480 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
481 zstream
->zalloc
= vnc_zlib_zalloc
;
482 zstream
->zfree
= vnc_zlib_zfree
;
484 err
= deflateInit2(zstream
, level
, Z_DEFLATED
, MAX_WBITS
,
485 MAX_MEM_LEVEL
, strategy
);
488 fprintf(stderr
, "VNC: error initializing zlib\n");
492 vs
->tight_levels
[stream_id
] = level
;
493 zstream
->opaque
= vs
;
496 if (vs
->tight_levels
[stream_id
] != level
) {
497 if (deflateParams(zstream
, level
, strategy
) != Z_OK
) {
500 vs
->tight_levels
[stream_id
] = level
;
505 static void tight_send_compact_size(VncState
*vs
, size_t len
)
509 char buf
[3] = {0, 0, 0};
511 buf
[bytes
++] = len
& 0x7F;
513 buf
[bytes
-1] |= 0x80;
514 buf
[bytes
++] = (len
>> 7) & 0x7F;
516 buf
[bytes
-1] |= 0x80;
517 buf
[bytes
++] = (len
>> 14) & 0xFF;
520 for (lpc
= 0; lpc
< bytes
; lpc
++) {
521 vnc_write_u8(vs
, buf
[lpc
]);
525 static int tight_compress_data(VncState
*vs
, int stream_id
, size_t bytes
,
526 int level
, int strategy
)
528 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
531 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
532 vnc_write(vs
, vs
->tight
.buffer
, vs
->tight
.offset
);
536 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
540 /* reserve memory in output buffer */
541 buffer_reserve(&vs
->tight_zlib
, bytes
+ 64);
544 zstream
->next_in
= vs
->tight
.buffer
;
545 zstream
->avail_in
= vs
->tight
.offset
;
546 zstream
->next_out
= vs
->tight_zlib
.buffer
+ vs
->tight_zlib
.offset
;
547 zstream
->avail_out
= vs
->tight_zlib
.capacity
- vs
->tight_zlib
.offset
;
548 zstream
->data_type
= Z_BINARY
;
549 previous_out
= zstream
->total_out
;
552 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
553 fprintf(stderr
, "VNC: error during tight compression\n");
557 vs
->tight_zlib
.offset
= vs
->tight_zlib
.capacity
- zstream
->avail_out
;
558 bytes
= zstream
->total_out
- previous_out
;
560 tight_send_compact_size(vs
, bytes
);
561 vnc_write(vs
, vs
->tight_zlib
.buffer
, bytes
);
563 buffer_reset(&vs
->tight_zlib
);
569 * Subencoding implementations.
571 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
575 int rshift
, gshift
, bshift
;
577 buf32
= (uint32_t *)buf
;
579 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
580 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
581 rshift
= vs
->clientds
.pf
.rshift
;
582 gshift
= vs
->clientds
.pf
.gshift
;
583 bshift
= vs
->clientds
.pf
.bshift
;
585 rshift
= 24 - vs
->clientds
.pf
.rshift
;
586 gshift
= 24 - vs
->clientds
.pf
.gshift
;
587 bshift
= 24 - vs
->clientds
.pf
.bshift
;
596 *buf
++ = (char)(pix
>> rshift
);
597 *buf
++ = (char)(pix
>> gshift
);
598 *buf
++ = (char)(pix
>> bshift
);
602 static int send_full_color_rect(VncState
*vs
, int w
, int h
)
607 vnc_write_u8(vs
, stream
<< 4); /* no flushing, no filter */
609 if (vs
->tight_pixel24
) {
610 tight_pack24(vs
, vs
->tight
.buffer
, w
* h
, &vs
->tight
.offset
);
613 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
616 bytes
= tight_compress_data(vs
, stream
, w
* h
* bytes
,
617 tight_conf
[vs
->tight_compression
].raw_zlib_level
,
623 static int send_solid_rect(VncState
*vs
)
627 vnc_write_u8(vs
, VNC_TIGHT_FILL
<< 4); /* no flushing, no filter */
629 if (vs
->tight_pixel24
) {
630 tight_pack24(vs
, vs
->tight
.buffer
, 1, &vs
->tight
.offset
);
633 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
636 vnc_write(vs
, vs
->tight
.buffer
, bytes
);
640 static int send_mono_rect(VncState
*vs
, int w
, int h
, uint32_t bg
, uint32_t fg
)
644 int level
= tight_conf
[vs
->tight_compression
].mono_zlib_level
;
646 bytes
= ((w
+ 7) / 8) * h
;
648 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
649 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
652 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
655 uint32_t buf
[2] = {bg
, fg
};
656 size_t ret
= sizeof (buf
);
658 if (vs
->tight_pixel24
) {
659 tight_pack24(vs
, (unsigned char*)buf
, 2, &ret
);
661 vnc_write(vs
, buf
, ret
);
663 tight_encode_mono_rect32(vs
->tight
.buffer
, w
, h
, bg
, fg
);
667 vnc_write(vs
, &bg
, 2);
668 vnc_write(vs
, &fg
, 2);
669 tight_encode_mono_rect16(vs
->tight
.buffer
, w
, h
, bg
, fg
);
672 vnc_write_u8(vs
, bg
);
673 vnc_write_u8(vs
, fg
);
674 tight_encode_mono_rect8(vs
->tight
.buffer
, w
, h
, bg
, fg
);
677 vs
->tight
.offset
= bytes
;
679 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
683 struct palette_cb_priv
{
688 static void write_palette(const char *key
, QObject
*obj
, void *opaque
)
690 struct palette_cb_priv
*priv
= opaque
;
691 VncState
*vs
= priv
->vs
;
692 uint32_t bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
693 uint8_t idx
= qint_get_int(qobject_to_qint(obj
));
696 uint32_t color
= tight_palette_buf2rgb(32, (uint8_t *)key
);
698 ((uint32_t*)priv
->header
)[idx
] = color
;
700 uint16_t color
= tight_palette_buf2rgb(16, (uint8_t *)key
);
702 ((uint16_t*)priv
->header
)[idx
] = color
;
706 static int send_palette_rect(VncState
*vs
, int w
, int h
, struct QDict
*palette
)
709 int level
= tight_conf
[vs
->tight_compression
].idx_zlib_level
;
713 colors
= qdict_size(palette
);
715 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
716 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
717 vnc_write_u8(vs
, colors
- 1);
719 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
722 size_t old_offset
, offset
;
723 uint32_t header
[qdict_size(palette
)];
724 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
726 old_offset
= vs
->output
.offset
;
727 qdict_iter(palette
, write_palette
, &priv
);
728 vnc_write(vs
, header
, sizeof(header
));
730 if (vs
->tight_pixel24
) {
731 tight_pack24(vs
, vs
->output
.buffer
+ old_offset
, colors
, &offset
);
732 vs
->output
.offset
= old_offset
+ offset
;
735 tight_encode_indexed_rect32(vs
->tight
.buffer
, w
* h
, palette
);
740 uint16_t header
[qdict_size(palette
)];
741 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
743 qdict_iter(palette
, write_palette
, &priv
);
744 vnc_write(vs
, header
, sizeof(header
));
745 tight_encode_indexed_rect16(vs
->tight
.buffer
, w
* h
, palette
);
749 return -1; /* No palette for 8bits colors */
753 vs
->tight
.offset
= bytes
;
755 bytes
= tight_compress_data(vs
, stream
, bytes
,
756 level
, Z_DEFAULT_STRATEGY
);
760 static void vnc_tight_start(VncState
*vs
)
762 buffer_reset(&vs
->tight
);
764 // make the output buffer be the zlib buffer, so we can compress it later
765 vs
->tight_tmp
= vs
->output
;
766 vs
->output
= vs
->tight
;
769 static void vnc_tight_stop(VncState
*vs
)
771 // switch back to normal output/zlib buffers
772 vs
->tight
= vs
->output
;
773 vs
->output
= vs
->tight_tmp
;
776 static int send_sub_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
778 struct QDict
*palette
= NULL
;
779 uint32_t bg
= 0, fg
= 0;
783 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
786 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
789 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
792 ret
= send_full_color_rect(vs
, w
, h
);
793 } else if (colors
== 1) {
794 ret
= send_solid_rect(vs
);
795 } else if (colors
== 2) {
796 ret
= send_mono_rect(vs
, w
, h
, bg
, fg
);
797 } else if (colors
<= 256) {
798 ret
= send_palette_rect(vs
, w
, h
, palette
);
804 static int send_sub_rect_solid(VncState
*vs
, int x
, int y
, int w
, int h
)
806 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
809 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
812 return send_solid_rect(vs
);
815 static int send_rect_simple(VncState
*vs
, int x
, int y
, int w
, int h
)
817 int max_size
, max_width
;
818 int max_sub_width
, max_sub_height
;
823 max_size
= tight_conf
[vs
->tight_compression
].max_rect_size
;
824 max_width
= tight_conf
[vs
->tight_compression
].max_rect_width
;
826 if (w
> max_width
|| w
* h
> max_size
) {
827 max_sub_width
= (w
> max_width
) ? max_width
: w
;
828 max_sub_height
= max_size
/ max_sub_width
;
830 for (dy
= 0; dy
< h
; dy
+= max_sub_height
) {
831 for (dx
= 0; dx
< w
; dx
+= max_width
) {
832 rw
= MIN(max_sub_width
, w
- dx
);
833 rh
= MIN(max_sub_height
, h
- dy
);
834 n
+= send_sub_rect(vs
, x
+dx
, y
+dy
, rw
, rh
);
838 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
844 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
845 int w
, int h
, int max_rows
)
850 /* Try to find large solid-color areas and send them separately. */
852 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
854 /* If a rectangle becomes too large, send its upper part now. */
856 if (dy
- y
>= max_rows
) {
857 n
+= send_rect_simple(vs
, x
, y
, w
, max_rows
);
862 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (y
+ h
- dy
));
864 for (dx
= x
; dx
< x
+ w
; dx
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
865 uint32_t color_value
;
866 int x_best
, y_best
, w_best
, h_best
;
868 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (x
+ w
- dx
));
870 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color_value
, false)) {
874 /* Get dimensions of solid-color area. */
876 find_best_solid_area(vs
, dx
, dy
, w
- (dx
- x
), h
- (dy
- y
),
877 color_value
, &w_best
, &h_best
);
879 /* Make sure a solid rectangle is large enough
880 (or the whole rectangle is of the same color). */
882 if (w_best
* h_best
!= w
* h
&&
883 w_best
* h_best
< VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE
) {
887 /* Try to extend solid rectangle to maximum size. */
889 x_best
= dx
; y_best
= dy
;
890 extend_solid_area(vs
, x
, y
, w
, h
, color_value
,
891 &x_best
, &y_best
, &w_best
, &h_best
);
893 /* Send rectangles at top and left to solid-color area. */
896 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
);
899 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
,
903 /* Send solid-color rectangle. */
904 n
+= send_sub_rect_solid(vs
, x_best
, y_best
, w_best
, h_best
);
906 /* Send remaining rectangles (at right and bottom). */
908 if (x_best
+ w_best
!= x
+ w
) {
909 n
+= vnc_tight_send_framebuffer_update(vs
, x_best
+w_best
,
914 if (y_best
+ h_best
!= y
+ h
) {
915 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
+h_best
,
916 w
, h
-(y_best
-y
)-h_best
);
919 /* Return after all recursive calls are done. */
923 return n
+ send_rect_simple(vs
, x
, y
, w
, h
);
926 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
931 if (vs
->clientds
.pf
.bytes_per_pixel
== 4 && vs
->clientds
.pf
.rmax
== 0xFF &&
932 vs
->clientds
.pf
.bmax
== 0xFF && vs
->clientds
.pf
.gmax
== 0xFF) {
933 vs
->tight_pixel24
= true;
935 vs
->tight_pixel24
= false;
938 if (w
* h
< VNC_TIGHT_MIN_SPLIT_RECT_SIZE
)
939 return send_rect_simple(vs
, x
, y
, w
, h
);
941 /* Calculate maximum number of rows in one non-solid rectangle. */
943 max_rows
= tight_conf
[vs
->tight_compression
].max_rect_size
;
944 max_rows
/= MIN(tight_conf
[vs
->tight_compression
].max_rect_width
, w
);
946 return find_large_solid_color_rect(vs
, x
, y
, w
, h
, max_rows
);
949 void vnc_tight_clear(VncState
*vs
)
952 for (i
=0; i
<ARRAY_SIZE(vs
->tight_stream
); i
++) {
953 if (vs
->tight_stream
[i
].opaque
) {
954 deflateEnd(&vs
->tight_stream
[i
]);
958 buffer_free(&vs
->tight
);
959 buffer_free(&vs
->tight_zlib
);