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