Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 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 "vnc.h" |
| 28 | |
| 29 | #define ZALLOC_ALIGNMENT 16 |
| 30 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 31 | void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size) |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 32 | { |
| 33 | void *p; |
| 34 | |
| 35 | size *= items; |
| 36 | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); |
| 37 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 38 | p = g_malloc0(size); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 39 | |
| 40 | return (p); |
| 41 | } |
| 42 | |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 43 | void vnc_zlib_zfree(void *x, void *addr) |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 44 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 45 | g_free(addr); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void vnc_zlib_start(VncState *vs) |
| 49 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 50 | buffer_reset(&vs->zlib.zlib); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 51 | |
| 52 | // make the output buffer be the zlib buffer, so we can compress it later |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 53 | vs->zlib.tmp = vs->output; |
| 54 | vs->output = vs->zlib.zlib; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Corentin Chary | b05ad29 | 2010-05-19 09:24:04 +0200 | [diff] [blame] | 57 | static int vnc_zlib_stop(VncState *vs) |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 58 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 59 | z_streamp zstream = &vs->zlib.stream; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 60 | int previous_out; |
| 61 | |
| 62 | // switch back to normal output/zlib buffers |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 63 | vs->zlib.zlib = vs->output; |
| 64 | vs->output = vs->zlib.tmp; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 65 | |
| 66 | // compress the zlib buffer |
| 67 | |
| 68 | // initialize the stream |
| 69 | // XXX need one stream per session |
| 70 | if (zstream->opaque != vs) { |
| 71 | int err; |
| 72 | |
Corentin Chary | b05ad29 | 2010-05-19 09:24:04 +0200 | [diff] [blame] | 73 | VNC_DEBUG("VNC: initializing zlib stream\n"); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 74 | VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs); |
Corentin Chary | 380282b | 2010-05-19 09:24:10 +0200 | [diff] [blame] | 75 | zstream->zalloc = vnc_zlib_zalloc; |
| 76 | zstream->zfree = vnc_zlib_zfree; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 77 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 78 | err = deflateInit2(zstream, vs->tight.compression, Z_DEFLATED, MAX_WBITS, |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 79 | MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 80 | |
| 81 | if (err != Z_OK) { |
| 82 | fprintf(stderr, "VNC: error initializing zlib\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 86 | vs->zlib.level = vs->tight.compression; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 87 | zstream->opaque = vs; |
| 88 | } |
| 89 | |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 90 | if (vs->tight.compression != vs->zlib.level) { |
| 91 | if (deflateParams(zstream, vs->tight.compression, |
Corentin Chary | 9f643ec | 2010-05-19 09:24:05 +0200 | [diff] [blame] | 92 | Z_DEFAULT_STRATEGY) != Z_OK) { |
| 93 | return -1; |
| 94 | } |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 95 | vs->zlib.level = vs->tight.compression; |
Corentin Chary | 9f643ec | 2010-05-19 09:24:05 +0200 | [diff] [blame] | 96 | } |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 97 | |
| 98 | // reserve memory in output buffer |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 99 | buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 100 | |
| 101 | // set pointers |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 102 | zstream->next_in = vs->zlib.zlib.buffer; |
| 103 | zstream->avail_in = vs->zlib.zlib.offset; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 104 | zstream->next_out = vs->output.buffer + vs->output.offset; |
| 105 | zstream->avail_out = vs->output.capacity - vs->output.offset; |
Michael Tokarev | 2caa9e9 | 2011-03-21 09:34:35 +0100 | [diff] [blame] | 106 | previous_out = zstream->avail_out; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 107 | zstream->data_type = Z_BINARY; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 108 | |
| 109 | // start encoding |
| 110 | if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) { |
| 111 | fprintf(stderr, "VNC: error during zlib compression\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | vs->output.offset = vs->output.capacity - zstream->avail_out; |
Michael Tokarev | 2caa9e9 | 2011-03-21 09:34:35 +0100 | [diff] [blame] | 116 | return previous_out - zstream->avail_out; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Corentin Chary | a885211 | 2010-05-19 09:24:09 +0200 | [diff] [blame] | 119 | int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 120 | { |
| 121 | int old_offset, new_offset, bytes_written; |
| 122 | |
| 123 | vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB); |
| 124 | |
| 125 | // remember where we put in the follow-up size |
| 126 | old_offset = vs->output.offset; |
| 127 | vnc_write_s32(vs, 0); |
| 128 | |
| 129 | // compress the stream |
| 130 | vnc_zlib_start(vs); |
| 131 | vnc_raw_send_framebuffer_update(vs, x, y, w, h); |
Corentin Chary | b05ad29 | 2010-05-19 09:24:04 +0200 | [diff] [blame] | 132 | bytes_written = vnc_zlib_stop(vs); |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 133 | |
| 134 | if (bytes_written == -1) |
Corentin Chary | a885211 | 2010-05-19 09:24:09 +0200 | [diff] [blame] | 135 | return 0; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 136 | |
| 137 | // hack in the size |
| 138 | new_offset = vs->output.offset; |
| 139 | vs->output.offset = old_offset; |
| 140 | vnc_write_u32(vs, bytes_written); |
| 141 | vs->output.offset = new_offset; |
Corentin Chary | a885211 | 2010-05-19 09:24:09 +0200 | [diff] [blame] | 142 | |
| 143 | return 1; |
Corentin Chary | 70a4568 | 2010-05-03 14:31:34 +0200 | [diff] [blame] | 144 | } |
Corentin Chary | 161c4f2 | 2010-05-19 09:24:08 +0200 | [diff] [blame] | 145 | |
| 146 | void vnc_zlib_clear(VncState *vs) |
| 147 | { |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 148 | if (vs->zlib.stream.opaque) { |
| 149 | deflateEnd(&vs->zlib.stream); |
Corentin Chary | 161c4f2 | 2010-05-19 09:24:08 +0200 | [diff] [blame] | 150 | } |
Corentin Chary | d1af0e0 | 2010-07-07 20:57:59 +0200 | [diff] [blame] | 151 | buffer_free(&vs->zlib.zlib); |
Corentin Chary | 161c4f2 | 2010-05-19 09:24:08 +0200 | [diff] [blame] | 152 | } |