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); \
180 tight_palette_insert(*palette, ci, bpp, max); \
182 for (i++; i < count; i++) { \
183 if (data[i] == ci) { \
186 if (!tight_palette_insert(*palette, (uint32_t)ci, \
194 return qdict_size(*palette); \
197 DEFINE_FILL_PALETTE_FUNCTION(8)
198 DEFINE_FILL_PALETTE_FUNCTION(16)
199 DEFINE_FILL_PALETTE_FUNCTION(32)
201 static int tight_fill_palette(VncState
*vs
, int x
, int y
,
202 size_t count
, uint32_t *bg
, uint32_t *fg
,
203 struct QDict
**palette
)
207 max
= count
/ tight_conf
[vs
->tight_compression
].idx_max_colors_divisor
;
209 count
>= tight_conf
[vs
->tight_compression
].mono_min_rect_size
) {
216 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
218 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
220 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
223 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
228 /* Callback to dump a palette with qdict_iter
229 static void print_palette(const char *key, QObject *obj, void *opaque)
231 uint8_t idx = qint_get_int(qobject_to_qint(obj));
232 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
234 fprintf(stderr, "%.2x ", (unsigned char)*key);
236 fprintf(stderr, "%.2x ", (unsigned char)*key);
238 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
243 * Converting truecolor samples into palette indices.
245 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
248 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
249 struct QDict *palette) { \
250 uint##bpp##_t *src; \
256 src = (uint##bpp##_t *) buf; \
258 for (i = 0; i < count; i++) { \
261 while (i < count && *src == rgb) { \
264 tight_palette_rgb2buf(rgb, bpp, key); \
265 if (!qdict_haskey(palette, (char *)key)) { \
267 * Should never happen, but don't break everything \
268 * if it does, use the first color instead \
272 idx = qdict_get_int(palette, (char *)key); \
281 DEFINE_IDX_ENCODE_FUNCTION(16)
282 DEFINE_IDX_ENCODE_FUNCTION(32)
284 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
287 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
288 uint##bpp##_t bg, uint##bpp##_t fg) { \
289 uint##bpp##_t *ptr; \
290 unsigned int value, mask; \
294 ptr = (uint##bpp##_t *) buf; \
295 aligned_width = w - w % 8; \
297 for (y = 0; y < h; y++) { \
298 for (x = 0; x < aligned_width; x += 8) { \
299 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
300 if (*ptr++ != bg) { \
304 if (bg_bits == 8) { \
308 mask = 0x80 >> bg_bits; \
310 for (bg_bits++; bg_bits < 8; bg_bits++) { \
312 if (*ptr++ != bg) { \
316 *buf++ = (uint8_t)value; \
325 for (; x < w; x++) { \
326 if (*ptr++ != bg) { \
331 *buf++ = (uint8_t)value; \
335 DEFINE_MONO_ENCODE_FUNCTION(8)
336 DEFINE_MONO_ENCODE_FUNCTION(16)
337 DEFINE_MONO_ENCODE_FUNCTION(32)
340 * Check if a rectangle is all of the same color. If needSameColor is
341 * set to non-zero, then also check that its color equals to the
342 * *colorPtr value. The result is 1 if the test is successfull, and in
343 * that case new color will be stored in *colorPtr.
346 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
349 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
350 uint32_t* color, bool samecolor) \
352 VncDisplay *vd = vs->vd; \
353 uint##bpp##_t *fbptr; \
357 fbptr = (uint##bpp##_t *) \
358 (vd->server->data + y * ds_get_linesize(vs->ds) + \
359 x * ds_get_bytes_per_pixel(vs->ds)); \
362 if (samecolor && (uint32_t)c != *color) { \
366 for (dy = 0; dy < h; dy++) { \
367 for (dx = 0; dx < w; dx++) { \
368 if (c != fbptr[dx]) { \
372 fbptr = (uint##bpp##_t *) \
373 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
376 *color = (uint32_t)c; \
380 DEFINE_CHECK_SOLID_FUNCTION(32)
381 DEFINE_CHECK_SOLID_FUNCTION(16)
382 DEFINE_CHECK_SOLID_FUNCTION(8)
384 static bool check_solid_tile(VncState
*vs
, int x
, int y
, int w
, int h
,
385 uint32_t* color
, bool samecolor
)
387 VncDisplay
*vd
= vs
->vd
;
389 switch(vd
->server
->pf
.bytes_per_pixel
) {
391 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
393 return check_solid_tile16(vs
, x
, y
, w
, h
, color
, samecolor
);
395 return check_solid_tile8(vs
, x
, y
, w
, h
, color
, samecolor
);
399 static void find_best_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
400 uint32_t color
, int *w_ptr
, int *h_ptr
)
404 int w_best
= 0, h_best
= 0;
408 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
410 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, y
+ h
- dy
);
411 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, w_prev
);
413 if (!check_solid_tile(vs
, x
, dy
, dw
, dh
, &color
, true)) {
417 for (dx
= x
+ dw
; dx
< x
+ w_prev
;) {
418 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, x
+ w_prev
- dx
);
420 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color
, true)) {
427 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
429 h_best
= dy
+ dh
- y
;
437 static void extend_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
438 uint32_t color
, int *x_ptr
, int *y_ptr
,
439 int *w_ptr
, int *h_ptr
)
443 /* Try to extend the area upwards. */
444 for ( cy
= *y_ptr
- 1;
445 cy
>= y
&& check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
447 *h_ptr
+= *y_ptr
- (cy
+ 1);
451 for ( cy
= *y_ptr
+ *h_ptr
;
453 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
455 *h_ptr
+= cy
- (*y_ptr
+ *h_ptr
);
457 /* ... to the left. */
458 for ( cx
= *x_ptr
- 1;
459 cx
>= x
&& check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
461 *w_ptr
+= *x_ptr
- (cx
+ 1);
464 /* ... to the right. */
465 for ( cx
= *x_ptr
+ *w_ptr
;
467 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
469 *w_ptr
+= cx
- (*x_ptr
+ *w_ptr
);
472 static int tight_init_stream(VncState
*vs
, int stream_id
,
473 int level
, int strategy
)
475 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
477 if (zstream
->opaque
== NULL
) {
480 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id
);
481 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
482 zstream
->zalloc
= vnc_zlib_zalloc
;
483 zstream
->zfree
= vnc_zlib_zfree
;
485 err
= deflateInit2(zstream
, level
, Z_DEFLATED
, MAX_WBITS
,
486 MAX_MEM_LEVEL
, strategy
);
489 fprintf(stderr
, "VNC: error initializing zlib\n");
493 vs
->tight_levels
[stream_id
] = level
;
494 zstream
->opaque
= vs
;
497 if (vs
->tight_levels
[stream_id
] != level
) {
498 if (deflateParams(zstream
, level
, strategy
) != Z_OK
) {
501 vs
->tight_levels
[stream_id
] = level
;
506 static void tight_send_compact_size(VncState
*vs
, size_t len
)
510 char buf
[3] = {0, 0, 0};
512 buf
[bytes
++] = len
& 0x7F;
514 buf
[bytes
-1] |= 0x80;
515 buf
[bytes
++] = (len
>> 7) & 0x7F;
517 buf
[bytes
-1] |= 0x80;
518 buf
[bytes
++] = (len
>> 14) & 0xFF;
521 for (lpc
= 0; lpc
< bytes
; lpc
++) {
522 vnc_write_u8(vs
, buf
[lpc
]);
526 static int tight_compress_data(VncState
*vs
, int stream_id
, size_t bytes
,
527 int level
, int strategy
)
529 z_streamp zstream
= &vs
->tight_stream
[stream_id
];
532 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
533 vnc_write(vs
, vs
->tight
.buffer
, vs
->tight
.offset
);
537 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
541 /* reserve memory in output buffer */
542 buffer_reserve(&vs
->tight_zlib
, bytes
+ 64);
545 zstream
->next_in
= vs
->tight
.buffer
;
546 zstream
->avail_in
= vs
->tight
.offset
;
547 zstream
->next_out
= vs
->tight_zlib
.buffer
+ vs
->tight_zlib
.offset
;
548 zstream
->avail_out
= vs
->tight_zlib
.capacity
- vs
->tight_zlib
.offset
;
549 zstream
->data_type
= Z_BINARY
;
550 previous_out
= zstream
->total_out
;
553 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
554 fprintf(stderr
, "VNC: error during tight compression\n");
558 vs
->tight_zlib
.offset
= vs
->tight_zlib
.capacity
- zstream
->avail_out
;
559 bytes
= zstream
->total_out
- previous_out
;
561 tight_send_compact_size(vs
, bytes
);
562 vnc_write(vs
, vs
->tight_zlib
.buffer
, bytes
);
564 buffer_reset(&vs
->tight_zlib
);
570 * Subencoding implementations.
572 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
576 int rshift
, gshift
, bshift
;
578 buf32
= (uint32_t *)buf
;
580 if ((vs
->clientds
.flags
& QEMU_BIG_ENDIAN_FLAG
) ==
581 (vs
->ds
->surface
->flags
& QEMU_BIG_ENDIAN_FLAG
)) {
582 rshift
= vs
->clientds
.pf
.rshift
;
583 gshift
= vs
->clientds
.pf
.gshift
;
584 bshift
= vs
->clientds
.pf
.bshift
;
586 rshift
= 24 - vs
->clientds
.pf
.rshift
;
587 gshift
= 24 - vs
->clientds
.pf
.gshift
;
588 bshift
= 24 - vs
->clientds
.pf
.bshift
;
597 *buf
++ = (char)(pix
>> rshift
);
598 *buf
++ = (char)(pix
>> gshift
);
599 *buf
++ = (char)(pix
>> bshift
);
603 static int send_full_color_rect(VncState
*vs
, int w
, int h
)
608 vnc_write_u8(vs
, stream
<< 4); /* no flushing, no filter */
610 if (vs
->tight_pixel24
) {
611 tight_pack24(vs
, vs
->tight
.buffer
, w
* h
, &vs
->tight
.offset
);
614 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
617 bytes
= tight_compress_data(vs
, stream
, w
* h
* bytes
,
618 tight_conf
[vs
->tight_compression
].raw_zlib_level
,
624 static int send_solid_rect(VncState
*vs
)
628 vnc_write_u8(vs
, VNC_TIGHT_FILL
<< 4); /* no flushing, no filter */
630 if (vs
->tight_pixel24
) {
631 tight_pack24(vs
, vs
->tight
.buffer
, 1, &vs
->tight
.offset
);
634 bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
637 vnc_write(vs
, vs
->tight
.buffer
, bytes
);
641 static int send_mono_rect(VncState
*vs
, int w
, int h
, uint32_t bg
, uint32_t fg
)
645 int level
= tight_conf
[vs
->tight_compression
].mono_zlib_level
;
647 bytes
= ((w
+ 7) / 8) * h
;
649 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
650 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
653 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
656 uint32_t buf
[2] = {bg
, fg
};
657 size_t ret
= sizeof (buf
);
659 if (vs
->tight_pixel24
) {
660 tight_pack24(vs
, (unsigned char*)buf
, 2, &ret
);
662 vnc_write(vs
, buf
, ret
);
664 tight_encode_mono_rect32(vs
->tight
.buffer
, w
, h
, bg
, fg
);
668 vnc_write(vs
, &bg
, 2);
669 vnc_write(vs
, &fg
, 2);
670 tight_encode_mono_rect16(vs
->tight
.buffer
, w
, h
, bg
, fg
);
673 vnc_write_u8(vs
, bg
);
674 vnc_write_u8(vs
, fg
);
675 tight_encode_mono_rect8(vs
->tight
.buffer
, w
, h
, bg
, fg
);
678 vs
->tight
.offset
= bytes
;
680 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
684 struct palette_cb_priv
{
689 static void write_palette(const char *key
, QObject
*obj
, void *opaque
)
691 struct palette_cb_priv
*priv
= opaque
;
692 VncState
*vs
= priv
->vs
;
693 uint32_t bytes
= vs
->clientds
.pf
.bytes_per_pixel
;
694 uint8_t idx
= qint_get_int(qobject_to_qint(obj
));
697 uint32_t color
= tight_palette_buf2rgb(32, (uint8_t *)key
);
699 ((uint32_t*)priv
->header
)[idx
] = color
;
701 uint16_t color
= tight_palette_buf2rgb(16, (uint8_t *)key
);
703 ((uint16_t*)priv
->header
)[idx
] = color
;
707 static int send_palette_rect(VncState
*vs
, int w
, int h
, struct QDict
*palette
)
710 int level
= tight_conf
[vs
->tight_compression
].idx_zlib_level
;
714 colors
= qdict_size(palette
);
716 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
717 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
718 vnc_write_u8(vs
, colors
- 1);
720 switch(vs
->clientds
.pf
.bytes_per_pixel
) {
723 size_t old_offset
, offset
;
724 uint32_t header
[qdict_size(palette
)];
725 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
727 old_offset
= vs
->output
.offset
;
728 qdict_iter(palette
, write_palette
, &priv
);
729 vnc_write(vs
, header
, sizeof(header
));
731 if (vs
->tight_pixel24
) {
732 tight_pack24(vs
, vs
->output
.buffer
+ old_offset
, colors
, &offset
);
733 vs
->output
.offset
= old_offset
+ offset
;
736 tight_encode_indexed_rect32(vs
->tight
.buffer
, w
* h
, palette
);
741 uint16_t header
[qdict_size(palette
)];
742 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
744 qdict_iter(palette
, write_palette
, &priv
);
745 vnc_write(vs
, header
, sizeof(header
));
746 tight_encode_indexed_rect16(vs
->tight
.buffer
, w
* h
, palette
);
750 return -1; /* No palette for 8bits colors */
754 vs
->tight
.offset
= bytes
;
756 bytes
= tight_compress_data(vs
, stream
, bytes
,
757 level
, Z_DEFAULT_STRATEGY
);
761 static void vnc_tight_start(VncState
*vs
)
763 buffer_reset(&vs
->tight
);
765 // make the output buffer be the zlib buffer, so we can compress it later
766 vs
->tight_tmp
= vs
->output
;
767 vs
->output
= vs
->tight
;
770 static void vnc_tight_stop(VncState
*vs
)
772 // switch back to normal output/zlib buffers
773 vs
->tight
= vs
->output
;
774 vs
->output
= vs
->tight_tmp
;
777 static int send_sub_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
779 struct QDict
*palette
= NULL
;
780 uint32_t bg
= 0, fg
= 0;
784 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
787 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
790 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
793 ret
= send_full_color_rect(vs
, w
, h
);
794 } else if (colors
== 1) {
795 ret
= send_solid_rect(vs
);
796 } else if (colors
== 2) {
797 ret
= send_mono_rect(vs
, w
, h
, bg
, fg
);
798 } else if (colors
<= 256) {
799 ret
= send_palette_rect(vs
, w
, h
, palette
);
805 static int send_sub_rect_solid(VncState
*vs
, int x
, int y
, int w
, int h
)
807 vnc_framebuffer_update(vs
, x
, y
, w
, h
, VNC_ENCODING_TIGHT
);
810 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
813 return send_solid_rect(vs
);
816 static int send_rect_simple(VncState
*vs
, int x
, int y
, int w
, int h
)
818 int max_size
, max_width
;
819 int max_sub_width
, max_sub_height
;
824 max_size
= tight_conf
[vs
->tight_compression
].max_rect_size
;
825 max_width
= tight_conf
[vs
->tight_compression
].max_rect_width
;
827 if (w
> max_width
|| w
* h
> max_size
) {
828 max_sub_width
= (w
> max_width
) ? max_width
: w
;
829 max_sub_height
= max_size
/ max_sub_width
;
831 for (dy
= 0; dy
< h
; dy
+= max_sub_height
) {
832 for (dx
= 0; dx
< w
; dx
+= max_width
) {
833 rw
= MIN(max_sub_width
, w
- dx
);
834 rh
= MIN(max_sub_height
, h
- dy
);
835 n
+= send_sub_rect(vs
, x
+dx
, y
+dy
, rw
, rh
);
839 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
845 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
846 int w
, int h
, int max_rows
)
851 /* Try to find large solid-color areas and send them separately. */
853 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
855 /* If a rectangle becomes too large, send its upper part now. */
857 if (dy
- y
>= max_rows
) {
858 n
+= send_rect_simple(vs
, x
, y
, w
, max_rows
);
863 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (y
+ h
- dy
));
865 for (dx
= x
; dx
< x
+ w
; dx
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
866 uint32_t color_value
;
867 int x_best
, y_best
, w_best
, h_best
;
869 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (x
+ w
- dx
));
871 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color_value
, false)) {
875 /* Get dimensions of solid-color area. */
877 find_best_solid_area(vs
, dx
, dy
, w
- (dx
- x
), h
- (dy
- y
),
878 color_value
, &w_best
, &h_best
);
880 /* Make sure a solid rectangle is large enough
881 (or the whole rectangle is of the same color). */
883 if (w_best
* h_best
!= w
* h
&&
884 w_best
* h_best
< VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE
) {
888 /* Try to extend solid rectangle to maximum size. */
890 x_best
= dx
; y_best
= dy
;
891 extend_solid_area(vs
, x
, y
, w
, h
, color_value
,
892 &x_best
, &y_best
, &w_best
, &h_best
);
894 /* Send rectangles at top and left to solid-color area. */
897 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
);
900 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
,
904 /* Send solid-color rectangle. */
905 n
+= send_sub_rect_solid(vs
, x_best
, y_best
, w_best
, h_best
);
907 /* Send remaining rectangles (at right and bottom). */
909 if (x_best
+ w_best
!= x
+ w
) {
910 n
+= vnc_tight_send_framebuffer_update(vs
, x_best
+w_best
,
915 if (y_best
+ h_best
!= y
+ h
) {
916 n
+= vnc_tight_send_framebuffer_update(vs
, x
, y_best
+h_best
,
917 w
, h
-(y_best
-y
)-h_best
);
920 /* Return after all recursive calls are done. */
924 return n
+ send_rect_simple(vs
, x
, y
, w
, h
);
927 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
932 if (vs
->clientds
.pf
.bytes_per_pixel
== 4 && vs
->clientds
.pf
.rmax
== 0xFF &&
933 vs
->clientds
.pf
.bmax
== 0xFF && vs
->clientds
.pf
.gmax
== 0xFF) {
934 vs
->tight_pixel24
= true;
936 vs
->tight_pixel24
= false;
939 if (w
* h
< VNC_TIGHT_MIN_SPLIT_RECT_SIZE
)
940 return send_rect_simple(vs
, x
, y
, w
, h
);
942 /* Calculate maximum number of rows in one non-solid rectangle. */
944 max_rows
= tight_conf
[vs
->tight_compression
].max_rect_size
;
945 max_rows
/= MIN(tight_conf
[vs
->tight_compression
].max_rect_width
, w
);
947 return find_large_solid_color_rect(vs
, x
, y
, w
, h
, max_rows
);
950 void vnc_tight_clear(VncState
*vs
)
953 for (i
=0; i
<ARRAY_SIZE(vs
->tight_stream
); i
++) {
954 if (vs
->tight_stream
[i
].opaque
) {
955 deflateEnd(&vs
->tight_stream
[i
]);
959 buffer_free(&vs
->tight
);
960 buffer_free(&vs
->tight_zlib
);