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
29 #include "config-host.h"
31 /* This needs to be before jpeglib.h line because of conflict with
32 INT32 definitions between jmorecfg.h (included by jpeglib.h) and
33 Win32 basetsd.h (included by windows.h). */
34 #include "qemu-common.h"
37 /* The following define is needed by pngconf.h. Otherwise it won't compile,
38 because setjmp.h was already included by qemu-common.h. */
39 #define PNG_SKIP_SETJMP_CHECK
42 #ifdef CONFIG_VNC_JPEG
47 #include "qemu/bswap.h"
48 #include "qapi/qmp/qint.h"
50 #include "vnc-enc-tight.h"
51 #include "vnc-palette.h"
53 /* Compression level stuff. The following array contains various
54 encoder parameters for each of 10 compression levels (0..9).
55 Last three parameters correspond to JPEG quality levels (0..9). */
58 int max_rect_size
, max_rect_width
;
59 int mono_min_rect_size
, gradient_min_rect_size
;
60 int idx_zlib_level
, mono_zlib_level
, raw_zlib_level
, gradient_zlib_level
;
61 int gradient_threshold
, gradient_threshold24
;
62 int idx_max_colors_divisor
;
63 int jpeg_quality
, jpeg_threshold
, jpeg_threshold24
;
65 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
66 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
67 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
68 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
69 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
70 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
71 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
72 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
73 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
74 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
78 static int tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
81 #ifdef CONFIG_VNC_JPEG
83 double jpeg_freq_min
; /* Don't send JPEG if the freq is bellow */
84 double jpeg_freq_threshold
; /* Always send JPEG if the freq is above */
85 int jpeg_idx
; /* Allow indexed JPEG */
86 int jpeg_full
; /* Allow full color JPEG */
87 } tight_jpeg_conf
[] = {
101 #ifdef CONFIG_VNC_PNG
102 static const struct {
103 int png_zlib_level
, png_filters
;
104 } tight_png_conf
[] = {
105 { 0, PNG_NO_FILTERS
},
106 { 1, PNG_NO_FILTERS
},
107 { 2, PNG_NO_FILTERS
},
108 { 3, PNG_NO_FILTERS
},
109 { 4, PNG_NO_FILTERS
},
110 { 5, PNG_ALL_FILTERS
},
111 { 6, PNG_ALL_FILTERS
},
112 { 7, PNG_ALL_FILTERS
},
113 { 8, PNG_ALL_FILTERS
},
114 { 9, PNG_ALL_FILTERS
},
117 static int send_png_rect(VncState
*vs
, int x
, int y
, int w
, int h
,
118 VncPalette
*palette
);
120 static bool tight_can_send_png_rect(VncState
*vs
, int w
, int h
)
122 if (vs
->tight
.type
!= VNC_ENCODING_TIGHT_PNG
) {
126 if (surface_bytes_per_pixel(vs
->vd
->ds
) == 1 ||
127 vs
->client_pf
.bytes_per_pixel
== 1) {
136 * Code to guess if given rectangle is suitable for smooth image
137 * compression (by applying "gradient" filter or JPEG coder).
141 tight_detect_smooth_image24(VncState
*vs
, int w
, int h
)
146 unsigned int stats
[256];
150 unsigned char *buf
= vs
->tight
.tight
.buffer
;
153 * If client is big-endian, color samples begin from the second
154 * byte (offset 1) of a 32-bit pixel value.
158 memset(stats
, 0, sizeof (stats
));
160 for (y
= 0, x
= 0; y
< h
&& x
< w
;) {
161 for (d
= 0; d
< h
- y
&& d
< w
- x
- VNC_TIGHT_DETECT_SUBROW_WIDTH
;
163 for (c
= 0; c
< 3; c
++) {
164 left
[c
] = buf
[((y
+d
)*w
+x
+d
)*4+off
+c
] & 0xFF;
166 for (dx
= 1; dx
<= VNC_TIGHT_DETECT_SUBROW_WIDTH
; dx
++) {
167 for (c
= 0; c
< 3; c
++) {
168 pix
= buf
[((y
+d
)*w
+x
+d
+dx
)*4+off
+c
] & 0xFF;
169 stats
[abs(pix
- left
[c
])]++;
184 /* 95% smooth or more ... */
185 if (stats
[0] * 33 / pixels
>= 95) {
190 for (c
= 1; c
< 8; c
++) {
191 errors
+= stats
[c
] * (c
* c
);
192 if (stats
[c
] == 0 || stats
[c
] > stats
[c
-1] * 2) {
196 for (; c
< 256; c
++) {
197 errors
+= stats
[c
] * (c
* c
);
199 errors
/= (pixels
* 3 - stats
[0]);
204 #define DEFINE_DETECT_FUNCTION(bpp) \
206 static unsigned int \
207 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
210 int max[3], shift[3]; \
213 unsigned int stats[256]; \
215 int sample, sum, left[3]; \
216 unsigned int errors; \
217 unsigned char *buf = vs->tight.tight.buffer; \
219 endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
220 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \
223 max[0] = vs->client_pf.rmax; \
224 max[1] = vs->client_pf.gmax; \
225 max[2] = vs->client_pf.bmax; \
226 shift[0] = vs->client_pf.rshift; \
227 shift[1] = vs->client_pf.gshift; \
228 shift[2] = vs->client_pf.bshift; \
230 memset(stats, 0, sizeof(stats)); \
233 while (y < h && x < w) { \
234 for (d = 0; d < h - y && \
235 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
236 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
238 pix = bswap##bpp(pix); \
240 for (c = 0; c < 3; c++) { \
241 left[c] = (int)(pix >> shift[c] & max[c]); \
243 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
245 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
247 pix = bswap##bpp(pix); \
250 for (c = 0; c < 3; c++) { \
251 sample = (int)(pix >> shift[c] & max[c]); \
252 sum += abs(sample - left[c]); \
271 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
276 for (c = 1; c < 8; c++) { \
277 errors += stats[c] * (c * c); \
278 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
282 for (; c < 256; c++) { \
283 errors += stats[c] * (c * c); \
285 errors /= (pixels - stats[0]); \
290 DEFINE_DETECT_FUNCTION(16)
291 DEFINE_DETECT_FUNCTION(32)
294 tight_detect_smooth_image(VncState
*vs
, int w
, int h
)
297 int compression
= vs
->tight
.compression
;
298 int quality
= vs
->tight
.quality
;
300 if (!vs
->vd
->lossy
) {
304 if (surface_bytes_per_pixel(vs
->vd
->ds
) == 1 ||
305 vs
->client_pf
.bytes_per_pixel
== 1 ||
306 w
< VNC_TIGHT_DETECT_MIN_WIDTH
|| h
< VNC_TIGHT_DETECT_MIN_HEIGHT
) {
310 if (vs
->tight
.quality
!= (uint8_t)-1) {
311 if (w
* h
< VNC_TIGHT_JPEG_MIN_RECT_SIZE
) {
315 if (w
* h
< tight_conf
[compression
].gradient_min_rect_size
) {
320 if (vs
->client_pf
.bytes_per_pixel
== 4) {
321 if (vs
->tight
.pixel24
) {
322 errors
= tight_detect_smooth_image24(vs
, w
, h
);
323 if (vs
->tight
.quality
!= (uint8_t)-1) {
324 return (errors
< tight_conf
[quality
].jpeg_threshold24
);
326 return (errors
< tight_conf
[compression
].gradient_threshold24
);
328 errors
= tight_detect_smooth_image32(vs
, w
, h
);
331 errors
= tight_detect_smooth_image16(vs
, w
, h
);
333 if (quality
!= (uint8_t)-1) {
334 return (errors
< tight_conf
[quality
].jpeg_threshold
);
336 return (errors
< tight_conf
[compression
].gradient_threshold
);
340 * Code to determine how many different colors used in rectangle.
342 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
345 tight_fill_palette##bpp(VncState *vs, int x, int y, \
346 int max, size_t count, \
347 uint32_t *bg, uint32_t *fg, \
348 VncPalette **palette) { \
349 uint##bpp##_t *data; \
350 uint##bpp##_t c0, c1, ci; \
353 data = (uint##bpp##_t *)vs->tight.tight.buffer; \
357 while (i < count && data[i] == c0) \
371 for (i++; i < count; i++) { \
375 } else if (ci == c1) { \
382 *bg = (uint32_t)c0; \
383 *fg = (uint32_t)c1; \
385 *bg = (uint32_t)c1; \
386 *fg = (uint32_t)c0; \
395 *palette = palette_new(max, bpp); \
396 palette_put(*palette, c0); \
397 palette_put(*palette, c1); \
398 palette_put(*palette, ci); \
400 for (i++; i < count; i++) { \
401 if (data[i] == ci) { \
405 if (!palette_put(*palette, (uint32_t)ci)) { \
411 return palette_size(*palette); \
414 DEFINE_FILL_PALETTE_FUNCTION(8)
415 DEFINE_FILL_PALETTE_FUNCTION(16)
416 DEFINE_FILL_PALETTE_FUNCTION(32)
418 static int tight_fill_palette(VncState
*vs
, int x
, int y
,
419 size_t count
, uint32_t *bg
, uint32_t *fg
,
420 VncPalette
**palette
)
424 max
= count
/ tight_conf
[vs
->tight
.compression
].idx_max_colors_divisor
;
426 count
>= tight_conf
[vs
->tight
.compression
].mono_min_rect_size
) {
433 switch (vs
->client_pf
.bytes_per_pixel
) {
435 return tight_fill_palette32(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
437 return tight_fill_palette16(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
440 return tight_fill_palette8(vs
, x
, y
, max
, count
, bg
, fg
, palette
);
446 * Converting truecolor samples into palette indices.
448 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
451 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
452 VncPalette *palette) { \
453 uint##bpp##_t *src; \
458 src = (uint##bpp##_t *) buf; \
460 for (i = 0; i < count; i++) { \
464 while (i < count && *src == rgb) { \
467 idx = palette_idx(palette, rgb); \
469 * Should never happen, but don't break everything \
470 * if it does, use the first color instead \
472 if (idx == (uint8_t)-1) { \
482 DEFINE_IDX_ENCODE_FUNCTION(16)
483 DEFINE_IDX_ENCODE_FUNCTION(32)
485 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
488 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
489 uint##bpp##_t bg, uint##bpp##_t fg) { \
490 uint##bpp##_t *ptr; \
491 unsigned int value, mask; \
495 ptr = (uint##bpp##_t *) buf; \
496 aligned_width = w - w % 8; \
498 for (y = 0; y < h; y++) { \
499 for (x = 0; x < aligned_width; x += 8) { \
500 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
501 if (*ptr++ != bg) { \
505 if (bg_bits == 8) { \
509 mask = 0x80 >> bg_bits; \
511 for (bg_bits++; bg_bits < 8; bg_bits++) { \
513 if (*ptr++ != bg) { \
517 *buf++ = (uint8_t)value; \
526 for (; x < w; x++) { \
527 if (*ptr++ != bg) { \
532 *buf++ = (uint8_t)value; \
536 DEFINE_MONO_ENCODE_FUNCTION(8)
537 DEFINE_MONO_ENCODE_FUNCTION(16)
538 DEFINE_MONO_ENCODE_FUNCTION(32)
541 * ``Gradient'' filter for 24-bit color samples.
542 * Should be called only when redMax, greenMax and blueMax are 255.
543 * Color components assumed to be byte-aligned.
547 tight_filter_gradient24(VncState
*vs
, uint8_t *buf
, int w
, int h
)
553 int here
[3], upper
[3], left
[3], upperleft
[3];
557 buf32
= (uint32_t *)buf
;
558 memset(vs
->tight
.gradient
.buffer
, 0, w
* 3 * sizeof(int));
560 if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
561 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) {
562 shift
[0] = vs
->client_pf
.rshift
;
563 shift
[1] = vs
->client_pf
.gshift
;
564 shift
[2] = vs
->client_pf
.bshift
;
566 shift
[0] = 24 - vs
->client_pf
.rshift
;
567 shift
[1] = 24 - vs
->client_pf
.gshift
;
568 shift
[2] = 24 - vs
->client_pf
.bshift
;
571 for (y
= 0; y
< h
; y
++) {
572 for (c
= 0; c
< 3; c
++) {
576 prev
= (int *)vs
->tight
.gradient
.buffer
;
577 for (x
= 0; x
< w
; x
++) {
579 for (c
= 0; c
< 3; c
++) {
580 upperleft
[c
] = upper
[c
];
583 here
[c
] = (int)(pix32
>> shift
[c
] & 0xFF);
586 prediction
= left
[c
] + upper
[c
] - upperleft
[c
];
587 if (prediction
< 0) {
589 } else if (prediction
> 0xFF) {
592 *buf
++ = (char)(here
[c
] - prediction
);
600 * ``Gradient'' filter for other color depths.
603 #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
606 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
608 uint##bpp##_t pix, diff; \
611 int max[3], shift[3]; \
612 int here[3], upper[3], left[3], upperleft[3]; \
616 memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \
618 endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
619 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \
621 max[0] = vs->client_pf.rmax; \
622 max[1] = vs->client_pf.gmax; \
623 max[2] = vs->client_pf.bmax; \
624 shift[0] = vs->client_pf.rshift; \
625 shift[1] = vs->client_pf.gshift; \
626 shift[2] = vs->client_pf.bshift; \
628 for (y = 0; y < h; y++) { \
629 for (c = 0; c < 3; c++) { \
633 prev = (int *)vs->tight.gradient.buffer; \
634 for (x = 0; x < w; x++) { \
637 pix = bswap##bpp(pix); \
640 for (c = 0; c < 3; c++) { \
641 upperleft[c] = upper[c]; \
644 here[c] = (int)(pix >> shift[c] & max[c]); \
647 prediction = left[c] + upper[c] - upperleft[c]; \
648 if (prediction < 0) { \
650 } else if (prediction > max[c]) { \
651 prediction = max[c]; \
653 diff |= ((here[c] - prediction) & max[c]) \
657 diff = bswap##bpp(diff); \
664 DEFINE_GRADIENT_FILTER_FUNCTION(16)
665 DEFINE_GRADIENT_FILTER_FUNCTION(32)
668 * Check if a rectangle is all of the same color. If needSameColor is
669 * set to non-zero, then also check that its color equals to the
670 * *colorPtr value. The result is 1 if the test is successful, and in
671 * that case new color will be stored in *colorPtr.
675 check_solid_tile32(VncState
*vs
, int x
, int y
, int w
, int h
,
676 uint32_t *color
, bool samecolor
)
678 VncDisplay
*vd
= vs
->vd
;
683 fbptr
= vnc_server_fb_ptr(vd
, x
, y
);
686 if (samecolor
&& (uint32_t)c
!= *color
) {
690 for (dy
= 0; dy
< h
; dy
++) {
691 for (dx
= 0; dx
< w
; dx
++) {
692 if (c
!= fbptr
[dx
]) {
697 ((uint8_t *)fbptr
+ vnc_server_fb_stride(vd
));
700 *color
= (uint32_t)c
;
704 static bool check_solid_tile(VncState
*vs
, int x
, int y
, int w
, int h
,
705 uint32_t* color
, bool samecolor
)
707 switch (VNC_SERVER_FB_BYTES
) {
709 return check_solid_tile32(vs
, x
, y
, w
, h
, color
, samecolor
);
713 static void find_best_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
714 uint32_t color
, int *w_ptr
, int *h_ptr
)
718 int w_best
= 0, h_best
= 0;
722 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
724 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, y
+ h
- dy
);
725 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, w_prev
);
727 if (!check_solid_tile(vs
, x
, dy
, dw
, dh
, &color
, true)) {
731 for (dx
= x
+ dw
; dx
< x
+ w_prev
;) {
732 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, x
+ w_prev
- dx
);
734 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color
, true)) {
741 if (w_prev
* (dy
+ dh
- y
) > w_best
* h_best
) {
743 h_best
= dy
+ dh
- y
;
751 static void extend_solid_area(VncState
*vs
, int x
, int y
, int w
, int h
,
752 uint32_t color
, int *x_ptr
, int *y_ptr
,
753 int *w_ptr
, int *h_ptr
)
757 /* Try to extend the area upwards. */
758 for ( cy
= *y_ptr
- 1;
759 cy
>= y
&& check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
761 *h_ptr
+= *y_ptr
- (cy
+ 1);
765 for ( cy
= *y_ptr
+ *h_ptr
;
767 check_solid_tile(vs
, *x_ptr
, cy
, *w_ptr
, 1, &color
, true);
769 *h_ptr
+= cy
- (*y_ptr
+ *h_ptr
);
771 /* ... to the left. */
772 for ( cx
= *x_ptr
- 1;
773 cx
>= x
&& check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
775 *w_ptr
+= *x_ptr
- (cx
+ 1);
778 /* ... to the right. */
779 for ( cx
= *x_ptr
+ *w_ptr
;
781 check_solid_tile(vs
, cx
, *y_ptr
, 1, *h_ptr
, &color
, true);
783 *w_ptr
+= cx
- (*x_ptr
+ *w_ptr
);
786 static int tight_init_stream(VncState
*vs
, int stream_id
,
787 int level
, int strategy
)
789 z_streamp zstream
= &vs
->tight
.stream
[stream_id
];
791 if (zstream
->opaque
== NULL
) {
794 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id
);
795 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream
->opaque
, vs
);
796 zstream
->zalloc
= vnc_zlib_zalloc
;
797 zstream
->zfree
= vnc_zlib_zfree
;
799 err
= deflateInit2(zstream
, level
, Z_DEFLATED
, MAX_WBITS
,
800 MAX_MEM_LEVEL
, strategy
);
803 fprintf(stderr
, "VNC: error initializing zlib\n");
807 vs
->tight
.levels
[stream_id
] = level
;
808 zstream
->opaque
= vs
;
811 if (vs
->tight
.levels
[stream_id
] != level
) {
812 if (deflateParams(zstream
, level
, strategy
) != Z_OK
) {
815 vs
->tight
.levels
[stream_id
] = level
;
820 static void tight_send_compact_size(VncState
*vs
, size_t len
)
824 char buf
[3] = {0, 0, 0};
826 buf
[bytes
++] = len
& 0x7F;
828 buf
[bytes
-1] |= 0x80;
829 buf
[bytes
++] = (len
>> 7) & 0x7F;
831 buf
[bytes
-1] |= 0x80;
832 buf
[bytes
++] = (len
>> 14) & 0xFF;
835 for (lpc
= 0; lpc
< bytes
; lpc
++) {
836 vnc_write_u8(vs
, buf
[lpc
]);
840 static int tight_compress_data(VncState
*vs
, int stream_id
, size_t bytes
,
841 int level
, int strategy
)
843 z_streamp zstream
= &vs
->tight
.stream
[stream_id
];
846 if (bytes
< VNC_TIGHT_MIN_TO_COMPRESS
) {
847 vnc_write(vs
, vs
->tight
.tight
.buffer
, vs
->tight
.tight
.offset
);
851 if (tight_init_stream(vs
, stream_id
, level
, strategy
)) {
855 /* reserve memory in output buffer */
856 buffer_reserve(&vs
->tight
.zlib
, bytes
+ 64);
859 zstream
->next_in
= vs
->tight
.tight
.buffer
;
860 zstream
->avail_in
= vs
->tight
.tight
.offset
;
861 zstream
->next_out
= vs
->tight
.zlib
.buffer
+ vs
->tight
.zlib
.offset
;
862 zstream
->avail_out
= vs
->tight
.zlib
.capacity
- vs
->tight
.zlib
.offset
;
863 previous_out
= zstream
->avail_out
;
864 zstream
->data_type
= Z_BINARY
;
867 if (deflate(zstream
, Z_SYNC_FLUSH
) != Z_OK
) {
868 fprintf(stderr
, "VNC: error during tight compression\n");
872 vs
->tight
.zlib
.offset
= vs
->tight
.zlib
.capacity
- zstream
->avail_out
;
873 /* ...how much data has actually been produced by deflate() */
874 bytes
= previous_out
- zstream
->avail_out
;
876 tight_send_compact_size(vs
, bytes
);
877 vnc_write(vs
, vs
->tight
.zlib
.buffer
, bytes
);
879 buffer_reset(&vs
->tight
.zlib
);
885 * Subencoding implementations.
887 static void tight_pack24(VncState
*vs
, uint8_t *buf
, size_t count
, size_t *ret
)
891 int rshift
, gshift
, bshift
;
893 buf32
= (uint32_t *)buf
;
895 if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
896 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) {
897 rshift
= vs
->client_pf
.rshift
;
898 gshift
= vs
->client_pf
.gshift
;
899 bshift
= vs
->client_pf
.bshift
;
901 rshift
= 24 - vs
->client_pf
.rshift
;
902 gshift
= 24 - vs
->client_pf
.gshift
;
903 bshift
= 24 - vs
->client_pf
.bshift
;
912 *buf
++ = (char)(pix
>> rshift
);
913 *buf
++ = (char)(pix
>> gshift
);
914 *buf
++ = (char)(pix
>> bshift
);
918 static int send_full_color_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
923 #ifdef CONFIG_VNC_PNG
924 if (tight_can_send_png_rect(vs
, w
, h
)) {
925 return send_png_rect(vs
, x
, y
, w
, h
, NULL
);
929 vnc_write_u8(vs
, stream
<< 4); /* no flushing, no filter */
931 if (vs
->tight
.pixel24
) {
932 tight_pack24(vs
, vs
->tight
.tight
.buffer
, w
* h
, &vs
->tight
.tight
.offset
);
935 bytes
= vs
->client_pf
.bytes_per_pixel
;
938 bytes
= tight_compress_data(vs
, stream
, w
* h
* bytes
,
939 tight_conf
[vs
->tight
.compression
].raw_zlib_level
,
945 static int send_solid_rect(VncState
*vs
)
949 vnc_write_u8(vs
, VNC_TIGHT_FILL
<< 4); /* no flushing, no filter */
951 if (vs
->tight
.pixel24
) {
952 tight_pack24(vs
, vs
->tight
.tight
.buffer
, 1, &vs
->tight
.tight
.offset
);
955 bytes
= vs
->client_pf
.bytes_per_pixel
;
958 vnc_write(vs
, vs
->tight
.tight
.buffer
, bytes
);
962 static int send_mono_rect(VncState
*vs
, int x
, int y
,
963 int w
, int h
, uint32_t bg
, uint32_t fg
)
967 int level
= tight_conf
[vs
->tight
.compression
].mono_zlib_level
;
969 #ifdef CONFIG_VNC_PNG
970 if (tight_can_send_png_rect(vs
, w
, h
)) {
972 int bpp
= vs
->client_pf
.bytes_per_pixel
* 8;
973 VncPalette
*palette
= palette_new(2, bpp
);
975 palette_put(palette
, bg
);
976 palette_put(palette
, fg
);
977 ret
= send_png_rect(vs
, x
, y
, w
, h
, palette
);
978 palette_destroy(palette
);
983 bytes
= ((w
+ 7) / 8) * h
;
985 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
986 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
989 switch (vs
->client_pf
.bytes_per_pixel
) {
992 uint32_t buf
[2] = {bg
, fg
};
993 size_t ret
= sizeof (buf
);
995 if (vs
->tight
.pixel24
) {
996 tight_pack24(vs
, (unsigned char*)buf
, 2, &ret
);
998 vnc_write(vs
, buf
, ret
);
1000 tight_encode_mono_rect32(vs
->tight
.tight
.buffer
, w
, h
, bg
, fg
);
1004 vnc_write(vs
, &bg
, 2);
1005 vnc_write(vs
, &fg
, 2);
1006 tight_encode_mono_rect16(vs
->tight
.tight
.buffer
, w
, h
, bg
, fg
);
1009 vnc_write_u8(vs
, bg
);
1010 vnc_write_u8(vs
, fg
);
1011 tight_encode_mono_rect8(vs
->tight
.tight
.buffer
, w
, h
, bg
, fg
);
1014 vs
->tight
.tight
.offset
= bytes
;
1016 bytes
= tight_compress_data(vs
, stream
, bytes
, level
, Z_DEFAULT_STRATEGY
);
1017 return (bytes
>= 0);
1020 struct palette_cb_priv
{
1023 #ifdef CONFIG_VNC_PNG
1024 png_colorp png_palette
;
1028 static void write_palette(int idx
, uint32_t color
, void *opaque
)
1030 struct palette_cb_priv
*priv
= opaque
;
1031 VncState
*vs
= priv
->vs
;
1032 uint32_t bytes
= vs
->client_pf
.bytes_per_pixel
;
1035 ((uint32_t*)priv
->header
)[idx
] = color
;
1037 ((uint16_t*)priv
->header
)[idx
] = color
;
1041 static bool send_gradient_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
1044 int level
= tight_conf
[vs
->tight
.compression
].gradient_zlib_level
;
1047 if (vs
->client_pf
.bytes_per_pixel
== 1) {
1048 return send_full_color_rect(vs
, x
, y
, w
, h
);
1051 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1052 vnc_write_u8(vs
, VNC_TIGHT_FILTER_GRADIENT
);
1054 buffer_reserve(&vs
->tight
.gradient
, w
* 3 * sizeof (int));
1056 if (vs
->tight
.pixel24
) {
1057 tight_filter_gradient24(vs
, vs
->tight
.tight
.buffer
, w
, h
);
1059 } else if (vs
->client_pf
.bytes_per_pixel
== 4) {
1060 tight_filter_gradient32(vs
, (uint32_t *)vs
->tight
.tight
.buffer
, w
, h
);
1063 tight_filter_gradient16(vs
, (uint16_t *)vs
->tight
.tight
.buffer
, w
, h
);
1067 buffer_reset(&vs
->tight
.gradient
);
1069 bytes
= w
* h
* bytes
;
1070 vs
->tight
.tight
.offset
= bytes
;
1072 bytes
= tight_compress_data(vs
, stream
, bytes
,
1074 return (bytes
>= 0);
1077 static int send_palette_rect(VncState
*vs
, int x
, int y
,
1078 int w
, int h
, VncPalette
*palette
)
1081 int level
= tight_conf
[vs
->tight
.compression
].idx_zlib_level
;
1085 #ifdef CONFIG_VNC_PNG
1086 if (tight_can_send_png_rect(vs
, w
, h
)) {
1087 return send_png_rect(vs
, x
, y
, w
, h
, palette
);
1091 colors
= palette_size(palette
);
1093 vnc_write_u8(vs
, (stream
| VNC_TIGHT_EXPLICIT_FILTER
) << 4);
1094 vnc_write_u8(vs
, VNC_TIGHT_FILTER_PALETTE
);
1095 vnc_write_u8(vs
, colors
- 1);
1097 switch (vs
->client_pf
.bytes_per_pixel
) {
1100 size_t old_offset
, offset
;
1101 uint32_t header
[palette_size(palette
)];
1102 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1104 old_offset
= vs
->output
.offset
;
1105 palette_iter(palette
, write_palette
, &priv
);
1106 vnc_write(vs
, header
, sizeof(header
));
1108 if (vs
->tight
.pixel24
) {
1109 tight_pack24(vs
, vs
->output
.buffer
+ old_offset
, colors
, &offset
);
1110 vs
->output
.offset
= old_offset
+ offset
;
1113 tight_encode_indexed_rect32(vs
->tight
.tight
.buffer
, w
* h
, palette
);
1118 uint16_t header
[palette_size(palette
)];
1119 struct palette_cb_priv priv
= { vs
, (uint8_t *)header
};
1121 palette_iter(palette
, write_palette
, &priv
);
1122 vnc_write(vs
, header
, sizeof(header
));
1123 tight_encode_indexed_rect16(vs
->tight
.tight
.buffer
, w
* h
, palette
);
1127 return -1; /* No palette for 8bits colors */
1131 vs
->tight
.tight
.offset
= bytes
;
1133 bytes
= tight_compress_data(vs
, stream
, bytes
,
1134 level
, Z_DEFAULT_STRATEGY
);
1135 return (bytes
>= 0);
1139 * JPEG compression stuff.
1141 #ifdef CONFIG_VNC_JPEG
1143 * Destination manager implementation for JPEG library.
1146 /* This is called once per encoding */
1147 static void jpeg_init_destination(j_compress_ptr cinfo
)
1149 VncState
*vs
= cinfo
->client_data
;
1150 Buffer
*buffer
= &vs
->tight
.jpeg
;
1152 cinfo
->dest
->next_output_byte
= (JOCTET
*)buffer
->buffer
+ buffer
->offset
;
1153 cinfo
->dest
->free_in_buffer
= (size_t)(buffer
->capacity
- buffer
->offset
);
1156 /* This is called when we ran out of buffer (shouldn't happen!) */
1157 static boolean
jpeg_empty_output_buffer(j_compress_ptr cinfo
)
1159 VncState
*vs
= cinfo
->client_data
;
1160 Buffer
*buffer
= &vs
->tight
.jpeg
;
1162 buffer
->offset
= buffer
->capacity
;
1163 buffer_reserve(buffer
, 2048);
1164 jpeg_init_destination(cinfo
);
1168 /* This is called when we are done processing data */
1169 static void jpeg_term_destination(j_compress_ptr cinfo
)
1171 VncState
*vs
= cinfo
->client_data
;
1172 Buffer
*buffer
= &vs
->tight
.jpeg
;
1174 buffer
->offset
= buffer
->capacity
- cinfo
->dest
->free_in_buffer
;
1177 static int send_jpeg_rect(VncState
*vs
, int x
, int y
, int w
, int h
, int quality
)
1179 struct jpeg_compress_struct cinfo
;
1180 struct jpeg_error_mgr jerr
;
1181 struct jpeg_destination_mgr manager
;
1182 pixman_image_t
*linebuf
;
1187 if (surface_bytes_per_pixel(vs
->vd
->ds
) == 1) {
1188 return send_full_color_rect(vs
, x
, y
, w
, h
);
1191 buffer_reserve(&vs
->tight
.jpeg
, 2048);
1193 cinfo
.err
= jpeg_std_error(&jerr
);
1194 jpeg_create_compress(&cinfo
);
1196 cinfo
.client_data
= vs
;
1197 cinfo
.image_width
= w
;
1198 cinfo
.image_height
= h
;
1199 cinfo
.input_components
= 3;
1200 cinfo
.in_color_space
= JCS_RGB
;
1202 jpeg_set_defaults(&cinfo
);
1203 jpeg_set_quality(&cinfo
, quality
, true);
1205 manager
.init_destination
= jpeg_init_destination
;
1206 manager
.empty_output_buffer
= jpeg_empty_output_buffer
;
1207 manager
.term_destination
= jpeg_term_destination
;
1208 cinfo
.dest
= &manager
;
1210 jpeg_start_compress(&cinfo
, true);
1212 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, w
);
1213 buf
= (uint8_t *)pixman_image_get_data(linebuf
);
1215 for (dy
= 0; dy
< h
; dy
++) {
1216 qemu_pixman_linebuf_fill(linebuf
, vs
->vd
->server
, w
, x
, y
+ dy
);
1217 jpeg_write_scanlines(&cinfo
, row
, 1);
1219 qemu_pixman_image_unref(linebuf
);
1221 jpeg_finish_compress(&cinfo
);
1222 jpeg_destroy_compress(&cinfo
);
1224 vnc_write_u8(vs
, VNC_TIGHT_JPEG
<< 4);
1226 tight_send_compact_size(vs
, vs
->tight
.jpeg
.offset
);
1227 vnc_write(vs
, vs
->tight
.jpeg
.buffer
, vs
->tight
.jpeg
.offset
);
1228 buffer_reset(&vs
->tight
.jpeg
);
1232 #endif /* CONFIG_VNC_JPEG */
1235 * PNG compression stuff.
1237 #ifdef CONFIG_VNC_PNG
1238 static void write_png_palette(int idx
, uint32_t pix
, void *opaque
)
1240 struct palette_cb_priv
*priv
= opaque
;
1241 VncState
*vs
= priv
->vs
;
1242 png_colorp color
= &priv
->png_palette
[idx
];
1244 if (vs
->tight
.pixel24
)
1246 color
->red
= (pix
>> vs
->client_pf
.rshift
) & vs
->client_pf
.rmax
;
1247 color
->green
= (pix
>> vs
->client_pf
.gshift
) & vs
->client_pf
.gmax
;
1248 color
->blue
= (pix
>> vs
->client_pf
.bshift
) & vs
->client_pf
.bmax
;
1252 int red
, green
, blue
;
1254 red
= (pix
>> vs
->client_pf
.rshift
) & vs
->client_pf
.rmax
;
1255 green
= (pix
>> vs
->client_pf
.gshift
) & vs
->client_pf
.gmax
;
1256 blue
= (pix
>> vs
->client_pf
.bshift
) & vs
->client_pf
.bmax
;
1257 color
->red
= ((red
* 255 + vs
->client_pf
.rmax
/ 2) /
1258 vs
->client_pf
.rmax
);
1259 color
->green
= ((green
* 255 + vs
->client_pf
.gmax
/ 2) /
1260 vs
->client_pf
.gmax
);
1261 color
->blue
= ((blue
* 255 + vs
->client_pf
.bmax
/ 2) /
1262 vs
->client_pf
.bmax
);
1266 static void png_write_data(png_structp png_ptr
, png_bytep data
,
1269 VncState
*vs
= png_get_io_ptr(png_ptr
);
1271 buffer_reserve(&vs
->tight
.png
, vs
->tight
.png
.offset
+ length
);
1272 memcpy(vs
->tight
.png
.buffer
+ vs
->tight
.png
.offset
, data
, length
);
1274 vs
->tight
.png
.offset
+= length
;
1277 static void png_flush_data(png_structp png_ptr
)
1281 static void *vnc_png_malloc(png_structp png_ptr
, png_size_t size
)
1283 return g_malloc(size
);
1286 static void vnc_png_free(png_structp png_ptr
, png_voidp ptr
)
1291 static int send_png_rect(VncState
*vs
, int x
, int y
, int w
, int h
,
1292 VncPalette
*palette
)
1294 png_byte color_type
;
1295 png_structp png_ptr
;
1297 png_colorp png_palette
= NULL
;
1298 pixman_image_t
*linebuf
;
1299 int level
= tight_png_conf
[vs
->tight
.compression
].png_zlib_level
;
1300 int filters
= tight_png_conf
[vs
->tight
.compression
].png_filters
;
1304 png_ptr
= png_create_write_struct_2(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
,
1305 NULL
, vnc_png_malloc
, vnc_png_free
);
1307 if (png_ptr
== NULL
)
1310 info_ptr
= png_create_info_struct(png_ptr
);
1312 if (info_ptr
== NULL
) {
1313 png_destroy_write_struct(&png_ptr
, NULL
);
1317 png_set_write_fn(png_ptr
, (void *) vs
, png_write_data
, png_flush_data
);
1318 png_set_compression_level(png_ptr
, level
);
1319 png_set_filter(png_ptr
, PNG_FILTER_TYPE_DEFAULT
, filters
);
1322 color_type
= PNG_COLOR_TYPE_PALETTE
;
1324 color_type
= PNG_COLOR_TYPE_RGB
;
1327 png_set_IHDR(png_ptr
, info_ptr
, w
, h
,
1328 8, color_type
, PNG_INTERLACE_NONE
,
1329 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1331 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1332 struct palette_cb_priv priv
;
1334 png_palette
= png_malloc(png_ptr
, sizeof(*png_palette
) *
1335 palette_size(palette
));
1338 priv
.png_palette
= png_palette
;
1339 palette_iter(palette
, write_png_palette
, &priv
);
1341 png_set_PLTE(png_ptr
, info_ptr
, png_palette
, palette_size(palette
));
1343 if (vs
->client_pf
.bytes_per_pixel
== 4) {
1344 tight_encode_indexed_rect32(vs
->tight
.tight
.buffer
, w
* h
, palette
);
1346 tight_encode_indexed_rect16(vs
->tight
.tight
.buffer
, w
* h
, palette
);
1350 png_write_info(png_ptr
, info_ptr
);
1352 buffer_reserve(&vs
->tight
.png
, 2048);
1353 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, w
);
1354 buf
= (uint8_t *)pixman_image_get_data(linebuf
);
1355 for (dy
= 0; dy
< h
; dy
++)
1357 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1358 memcpy(buf
, vs
->tight
.tight
.buffer
+ (dy
* w
), w
);
1360 qemu_pixman_linebuf_fill(linebuf
, vs
->vd
->server
, w
, x
, y
+ dy
);
1362 png_write_row(png_ptr
, buf
);
1364 qemu_pixman_image_unref(linebuf
);
1366 png_write_end(png_ptr
, NULL
);
1368 if (color_type
== PNG_COLOR_TYPE_PALETTE
) {
1369 png_free(png_ptr
, png_palette
);
1372 png_destroy_write_struct(&png_ptr
, &info_ptr
);
1374 vnc_write_u8(vs
, VNC_TIGHT_PNG
<< 4);
1376 tight_send_compact_size(vs
, vs
->tight
.png
.offset
);
1377 vnc_write(vs
, vs
->tight
.png
.buffer
, vs
->tight
.png
.offset
);
1378 buffer_reset(&vs
->tight
.png
);
1381 #endif /* CONFIG_VNC_PNG */
1383 static void vnc_tight_start(VncState
*vs
)
1385 buffer_reset(&vs
->tight
.tight
);
1387 // make the output buffer be the zlib buffer, so we can compress it later
1388 vs
->tight
.tmp
= vs
->output
;
1389 vs
->output
= vs
->tight
.tight
;
1392 static void vnc_tight_stop(VncState
*vs
)
1394 // switch back to normal output/zlib buffers
1395 vs
->tight
.tight
= vs
->output
;
1396 vs
->output
= vs
->tight
.tmp
;
1399 static int send_sub_rect_nojpeg(VncState
*vs
, int x
, int y
, int w
, int h
,
1400 int bg
, int fg
, int colors
, VncPalette
*palette
)
1405 if (tight_detect_smooth_image(vs
, w
, h
)) {
1406 ret
= send_gradient_rect(vs
, x
, y
, w
, h
);
1408 ret
= send_full_color_rect(vs
, x
, y
, w
, h
);
1410 } else if (colors
== 1) {
1411 ret
= send_solid_rect(vs
);
1412 } else if (colors
== 2) {
1413 ret
= send_mono_rect(vs
, x
, y
, w
, h
, bg
, fg
);
1414 } else if (colors
<= 256) {
1415 ret
= send_palette_rect(vs
, x
, y
, w
, h
, palette
);
1422 #ifdef CONFIG_VNC_JPEG
1423 static int send_sub_rect_jpeg(VncState
*vs
, int x
, int y
, int w
, int h
,
1424 int bg
, int fg
, int colors
,
1425 VncPalette
*palette
, bool force
)
1430 if (force
|| (tight_jpeg_conf
[vs
->tight
.quality
].jpeg_full
&&
1431 tight_detect_smooth_image(vs
, w
, h
))) {
1432 int quality
= tight_conf
[vs
->tight
.quality
].jpeg_quality
;
1434 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1436 ret
= send_full_color_rect(vs
, x
, y
, w
, h
);
1438 } else if (colors
== 1) {
1439 ret
= send_solid_rect(vs
);
1440 } else if (colors
== 2) {
1441 ret
= send_mono_rect(vs
, x
, y
, w
, h
, bg
, fg
);
1442 } else if (colors
<= 256) {
1443 if (force
|| (colors
> 96 &&
1444 tight_jpeg_conf
[vs
->tight
.quality
].jpeg_idx
&&
1445 tight_detect_smooth_image(vs
, w
, h
))) {
1446 int quality
= tight_conf
[vs
->tight
.quality
].jpeg_quality
;
1448 ret
= send_jpeg_rect(vs
, x
, y
, w
, h
, quality
);
1450 ret
= send_palette_rect(vs
, x
, y
, w
, h
, palette
);
1459 static int send_sub_rect(VncState
*vs
, int x
, int y
, int w
, int h
)
1461 VncPalette
*palette
= NULL
;
1462 uint32_t bg
= 0, fg
= 0;
1465 #ifdef CONFIG_VNC_JPEG
1466 bool force_jpeg
= false;
1467 bool allow_jpeg
= true;
1470 vnc_framebuffer_update(vs
, x
, y
, w
, h
, vs
->tight
.type
);
1472 vnc_tight_start(vs
);
1473 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
1476 #ifdef CONFIG_VNC_JPEG
1477 if (!vs
->vd
->non_adaptive
&& vs
->tight
.quality
!= (uint8_t)-1) {
1478 double freq
= vnc_update_freq(vs
, x
, y
, w
, h
);
1480 if (freq
< tight_jpeg_conf
[vs
->tight
.quality
].jpeg_freq_min
) {
1483 if (freq
>= tight_jpeg_conf
[vs
->tight
.quality
].jpeg_freq_threshold
) {
1485 vnc_sent_lossy_rect(vs
, x
, y
, w
, h
);
1490 colors
= tight_fill_palette(vs
, x
, y
, w
* h
, &fg
, &bg
, &palette
);
1492 #ifdef CONFIG_VNC_JPEG
1493 if (allow_jpeg
&& vs
->tight
.quality
!= (uint8_t)-1) {
1494 ret
= send_sub_rect_jpeg(vs
, x
, y
, w
, h
, bg
, fg
, colors
, palette
,
1497 ret
= send_sub_rect_nojpeg(vs
, x
, y
, w
, h
, bg
, fg
, colors
, palette
);
1500 ret
= send_sub_rect_nojpeg(vs
, x
, y
, w
, h
, bg
, fg
, colors
, palette
);
1503 palette_destroy(palette
);
1507 static int send_sub_rect_solid(VncState
*vs
, int x
, int y
, int w
, int h
)
1509 vnc_framebuffer_update(vs
, x
, y
, w
, h
, vs
->tight
.type
);
1511 vnc_tight_start(vs
);
1512 vnc_raw_send_framebuffer_update(vs
, x
, y
, w
, h
);
1515 return send_solid_rect(vs
);
1518 static int send_rect_simple(VncState
*vs
, int x
, int y
, int w
, int h
,
1521 int max_size
, max_width
;
1522 int max_sub_width
, max_sub_height
;
1527 max_size
= tight_conf
[vs
->tight
.compression
].max_rect_size
;
1528 max_width
= tight_conf
[vs
->tight
.compression
].max_rect_width
;
1530 if (split
&& (w
> max_width
|| w
* h
> max_size
)) {
1531 max_sub_width
= (w
> max_width
) ? max_width
: w
;
1532 max_sub_height
= max_size
/ max_sub_width
;
1534 for (dy
= 0; dy
< h
; dy
+= max_sub_height
) {
1535 for (dx
= 0; dx
< w
; dx
+= max_width
) {
1536 rw
= MIN(max_sub_width
, w
- dx
);
1537 rh
= MIN(max_sub_height
, h
- dy
);
1538 n
+= send_sub_rect(vs
, x
+dx
, y
+dy
, rw
, rh
);
1542 n
+= send_sub_rect(vs
, x
, y
, w
, h
);
1548 static int find_large_solid_color_rect(VncState
*vs
, int x
, int y
,
1549 int w
, int h
, int max_rows
)
1554 /* Try to find large solid-color areas and send them separately. */
1556 for (dy
= y
; dy
< y
+ h
; dy
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1558 /* If a rectangle becomes too large, send its upper part now. */
1560 if (dy
- y
>= max_rows
) {
1561 n
+= send_rect_simple(vs
, x
, y
, w
, max_rows
, true);
1566 dh
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (y
+ h
- dy
));
1568 for (dx
= x
; dx
< x
+ w
; dx
+= VNC_TIGHT_MAX_SPLIT_TILE_SIZE
) {
1569 uint32_t color_value
;
1570 int x_best
, y_best
, w_best
, h_best
;
1572 dw
= MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE
, (x
+ w
- dx
));
1574 if (!check_solid_tile(vs
, dx
, dy
, dw
, dh
, &color_value
, false)) {
1578 /* Get dimensions of solid-color area. */
1580 find_best_solid_area(vs
, dx
, dy
, w
- (dx
- x
), h
- (dy
- y
),
1581 color_value
, &w_best
, &h_best
);
1583 /* Make sure a solid rectangle is large enough
1584 (or the whole rectangle is of the same color). */
1586 if (w_best
* h_best
!= w
* h
&&
1587 w_best
* h_best
< VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE
) {
1591 /* Try to extend solid rectangle to maximum size. */
1593 x_best
= dx
; y_best
= dy
;
1594 extend_solid_area(vs
, x
, y
, w
, h
, color_value
,
1595 &x_best
, &y_best
, &w_best
, &h_best
);
1597 /* Send rectangles at top and left to solid-color area. */
1600 n
+= send_rect_simple(vs
, x
, y
, w
, y_best
-y
, true);
1603 n
+= tight_send_framebuffer_update(vs
, x
, y_best
,
1607 /* Send solid-color rectangle. */
1608 n
+= send_sub_rect_solid(vs
, x_best
, y_best
, w_best
, h_best
);
1610 /* Send remaining rectangles (at right and bottom). */
1612 if (x_best
+ w_best
!= x
+ w
) {
1613 n
+= tight_send_framebuffer_update(vs
, x_best
+w_best
,
1615 w
-(x_best
-x
)-w_best
,
1618 if (y_best
+ h_best
!= y
+ h
) {
1619 n
+= tight_send_framebuffer_update(vs
, x
, y_best
+h_best
,
1620 w
, h
-(y_best
-y
)-h_best
);
1623 /* Return after all recursive calls are done. */
1627 return n
+ send_rect_simple(vs
, x
, y
, w
, h
, true);
1630 static int tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1635 if (vs
->client_pf
.bytes_per_pixel
== 4 && vs
->client_pf
.rmax
== 0xFF &&
1636 vs
->client_pf
.bmax
== 0xFF && vs
->client_pf
.gmax
== 0xFF) {
1637 vs
->tight
.pixel24
= true;
1639 vs
->tight
.pixel24
= false;
1642 #ifdef CONFIG_VNC_JPEG
1643 if (vs
->tight
.quality
!= (uint8_t)-1) {
1644 double freq
= vnc_update_freq(vs
, x
, y
, w
, h
);
1646 if (freq
> tight_jpeg_conf
[vs
->tight
.quality
].jpeg_freq_threshold
) {
1647 return send_rect_simple(vs
, x
, y
, w
, h
, false);
1652 if (w
* h
< VNC_TIGHT_MIN_SPLIT_RECT_SIZE
) {
1653 return send_rect_simple(vs
, x
, y
, w
, h
, true);
1656 /* Calculate maximum number of rows in one non-solid rectangle. */
1658 max_rows
= tight_conf
[vs
->tight
.compression
].max_rect_size
;
1659 max_rows
/= MIN(tight_conf
[vs
->tight
.compression
].max_rect_width
, w
);
1661 return find_large_solid_color_rect(vs
, x
, y
, w
, h
, max_rows
);
1664 int vnc_tight_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1667 vs
->tight
.type
= VNC_ENCODING_TIGHT
;
1668 return tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
1671 int vnc_tight_png_send_framebuffer_update(VncState
*vs
, int x
, int y
,
1674 vs
->tight
.type
= VNC_ENCODING_TIGHT_PNG
;
1675 return tight_send_framebuffer_update(vs
, x
, y
, w
, h
);
1678 void vnc_tight_clear(VncState
*vs
)
1681 for (i
=0; i
<ARRAY_SIZE(vs
->tight
.stream
); i
++) {
1682 if (vs
->tight
.stream
[i
].opaque
) {
1683 deflateEnd(&vs
->tight
.stream
[i
]);
1687 buffer_free(&vs
->tight
.tight
);
1688 buffer_free(&vs
->tight
.zlib
);
1689 buffer_free(&vs
->tight
.gradient
);
1690 #ifdef CONFIG_VNC_JPEG
1691 buffer_free(&vs
->tight
.jpeg
);
1693 #ifdef CONFIG_VNC_PNG
1694 buffer_free(&vs
->tight
.png
);