blob: a823ed4274ef18be57ea4a2bb0f313c25dc79772 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2004-2005 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// PollingManager.cxx
20//
21
22// FIXME: Don't compare pixels already marked as changed.
23// FIXME: Use Image::copyPixels() instead of Image::updateRect()?
24// In that case, note the fact that arguments are not checked.
25
26#include <stdio.h>
27#include <string.h>
28#include <time.h>
29#include <X11/Xlib.h>
30#include <rfb/LogWriter.h>
31#include <rfb/VNCServer.h>
32#include <rfb/Configuration.h>
33#include <rfb/ServerCore.h>
34
35#include <x0vncserver/PollingManager.h>
36
37static LogWriter vlog("PollingMgr");
38
39BoolParameter PollingManager::pollPointer
40("PollPointer",
41 "DEBUG: Poll area under the pointer with higher priority",
42 true);
43
44IntParameter PollingManager::pollingType
45("PollingType",
46 "DEBUG: Select particular polling algorithm (0..3)",
47 3);
48
49const int PollingManager::m_pollingOrder[32] = {
50 0, 16, 8, 24, 4, 20, 12, 28,
51 10, 26, 18, 2, 22, 6, 30, 14,
52 1, 17, 9, 25, 7, 23, 15, 31,
53 19, 3, 27, 11, 29, 13, 5, 21
54};
55
56//
57// Constructor.
58//
59// Note that dpy and image should remain valid during the object
60// lifetime, while factory is used only in the constructor itself.
61//
62
63PollingManager::PollingManager(Display *dpy, Image *image,
64 ImageFactory *factory,
65 int offsetLeft, int offsetTop)
66 : m_dpy(dpy), m_server(0), m_image(image),
67 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
68 m_pointerPosKnown(false), m_pollingStep(0)
69{
70 // Save width and height of the screen (and the image).
71 m_width = m_image->xim->width;
72 m_height = m_image->xim->height;
73
74 // Compute width and height in 32x32 tiles.
75 m_widthTiles = (m_width + 31) / 32;
76 m_heightTiles = (m_height + 31) / 32;
77
78 // Get initial screen image.
79 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
80
81 // Create additional images used in polling algorithms, warn if
82 // underlying class names are different from the class name of the
83 // primary image.
84 m_rowImage = factory->newImage(m_dpy, m_width, 1);
85 m_tileImage = factory->newImage(m_dpy, 32, 32);
86 m_areaImage = factory->newImage(m_dpy, 128, 128);
87 if (strcmp(m_image->className(), m_rowImage->className()) != 0 ||
88 strcmp(m_image->className(), m_tileImage->className()) != 0 ||
89 strcmp(m_image->className(), m_areaImage->className()) != 0) {
90 vlog.error("Image types do not match (%s, %s, %s)",
91 m_rowImage->className(),
92 m_tileImage->className(),
93 m_areaImage->className());
94 }
95
96 // FIXME: Extend the comment.
97 // Create a matrix with one byte per each 32x32 tile. It will be
98 // used to limit the rate of updates on continuously-changed screen
99 // areas (like video).
100 int numTiles = m_widthTiles * m_heightTiles;
101 m_statusMatrix = new char[numTiles];
102 memset(m_statusMatrix, 0, numTiles);
103
104 // FIXME: Extend the comment.
105 // Create a matrix with one byte per each 32x32 tile. It will be
106 // used to limit the rate of updates on continuously-changed screen
107 // areas (like video).
108 m_rateMatrix = new char[numTiles];
109 m_videoFlags = new char[numTiles];
110 m_changedFlags = new char[numTiles];
111 memset(m_rateMatrix, 0, numTiles);
112 memset(m_videoFlags, 0, numTiles);
113 memset(m_changedFlags, 0, numTiles);
114}
115
116PollingManager::~PollingManager()
117{
118 delete[] m_changedFlags;
119 delete[] m_videoFlags;
120 delete[] m_rateMatrix;
121
122 delete[] m_statusMatrix;
123
124 delete m_areaImage;
125 delete m_tileImage;
126 delete m_rowImage;
127}
128
129//
130// Register VNCServer object.
131//
132
133void PollingManager::setVNCServer(VNCServer *s)
134{
135 m_server = s;
136}
137
138//
139// Update current pointer position which may be used as a hint for
140// polling algorithms.
141//
142
143void PollingManager::setPointerPos(const Point &pos)
144{
145 m_pointerPosTime = time(NULL);
146 m_pointerPos = pos;
147 m_pointerPosKnown = true;
148}
149
150//
151// Indicate that current pointer position is unknown.
152//
153
154void PollingManager::unsetPointerPos()
155{
156 m_pointerPosKnown = false;
157}
158
159//
160// DEBUG: Measuring time spent in the poll() function,
161// as well as time intervals between poll() calls.
162//
163
164#ifdef DEBUG
165void PollingManager::debugBeforePoll()
166{
167 TimeMillis timeNow;
168 int diff = timeNow.diffFrom(m_timeSaved);
169 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
170 m_timeSaved = timeNow;
171}
172
173void PollingManager::debugAfterPoll()
174{
175 TimeMillis timeNow;
176 int diff = timeNow.diffFrom(m_timeSaved);
177 fprintf(stderr, "[poll%4dms]\n", diff);
178 m_timeSaved = timeNow;
179}
180
181#endif
182
183//
184// Search for changed rectangles on the screen.
185//
186
187void PollingManager::poll()
188{
189#ifdef DEBUG
190 debugBeforePoll();
191#endif
192
193 // First step: full-screen polling.
194
195 bool changes1 = false;
196
197 switch((int)pollingType) {
198 case 0:
199 changes1 = poll_Dumb();
200 break;
201 case 1:
202 changes1 = poll_Traditional();
203 break;
204 case 2:
205 changes1 = poll_SkipCycles();
206 break;
207//case 3:
208 default:
209 changes1 = poll_DetectVideo();
210 break;
211 }
212
213 // Second step: optional thorough polling of the area around the pointer.
214 // We do that only if the pointer position is known and was set recently.
215
216 bool changes2 = false;
217 if (pollPointer) {
218 if (m_pointerPosKnown && time(NULL) - m_pointerPosTime >= 5) {
219 unsetPointerPos();
220 }
221 if (m_pointerPosKnown) {
222 changes2 = pollPointerArea();
223 }
224 }
225
226 // Update if needed.
227
228 if (changes1 || changes2)
229 m_server->tryUpdate();
230
231#ifdef DEBUG
232 debugAfterPoll();
233#endif
234}
235
236bool PollingManager::poll_DetectVideo()
237{
238 if (!m_server)
239 return false;
240
241 const int GRAND_STEP_DIVISOR = 8;
242 const int VIDEO_THRESHOLD_0 = 3;
243 const int VIDEO_THRESHOLD_1 = 5;
244
245 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
246
247 // FIXME: Save shortcuts in member variables?
248 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
249 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
250 int bytesPerLine = m_image->xim->bytes_per_line;
251
252 Rect rect;
253 int nTilesChanged = 0;
254 int idx = 0;
255
256 for (int y = 0; y * 32 < m_height; y++) {
257 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
258 if (scanLine >= tile_h)
259 break;
260 int scan_y = y * 32 + scanLine;
261 getRow(scan_y);
262 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
263 char *ptr_new = m_rowImage->xim->data;
264 for (int x = 0; x * 32 < m_width; x++) {
265 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
266 int nBytes = tile_w * bytesPerPixel;
267
268 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
269 m_rateMatrix[idx] += wasChanged;
270
271 if (grandStep) {
272 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
273 m_videoFlags[idx] = 0;
274 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
275 m_videoFlags[idx] = 1;
276 }
277 m_rateMatrix[idx] = 0;
278 }
279
280 m_changedFlags[idx] |= wasChanged;
281 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
282 getTile32(x, y, tile_w, tile_h);
283 m_image->updateRect(m_tileImage, x * 32, y * 32);
284 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
285 m_server->add_changed(rect);
286 nTilesChanged++;
287 m_changedFlags[idx] = 0;
288 }
289
290 ptr_old += nBytes;
291 ptr_new += nBytes;
292 idx++;
293 }
294 }
295
296 if (grandStep)
297 adjustVideoArea();
298
299 return (nTilesChanged != 0);
300}
301
302bool PollingManager::poll_SkipCycles()
303{
304 if (!m_server)
305 return false;
306
307 enum {
308 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
309 };
310
311 bool grandStep = (m_pollingStep % 8 == 0);
312
313 int nTilesChanged = 0;
314 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
315 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
316 int bytesPerLine = m_image->xim->bytes_per_line;
317 char *pstatus = m_statusMatrix;
318 bool wasChanged;
319 Rect rect;
320
321 for (int y = 0; y * 32 < m_height; y++) {
322 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
323 if (scanLine >= tile_h)
324 scanLine %= tile_h;
325 int scan_y = y * 32 + scanLine;
326 getRow(scan_y);
327 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
328 char *ptr_new = m_rowImage->xim->data;
329 for (int x = 0; x * 32 < m_width; x++) {
330 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
331 int nBytes = tile_w * bytesPerPixel;
332
333 if (grandStep || *pstatus != CHANGED_AGAIN) {
334 wasChanged = (*pstatus == CHANGED_AGAIN) ?
335 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
336 if (wasChanged) {
337 if (grandStep || *pstatus == NOT_CHANGED) {
338 getTile32(x, y, tile_w, tile_h);
339 m_image->updateRect(m_tileImage, x * 32, y * 32);
340 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
341 m_server->add_changed(rect);
342 nTilesChanged++;
343 *pstatus = CHANGED_ONCE;
344 } else {
345 *pstatus = CHANGED_AGAIN;
346 }
347 } else if (grandStep) {
348 *pstatus = NOT_CHANGED;
349 }
350 }
351
352 ptr_old += nBytes;
353 ptr_new += nBytes;
354 pstatus++;
355 }
356 }
357
358 return (nTilesChanged != 0);
359}
360
361bool PollingManager::poll_Traditional()
362{
363 if (!m_server)
364 return false;
365
366 int nTilesChanged = 0;
367 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
368 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
369 int bytesPerLine = m_image->xim->bytes_per_line;
370 Rect rect;
371
372 for (int y = 0; y * 32 < m_height; y++) {
373 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
374 if (scanLine >= tile_h)
375 break;
376 int scan_y = y * 32 + scanLine;
377 getRow(scan_y);
378 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
379 char *ptr_new = m_rowImage->xim->data;
380 for (int x = 0; x * 32 < m_width; x++) {
381 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
382 int nBytes = tile_w * bytesPerPixel;
383 if (memcmp(ptr_old, ptr_new, nBytes)) {
384 getTile32(x, y, tile_w, tile_h);
385 m_image->updateRect(m_tileImage, x * 32, y * 32);
386 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
387 m_server->add_changed(rect);
388 nTilesChanged++;
389 }
390 ptr_old += nBytes;
391 ptr_new += nBytes;
392 }
393 }
394
395 return (nTilesChanged != 0);
396}
397
398//
399// Simplest polling method, from the original x0vncserver of VNC4.
400//
401
402bool PollingManager::poll_Dumb()
403{
404 if (!m_server)
405 return false;
406
407 getScreen();
408 Rect rect(0, 0, m_width, m_height);
409 m_server->add_changed(rect);
410
411 // Report that some changes have been detected.
412 return true;
413}
414
415//
416// Compute coordinates of the rectangle around the pointer.
417//
418// ASSUMES: (m_pointerPosKnown != false)
419//
420
421void PollingManager::computePointerArea(Rect *r)
422{
423 int x = m_pointerPos.x - 64;
424 int y = m_pointerPos.y - 64;
425 int w = 128;
426 int h = 128;
427 if (x < 0) {
428 w += x; x = 0;
429 }
430 if (x + w > m_width) {
431 w = m_width - x;
432 }
433 if (y < 0) {
434 h += y; y = 0;
435 }
436 if (y + h > m_height) {
437 h = m_height - y;
438 }
439
440 r->setXYWH(x, y, w, h);
441}
442
443//
444// Poll the area under current pointer position. Each pixel of the
445// area should be compared. Using such polling option gives higher
446// priority to screen area under the pointer.
447//
448// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
449//
450
451bool PollingManager::pollPointerArea()
452{
453 Rect r;
454 computePointerArea(&r);
455
456 // Shortcuts for coordinates.
457 int x = r.tl.x, y = r.tl.y;
458 int w = r.width(), h = r.height();
459
460 // Get new pixels.
461 getArea128(x, y, w, h);
462
463 // Now, try to minimize the rectangle by cutting out unchanged
464 // borders (at top and bottom).
465 //
466 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
467 // to produce a region instead of a rectangle. If there would
468 // be just one universal polling algorithm, it could be
469 // better to integrate pointer area polling into that
470 // algorithm, instead of a separate pollPointerArea()
471 // function.
472
473 // Shortcuts.
474 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
475 int oldBytesPerLine = m_image->xim->bytes_per_line;
476 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
477 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
478 char *newPtr = m_areaImage->xim->data;
479
480 // Check and cut out unchanged rows at the top.
481 int ty;
482 for (ty = 0; ty < h; ty++) {
483 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
484 break;
485 oldPtr += oldBytesPerLine;
486 newPtr += newBytesPerLine;
487 }
488 if (ty == h) {
489 return false; // no changes at all
490 }
491 y += ty; h -= ty;
492
493 // Check and cut out unchanged rows at the bottom.
494 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
495 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
496 int by;
497 for (by = 0; by < h - 1; by++) {
498 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
499 break;
500 oldPtr -= oldBytesPerLine;
501 newPtr -= newBytesPerLine;
502 }
503 h -= by;
504
505 // Copy pixels.
506 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
507
508 // Report updates to the server.
509 Rect rect(x, y, x+w, y+h);
510 m_server->add_changed(rect);
511 return true;
512}
513
514//
515// Make video area pattern more regular.
516//
517// FIXME: Replace the above with a normal comment.
518// FIXME: Is the function efficient enough?
519//
520
521void PollingManager::adjustVideoArea()
522{
523 char newFlags[m_widthTiles * m_heightTiles];
524 char *ptr = newFlags;
525 int x, y;
526
527 // DEBUG:
528 // int nVideoTiles = 0;
529
530 for (y = 0; y < m_heightTiles; y++) {
531 for (x = 0; x < m_widthTiles; x++) {
532
533 // DEBUG:
534 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
535
536 int weightedSum = 0, n;
537 if (y > 0 && x > 0) {
538 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
539 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
540 m_videoFlags[(y-1) * m_widthTiles + x ]);
541 if (n == 3) {
542 *ptr++ = 1;
543 continue;
544 }
545 weightedSum += n;
546 }
547 if (y > 0 && x < m_widthTiles - 1) {
548 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
549 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
550 m_videoFlags[(y-1) * m_widthTiles + x ]);
551 if (n == 3) {
552 *ptr++ = 1;
553 continue;
554 }
555 weightedSum += n;
556 }
557 if (y < m_heightTiles - 1 && x > 0) {
558 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
559 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
560 m_videoFlags[(y+1) * m_widthTiles + x ]);
561 if (n == 3) {
562 *ptr++ = 1;
563 continue;
564 }
565 weightedSum += n;
566 }
567 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
568 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
569 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
570 m_videoFlags[(y+1) * m_widthTiles + x ]);
571 if (n == 3) {
572 *ptr++ = 1;
573 continue;
574 }
575 weightedSum += n;
576 }
577 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
578 }
579 }
580
581 /*
582 /// DEBUG: ------------------------------------------------------
583 if (nVideoTiles) {
584 for (y = 0; y < m_heightTiles; y++) {
585 for (x = 0; x < m_widthTiles; x++) {
586 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
587 }
588 printf(" ");
589 for (x = 0; x < m_widthTiles; x++) {
590 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
591 }
592 printf("\n");
593 }
594 printf("\n");
595 }
596 /// -------------------------------------------------------------
597 */
598
599 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
600}