Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU VNC display driver: tight encoding |
| 3 | * |
| 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. |
| 7 | * |
| 8 | * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> |
| 9 | * |
| 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: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | * THE SOFTWARE. |
| 27 | */ |
| 28 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 29 | #include "qemu/osdep.h" |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 30 | |
Roy Tam | f26e428 | 2011-06-18 13:13:39 +0800 | [diff] [blame] | 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" |
| 35 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_VNC_PNG |
Stefan Weil | 2fb0c09 | 2011-06-26 19:29:13 +0000 | [diff] [blame] | 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 |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 40 | #include <png.h> |
| 41 | #endif |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 43 | #include <jpeglib.h> |
| 44 | #endif |
| 45 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 46 | #include "qemu/bswap.h" |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 47 | #include "vnc.h" |
Corentin Chary | 245f7b5 | 2010-07-07 20:57:53 +0200 | [diff] [blame] | 48 | #include "vnc-enc-tight.h" |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 49 | #include "vnc-palette.h" |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 50 | |
| 51 | /* Compression level stuff. The following array contains various |
| 52 | encoder parameters for each of 10 compression levels (0..9). |
| 53 | Last three parameters correspond to JPEG quality levels (0..9). */ |
| 54 | |
| 55 | static const struct { |
| 56 | int max_rect_size, max_rect_width; |
| 57 | int mono_min_rect_size, gradient_min_rect_size; |
| 58 | int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level; |
| 59 | int gradient_threshold, gradient_threshold24; |
| 60 | int idx_max_colors_divisor; |
| 61 | int jpeg_quality, jpeg_threshold, jpeg_threshold24; |
| 62 | } tight_conf[] = { |
| 63 | { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 }, |
| 64 | { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 }, |
| 65 | { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 }, |
| 66 | { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 }, |
| 67 | { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 }, |
| 68 | { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 }, |
| 69 | { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 }, |
| 70 | { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 }, |
| 71 | { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 }, |
| 72 | { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 } |
| 73 | }; |
| 74 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 75 | |
| 76 | static int tight_send_framebuffer_update(VncState *vs, int x, int y, |
| 77 | int w, int h); |
| 78 | |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_VNC_JPEG |
| 80 | static const struct { |
| 81 | double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */ |
| 82 | double jpeg_freq_threshold; /* Always send JPEG if the freq is above */ |
| 83 | int jpeg_idx; /* Allow indexed JPEG */ |
| 84 | int jpeg_full; /* Allow full color JPEG */ |
| 85 | } tight_jpeg_conf[] = { |
Corentin Chary | 8cb4a6b | 2011-02-04 09:06:07 +0100 | [diff] [blame] | 86 | { 0, 8, 1, 1 }, |
| 87 | { 0, 8, 1, 1 }, |
| 88 | { 0, 8, 1, 1 }, |
| 89 | { 0, 8, 1, 1 }, |
| 90 | { 0, 10, 1, 1 }, |
| 91 | { 0.1, 10, 1, 1 }, |
| 92 | { 0.2, 10, 1, 1 }, |
| 93 | { 0.3, 12, 0, 0 }, |
| 94 | { 0.4, 14, 0, 0 }, |
| 95 | { 0.5, 16, 0, 0 }, |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 96 | }; |
| 97 | #endif |
| 98 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 99 | #ifdef CONFIG_VNC_PNG |
Corentin Chary | 3941bf6 | 2010-07-07 20:57:57 +0200 | [diff] [blame] | 100 | static const struct { |
| 101 | int png_zlib_level, png_filters; |
| 102 | } tight_png_conf[] = { |
| 103 | { 0, PNG_NO_FILTERS }, |
| 104 | { 1, PNG_NO_FILTERS }, |
| 105 | { 2, PNG_NO_FILTERS }, |
| 106 | { 3, PNG_NO_FILTERS }, |
| 107 | { 4, PNG_NO_FILTERS }, |
| 108 | { 5, PNG_ALL_FILTERS }, |
| 109 | { 6, PNG_ALL_FILTERS }, |
| 110 | { 7, PNG_ALL_FILTERS }, |
| 111 | { 8, PNG_ALL_FILTERS }, |
| 112 | { 9, PNG_ALL_FILTERS }, |
| 113 | }; |
| 114 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 115 | static int send_png_rect(VncState *vs, int x, int y, int w, int h, |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 116 | VncPalette *palette); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 117 | |
| 118 | static bool tight_can_send_png_rect(VncState *vs, int w, int h) |
| 119 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 120 | if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Gerd Hoffmann | d39fa6d | 2013-02-28 17:16:48 +0100 | [diff] [blame] | 124 | if (surface_bytes_per_pixel(vs->vd->ds) == 1 || |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 125 | vs->client_pf.bytes_per_pixel == 1) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | #endif |
| 132 | |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 133 | /* |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 134 | * Code to guess if given rectangle is suitable for smooth image |
| 135 | * compression (by applying "gradient" filter or JPEG coder). |
| 136 | */ |
| 137 | |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 138 | static unsigned int |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 139 | tight_detect_smooth_image24(VncState *vs, int w, int h) |
| 140 | { |
| 141 | int off; |
| 142 | int x, y, d, dx; |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 143 | unsigned int c; |
| 144 | unsigned int stats[256]; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 145 | int pixels = 0; |
| 146 | int pix, left[3]; |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 147 | unsigned int errors; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 148 | unsigned char *buf = vs->tight.tight.buffer; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * If client is big-endian, color samples begin from the second |
| 152 | * byte (offset 1) of a 32-bit pixel value. |
| 153 | */ |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 154 | off = vs->client_be; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 155 | |
| 156 | memset(stats, 0, sizeof (stats)); |
| 157 | |
| 158 | for (y = 0, x = 0; y < h && x < w;) { |
| 159 | for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; |
| 160 | d++) { |
| 161 | for (c = 0; c < 3; c++) { |
| 162 | left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF; |
| 163 | } |
| 164 | for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) { |
| 165 | for (c = 0; c < 3; c++) { |
| 166 | pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF; |
| 167 | stats[abs(pix - left[c])]++; |
| 168 | left[c] = pix; |
| 169 | } |
| 170 | pixels++; |
| 171 | } |
| 172 | } |
| 173 | if (w > h) { |
| 174 | x += h; |
| 175 | y = 0; |
| 176 | } else { |
| 177 | x = 0; |
| 178 | y += w; |
| 179 | } |
| 180 | } |
| 181 | |
Gonglei | b529915 | 2014-05-28 21:21:35 +0800 | [diff] [blame] | 182 | if (pixels == 0) { |
| 183 | return 0; |
| 184 | } |
| 185 | |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 186 | /* 95% smooth or more ... */ |
| 187 | if (stats[0] * 33 / pixels >= 95) { |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | errors = 0; |
| 192 | for (c = 1; c < 8; c++) { |
| 193 | errors += stats[c] * (c * c); |
| 194 | if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { |
| 195 | return 0; |
| 196 | } |
| 197 | } |
| 198 | for (; c < 256; c++) { |
| 199 | errors += stats[c] * (c * c); |
| 200 | } |
| 201 | errors /= (pixels * 3 - stats[0]); |
| 202 | |
| 203 | return errors; |
| 204 | } |
| 205 | |
| 206 | #define DEFINE_DETECT_FUNCTION(bpp) \ |
| 207 | \ |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 208 | static unsigned int \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 209 | tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \ |
| 210 | bool endian; \ |
| 211 | uint##bpp##_t pix; \ |
| 212 | int max[3], shift[3]; \ |
| 213 | int x, y, d, dx; \ |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 214 | unsigned int c; \ |
| 215 | unsigned int stats[256]; \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 216 | int pixels = 0; \ |
| 217 | int sample, sum, left[3]; \ |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 218 | unsigned int errors; \ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 219 | unsigned char *buf = vs->tight.tight.buffer; \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 220 | \ |
Benjamin Herrenschmidt | 77bfcf2 | 2014-06-21 14:58:06 +1000 | [diff] [blame] | 221 | endian = 0; /* FIXME */ \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 222 | \ |
| 223 | \ |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 224 | max[0] = vs->client_pf.rmax; \ |
| 225 | max[1] = vs->client_pf.gmax; \ |
| 226 | max[2] = vs->client_pf.bmax; \ |
| 227 | shift[0] = vs->client_pf.rshift; \ |
| 228 | shift[1] = vs->client_pf.gshift; \ |
| 229 | shift[2] = vs->client_pf.bshift; \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 230 | \ |
| 231 | memset(stats, 0, sizeof(stats)); \ |
| 232 | \ |
| 233 | y = 0, x = 0; \ |
| 234 | while (y < h && x < w) { \ |
| 235 | for (d = 0; d < h - y && \ |
| 236 | d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \ |
| 237 | pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \ |
| 238 | if (endian) { \ |
Izumi Tsutsui | ba5e7f8 | 2010-08-08 18:54:05 +0900 | [diff] [blame] | 239 | pix = bswap##bpp(pix); \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 240 | } \ |
| 241 | for (c = 0; c < 3; c++) { \ |
| 242 | left[c] = (int)(pix >> shift[c] & max[c]); \ |
| 243 | } \ |
| 244 | for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \ |
| 245 | dx++) { \ |
| 246 | pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \ |
| 247 | if (endian) { \ |
Izumi Tsutsui | ba5e7f8 | 2010-08-08 18:54:05 +0900 | [diff] [blame] | 248 | pix = bswap##bpp(pix); \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 249 | } \ |
| 250 | sum = 0; \ |
| 251 | for (c = 0; c < 3; c++) { \ |
| 252 | sample = (int)(pix >> shift[c] & max[c]); \ |
| 253 | sum += abs(sample - left[c]); \ |
| 254 | left[c] = sample; \ |
| 255 | } \ |
| 256 | if (sum > 255) { \ |
| 257 | sum = 255; \ |
| 258 | } \ |
| 259 | stats[sum]++; \ |
| 260 | pixels++; \ |
| 261 | } \ |
| 262 | } \ |
| 263 | if (w > h) { \ |
| 264 | x += h; \ |
| 265 | y = 0; \ |
| 266 | } else { \ |
| 267 | x = 0; \ |
| 268 | y += w; \ |
| 269 | } \ |
| 270 | } \ |
Gonglei | b529915 | 2014-05-28 21:21:35 +0800 | [diff] [blame] | 271 | if (pixels == 0) { \ |
| 272 | return 0; \ |
| 273 | } \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 274 | if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \ |
| 275 | return 0; \ |
| 276 | } \ |
| 277 | \ |
| 278 | errors = 0; \ |
| 279 | for (c = 1; c < 8; c++) { \ |
| 280 | errors += stats[c] * (c * c); \ |
| 281 | if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \ |
| 282 | return 0; \ |
| 283 | } \ |
| 284 | } \ |
| 285 | for (; c < 256; c++) { \ |
| 286 | errors += stats[c] * (c * c); \ |
| 287 | } \ |
| 288 | errors /= (pixels - stats[0]); \ |
| 289 | \ |
| 290 | return errors; \ |
| 291 | } |
| 292 | |
| 293 | DEFINE_DETECT_FUNCTION(16) |
| 294 | DEFINE_DETECT_FUNCTION(32) |
| 295 | |
| 296 | static int |
| 297 | tight_detect_smooth_image(VncState *vs, int w, int h) |
| 298 | { |
Blue Swirl | 249cdb4 | 2010-07-27 17:26:08 +0000 | [diff] [blame] | 299 | unsigned int errors; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 300 | int compression = vs->tight.compression; |
| 301 | int quality = vs->tight.quality; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 302 | |
Corentin Chary | 6f9c78c | 2010-07-07 20:57:51 +0200 | [diff] [blame] | 303 | if (!vs->vd->lossy) { |
| 304 | return 0; |
| 305 | } |
| 306 | |
Gerd Hoffmann | d39fa6d | 2013-02-28 17:16:48 +0100 | [diff] [blame] | 307 | if (surface_bytes_per_pixel(vs->vd->ds) == 1 || |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 308 | vs->client_pf.bytes_per_pixel == 1 || |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 309 | w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) { |
| 310 | return 0; |
| 311 | } |
| 312 | |
Blue Swirl | 7bccf57 | 2010-07-27 15:32:39 +0000 | [diff] [blame] | 313 | if (vs->tight.quality != (uint8_t)-1) { |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 314 | if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) { |
| 315 | return 0; |
| 316 | } |
| 317 | } else { |
| 318 | if (w * h < tight_conf[compression].gradient_min_rect_size) { |
| 319 | return 0; |
| 320 | } |
| 321 | } |
| 322 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 323 | if (vs->client_pf.bytes_per_pixel == 4) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 324 | if (vs->tight.pixel24) { |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 325 | errors = tight_detect_smooth_image24(vs, w, h); |
Blue Swirl | 7bccf57 | 2010-07-27 15:32:39 +0000 | [diff] [blame] | 326 | if (vs->tight.quality != (uint8_t)-1) { |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 327 | return (errors < tight_conf[quality].jpeg_threshold24); |
| 328 | } |
| 329 | return (errors < tight_conf[compression].gradient_threshold24); |
| 330 | } else { |
| 331 | errors = tight_detect_smooth_image32(vs, w, h); |
| 332 | } |
| 333 | } else { |
| 334 | errors = tight_detect_smooth_image16(vs, w, h); |
| 335 | } |
Markus Armbruster | 2e7bcdb | 2014-02-21 16:42:52 +0100 | [diff] [blame] | 336 | if (quality != (uint8_t)-1) { |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 337 | return (errors < tight_conf[quality].jpeg_threshold); |
| 338 | } |
| 339 | return (errors < tight_conf[compression].gradient_threshold); |
| 340 | } |
| 341 | |
| 342 | /* |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 343 | * Code to determine how many different colors used in rectangle. |
| 344 | */ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 345 | #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \ |
| 346 | \ |
| 347 | static int \ |
| 348 | tight_fill_palette##bpp(VncState *vs, int x, int y, \ |
| 349 | int max, size_t count, \ |
| 350 | uint32_t *bg, uint32_t *fg, \ |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 351 | VncPalette *palette) { \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 352 | uint##bpp##_t *data; \ |
| 353 | uint##bpp##_t c0, c1, ci; \ |
| 354 | int i, n0, n1; \ |
| 355 | \ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 356 | data = (uint##bpp##_t *)vs->tight.tight.buffer; \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 357 | \ |
| 358 | c0 = data[0]; \ |
| 359 | i = 1; \ |
| 360 | while (i < count && data[i] == c0) \ |
| 361 | i++; \ |
| 362 | if (i >= count) { \ |
| 363 | *bg = *fg = c0; \ |
| 364 | return 1; \ |
| 365 | } \ |
| 366 | \ |
| 367 | if (max < 2) { \ |
| 368 | return 0; \ |
| 369 | } \ |
| 370 | \ |
| 371 | n0 = i; \ |
| 372 | c1 = data[i]; \ |
| 373 | n1 = 0; \ |
| 374 | for (i++; i < count; i++) { \ |
| 375 | ci = data[i]; \ |
| 376 | if (ci == c0) { \ |
| 377 | n0++; \ |
| 378 | } else if (ci == c1) { \ |
| 379 | n1++; \ |
| 380 | } else \ |
| 381 | break; \ |
| 382 | } \ |
| 383 | if (i >= count) { \ |
| 384 | if (n0 > n1) { \ |
| 385 | *bg = (uint32_t)c0; \ |
| 386 | *fg = (uint32_t)c1; \ |
| 387 | } else { \ |
| 388 | *bg = (uint32_t)c1; \ |
| 389 | *fg = (uint32_t)c0; \ |
| 390 | } \ |
| 391 | return 2; \ |
| 392 | } \ |
| 393 | \ |
| 394 | if (max == 2) { \ |
| 395 | return 0; \ |
| 396 | } \ |
| 397 | \ |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 398 | palette_init(palette, max, bpp); \ |
| 399 | palette_put(palette, c0); \ |
| 400 | palette_put(palette, c1); \ |
| 401 | palette_put(palette, ci); \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 402 | \ |
| 403 | for (i++; i < count; i++) { \ |
| 404 | if (data[i] == ci) { \ |
| 405 | continue; \ |
| 406 | } else { \ |
Corentin Chary | 5d8efe3 | 2010-07-07 20:57:54 +0200 | [diff] [blame] | 407 | ci = data[i]; \ |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 408 | if (!palette_put(palette, (uint32_t)ci)) { \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 409 | return 0; \ |
| 410 | } \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 411 | } \ |
| 412 | } \ |
| 413 | \ |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 414 | return palette_size(palette); \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | DEFINE_FILL_PALETTE_FUNCTION(8) |
| 418 | DEFINE_FILL_PALETTE_FUNCTION(16) |
| 419 | DEFINE_FILL_PALETTE_FUNCTION(32) |
| 420 | |
| 421 | static int tight_fill_palette(VncState *vs, int x, int y, |
| 422 | size_t count, uint32_t *bg, uint32_t *fg, |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 423 | VncPalette *palette) |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 424 | { |
| 425 | int max; |
| 426 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 427 | max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 428 | if (max < 2 && |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 429 | count >= tight_conf[vs->tight.compression].mono_min_rect_size) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 430 | max = 2; |
| 431 | } |
| 432 | if (max >= 256) { |
| 433 | max = 256; |
| 434 | } |
| 435 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 436 | switch (vs->client_pf.bytes_per_pixel) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 437 | case 4: |
| 438 | return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette); |
| 439 | case 2: |
| 440 | return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette); |
| 441 | default: |
| 442 | max = 2; |
| 443 | return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette); |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 448 | /* |
| 449 | * Converting truecolor samples into palette indices. |
| 450 | */ |
| 451 | #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \ |
| 452 | \ |
| 453 | static void \ |
| 454 | tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \ |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 455 | VncPalette *palette) { \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 456 | uint##bpp##_t *src; \ |
| 457 | uint##bpp##_t rgb; \ |
Corentin Chary | 54d43ea | 2010-06-01 23:05:42 +0200 | [diff] [blame] | 458 | int i, rep; \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 459 | uint8_t idx; \ |
| 460 | \ |
| 461 | src = (uint##bpp##_t *) buf; \ |
| 462 | \ |
Herongguang (Stephen) | 3f7e51b | 2016-07-12 17:31:23 +0800 | [diff] [blame] | 463 | for (i = 0; i < count; ) { \ |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 464 | \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 465 | rgb = *src++; \ |
Herongguang (Stephen) | 3f7e51b | 2016-07-12 17:31:23 +0800 | [diff] [blame] | 466 | i++; \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 467 | rep = 0; \ |
Corentin Chary | 54d43ea | 2010-06-01 23:05:42 +0200 | [diff] [blame] | 468 | while (i < count && *src == rgb) { \ |
| 469 | rep++, src++, i++; \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 470 | } \ |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 471 | idx = palette_idx(palette, rgb); \ |
| 472 | /* \ |
| 473 | * Should never happen, but don't break everything \ |
| 474 | * if it does, use the first color instead \ |
| 475 | */ \ |
Blue Swirl | 7bccf57 | 2010-07-27 15:32:39 +0000 | [diff] [blame] | 476 | if (idx == (uint8_t)-1) { \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 477 | idx = 0; \ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 478 | } \ |
| 479 | while (rep >= 0) { \ |
| 480 | *buf++ = idx; \ |
| 481 | rep--; \ |
| 482 | } \ |
| 483 | } \ |
| 484 | } |
| 485 | |
| 486 | DEFINE_IDX_ENCODE_FUNCTION(16) |
| 487 | DEFINE_IDX_ENCODE_FUNCTION(32) |
| 488 | |
| 489 | #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \ |
| 490 | \ |
| 491 | static void \ |
| 492 | tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \ |
| 493 | uint##bpp##_t bg, uint##bpp##_t fg) { \ |
| 494 | uint##bpp##_t *ptr; \ |
| 495 | unsigned int value, mask; \ |
| 496 | int aligned_width; \ |
| 497 | int x, y, bg_bits; \ |
| 498 | \ |
| 499 | ptr = (uint##bpp##_t *) buf; \ |
| 500 | aligned_width = w - w % 8; \ |
| 501 | \ |
| 502 | for (y = 0; y < h; y++) { \ |
| 503 | for (x = 0; x < aligned_width; x += 8) { \ |
| 504 | for (bg_bits = 0; bg_bits < 8; bg_bits++) { \ |
| 505 | if (*ptr++ != bg) { \ |
| 506 | break; \ |
| 507 | } \ |
| 508 | } \ |
| 509 | if (bg_bits == 8) { \ |
| 510 | *buf++ = 0; \ |
| 511 | continue; \ |
| 512 | } \ |
| 513 | mask = 0x80 >> bg_bits; \ |
| 514 | value = mask; \ |
| 515 | for (bg_bits++; bg_bits < 8; bg_bits++) { \ |
| 516 | mask >>= 1; \ |
| 517 | if (*ptr++ != bg) { \ |
| 518 | value |= mask; \ |
| 519 | } \ |
| 520 | } \ |
| 521 | *buf++ = (uint8_t)value; \ |
| 522 | } \ |
| 523 | \ |
| 524 | mask = 0x80; \ |
| 525 | value = 0; \ |
| 526 | if (x >= w) { \ |
| 527 | continue; \ |
| 528 | } \ |
| 529 | \ |
| 530 | for (; x < w; x++) { \ |
| 531 | if (*ptr++ != bg) { \ |
| 532 | value |= mask; \ |
| 533 | } \ |
| 534 | mask >>= 1; \ |
| 535 | } \ |
| 536 | *buf++ = (uint8_t)value; \ |
| 537 | } \ |
| 538 | } |
| 539 | |
| 540 | DEFINE_MONO_ENCODE_FUNCTION(8) |
| 541 | DEFINE_MONO_ENCODE_FUNCTION(16) |
| 542 | DEFINE_MONO_ENCODE_FUNCTION(32) |
| 543 | |
| 544 | /* |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 545 | * ``Gradient'' filter for 24-bit color samples. |
| 546 | * Should be called only when redMax, greenMax and blueMax are 255. |
| 547 | * Color components assumed to be byte-aligned. |
| 548 | */ |
| 549 | |
| 550 | static void |
| 551 | tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h) |
| 552 | { |
| 553 | uint32_t *buf32; |
| 554 | uint32_t pix32; |
| 555 | int shift[3]; |
| 556 | int *prev; |
| 557 | int here[3], upper[3], left[3], upperleft[3]; |
| 558 | int prediction; |
| 559 | int x, y, c; |
| 560 | |
| 561 | buf32 = (uint32_t *)buf; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 562 | memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 563 | |
Benjamin Herrenschmidt | 77bfcf2 | 2014-06-21 14:58:06 +1000 | [diff] [blame] | 564 | if (1 /* FIXME */) { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 565 | shift[0] = vs->client_pf.rshift; |
| 566 | shift[1] = vs->client_pf.gshift; |
| 567 | shift[2] = vs->client_pf.bshift; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 568 | } else { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 569 | shift[0] = 24 - vs->client_pf.rshift; |
| 570 | shift[1] = 24 - vs->client_pf.gshift; |
| 571 | shift[2] = 24 - vs->client_pf.bshift; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | for (y = 0; y < h; y++) { |
| 575 | for (c = 0; c < 3; c++) { |
| 576 | upper[c] = 0; |
| 577 | here[c] = 0; |
| 578 | } |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 579 | prev = (int *)vs->tight.gradient.buffer; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 580 | for (x = 0; x < w; x++) { |
| 581 | pix32 = *buf32++; |
| 582 | for (c = 0; c < 3; c++) { |
| 583 | upperleft[c] = upper[c]; |
| 584 | left[c] = here[c]; |
| 585 | upper[c] = *prev; |
| 586 | here[c] = (int)(pix32 >> shift[c] & 0xFF); |
| 587 | *prev++ = here[c]; |
| 588 | |
| 589 | prediction = left[c] + upper[c] - upperleft[c]; |
| 590 | if (prediction < 0) { |
| 591 | prediction = 0; |
| 592 | } else if (prediction > 0xFF) { |
| 593 | prediction = 0xFF; |
| 594 | } |
| 595 | *buf++ = (char)(here[c] - prediction); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /* |
| 603 | * ``Gradient'' filter for other color depths. |
| 604 | */ |
| 605 | |
| 606 | #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \ |
| 607 | \ |
| 608 | static void \ |
| 609 | tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \ |
| 610 | int w, int h) { \ |
| 611 | uint##bpp##_t pix, diff; \ |
| 612 | bool endian; \ |
| 613 | int *prev; \ |
| 614 | int max[3], shift[3]; \ |
| 615 | int here[3], upper[3], left[3], upperleft[3]; \ |
| 616 | int prediction; \ |
| 617 | int x, y, c; \ |
| 618 | \ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 619 | memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 620 | \ |
Benjamin Herrenschmidt | 77bfcf2 | 2014-06-21 14:58:06 +1000 | [diff] [blame] | 621 | endian = 0; /* FIXME */ \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 622 | \ |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 623 | max[0] = vs->client_pf.rmax; \ |
| 624 | max[1] = vs->client_pf.gmax; \ |
| 625 | max[2] = vs->client_pf.bmax; \ |
| 626 | shift[0] = vs->client_pf.rshift; \ |
| 627 | shift[1] = vs->client_pf.gshift; \ |
| 628 | shift[2] = vs->client_pf.bshift; \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 629 | \ |
| 630 | for (y = 0; y < h; y++) { \ |
| 631 | for (c = 0; c < 3; c++) { \ |
| 632 | upper[c] = 0; \ |
| 633 | here[c] = 0; \ |
| 634 | } \ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 635 | prev = (int *)vs->tight.gradient.buffer; \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 636 | for (x = 0; x < w; x++) { \ |
| 637 | pix = *buf; \ |
| 638 | if (endian) { \ |
Izumi Tsutsui | ba5e7f8 | 2010-08-08 18:54:05 +0900 | [diff] [blame] | 639 | pix = bswap##bpp(pix); \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 640 | } \ |
| 641 | diff = 0; \ |
| 642 | for (c = 0; c < 3; c++) { \ |
| 643 | upperleft[c] = upper[c]; \ |
| 644 | left[c] = here[c]; \ |
| 645 | upper[c] = *prev; \ |
| 646 | here[c] = (int)(pix >> shift[c] & max[c]); \ |
| 647 | *prev++ = here[c]; \ |
| 648 | \ |
| 649 | prediction = left[c] + upper[c] - upperleft[c]; \ |
| 650 | if (prediction < 0) { \ |
| 651 | prediction = 0; \ |
| 652 | } else if (prediction > max[c]) { \ |
| 653 | prediction = max[c]; \ |
| 654 | } \ |
| 655 | diff |= ((here[c] - prediction) & max[c]) \ |
| 656 | << shift[c]; \ |
| 657 | } \ |
| 658 | if (endian) { \ |
Izumi Tsutsui | ba5e7f8 | 2010-08-08 18:54:05 +0900 | [diff] [blame] | 659 | diff = bswap##bpp(diff); \ |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 660 | } \ |
| 661 | *buf++ = diff; \ |
| 662 | } \ |
| 663 | } \ |
| 664 | } |
| 665 | |
| 666 | DEFINE_GRADIENT_FILTER_FUNCTION(16) |
| 667 | DEFINE_GRADIENT_FILTER_FUNCTION(32) |
| 668 | |
| 669 | /* |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 670 | * Check if a rectangle is all of the same color. If needSameColor is |
| 671 | * set to non-zero, then also check that its color equals to the |
Stefan Weil | b0cd712 | 2010-08-06 21:53:45 +0200 | [diff] [blame] | 672 | * *colorPtr value. The result is 1 if the test is successful, and in |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 673 | * that case new color will be stored in *colorPtr. |
| 674 | */ |
| 675 | |
Gerd Hoffmann | 9436268 | 2012-10-11 12:11:27 +0200 | [diff] [blame] | 676 | static bool |
| 677 | check_solid_tile32(VncState *vs, int x, int y, int w, int h, |
| 678 | uint32_t *color, bool samecolor) |
| 679 | { |
| 680 | VncDisplay *vd = vs->vd; |
| 681 | uint32_t *fbptr; |
| 682 | uint32_t c; |
| 683 | int dx, dy; |
| 684 | |
| 685 | fbptr = vnc_server_fb_ptr(vd, x, y); |
| 686 | |
| 687 | c = *fbptr; |
| 688 | if (samecolor && (uint32_t)c != *color) { |
| 689 | return false; |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Gerd Hoffmann | 9436268 | 2012-10-11 12:11:27 +0200 | [diff] [blame] | 692 | for (dy = 0; dy < h; dy++) { |
| 693 | for (dx = 0; dx < w; dx++) { |
| 694 | if (c != fbptr[dx]) { |
| 695 | return false; |
| 696 | } |
| 697 | } |
| 698 | fbptr = (uint32_t *) |
| 699 | ((uint8_t *)fbptr + vnc_server_fb_stride(vd)); |
| 700 | } |
| 701 | |
| 702 | *color = (uint32_t)c; |
| 703 | return true; |
| 704 | } |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 705 | |
| 706 | static bool check_solid_tile(VncState *vs, int x, int y, int w, int h, |
| 707 | uint32_t* color, bool samecolor) |
| 708 | { |
Alex Bennée | d9d2663 | 2016-09-22 11:13:08 +0100 | [diff] [blame] | 709 | QEMU_BUILD_BUG_ON(VNC_SERVER_FB_BYTES != 4); |
| 710 | return check_solid_tile32(vs, x, y, w, h, color, samecolor); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 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) |
| 715 | { |
| 716 | int dx, dy, dw, dh; |
| 717 | int w_prev; |
| 718 | int w_best = 0, h_best = 0; |
| 719 | |
| 720 | w_prev = w; |
| 721 | |
| 722 | for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 723 | |
| 724 | dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy); |
| 725 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev); |
| 726 | |
| 727 | if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) { |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | for (dx = x + dw; dx < x + w_prev;) { |
| 732 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx); |
| 733 | |
| 734 | if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) { |
| 735 | break; |
| 736 | } |
| 737 | dx += dw; |
| 738 | } |
| 739 | |
| 740 | w_prev = dx - x; |
| 741 | if (w_prev * (dy + dh - y) > w_best * h_best) { |
| 742 | w_best = w_prev; |
| 743 | h_best = dy + dh - y; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | *w_ptr = w_best; |
| 748 | *h_ptr = h_best; |
| 749 | } |
| 750 | |
| 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) |
| 754 | { |
| 755 | int cx, cy; |
| 756 | |
| 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); |
| 760 | cy-- ); |
| 761 | *h_ptr += *y_ptr - (cy + 1); |
| 762 | *y_ptr = cy + 1; |
| 763 | |
| 764 | /* ... downwards. */ |
| 765 | for ( cy = *y_ptr + *h_ptr; |
| 766 | cy < y + h && |
| 767 | check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); |
| 768 | cy++ ); |
| 769 | *h_ptr += cy - (*y_ptr + *h_ptr); |
| 770 | |
| 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); |
| 774 | cx-- ); |
| 775 | *w_ptr += *x_ptr - (cx + 1); |
| 776 | *x_ptr = cx + 1; |
| 777 | |
| 778 | /* ... to the right. */ |
| 779 | for ( cx = *x_ptr + *w_ptr; |
| 780 | cx < x + w && |
| 781 | check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); |
| 782 | cx++ ); |
| 783 | *w_ptr += cx - (*x_ptr + *w_ptr); |
| 784 | } |
| 785 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 786 | static int tight_init_stream(VncState *vs, int stream_id, |
| 787 | int level, int strategy) |
| 788 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 789 | z_streamp zstream = &vs->tight.stream[stream_id]; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 790 | |
| 791 | if (zstream->opaque == NULL) { |
| 792 | int err; |
| 793 | |
| 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; |
| 798 | |
| 799 | err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS, |
| 800 | MAX_MEM_LEVEL, strategy); |
| 801 | |
| 802 | if (err != Z_OK) { |
| 803 | fprintf(stderr, "VNC: error initializing zlib\n"); |
| 804 | return -1; |
| 805 | } |
| 806 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 807 | vs->tight.levels[stream_id] = level; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 808 | zstream->opaque = vs; |
| 809 | } |
| 810 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 811 | if (vs->tight.levels[stream_id] != level) { |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 812 | if (deflateParams(zstream, level, strategy) != Z_OK) { |
| 813 | return -1; |
| 814 | } |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 815 | vs->tight.levels[stream_id] = level; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static void tight_send_compact_size(VncState *vs, size_t len) |
| 821 | { |
| 822 | int lpc = 0; |
| 823 | int bytes = 0; |
| 824 | char buf[3] = {0, 0, 0}; |
| 825 | |
| 826 | buf[bytes++] = len & 0x7F; |
| 827 | if (len > 0x7F) { |
| 828 | buf[bytes-1] |= 0x80; |
| 829 | buf[bytes++] = (len >> 7) & 0x7F; |
| 830 | if (len > 0x3FFF) { |
| 831 | buf[bytes-1] |= 0x80; |
| 832 | buf[bytes++] = (len >> 14) & 0xFF; |
| 833 | } |
| 834 | } |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 835 | for (lpc = 0; lpc < bytes; lpc++) { |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 836 | vnc_write_u8(vs, buf[lpc]); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | static int tight_compress_data(VncState *vs, int stream_id, size_t bytes, |
| 841 | int level, int strategy) |
| 842 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 843 | z_streamp zstream = &vs->tight.stream[stream_id]; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 844 | int previous_out; |
| 845 | |
| 846 | if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 847 | vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 848 | return bytes; |
| 849 | } |
| 850 | |
| 851 | if (tight_init_stream(vs, stream_id, level, strategy)) { |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | /* reserve memory in output buffer */ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 856 | buffer_reserve(&vs->tight.zlib, bytes + 64); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 857 | |
| 858 | /* set pointers */ |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 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; |
Michael Tokarev | 2caa9e9 | 2011-03-21 09:34:35 +0100 | [diff] [blame] | 863 | previous_out = zstream->avail_out; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 864 | zstream->data_type = Z_BINARY; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 865 | |
| 866 | /* start encoding */ |
| 867 | if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) { |
| 868 | fprintf(stderr, "VNC: error during tight compression\n"); |
| 869 | return -1; |
| 870 | } |
| 871 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 872 | vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out; |
Michael Tokarev | 2caa9e9 | 2011-03-21 09:34:35 +0100 | [diff] [blame] | 873 | /* ...how much data has actually been produced by deflate() */ |
| 874 | bytes = previous_out - zstream->avail_out; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 875 | |
| 876 | tight_send_compact_size(vs, bytes); |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 877 | vnc_write(vs, vs->tight.zlib.buffer, bytes); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 878 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 879 | buffer_reset(&vs->tight.zlib); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 880 | |
| 881 | return bytes; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Subencoding implementations. |
| 886 | */ |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 887 | static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret) |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 888 | { |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 889 | uint32_t *buf32; |
| 890 | uint32_t pix; |
| 891 | int rshift, gshift, bshift; |
| 892 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 893 | buf32 = (uint32_t *)buf; |
| 894 | |
Benjamin Herrenschmidt | 77bfcf2 | 2014-06-21 14:58:06 +1000 | [diff] [blame] | 895 | if (1 /* FIXME */) { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 896 | rshift = vs->client_pf.rshift; |
| 897 | gshift = vs->client_pf.gshift; |
| 898 | bshift = vs->client_pf.bshift; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 899 | } else { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 900 | rshift = 24 - vs->client_pf.rshift; |
| 901 | gshift = 24 - vs->client_pf.gshift; |
| 902 | bshift = 24 - vs->client_pf.bshift; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 905 | if (ret) { |
| 906 | *ret = count * 3; |
| 907 | } |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 908 | |
| 909 | while (count--) { |
| 910 | pix = *buf32++; |
| 911 | *buf++ = (char)(pix >> rshift); |
| 912 | *buf++ = (char)(pix >> gshift); |
| 913 | *buf++ = (char)(pix >> bshift); |
| 914 | } |
| 915 | } |
| 916 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 917 | static int send_full_color_rect(VncState *vs, int x, int y, int w, int h) |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 918 | { |
| 919 | int stream = 0; |
Jes Sorensen | 2116eff | 2010-08-31 09:30:36 +0200 | [diff] [blame] | 920 | ssize_t bytes; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 921 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 922 | #ifdef CONFIG_VNC_PNG |
| 923 | if (tight_can_send_png_rect(vs, w, h)) { |
| 924 | return send_png_rect(vs, x, y, w, h, NULL); |
| 925 | } |
| 926 | #endif |
| 927 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 928 | vnc_write_u8(vs, stream << 4); /* no flushing, no filter */ |
| 929 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 930 | if (vs->tight.pixel24) { |
| 931 | tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 932 | bytes = 3; |
| 933 | } else { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 934 | bytes = vs->client_pf.bytes_per_pixel; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | bytes = tight_compress_data(vs, stream, w * h * bytes, |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 938 | tight_conf[vs->tight.compression].raw_zlib_level, |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 939 | Z_DEFAULT_STRATEGY); |
| 940 | |
| 941 | return (bytes >= 0); |
| 942 | } |
| 943 | |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 944 | static int send_solid_rect(VncState *vs) |
| 945 | { |
| 946 | size_t bytes; |
| 947 | |
| 948 | vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */ |
| 949 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 950 | if (vs->tight.pixel24) { |
| 951 | tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 952 | bytes = 3; |
| 953 | } else { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 954 | bytes = vs->client_pf.bytes_per_pixel; |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 957 | vnc_write(vs, vs->tight.tight.buffer, bytes); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 958 | return 1; |
| 959 | } |
| 960 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 961 | static int send_mono_rect(VncState *vs, int x, int y, |
| 962 | int w, int h, uint32_t bg, uint32_t fg) |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 963 | { |
Jes Sorensen | 2116eff | 2010-08-31 09:30:36 +0200 | [diff] [blame] | 964 | ssize_t bytes; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 965 | int stream = 1; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 966 | int level = tight_conf[vs->tight.compression].mono_zlib_level; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 967 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 968 | #ifdef CONFIG_VNC_PNG |
| 969 | if (tight_can_send_png_rect(vs, w, h)) { |
| 970 | int ret; |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 971 | int bpp = vs->client_pf.bytes_per_pixel * 8; |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 972 | VncPalette *palette = palette_new(2, bpp); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 973 | |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 974 | palette_put(palette, bg); |
| 975 | palette_put(palette, fg); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 976 | ret = send_png_rect(vs, x, y, w, h, palette); |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 977 | palette_destroy(palette); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 978 | return ret; |
| 979 | } |
| 980 | #endif |
| 981 | |
Marc-André Lureau | 659c90e | 2017-06-22 13:04:16 +0200 | [diff] [blame] | 982 | bytes = (DIV_ROUND_UP(w, 8)) * h; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 983 | |
| 984 | vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); |
| 985 | vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); |
| 986 | vnc_write_u8(vs, 1); |
| 987 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 988 | switch (vs->client_pf.bytes_per_pixel) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 989 | case 4: |
| 990 | { |
| 991 | uint32_t buf[2] = {bg, fg}; |
| 992 | size_t ret = sizeof (buf); |
| 993 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 994 | if (vs->tight.pixel24) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 995 | tight_pack24(vs, (unsigned char*)buf, 2, &ret); |
| 996 | } |
| 997 | vnc_write(vs, buf, ret); |
| 998 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 999 | tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1000 | break; |
| 1001 | } |
| 1002 | case 2: |
| 1003 | vnc_write(vs, &bg, 2); |
| 1004 | vnc_write(vs, &fg, 2); |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1005 | tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1006 | break; |
| 1007 | default: |
| 1008 | vnc_write_u8(vs, bg); |
| 1009 | vnc_write_u8(vs, fg); |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1010 | tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1011 | break; |
| 1012 | } |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1013 | vs->tight.tight.offset = bytes; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1014 | |
| 1015 | bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY); |
| 1016 | return (bytes >= 0); |
| 1017 | } |
| 1018 | |
| 1019 | struct palette_cb_priv { |
| 1020 | VncState *vs; |
| 1021 | uint8_t *header; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1022 | #ifdef CONFIG_VNC_PNG |
| 1023 | png_colorp png_palette; |
| 1024 | #endif |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1025 | }; |
| 1026 | |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1027 | static void write_palette(int idx, uint32_t color, void *opaque) |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1028 | { |
| 1029 | struct palette_cb_priv *priv = opaque; |
| 1030 | VncState *vs = priv->vs; |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1031 | uint32_t bytes = vs->client_pf.bytes_per_pixel; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1032 | |
| 1033 | if (bytes == 4) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1034 | ((uint32_t*)priv->header)[idx] = color; |
| 1035 | } else { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1036 | ((uint16_t*)priv->header)[idx] = color; |
| 1037 | } |
| 1038 | } |
| 1039 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1040 | static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h) |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1041 | { |
| 1042 | int stream = 3; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1043 | int level = tight_conf[vs->tight.compression].gradient_zlib_level; |
Jes Sorensen | 2116eff | 2010-08-31 09:30:36 +0200 | [diff] [blame] | 1044 | ssize_t bytes; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1045 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1046 | if (vs->client_pf.bytes_per_pixel == 1) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1047 | return send_full_color_rect(vs, x, y, w, h); |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1048 | } |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1049 | |
| 1050 | vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); |
| 1051 | vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT); |
| 1052 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1053 | buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int)); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1054 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1055 | if (vs->tight.pixel24) { |
| 1056 | tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1057 | bytes = 3; |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1058 | } else if (vs->client_pf.bytes_per_pixel == 4) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1059 | tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1060 | bytes = 4; |
| 1061 | } else { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1062 | tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1063 | bytes = 2; |
| 1064 | } |
| 1065 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1066 | buffer_reset(&vs->tight.gradient); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1067 | |
| 1068 | bytes = w * h * bytes; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1069 | vs->tight.tight.offset = bytes; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1070 | |
| 1071 | bytes = tight_compress_data(vs, stream, bytes, |
| 1072 | level, Z_FILTERED); |
| 1073 | return (bytes >= 0); |
| 1074 | } |
| 1075 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1076 | static int send_palette_rect(VncState *vs, int x, int y, |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1077 | int w, int h, VncPalette *palette) |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1078 | { |
| 1079 | int stream = 2; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1080 | int level = tight_conf[vs->tight.compression].idx_zlib_level; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1081 | int colors; |
Jes Sorensen | 2116eff | 2010-08-31 09:30:36 +0200 | [diff] [blame] | 1082 | ssize_t bytes; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1083 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1084 | #ifdef CONFIG_VNC_PNG |
| 1085 | if (tight_can_send_png_rect(vs, w, h)) { |
| 1086 | return send_png_rect(vs, x, y, w, h, palette); |
| 1087 | } |
| 1088 | #endif |
| 1089 | |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1090 | colors = palette_size(palette); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1091 | |
| 1092 | vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); |
| 1093 | vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); |
| 1094 | vnc_write_u8(vs, colors - 1); |
| 1095 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1096 | switch (vs->client_pf.bytes_per_pixel) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1097 | case 4: |
| 1098 | { |
| 1099 | size_t old_offset, offset; |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1100 | uint32_t header[palette_size(palette)]; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1101 | struct palette_cb_priv priv = { vs, (uint8_t *)header }; |
| 1102 | |
| 1103 | old_offset = vs->output.offset; |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1104 | palette_iter(palette, write_palette, &priv); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1105 | vnc_write(vs, header, sizeof(header)); |
| 1106 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1107 | if (vs->tight.pixel24) { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1108 | tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset); |
| 1109 | vs->output.offset = old_offset + offset; |
| 1110 | } |
| 1111 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1112 | tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | case 2: |
| 1116 | { |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1117 | uint16_t header[palette_size(palette)]; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1118 | struct palette_cb_priv priv = { vs, (uint8_t *)header }; |
| 1119 | |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1120 | palette_iter(palette, write_palette, &priv); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1121 | vnc_write(vs, header, sizeof(header)); |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1122 | tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | default: |
| 1126 | return -1; /* No palette for 8bits colors */ |
| 1127 | break; |
| 1128 | } |
| 1129 | bytes = w * h; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1130 | vs->tight.tight.offset = bytes; |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1131 | |
| 1132 | bytes = tight_compress_data(vs, stream, bytes, |
| 1133 | level, Z_DEFAULT_STRATEGY); |
| 1134 | return (bytes >= 0); |
| 1135 | } |
| 1136 | |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1137 | /* |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1138 | * JPEG compression stuff. |
| 1139 | */ |
| 1140 | #ifdef CONFIG_VNC_JPEG |
| 1141 | /* |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1142 | * Destination manager implementation for JPEG library. |
| 1143 | */ |
| 1144 | |
| 1145 | /* This is called once per encoding */ |
| 1146 | static void jpeg_init_destination(j_compress_ptr cinfo) |
| 1147 | { |
| 1148 | VncState *vs = cinfo->client_data; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1149 | Buffer *buffer = &vs->tight.jpeg; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1150 | |
| 1151 | cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset; |
| 1152 | cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset); |
| 1153 | } |
| 1154 | |
| 1155 | /* This is called when we ran out of buffer (shouldn't happen!) */ |
| 1156 | static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo) |
| 1157 | { |
| 1158 | VncState *vs = cinfo->client_data; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1159 | Buffer *buffer = &vs->tight.jpeg; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1160 | |
| 1161 | buffer->offset = buffer->capacity; |
| 1162 | buffer_reserve(buffer, 2048); |
| 1163 | jpeg_init_destination(cinfo); |
| 1164 | return TRUE; |
| 1165 | } |
| 1166 | |
| 1167 | /* This is called when we are done processing data */ |
| 1168 | static void jpeg_term_destination(j_compress_ptr cinfo) |
| 1169 | { |
| 1170 | VncState *vs = cinfo->client_data; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1171 | Buffer *buffer = &vs->tight.jpeg; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1172 | |
| 1173 | buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer; |
| 1174 | } |
| 1175 | |
| 1176 | static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality) |
| 1177 | { |
| 1178 | struct jpeg_compress_struct cinfo; |
| 1179 | struct jpeg_error_mgr jerr; |
| 1180 | struct jpeg_destination_mgr manager; |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1181 | pixman_image_t *linebuf; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1182 | JSAMPROW row[1]; |
| 1183 | uint8_t *buf; |
| 1184 | int dy; |
| 1185 | |
Gerd Hoffmann | d39fa6d | 2013-02-28 17:16:48 +0100 | [diff] [blame] | 1186 | if (surface_bytes_per_pixel(vs->vd->ds) == 1) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1187 | return send_full_color_rect(vs, x, y, w, h); |
Gerd Hoffmann | d39fa6d | 2013-02-28 17:16:48 +0100 | [diff] [blame] | 1188 | } |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1189 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1190 | buffer_reserve(&vs->tight.jpeg, 2048); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1191 | |
| 1192 | cinfo.err = jpeg_std_error(&jerr); |
| 1193 | jpeg_create_compress(&cinfo); |
| 1194 | |
| 1195 | cinfo.client_data = vs; |
| 1196 | cinfo.image_width = w; |
| 1197 | cinfo.image_height = h; |
| 1198 | cinfo.input_components = 3; |
| 1199 | cinfo.in_color_space = JCS_RGB; |
| 1200 | |
| 1201 | jpeg_set_defaults(&cinfo); |
| 1202 | jpeg_set_quality(&cinfo, quality, true); |
| 1203 | |
| 1204 | manager.init_destination = jpeg_init_destination; |
| 1205 | manager.empty_output_buffer = jpeg_empty_output_buffer; |
| 1206 | manager.term_destination = jpeg_term_destination; |
| 1207 | cinfo.dest = &manager; |
| 1208 | |
| 1209 | jpeg_start_compress(&cinfo, true); |
| 1210 | |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1211 | linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, w); |
| 1212 | buf = (uint8_t *)pixman_image_get_data(linebuf); |
Corentin Chary | d9c18c2 | 2010-07-07 20:57:55 +0200 | [diff] [blame] | 1213 | row[0] = buf; |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1214 | for (dy = 0; dy < h; dy++) { |
Gerd Hoffmann | bc210eb | 2012-12-14 07:54:24 +0000 | [diff] [blame] | 1215 | qemu_pixman_linebuf_fill(linebuf, vs->vd->server, w, x, y + dy); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1216 | jpeg_write_scanlines(&cinfo, row, 1); |
| 1217 | } |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1218 | qemu_pixman_image_unref(linebuf); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1219 | |
| 1220 | jpeg_finish_compress(&cinfo); |
| 1221 | jpeg_destroy_compress(&cinfo); |
| 1222 | |
| 1223 | vnc_write_u8(vs, VNC_TIGHT_JPEG << 4); |
| 1224 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1225 | tight_send_compact_size(vs, vs->tight.jpeg.offset); |
| 1226 | vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset); |
| 1227 | buffer_reset(&vs->tight.jpeg); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1228 | |
| 1229 | return 1; |
| 1230 | } |
| 1231 | #endif /* CONFIG_VNC_JPEG */ |
| 1232 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1233 | /* |
| 1234 | * PNG compression stuff. |
| 1235 | */ |
| 1236 | #ifdef CONFIG_VNC_PNG |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1237 | static void write_png_palette(int idx, uint32_t pix, void *opaque) |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1238 | { |
| 1239 | struct palette_cb_priv *priv = opaque; |
| 1240 | VncState *vs = priv->vs; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1241 | png_colorp color = &priv->png_palette[idx]; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1242 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1243 | if (vs->tight.pixel24) |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1244 | { |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1245 | color->red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax; |
| 1246 | color->green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax; |
| 1247 | color->blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1248 | } |
| 1249 | else |
| 1250 | { |
| 1251 | int red, green, blue; |
| 1252 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1253 | red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax; |
| 1254 | green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax; |
| 1255 | blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax; |
| 1256 | color->red = ((red * 255 + vs->client_pf.rmax / 2) / |
| 1257 | vs->client_pf.rmax); |
| 1258 | color->green = ((green * 255 + vs->client_pf.gmax / 2) / |
| 1259 | vs->client_pf.gmax); |
| 1260 | color->blue = ((blue * 255 + vs->client_pf.bmax / 2) / |
| 1261 | vs->client_pf.bmax); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | static void png_write_data(png_structp png_ptr, png_bytep data, |
| 1266 | png_size_t length) |
| 1267 | { |
| 1268 | VncState *vs = png_get_io_ptr(png_ptr); |
| 1269 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1270 | buffer_reserve(&vs->tight.png, vs->tight.png.offset + length); |
| 1271 | memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1272 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1273 | vs->tight.png.offset += length; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | static void png_flush_data(png_structp png_ptr) |
| 1277 | { |
| 1278 | } |
| 1279 | |
| 1280 | static void *vnc_png_malloc(png_structp png_ptr, png_size_t size) |
| 1281 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1282 | return g_malloc(size); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static void vnc_png_free(png_structp png_ptr, png_voidp ptr) |
| 1286 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1287 | g_free(ptr); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static int send_png_rect(VncState *vs, int x, int y, int w, int h, |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1291 | VncPalette *palette) |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1292 | { |
| 1293 | png_byte color_type; |
| 1294 | png_structp png_ptr; |
| 1295 | png_infop info_ptr; |
| 1296 | png_colorp png_palette = NULL; |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1297 | pixman_image_t *linebuf; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1298 | int level = tight_png_conf[vs->tight.compression].png_zlib_level; |
| 1299 | int filters = tight_png_conf[vs->tight.compression].png_filters; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1300 | uint8_t *buf; |
| 1301 | int dy; |
| 1302 | |
| 1303 | png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, |
| 1304 | NULL, vnc_png_malloc, vnc_png_free); |
| 1305 | |
| 1306 | if (png_ptr == NULL) |
| 1307 | return -1; |
| 1308 | |
| 1309 | info_ptr = png_create_info_struct(png_ptr); |
| 1310 | |
| 1311 | if (info_ptr == NULL) { |
| 1312 | png_destroy_write_struct(&png_ptr, NULL); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | |
| 1316 | png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data); |
| 1317 | png_set_compression_level(png_ptr, level); |
Corentin Chary | 3941bf6 | 2010-07-07 20:57:57 +0200 | [diff] [blame] | 1318 | png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (palette) { |
| 1321 | color_type = PNG_COLOR_TYPE_PALETTE; |
| 1322 | } else { |
| 1323 | color_type = PNG_COLOR_TYPE_RGB; |
| 1324 | } |
| 1325 | |
| 1326 | png_set_IHDR(png_ptr, info_ptr, w, h, |
| 1327 | 8, color_type, PNG_INTERLACE_NONE, |
| 1328 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 1329 | |
| 1330 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 1331 | struct palette_cb_priv priv; |
| 1332 | |
| 1333 | png_palette = png_malloc(png_ptr, sizeof(*png_palette) * |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1334 | palette_size(palette)); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1335 | |
| 1336 | priv.vs = vs; |
| 1337 | priv.png_palette = png_palette; |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1338 | palette_iter(palette, write_png_palette, &priv); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1339 | |
Corentin Chary | 5136a05 | 2010-07-07 20:57:58 +0200 | [diff] [blame] | 1340 | png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette)); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1341 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1342 | if (vs->client_pf.bytes_per_pixel == 4) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1343 | tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1344 | } else { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1345 | tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | png_write_info(png_ptr, info_ptr); |
| 1350 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1351 | buffer_reserve(&vs->tight.png, 2048); |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1352 | linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, w); |
| 1353 | buf = (uint8_t *)pixman_image_get_data(linebuf); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1354 | for (dy = 0; dy < h; dy++) |
| 1355 | { |
| 1356 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1357 | memcpy(buf, vs->tight.tight.buffer + (dy * w), w); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1358 | } else { |
Gerd Hoffmann | bc210eb | 2012-12-14 07:54:24 +0000 | [diff] [blame] | 1359 | qemu_pixman_linebuf_fill(linebuf, vs->vd->server, w, x, y + dy); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1360 | } |
| 1361 | png_write_row(png_ptr, buf); |
| 1362 | } |
Gerd Hoffmann | 47683d6 | 2012-10-11 12:04:33 +0200 | [diff] [blame] | 1363 | qemu_pixman_image_unref(linebuf); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1364 | |
| 1365 | png_write_end(png_ptr, NULL); |
| 1366 | |
| 1367 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 1368 | png_free(png_ptr, png_palette); |
| 1369 | } |
| 1370 | |
| 1371 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 1372 | |
| 1373 | vnc_write_u8(vs, VNC_TIGHT_PNG << 4); |
| 1374 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1375 | tight_send_compact_size(vs, vs->tight.png.offset); |
| 1376 | vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset); |
| 1377 | buffer_reset(&vs->tight.png); |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1378 | return 1; |
| 1379 | } |
| 1380 | #endif /* CONFIG_VNC_PNG */ |
| 1381 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1382 | static void vnc_tight_start(VncState *vs) |
| 1383 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1384 | buffer_reset(&vs->tight.tight); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1385 | |
| 1386 | // make the output buffer be the zlib buffer, so we can compress it later |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1387 | vs->tight.tmp = vs->output; |
| 1388 | vs->output = vs->tight.tight; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | static void vnc_tight_stop(VncState *vs) |
| 1392 | { |
| 1393 | // switch back to normal output/zlib buffers |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1394 | vs->tight.tight = vs->output; |
| 1395 | vs->output = vs->tight.tmp; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1398 | static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h, |
| 1399 | int bg, int fg, int colors, VncPalette *palette) |
| 1400 | { |
| 1401 | int ret; |
| 1402 | |
| 1403 | if (colors == 0) { |
| 1404 | if (tight_detect_smooth_image(vs, w, h)) { |
| 1405 | ret = send_gradient_rect(vs, x, y, w, h); |
| 1406 | } else { |
| 1407 | ret = send_full_color_rect(vs, x, y, w, h); |
| 1408 | } |
| 1409 | } else if (colors == 1) { |
| 1410 | ret = send_solid_rect(vs); |
| 1411 | } else if (colors == 2) { |
| 1412 | ret = send_mono_rect(vs, x, y, w, h, bg, fg); |
| 1413 | } else if (colors <= 256) { |
| 1414 | ret = send_palette_rect(vs, x, y, w, h, palette); |
Blue Swirl | d167f9b | 2010-07-27 15:34:10 +0000 | [diff] [blame] | 1415 | } else { |
| 1416 | ret = 0; |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1417 | } |
| 1418 | return ret; |
| 1419 | } |
| 1420 | |
| 1421 | #ifdef CONFIG_VNC_JPEG |
| 1422 | static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h, |
| 1423 | int bg, int fg, int colors, |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1424 | VncPalette *palette, bool force) |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1425 | { |
| 1426 | int ret; |
| 1427 | |
| 1428 | if (colors == 0) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1429 | if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full && |
| 1430 | tight_detect_smooth_image(vs, w, h))) { |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1431 | int quality = tight_conf[vs->tight.quality].jpeg_quality; |
| 1432 | |
| 1433 | ret = send_jpeg_rect(vs, x, y, w, h, quality); |
| 1434 | } else { |
| 1435 | ret = send_full_color_rect(vs, x, y, w, h); |
| 1436 | } |
| 1437 | } else if (colors == 1) { |
| 1438 | ret = send_solid_rect(vs); |
| 1439 | } else if (colors == 2) { |
| 1440 | ret = send_mono_rect(vs, x, y, w, h, bg, fg); |
| 1441 | } else if (colors <= 256) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1442 | if (force || (colors > 96 && |
| 1443 | tight_jpeg_conf[vs->tight.quality].jpeg_idx && |
| 1444 | tight_detect_smooth_image(vs, w, h))) { |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1445 | int quality = tight_conf[vs->tight.quality].jpeg_quality; |
| 1446 | |
| 1447 | ret = send_jpeg_rect(vs, x, y, w, h, quality); |
| 1448 | } else { |
| 1449 | ret = send_palette_rect(vs, x, y, w, h, palette); |
| 1450 | } |
Blue Swirl | ad7ee4a | 2010-07-31 19:43:37 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | ret = 0; |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1453 | } |
| 1454 | return ret; |
| 1455 | } |
| 1456 | #endif |
| 1457 | |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1458 | static __thread VncPalette *color_count_palette; |
| 1459 | static __thread Notifier vnc_tight_cleanup_notifier; |
| 1460 | |
| 1461 | static void vnc_tight_cleanup(Notifier *n, void *value) |
| 1462 | { |
| 1463 | g_free(color_count_palette); |
| 1464 | color_count_palette = NULL; |
| 1465 | } |
Peter Lieven | 095497f | 2016-06-30 12:00:46 +0200 | [diff] [blame] | 1466 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1467 | static int send_sub_rect(VncState *vs, int x, int y, int w, int h) |
| 1468 | { |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1469 | uint32_t bg = 0, fg = 0; |
| 1470 | int colors; |
| 1471 | int ret = 0; |
Peter Maydell | cf76a1c | 2011-02-24 16:04:22 +0000 | [diff] [blame] | 1472 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1473 | bool force_jpeg = false; |
| 1474 | bool allow_jpeg = true; |
Peter Maydell | cf76a1c | 2011-02-24 16:04:22 +0000 | [diff] [blame] | 1475 | #endif |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1476 | |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1477 | if (!color_count_palette) { |
| 1478 | color_count_palette = g_malloc(sizeof(VncPalette)); |
| 1479 | vnc_tight_cleanup_notifier.notify = vnc_tight_cleanup; |
| 1480 | qemu_thread_atexit_add(&vnc_tight_cleanup_notifier); |
| 1481 | } |
| 1482 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1483 | vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1484 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1485 | vnc_tight_start(vs); |
| 1486 | vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
| 1487 | vnc_tight_stop(vs); |
| 1488 | |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1489 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | 80e0c8c | 2011-02-04 09:06:08 +0100 | [diff] [blame] | 1490 | if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1491 | double freq = vnc_update_freq(vs, x, y, w, h); |
| 1492 | |
| 1493 | if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) { |
| 1494 | allow_jpeg = false; |
| 1495 | } |
| 1496 | if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) { |
| 1497 | force_jpeg = true; |
| 1498 | vnc_sent_lossy_rect(vs, x, y, w, h); |
| 1499 | } |
| 1500 | } |
| 1501 | #endif |
| 1502 | |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1503 | colors = tight_fill_palette(vs, x, y, w * h, &bg, &fg, color_count_palette); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1504 | |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1505 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1506 | if (allow_jpeg && vs->tight.quality != (uint8_t)-1) { |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1507 | ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, |
| 1508 | color_count_palette, force_jpeg); |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1509 | } else { |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1510 | ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, |
| 1511 | color_count_palette); |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1512 | } |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1513 | #else |
Peter Lieven | 66668d1 | 2016-07-15 11:45:11 +0200 | [diff] [blame] | 1514 | ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, |
| 1515 | color_count_palette); |
Corentin Chary | 03817eb | 2010-07-07 20:58:05 +0200 | [diff] [blame] | 1516 | #endif |
| 1517 | |
Corentin Chary | aa7d73f | 2010-05-19 09:24:12 +0200 | [diff] [blame] | 1518 | return ret; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1521 | static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h) |
| 1522 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1523 | vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1524 | |
| 1525 | vnc_tight_start(vs); |
| 1526 | vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
| 1527 | vnc_tight_stop(vs); |
| 1528 | |
| 1529 | return send_solid_rect(vs); |
| 1530 | } |
| 1531 | |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1532 | static int send_rect_simple(VncState *vs, int x, int y, int w, int h, |
| 1533 | bool split) |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1534 | { |
| 1535 | int max_size, max_width; |
| 1536 | int max_sub_width, max_sub_height; |
| 1537 | int dx, dy; |
| 1538 | int rw, rh; |
| 1539 | int n = 0; |
| 1540 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1541 | max_size = tight_conf[vs->tight.compression].max_rect_size; |
| 1542 | max_width = tight_conf[vs->tight.compression].max_rect_width; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1543 | |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1544 | if (split && (w > max_width || w * h > max_size)) { |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1545 | max_sub_width = (w > max_width) ? max_width : w; |
| 1546 | max_sub_height = max_size / max_sub_width; |
| 1547 | |
| 1548 | for (dy = 0; dy < h; dy += max_sub_height) { |
| 1549 | for (dx = 0; dx < w; dx += max_width) { |
| 1550 | rw = MIN(max_sub_width, w - dx); |
| 1551 | rh = MIN(max_sub_height, h - dy); |
| 1552 | n += send_sub_rect(vs, x+dx, y+dy, rw, rh); |
| 1553 | } |
| 1554 | } |
| 1555 | } else { |
| 1556 | n += send_sub_rect(vs, x, y, w, h); |
| 1557 | } |
| 1558 | |
| 1559 | return n; |
| 1560 | } |
| 1561 | |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1562 | static int find_large_solid_color_rect(VncState *vs, int x, int y, |
| 1563 | int w, int h, int max_rows) |
| 1564 | { |
| 1565 | int dx, dy, dw, dh; |
| 1566 | int n = 0; |
| 1567 | |
| 1568 | /* Try to find large solid-color areas and send them separately. */ |
| 1569 | |
| 1570 | for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 1571 | |
| 1572 | /* If a rectangle becomes too large, send its upper part now. */ |
| 1573 | |
| 1574 | if (dy - y >= max_rows) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1575 | n += send_rect_simple(vs, x, y, w, max_rows, true); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1576 | y += max_rows; |
| 1577 | h -= max_rows; |
| 1578 | } |
| 1579 | |
| 1580 | dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy)); |
| 1581 | |
| 1582 | for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 1583 | uint32_t color_value; |
| 1584 | int x_best, y_best, w_best, h_best; |
| 1585 | |
| 1586 | dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx)); |
| 1587 | |
| 1588 | if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) { |
| 1589 | continue ; |
| 1590 | } |
| 1591 | |
| 1592 | /* Get dimensions of solid-color area. */ |
| 1593 | |
| 1594 | find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y), |
| 1595 | color_value, &w_best, &h_best); |
| 1596 | |
| 1597 | /* Make sure a solid rectangle is large enough |
| 1598 | (or the whole rectangle is of the same color). */ |
| 1599 | |
| 1600 | if (w_best * h_best != w * h && |
| 1601 | w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) { |
| 1602 | continue; |
| 1603 | } |
| 1604 | |
| 1605 | /* Try to extend solid rectangle to maximum size. */ |
| 1606 | |
| 1607 | x_best = dx; y_best = dy; |
| 1608 | extend_solid_area(vs, x, y, w, h, color_value, |
| 1609 | &x_best, &y_best, &w_best, &h_best); |
| 1610 | |
| 1611 | /* Send rectangles at top and left to solid-color area. */ |
| 1612 | |
| 1613 | if (y_best != y) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1614 | n += send_rect_simple(vs, x, y, w, y_best-y, true); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1615 | } |
| 1616 | if (x_best != x) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1617 | n += tight_send_framebuffer_update(vs, x, y_best, |
| 1618 | x_best-x, h_best); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /* Send solid-color rectangle. */ |
| 1622 | n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best); |
| 1623 | |
| 1624 | /* Send remaining rectangles (at right and bottom). */ |
| 1625 | |
| 1626 | if (x_best + w_best != x + w) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1627 | n += tight_send_framebuffer_update(vs, x_best+w_best, |
| 1628 | y_best, |
| 1629 | w-(x_best-x)-w_best, |
| 1630 | h_best); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1631 | } |
| 1632 | if (y_best + h_best != y + h) { |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1633 | n += tight_send_framebuffer_update(vs, x, y_best+h_best, |
| 1634 | w, h-(y_best-y)-h_best); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* Return after all recursive calls are done. */ |
| 1638 | return n; |
| 1639 | } |
| 1640 | } |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1641 | return n + send_rect_simple(vs, x, y, w, h, true); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1644 | static int tight_send_framebuffer_update(VncState *vs, int x, int y, |
| 1645 | int w, int h) |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1646 | { |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1647 | int max_rows; |
| 1648 | |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 1649 | if (vs->client_pf.bytes_per_pixel == 4 && vs->client_pf.rmax == 0xFF && |
| 1650 | vs->client_pf.bmax == 0xFF && vs->client_pf.gmax == 0xFF) { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1651 | vs->tight.pixel24 = true; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1652 | } else { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1653 | vs->tight.pixel24 = false; |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Peter Maydell | cf76a1c | 2011-02-24 16:04:22 +0000 | [diff] [blame] | 1656 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | 368d258 | 2011-02-04 09:06:02 +0100 | [diff] [blame] | 1657 | if (vs->tight.quality != (uint8_t)-1) { |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1658 | double freq = vnc_update_freq(vs, x, y, w, h); |
| 1659 | |
| 1660 | if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) { |
| 1661 | return send_rect_simple(vs, x, y, w, h, false); |
| 1662 | } |
| 1663 | } |
Peter Maydell | cf76a1c | 2011-02-24 16:04:22 +0000 | [diff] [blame] | 1664 | #endif |
Corentin Chary | ce702e9 | 2011-02-04 09:05:57 +0100 | [diff] [blame] | 1665 | |
| 1666 | if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) { |
| 1667 | return send_rect_simple(vs, x, y, w, h, true); |
| 1668 | } |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1669 | |
| 1670 | /* Calculate maximum number of rows in one non-solid rectangle. */ |
| 1671 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1672 | max_rows = tight_conf[vs->tight.compression].max_rect_size; |
| 1673 | max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w); |
Corentin Chary | b4bea3f | 2010-05-19 09:24:11 +0200 | [diff] [blame] | 1674 | |
| 1675 | return find_large_solid_color_rect(vs, x, y, w, h, max_rows); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1678 | int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, |
| 1679 | int w, int h) |
| 1680 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1681 | vs->tight.type = VNC_ENCODING_TIGHT; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1682 | return tight_send_framebuffer_update(vs, x, y, w, h); |
| 1683 | } |
| 1684 | |
| 1685 | int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y, |
| 1686 | int w, int h) |
| 1687 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1688 | vs->tight.type = VNC_ENCODING_TIGHT_PNG; |
Corentin Chary | efe556a | 2010-07-07 20:57:56 +0200 | [diff] [blame] | 1689 | return tight_send_framebuffer_update(vs, x, y, w, h); |
| 1690 | } |
| 1691 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1692 | void vnc_tight_clear(VncState *vs) |
| 1693 | { |
| 1694 | int i; |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1695 | for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) { |
| 1696 | if (vs->tight.stream[i].opaque) { |
| 1697 | deflateEnd(&vs->tight.stream[i]); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1701 | buffer_free(&vs->tight.tight); |
| 1702 | buffer_free(&vs->tight.zlib); |
| 1703 | buffer_free(&vs->tight.gradient); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1704 | #ifdef CONFIG_VNC_JPEG |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 1705 | buffer_free(&vs->tight.jpeg); |
Corentin Chary | 2f6f5c7 | 2010-07-07 20:57:49 +0200 | [diff] [blame] | 1706 | #endif |
Corentin Chary | b5469b1 | 2010-07-07 20:58:00 +0200 | [diff] [blame] | 1707 | #ifdef CONFIG_VNC_PNG |
| 1708 | buffer_free(&vs->tight.png); |
| 1709 | #endif |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 1710 | } |