ia64/xen-unstable

view tools/ioemu/patches/vnc-fixes @ 10804:060025203f54

[qemu] Re-calculate color_table after color depth reset.
VNC client may reset color depth after connection, so if we don't
re-calculate color_table, monitor/console's background is abnormal.

Signed-off-by: Yang Xiaowei <xiaowei.yang@intel.com>
Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author chris@kneesaa.uk.xensource.com
date Wed Jul 26 14:20:13 2006 +0100 (2006-07-26)
parents 42aa63188a88
children 4c2a281cd1e7
line source
1 Index: ioemu/vl.c
2 ===================================================================
3 --- ioemu.orig/vl.c 2006-07-26 13:39:18.439320475 +0100
4 +++ ioemu/vl.c 2006-07-26 14:13:44.488196651 +0100
5 @@ -6003,8 +6003,10 @@
6 kernel_filename, kernel_cmdline, initrd_filename,
7 timeoffset);
9 - gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
10 - qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
11 + if (vnc_display == -1) {
12 + gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
13 + qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
14 + }
16 #ifdef CONFIG_GDBSTUB
17 if (use_gdbstub) {
18 Index: ioemu/vnc.c
19 ===================================================================
20 --- ioemu.orig/vnc.c 2006-07-26 13:39:18.437320689 +0100
21 +++ ioemu/vnc.c 2006-07-26 14:16:51.636538318 +0100
22 @@ -3,6 +3,7 @@
23 *
24 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
25 * Copyright (C) 2006 Fabrice Bellard
26 + * Copyright (C) 2006 Christian Limpach <Christian.Limpach@xensource.com>
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a copy
29 * of this software and associated documentation files (the "Software"), to deal
30 @@ -51,7 +52,11 @@
31 int need_update;
32 int width;
33 int height;
34 - uint64_t dirty_row[768];
35 + uint64_t *dirty_row; /* screen regions which are possibly dirty */
36 + int dirty_pixel_shift;
37 + uint64_t *update_row; /* outstanding updates */
38 + int has_update; /* there's outstanding updates in the
39 + * visible area */
40 char *old_data;
41 int depth;
42 int has_resize;
43 @@ -62,13 +67,25 @@
45 VncReadEvent *read_handler;
46 size_t read_handler_expect;
47 +
48 + int visible_x;
49 + int visible_y;
50 + int visible_w;
51 + int visible_h;
52 +
53 + int slow_client;
54 };
56 +#define DIRTY_PIXEL_BITS 64
57 +#define X2DP_DOWN(vs, x) ((x) >> (vs)->dirty_pixel_shift)
58 +#define X2DP_UP(vs, x) \
59 + (((x) + (1ULL << (vs)->dirty_pixel_shift) - 1) >> (vs)->dirty_pixel_shift)
60 +#define DP2X(vs, x) ((x) << (vs)->dirty_pixel_shift)
61 +
62 /* TODO
63 1) Get the queue working for IO.
64 2) there is some weirdness when using the -S option (the screen is grey
65 and not totally invalidated
66 - 3) resolutions > 1024
67 */
69 static void vnc_write(VncState *vs, const void *data, size_t len);
70 @@ -77,22 +94,38 @@
71 static void vnc_write_u16(VncState *vs, uint16_t value);
72 static void vnc_write_u8(VncState *vs, uint8_t value);
73 static void vnc_flush(VncState *vs);
74 +static void _vnc_update_client(void *opaque);
75 static void vnc_update_client(void *opaque);
76 static void vnc_client_read(void *opaque);
77 +static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h);
79 -static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
80 +static void set_bits_in_row(VncState *vs, uint64_t *row,
81 + int x, int y, int w, int h)
82 {
83 - VncState *vs = ds->opaque;
84 + int x1, x2;
85 uint64_t mask;
87 - h += y;
88 - if (w != 1024)
89 - mask = ((1ULL << (w / 16)) - 1) << (x / 16);
90 + if (w == 0)
91 + return;
92 +
93 + x1 = X2DP_DOWN(vs, x);
94 + x2 = X2DP_UP(vs, x + w);
95 +
96 + if (X2DP_UP(vs, w) != DIRTY_PIXEL_BITS)
97 + mask = ((1ULL << (x2 - x1)) - 1) << x1;
98 else
99 mask = ~(0ULL);
101 + h += y;
102 for (; y < h; y++)
103 - vs->dirty_row[y] |= mask;
104 + row[y] |= mask;
105 +}
106 +
107 +static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
108 +{
109 + VncState *vs = ds->opaque;
110 +
111 + set_bits_in_row(vs, vs->dirty_row, x, y, w, h);
112 }
114 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
115 @@ -109,16 +142,23 @@
116 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
117 {
118 VncState *vs = ds->opaque;
119 + int o;
121 ds->data = realloc(ds->data, w * h * vs->depth);
122 vs->old_data = realloc(vs->old_data, w * h * vs->depth);
123 + vs->dirty_row = realloc(vs->dirty_row, h * sizeof(vs->dirty_row[0]));
124 + vs->update_row = realloc(vs->update_row, h * sizeof(vs->dirty_row[0]));
126 - if (ds->data == NULL || vs->old_data == NULL) {
127 + if (ds->data == NULL || vs->old_data == NULL ||
128 + vs->dirty_row == NULL || vs->update_row == NULL) {
129 fprintf(stderr, "vnc: memory allocation failed\n");
130 exit(1);
131 }
133 - ds->depth = vs->depth * 8;
134 + if (ds->depth != vs->depth * 8) {
135 + ds->depth = vs->depth * 8;
136 + set_color_table(ds);
137 + }
138 ds->width = w;
139 ds->height = h;
140 ds->linesize = w * vs->depth;
141 @@ -131,6 +171,10 @@
142 vs->width = ds->width;
143 vs->height = ds->height;
144 }
145 + vs->dirty_pixel_shift = 0;
146 + for (o = DIRTY_PIXEL_BITS; o < ds->width; o *= 2)
147 + vs->dirty_pixel_shift++;
148 + framebuffer_set_updated(vs, 0, 0, ds->width, ds->height);
149 }
151 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
152 @@ -215,8 +259,20 @@
153 int y = 0;
154 int pitch = ds->linesize;
155 VncState *vs = ds->opaque;
156 + int updating_client = !vs->slow_client;
158 - vnc_update_client(vs);
159 + if (src_x < vs->visible_x || src_y < vs->visible_y ||
160 + dst_x < vs->visible_x || dst_y < vs->visible_y ||
161 + (src_x + w) > (vs->visible_x + vs->visible_w) ||
162 + (src_y + h) > (vs->visible_y + vs->visible_h) ||
163 + (dst_x + w) > (vs->visible_x + vs->visible_w) ||
164 + (dst_y + h) > (vs->visible_y + vs->visible_h))
165 + updating_client = 0;
166 +
167 + if (updating_client) {
168 + vs->need_update = 1;
169 + _vnc_update_client(vs);
170 + }
172 if (dst_y > src_y) {
173 y = h - 1;
174 @@ -238,31 +294,34 @@
175 old_row += pitch;
176 }
178 - vnc_write_u8(vs, 0); /* msg id */
179 - vnc_write_u8(vs, 0);
180 - vnc_write_u16(vs, 1); /* number of rects */
181 - vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
182 - vnc_write_u16(vs, src_x);
183 - vnc_write_u16(vs, src_y);
184 - vnc_flush(vs);
185 + if (updating_client && vs->csock != -1 && !vs->has_update) {
186 + vnc_write_u8(vs, 0); /* msg id */
187 + vnc_write_u8(vs, 0);
188 + vnc_write_u16(vs, 1); /* number of rects */
189 + vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
190 + vnc_write_u16(vs, src_x);
191 + vnc_write_u16(vs, src_y);
192 + vnc_flush(vs);
193 + } else
194 + framebuffer_set_updated(vs, dst_x, dst_y, w, h);
195 }
197 -static int find_dirty_height(VncState *vs, int y, int last_x, int x)
198 +static int find_update_height(VncState *vs, int y, int maxy, int last_x, int x)
199 {
200 int h;
202 - for (h = 1; h < (vs->height - y); h++) {
203 + for (h = 1; y + h < maxy; h++) {
204 int tmp_x;
205 - if (!(vs->dirty_row[y + h] & (1ULL << last_x)))
206 + if (!(vs->update_row[y + h] & (1ULL << last_x)))
207 break;
208 for (tmp_x = last_x; tmp_x < x; tmp_x++)
209 - vs->dirty_row[y + h] &= ~(1ULL << tmp_x);
210 + vs->update_row[y + h] &= ~(1ULL << tmp_x);
211 }
213 return h;
214 }
216 -static void vnc_update_client(void *opaque)
217 +static void _vnc_update_client(void *opaque)
218 {
219 VncState *vs = opaque;
220 int64_t now = qemu_get_clock(rt_clock);
221 @@ -274,11 +333,12 @@
222 uint64_t width_mask;
223 int n_rectangles;
224 int saved_offset;
225 - int has_dirty = 0;
226 -
227 - width_mask = (1ULL << (vs->width / 16)) - 1;
228 + int maxx, maxy;
229 + int tile_bytes = vs->depth * DP2X(vs, 1);
231 - if (vs->width == 1024)
232 + if (vs->width != DP2X(vs, DIRTY_PIXEL_BITS))
233 + width_mask = (1ULL << X2DP_UP(vs, vs->ds->width)) - 1;
234 + else
235 width_mask = ~(0ULL);
237 /* Walk through the dirty map and eliminate tiles that
238 @@ -294,16 +354,18 @@
239 ptr = row;
240 old_ptr = old_row;
242 - for (x = 0; x < vs->ds->width; x += 16) {
243 - if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
244 - vs->dirty_row[y] &= ~(1ULL << (x / 16));
245 - } else {
246 - has_dirty = 1;
247 - memcpy(old_ptr, ptr, 16 * vs->depth);
248 + for (x = 0; x < X2DP_UP(vs, vs->ds->width); x++) {
249 + if (vs->dirty_row[y] & (1ULL << x)) {
250 + if (memcmp(old_ptr, ptr, tile_bytes)) {
251 + vs->has_update = 1;
252 + vs->update_row[y] |= (1ULL << x);
253 + memcpy(old_ptr, ptr, tile_bytes);
254 + }
255 + vs->dirty_row[y] &= ~(1ULL << x);
256 }
258 - ptr += 16 * vs->depth;
259 - old_ptr += 16 * vs->depth;
260 + ptr += tile_bytes;
261 + old_ptr += tile_bytes;
262 }
263 }
265 @@ -311,7 +373,8 @@
266 old_row += vs->ds->linesize;
267 }
269 - if (!has_dirty)
270 + if (!vs->has_update || vs->visible_y >= vs->ds->height ||
271 + vs->visible_x >= vs->ds->width)
272 goto out;
274 /* Count rectangles */
275 @@ -321,40 +384,61 @@
276 saved_offset = vs->output.offset;
277 vnc_write_u16(vs, 0);
279 - for (y = 0; y < vs->height; y++) {
280 + maxy = vs->visible_y + vs->visible_h;
281 + if (maxy > vs->ds->height)
282 + maxy = vs->ds->height;
283 + maxx = vs->visible_x + vs->visible_w;
284 + if (maxx > vs->ds->width)
285 + maxx = vs->ds->width;
286 +
287 + for (y = vs->visible_y; y < maxy; y++) {
288 int x;
289 int last_x = -1;
290 - for (x = 0; x < vs->width / 16; x++) {
291 - if (vs->dirty_row[y] & (1ULL << x)) {
292 - if (last_x == -1) {
293 + for (x = X2DP_DOWN(vs, vs->visible_x);
294 + x < X2DP_UP(vs, maxx); x++) {
295 + if (vs->update_row[y] & (1ULL << x)) {
296 + if (last_x == -1)
297 last_x = x;
298 - }
299 - vs->dirty_row[y] &= ~(1ULL << x);
300 + vs->update_row[y] &= ~(1ULL << x);
301 } else {
302 if (last_x != -1) {
303 - int h = find_dirty_height(vs, y, last_x, x);
304 - send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
305 + int h = find_update_height(vs, y, maxy, last_x, x);
306 + send_framebuffer_update(vs, DP2X(vs, last_x), y,
307 + DP2X(vs, (x - last_x)), h);
308 n_rectangles++;
309 }
310 last_x = -1;
311 }
312 }
313 if (last_x != -1) {
314 - int h = find_dirty_height(vs, y, last_x, x);
315 - send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
316 + int h = find_update_height(vs, y, maxy, last_x, x);
317 + send_framebuffer_update(vs, DP2X(vs, last_x), y,
318 + DP2X(vs, (x - last_x)), h);
319 n_rectangles++;
320 }
321 }
322 vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
323 vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
324 - vnc_flush(vs);
326 - }
327 + vs->has_update = 0;
328 + vs->need_update = 0;
329 + vnc_flush(vs);
330 + vs->slow_client = 0;
331 + } else
332 + vs->slow_client = 1;
334 out:
335 qemu_mod_timer(vs->timer, now + VNC_REFRESH_INTERVAL);
336 }
338 +static void vnc_update_client(void *opaque)
339 +{
340 + VncState *vs = opaque;
341 +
342 + vs->ds->dpy_refresh(vs->ds);
343 + _vnc_update_client(vs);
344 +}
345 +
346 static void vnc_timer_init(VncState *vs)
347 {
348 if (vs->timer == NULL) {
349 @@ -365,8 +449,6 @@
351 static void vnc_dpy_refresh(DisplayState *ds)
352 {
353 - VncState *vs = ds->opaque;
354 - vnc_timer_init(vs);
355 vga_hw_update();
356 }
358 @@ -402,7 +484,7 @@
360 static void buffer_reset(Buffer *buffer)
361 {
362 - buffer->offset = 0;
363 + buffer->offset = 0;
364 }
366 static void buffer_append(Buffer *buffer, const void *data, size_t len)
367 @@ -443,12 +525,12 @@
368 if (!ret)
369 return;
371 - memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
372 + memmove(vs->output.buffer, vs->output.buffer + ret,
373 + vs->output.offset - ret);
374 vs->output.offset -= ret;
376 - if (vs->output.offset == 0) {
377 + if (vs->output.offset == 0)
378 qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
379 - }
380 }
382 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
383 @@ -480,11 +562,11 @@
384 return;
386 if (!ret) {
387 - memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
388 + memmove(vs->input.buffer, vs->input.buffer + len,
389 + vs->input.offset - len);
390 vs->input.offset -= len;
391 - } else {
392 + } else
393 vs->read_handler_expect = ret;
394 - }
395 }
396 }
398 @@ -492,9 +574,9 @@
399 {
400 buffer_reserve(&vs->output, len);
402 - if (buffer_empty(&vs->output)) {
403 - qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
404 - }
405 + if (buffer_empty(&vs->output))
406 + qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
407 + vnc_client_write, vs);
409 buffer_append(&vs->output, data, len);
410 }
411 @@ -616,24 +698,25 @@
412 do_key_event(vs, down, sym);
413 }
415 +static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h)
416 +{
417 +
418 + set_bits_in_row(vs, vs->update_row, x, y, w, h);
419 +
420 + vs->has_update = 1;
421 +}
422 +
423 static void framebuffer_update_request(VncState *vs, int incremental,
424 int x_position, int y_position,
425 int w, int h)
426 {
427 - int i;
428 vs->need_update = 1;
429 - if (!incremental) {
430 - char *old_row = vs->old_data + y_position * vs->ds->linesize;
431 -
432 - for (i = 0; i < h; i++) {
433 - vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1;
434 - if (vs->ds->width == 1024) {
435 - vs->dirty_row[y_position + i] = ~(0ULL);
436 - }
437 - memset(old_row, 42, vs->ds->width * vs->depth);
438 - old_row += vs->ds->linesize;
439 - }
440 - }
441 + if (!incremental)
442 + framebuffer_set_updated(vs, x_position, y_position, w, h);
443 + vs->visible_x = x_position;
444 + vs->visible_y = y_position;
445 + vs->visible_w = w;
446 + vs->visible_h = h;
447 }
449 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
450 @@ -690,8 +773,6 @@
451 vnc_client_error(vs);
453 vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
454 - memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
455 - memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
457 vga_hw_invalidate();
458 vga_hw_update();
459 @@ -848,11 +929,11 @@
460 vnc_write(vs, "RFB 003.003\n", 12);
461 vnc_flush(vs);
462 vnc_read_when(vs, protocol_version, 12);
463 - memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
464 - memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
465 + framebuffer_set_updated(vs, 0, 0, vs->ds->width, vs->ds->height);
466 vs->has_resize = 0;
467 vs->has_hextile = 0;
468 vs->ds->dpy_copy = NULL;
469 + vnc_timer_init(vs);
470 }
471 }
473 @@ -909,17 +990,15 @@
474 exit(1);
475 }
477 - ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
478 - if (ret == -1) {
479 + ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read,
480 + NULL, vs);
481 + if (ret == -1)
482 exit(1);
483 - }
485 vs->ds->data = NULL;
486 vs->ds->dpy_update = vnc_dpy_update;
487 vs->ds->dpy_resize = vnc_dpy_resize;
488 vs->ds->dpy_refresh = vnc_dpy_refresh;
490 - memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
491 -
492 vnc_dpy_resize(vs->ds, 640, 400);
493 }