master
c 155 lines 4.61 KB
Raw
1 /*
2 * QEMU VNC display driver: zlib encoding
3 *
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include "qemu/osdep.h"
28 #include "vnc.h"
29 #include "trace.h"
30
31 #define ZALLOC_ALIGNMENT 16
32
33 void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
34 {
35 void *p;
36
37 size *= items;
38 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
39
40 p = g_malloc0(size);
41
42 return (p);
43 }
44
45 void vnc_zlib_zfree(void *x, void *addr)
46 {
47 g_free(addr);
48 }
49
50 static void vnc_zlib_start(VncState *vs, VncWorker *worker)
51 {
52 buffer_reset(&worker->zlib.zlib);
53
54 // make the output buffer be the zlib buffer, so we can compress it later
55 worker->zlib.tmp = vs->output;
56 vs->output = worker->zlib.zlib;
57 }
58
59 static int vnc_zlib_stop(VncState *vs, VncWorker *worker)
60 {
61 z_streamp zstream = &worker->zlib.stream;
62 int previous_out;
63
64 // switch back to normal output/zlib buffers
65 worker->zlib.zlib = vs->output;
66 vs->output = worker->zlib.tmp;
67
68 // compress the zlib buffer
69
70 // initialize the stream
71 // XXX need one stream per session
72 if (zstream->opaque != vs) {
73 int err;
74
75 trace_vnc_zlib_init(vs, zstream->opaque);
76 zstream->zalloc = vnc_zlib_zalloc;
77 zstream->zfree = vnc_zlib_zfree;
78
79 err = deflateInit2(zstream, worker->tight.compression, Z_DEFLATED,
80 MAX_WBITS,
81 MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
82
83 if (err != Z_OK) {
84 fprintf(stderr, "VNC: error initializing zlib\n");
85 return -1;
86 }
87
88 worker->zlib.level = worker->tight.compression;
89 zstream->opaque = vs;
90 }
91
92 if (worker->tight.compression != worker->zlib.level) {
93 if (deflateParams(zstream, worker->tight.compression,
94 Z_DEFAULT_STRATEGY) != Z_OK) {
95 return -1;
96 }
97 worker->zlib.level = worker->tight.compression;
98 }
99
100 // reserve memory in output buffer
101 buffer_reserve(&vs->output, worker->zlib.zlib.offset + 64);
102
103 // set pointers
104 zstream->next_in = worker->zlib.zlib.buffer;
105 zstream->avail_in = worker->zlib.zlib.offset;
106 zstream->next_out = vs->output.buffer + vs->output.offset;
107 zstream->avail_out = vs->output.capacity - vs->output.offset;
108 previous_out = zstream->avail_out;
109 zstream->data_type = Z_BINARY;
110
111 // start encoding
112 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
113 fprintf(stderr, "VNC: error during zlib compression\n");
114 return -1;
115 }
116
117 vs->output.offset = vs->output.capacity - zstream->avail_out;
118 return previous_out - zstream->avail_out;
119 }
120
121 int vnc_zlib_send_framebuffer_update(VncState *vs, VncWorker *worker,
122 int x, int y, int w, int h)
123 {
124 int old_offset, new_offset, bytes_written;
125
126 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
127
128 // remember where we put in the follow-up size
129 old_offset = vs->output.offset;
130 vnc_write_s32(vs, 0);
131
132 // compress the stream
133 vnc_zlib_start(vs, worker);
134 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
135 bytes_written = vnc_zlib_stop(vs, worker);
136
137 if (bytes_written == -1)
138 return 0;
139
140 // hack in the size
141 new_offset = vs->output.offset;
142 vs->output.offset = old_offset;
143 vnc_write_u32(vs, bytes_written);
144 vs->output.offset = new_offset;
145
146 return 1;
147 }
148
149 void vnc_zlib_clear(VncWorker *worker)
150 {
151 if (worker->zlib.stream.opaque) {
152 deflateEnd(&worker->zlib.stream);
153 }
154 buffer_free(&worker->zlib.zlib);
155 }