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