Coverage Report

Created: 2026-11-20 08:13

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