Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* 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 | |
| 37 | static LogWriter vlog("PollingMgr"); |
| 38 | |
| 39 | BoolParameter PollingManager::pollPointer |
| 40 | ("PollPointer", |
| 41 | "DEBUG: Poll area under the pointer with higher priority", |
Constantin Kaplinsky | 344c3fe | 2007-07-24 08:35:43 +0000 | [diff] [blame] | 42 | false); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 43 | |
| 44 | IntParameter PollingManager::pollingType |
| 45 | ("PollingType", |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 46 | "DEBUG: Select particular polling algorithm (0..4)", |
| 47 | 4); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 48 | |
| 49 | const 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 | |
| 63 | PollingManager::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 | |
| 116 | PollingManager::~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 | |
| 133 | void 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 | |
| 143 | void 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 | |
| 154 | void 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 |
| 165 | void 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 | |
| 173 | void 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 | |
| 187 | void 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; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 207 | case 3: |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 208 | changes1 = poll_DetectVideo(); |
| 209 | break; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 210 | //case 4: |
| 211 | default: |
| 212 | changes1 = poll_New(); |
| 213 | break; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Second step: optional thorough polling of the area around the pointer. |
| 217 | // We do that only if the pointer position is known and was set recently. |
| 218 | |
| 219 | bool changes2 = false; |
| 220 | if (pollPointer) { |
| 221 | if (m_pointerPosKnown && time(NULL) - m_pointerPosTime >= 5) { |
| 222 | unsetPointerPos(); |
| 223 | } |
| 224 | if (m_pointerPosKnown) { |
| 225 | changes2 = pollPointerArea(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Update if needed. |
| 230 | |
| 231 | if (changes1 || changes2) |
| 232 | m_server->tryUpdate(); |
| 233 | |
| 234 | #ifdef DEBUG |
| 235 | debugAfterPoll(); |
| 236 | #endif |
| 237 | } |
| 238 | |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 239 | bool PollingManager::poll_New() |
| 240 | { |
| 241 | if (!m_server) |
| 242 | return false; |
| 243 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 244 | // mxChanged[] array will hold boolean values corresponding to each |
| 245 | // 32x32 tile. If a value is true, then we've detected a change in |
Constantin Kaplinsky | 5e2f69f | 2007-10-07 13:08:18 +0000 | [diff] [blame] | 246 | // that tile. Initially, we fill in the array with zero values. |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 247 | bool *mxChanged = new bool[m_widthTiles * m_heightTiles]; |
Constantin Kaplinsky | f25307a | 2007-10-08 13:49:44 +0000 | [diff] [blame] | 248 | memset(mxChanged, 0, m_widthTiles * m_heightTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 249 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 250 | // First pass over the framebuffer. Here we scan 1/32 part of the |
| 251 | // framebuffer -- that is, one line in each (32 * m_width) stripe. |
| 252 | // We compare the pixels of that line with previous framebuffer |
| 253 | // contents and raise corresponding member values of mxChanged[]. |
| 254 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
| 255 | bool *pmxChanged = mxChanged; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 256 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 257 | for (int y = scanOffset; y < m_height; y += 32) { |
| 258 | nTilesChanged += checkRow(0, y, m_width, pmxChanged); |
| 259 | pmxChanged += m_widthTiles; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Inform the server about the changes. |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 263 | if (nTilesChanged) |
| 264 | sendChanges(mxChanged); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 265 | |
| 266 | // Cleanup. |
| 267 | delete[] mxChanged; |
| 268 | |
| 269 | return (nTilesChanged != 0); |
| 270 | } |
| 271 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 272 | int PollingManager::checkRow(int x, int y, int w, bool *pmxChanged) |
| 273 | { |
| 274 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 275 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 276 | |
Constantin Kaplinsky | 29d3205 | 2007-10-08 14:16:02 +0000 | [diff] [blame^] | 277 | if (x == 0 && w == m_width) { |
| 278 | getFullRow(y); // use more efficient method if possible |
| 279 | } else { |
| 280 | getRow(x, y, w); |
| 281 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 282 | |
| 283 | char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel; |
| 284 | char *ptr_new = m_rowImage->xim->data; |
| 285 | |
| 286 | int nTilesChanged = 0; |
| 287 | for (int i = 0; i < (w + 31) / 32; i++) { |
| 288 | int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32; |
| 289 | int nBytes = tile_w * bytesPerPixel; |
| 290 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
| 291 | *pmxChanged = true; |
| 292 | nTilesChanged++; |
| 293 | } |
| 294 | pmxChanged++; |
| 295 | ptr_old += nBytes; |
| 296 | ptr_new += nBytes; |
| 297 | } |
| 298 | |
| 299 | return nTilesChanged; |
| 300 | } |
| 301 | |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 302 | void PollingManager::sendChanges(bool *pmxChanged) |
| 303 | { |
| 304 | Rect rect; |
| 305 | for (int y = 0; y < m_heightTiles; y++) { |
| 306 | for (int x = 0; x < m_widthTiles; x++) { |
| 307 | if (*pmxChanged++) { |
| 308 | // Count successive tiles marked as changed. |
| 309 | int count = 1; |
| 310 | while (x + count < m_widthTiles && *pmxChanged++) { |
| 311 | count++; |
| 312 | } |
| 313 | // Compute the coordinates and the size of this band. |
| 314 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 315 | if (rect.br.x > m_width) |
| 316 | rect.br.x = m_width; |
| 317 | if (rect.br.y > m_height) |
| 318 | rect.br.y = m_height; |
| 319 | // Add to the changed region maintained by the server. |
| 320 | getScreenRect(rect); |
| 321 | m_server->add_changed(rect); |
| 322 | // Skip processed tiles. |
| 323 | x += count; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 329 | bool PollingManager::poll_DetectVideo() |
| 330 | { |
| 331 | if (!m_server) |
| 332 | return false; |
| 333 | |
| 334 | const int GRAND_STEP_DIVISOR = 8; |
| 335 | const int VIDEO_THRESHOLD_0 = 3; |
| 336 | const int VIDEO_THRESHOLD_1 = 5; |
| 337 | |
| 338 | bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0); |
| 339 | |
| 340 | // FIXME: Save shortcuts in member variables? |
| 341 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 342 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 343 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 344 | |
| 345 | Rect rect; |
| 346 | int nTilesChanged = 0; |
| 347 | int idx = 0; |
| 348 | |
| 349 | for (int y = 0; y * 32 < m_height; y++) { |
| 350 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 351 | if (scanLine >= tile_h) |
| 352 | break; |
| 353 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 354 | getFullRow(scan_y); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 355 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 356 | char *ptr_new = m_rowImage->xim->data; |
| 357 | for (int x = 0; x * 32 < m_width; x++) { |
| 358 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 359 | int nBytes = tile_w * bytesPerPixel; |
| 360 | |
| 361 | char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 362 | m_rateMatrix[idx] += wasChanged; |
| 363 | |
| 364 | if (grandStep) { |
| 365 | if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) { |
| 366 | m_videoFlags[idx] = 0; |
| 367 | } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) { |
| 368 | m_videoFlags[idx] = 1; |
| 369 | } |
| 370 | m_rateMatrix[idx] = 0; |
| 371 | } |
| 372 | |
| 373 | m_changedFlags[idx] |= wasChanged; |
| 374 | if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) { |
| 375 | getTile32(x, y, tile_w, tile_h); |
| 376 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 377 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 378 | m_server->add_changed(rect); |
| 379 | nTilesChanged++; |
| 380 | m_changedFlags[idx] = 0; |
| 381 | } |
| 382 | |
| 383 | ptr_old += nBytes; |
| 384 | ptr_new += nBytes; |
| 385 | idx++; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (grandStep) |
| 390 | adjustVideoArea(); |
| 391 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 392 | bool videoDetected = false; |
| 393 | Rect r; |
| 394 | getVideoAreaRect(&r); |
| 395 | m_server->set_video_area(r); |
| 396 | videoDetected = !r.is_empty(); |
| 397 | if (videoDetected) { |
| 398 | m_image->get(DefaultRootWindow(m_dpy), |
| 399 | m_offsetLeft + r.tl.x, m_offsetTop + r.tl.y, |
| 400 | r.width(), r.height(), r.tl.x, r.tl.y); |
| 401 | } |
| 402 | |
Constantin Kaplinsky | 344c3fe | 2007-07-24 08:35:43 +0000 | [diff] [blame] | 403 | #ifdef DEBUG |
| 404 | if (nTilesChanged != 0) { |
| 405 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 406 | } |
| 407 | #endif |
| 408 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 409 | return (nTilesChanged != 0 || videoDetected); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | bool PollingManager::poll_SkipCycles() |
| 413 | { |
| 414 | if (!m_server) |
| 415 | return false; |
| 416 | |
| 417 | enum { |
| 418 | NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN |
| 419 | }; |
| 420 | |
| 421 | bool grandStep = (m_pollingStep % 8 == 0); |
| 422 | |
| 423 | int nTilesChanged = 0; |
| 424 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 425 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 426 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 427 | char *pstatus = m_statusMatrix; |
| 428 | bool wasChanged; |
| 429 | Rect rect; |
| 430 | |
| 431 | for (int y = 0; y * 32 < m_height; y++) { |
| 432 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 433 | if (scanLine >= tile_h) |
| 434 | scanLine %= tile_h; |
| 435 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 436 | getFullRow(scan_y); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 437 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 438 | char *ptr_new = m_rowImage->xim->data; |
| 439 | for (int x = 0; x * 32 < m_width; x++) { |
| 440 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 441 | int nBytes = tile_w * bytesPerPixel; |
| 442 | |
| 443 | if (grandStep || *pstatus != CHANGED_AGAIN) { |
| 444 | wasChanged = (*pstatus == CHANGED_AGAIN) ? |
| 445 | true : (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 446 | if (wasChanged) { |
| 447 | if (grandStep || *pstatus == NOT_CHANGED) { |
| 448 | getTile32(x, y, tile_w, tile_h); |
| 449 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 450 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 451 | m_server->add_changed(rect); |
| 452 | nTilesChanged++; |
| 453 | *pstatus = CHANGED_ONCE; |
| 454 | } else { |
| 455 | *pstatus = CHANGED_AGAIN; |
| 456 | } |
| 457 | } else if (grandStep) { |
| 458 | *pstatus = NOT_CHANGED; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | ptr_old += nBytes; |
| 463 | ptr_new += nBytes; |
| 464 | pstatus++; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return (nTilesChanged != 0); |
| 469 | } |
| 470 | |
| 471 | bool PollingManager::poll_Traditional() |
| 472 | { |
| 473 | if (!m_server) |
| 474 | return false; |
| 475 | |
| 476 | int nTilesChanged = 0; |
| 477 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 478 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 479 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 480 | Rect rect; |
| 481 | |
| 482 | for (int y = 0; y * 32 < m_height; y++) { |
| 483 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 484 | if (scanLine >= tile_h) |
| 485 | break; |
| 486 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 487 | getFullRow(scan_y); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 488 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 489 | char *ptr_new = m_rowImage->xim->data; |
| 490 | for (int x = 0; x * 32 < m_width; x++) { |
| 491 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 492 | int nBytes = tile_w * bytesPerPixel; |
| 493 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
| 494 | getTile32(x, y, tile_w, tile_h); |
| 495 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 496 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 497 | m_server->add_changed(rect); |
| 498 | nTilesChanged++; |
| 499 | } |
| 500 | ptr_old += nBytes; |
| 501 | ptr_new += nBytes; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return (nTilesChanged != 0); |
| 506 | } |
| 507 | |
| 508 | // |
| 509 | // Simplest polling method, from the original x0vncserver of VNC4. |
| 510 | // |
| 511 | |
| 512 | bool PollingManager::poll_Dumb() |
| 513 | { |
| 514 | if (!m_server) |
| 515 | return false; |
| 516 | |
| 517 | getScreen(); |
| 518 | Rect rect(0, 0, m_width, m_height); |
| 519 | m_server->add_changed(rect); |
| 520 | |
| 521 | // Report that some changes have been detected. |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // |
| 526 | // Compute coordinates of the rectangle around the pointer. |
| 527 | // |
| 528 | // ASSUMES: (m_pointerPosKnown != false) |
| 529 | // |
| 530 | |
| 531 | void PollingManager::computePointerArea(Rect *r) |
| 532 | { |
| 533 | int x = m_pointerPos.x - 64; |
| 534 | int y = m_pointerPos.y - 64; |
| 535 | int w = 128; |
| 536 | int h = 128; |
| 537 | if (x < 0) { |
| 538 | w += x; x = 0; |
| 539 | } |
| 540 | if (x + w > m_width) { |
| 541 | w = m_width - x; |
| 542 | } |
| 543 | if (y < 0) { |
| 544 | h += y; y = 0; |
| 545 | } |
| 546 | if (y + h > m_height) { |
| 547 | h = m_height - y; |
| 548 | } |
| 549 | |
| 550 | r->setXYWH(x, y, w, h); |
| 551 | } |
| 552 | |
| 553 | // |
| 554 | // Poll the area under current pointer position. Each pixel of the |
| 555 | // area should be compared. Using such polling option gives higher |
| 556 | // priority to screen area under the pointer. |
| 557 | // |
| 558 | // ASSUMES: (m_server != NULL && m_pointerPosKnown != false) |
| 559 | // |
| 560 | |
| 561 | bool PollingManager::pollPointerArea() |
| 562 | { |
| 563 | Rect r; |
| 564 | computePointerArea(&r); |
| 565 | |
| 566 | // Shortcuts for coordinates. |
| 567 | int x = r.tl.x, y = r.tl.y; |
| 568 | int w = r.width(), h = r.height(); |
| 569 | |
| 570 | // Get new pixels. |
| 571 | getArea128(x, y, w, h); |
| 572 | |
| 573 | // Now, try to minimize the rectangle by cutting out unchanged |
| 574 | // borders (at top and bottom). |
| 575 | // |
| 576 | // FIXME: Perhaps we should work on 32x32 tiles (properly aligned) |
| 577 | // to produce a region instead of a rectangle. If there would |
| 578 | // be just one universal polling algorithm, it could be |
| 579 | // better to integrate pointer area polling into that |
| 580 | // algorithm, instead of a separate pollPointerArea() |
| 581 | // function. |
| 582 | |
| 583 | // Shortcuts. |
| 584 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 585 | int oldBytesPerLine = m_image->xim->bytes_per_line; |
| 586 | int newBytesPerLine = m_areaImage->xim->bytes_per_line; |
| 587 | char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel; |
| 588 | char *newPtr = m_areaImage->xim->data; |
| 589 | |
| 590 | // Check and cut out unchanged rows at the top. |
| 591 | int ty; |
| 592 | for (ty = 0; ty < h; ty++) { |
| 593 | if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0) |
| 594 | break; |
| 595 | oldPtr += oldBytesPerLine; |
| 596 | newPtr += newBytesPerLine; |
| 597 | } |
| 598 | if (ty == h) { |
| 599 | return false; // no changes at all |
| 600 | } |
| 601 | y += ty; h -= ty; |
| 602 | |
| 603 | // Check and cut out unchanged rows at the bottom. |
| 604 | oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel; |
| 605 | newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine; |
| 606 | int by; |
| 607 | for (by = 0; by < h - 1; by++) { |
| 608 | if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0) |
| 609 | break; |
| 610 | oldPtr -= oldBytesPerLine; |
| 611 | newPtr -= newBytesPerLine; |
| 612 | } |
| 613 | h -= by; |
| 614 | |
| 615 | // Copy pixels. |
| 616 | m_image->updateRect(m_areaImage, x, y, 0, ty, w, h); |
| 617 | |
| 618 | // Report updates to the server. |
| 619 | Rect rect(x, y, x+w, y+h); |
| 620 | m_server->add_changed(rect); |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | // |
| 625 | // Make video area pattern more regular. |
| 626 | // |
| 627 | // FIXME: Replace the above with a normal comment. |
| 628 | // FIXME: Is the function efficient enough? |
| 629 | // |
| 630 | |
| 631 | void PollingManager::adjustVideoArea() |
| 632 | { |
| 633 | char newFlags[m_widthTiles * m_heightTiles]; |
| 634 | char *ptr = newFlags; |
| 635 | int x, y; |
| 636 | |
| 637 | // DEBUG: |
| 638 | // int nVideoTiles = 0; |
| 639 | |
| 640 | for (y = 0; y < m_heightTiles; y++) { |
| 641 | for (x = 0; x < m_widthTiles; x++) { |
| 642 | |
| 643 | // DEBUG: |
| 644 | // nVideoTiles += m_videoFlags[y * m_widthTiles + x]; |
| 645 | |
| 646 | int weightedSum = 0, n; |
| 647 | if (y > 0 && x > 0) { |
| 648 | n = (m_videoFlags[ y * m_widthTiles + (x-1)] + |
| 649 | m_videoFlags[(y-1) * m_widthTiles + (x-1)] + |
| 650 | m_videoFlags[(y-1) * m_widthTiles + x ]); |
| 651 | if (n == 3) { |
| 652 | *ptr++ = 1; |
| 653 | continue; |
| 654 | } |
| 655 | weightedSum += n; |
| 656 | } |
| 657 | if (y > 0 && x < m_widthTiles - 1) { |
| 658 | n = (m_videoFlags[ y * m_widthTiles + (x+1)] + |
| 659 | m_videoFlags[(y-1) * m_widthTiles + (x+1)] + |
| 660 | m_videoFlags[(y-1) * m_widthTiles + x ]); |
| 661 | if (n == 3) { |
| 662 | *ptr++ = 1; |
| 663 | continue; |
| 664 | } |
| 665 | weightedSum += n; |
| 666 | } |
| 667 | if (y < m_heightTiles - 1 && x > 0) { |
| 668 | n = (m_videoFlags[ y * m_widthTiles + (x-1)] + |
| 669 | m_videoFlags[(y+1) * m_widthTiles + (x-1)] + |
| 670 | m_videoFlags[(y+1) * m_widthTiles + x ]); |
| 671 | if (n == 3) { |
| 672 | *ptr++ = 1; |
| 673 | continue; |
| 674 | } |
| 675 | weightedSum += n; |
| 676 | } |
| 677 | if (y < m_heightTiles - 1 && x < m_widthTiles - 1) { |
| 678 | n = (m_videoFlags[ y * m_widthTiles + (x+1)] + |
| 679 | m_videoFlags[(y+1) * m_widthTiles + (x+1)] + |
| 680 | m_videoFlags[(y+1) * m_widthTiles + x ]); |
| 681 | if (n == 3) { |
| 682 | *ptr++ = 1; |
| 683 | continue; |
| 684 | } |
| 685 | weightedSum += n; |
| 686 | } |
| 687 | *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x]; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | /// DEBUG: ------------------------------------------------------ |
| 693 | if (nVideoTiles) { |
| 694 | for (y = 0; y < m_heightTiles; y++) { |
| 695 | for (x = 0; x < m_widthTiles; x++) { |
| 696 | printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':'); |
| 697 | } |
| 698 | printf(" "); |
| 699 | for (x = 0; x < m_widthTiles; x++) { |
| 700 | printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':'); |
| 701 | } |
| 702 | printf("\n"); |
| 703 | } |
| 704 | printf("\n"); |
| 705 | } |
| 706 | /// ------------------------------------------------------------- |
| 707 | */ |
| 708 | |
| 709 | memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles); |
| 710 | } |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 711 | |
| 712 | void |
| 713 | PollingManager::getVideoAreaRect(Rect *result) |
| 714 | { |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 715 | int *mx_hlen, *mx_vlen; |
| 716 | constructLengthMatrices(&mx_hlen, &mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 717 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 718 | int full_h = m_heightTiles; |
| 719 | int full_w = m_widthTiles; |
| 720 | int x, y; |
| 721 | Rect max_rect(0, 0, 0, 0); |
| 722 | Rect local_rect; |
| 723 | |
| 724 | for (y = 0; y < full_h; y++) { |
| 725 | for (x = 0; x < full_w; x++) { |
| 726 | int max_w = mx_hlen[y * full_w + x]; |
| 727 | int max_h = mx_vlen[y * full_w + x]; |
| 728 | if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) { |
| 729 | local_rect.tl.x = x; |
| 730 | local_rect.tl.y = y; |
| 731 | findMaxLocalRect(&local_rect, mx_hlen, mx_vlen); |
| 732 | if (local_rect.area() > max_rect.area()) { |
| 733 | max_rect = local_rect; |
| 734 | } |
| 735 | } |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 739 | destroyLengthMatrices(mx_hlen, mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 740 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 741 | max_rect.tl.x *= 32; |
| 742 | max_rect.tl.y *= 32; |
| 743 | max_rect.br.x *= 32; |
| 744 | max_rect.br.y *= 32; |
| 745 | if (max_rect.br.x > m_width) |
| 746 | max_rect.br.x = m_width; |
| 747 | if (max_rect.br.y > m_height) |
| 748 | max_rect.br.y = m_height; |
| 749 | *result = max_rect; |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 750 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 751 | if (!result->is_empty()) { |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 752 | fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n", |
| 753 | result->width(), result->height(), result->tl.x, result->tl.y); |
| 754 | } |
| 755 | } |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 756 | |
| 757 | void |
| 758 | PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v) |
| 759 | { |
| 760 | // Handy shortcuts. |
| 761 | int h = m_heightTiles; |
| 762 | int w = m_widthTiles; |
| 763 | |
| 764 | // Allocate memory. |
| 765 | int *mx_h = new int[h * w]; |
| 766 | memset(mx_h, 0, h * w * sizeof(int)); |
| 767 | int *mx_v = new int[h * w]; |
| 768 | memset(mx_v, 0, h * w * sizeof(int)); |
| 769 | |
| 770 | int x, y, len, i; |
| 771 | |
| 772 | // Fill in horizontal length matrix. |
| 773 | for (y = 0; y < h; y++) { |
| 774 | for (x = 0; x < w; x++) { |
| 775 | len = 0; |
| 776 | while (x + len < w && m_videoFlags[y * w + x + len]) { |
| 777 | len++; |
| 778 | } |
| 779 | for (i = 0; i < len; i++) { |
| 780 | mx_h[y * w + x + i] = len - i; |
| 781 | } |
| 782 | x += len; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // Fill in vertical length matrix. |
| 787 | for (x = 0; x < w; x++) { |
| 788 | for (y = 0; y < h; y++) { |
| 789 | len = 0; |
| 790 | while (y + len < h && m_videoFlags[(y + len) * w + x]) { |
| 791 | len++; |
| 792 | } |
| 793 | for (i = 0; i < len; i++) { |
| 794 | mx_v[(y + i) * w + x] = len - i; |
| 795 | } |
| 796 | y += len; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | *pmx_h = mx_h; |
| 801 | *pmx_v = mx_v; |
| 802 | } |
| 803 | |
| 804 | void |
| 805 | PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v) |
| 806 | { |
| 807 | delete[] mx_h; |
| 808 | delete[] mx_v; |
| 809 | } |
| 810 | |
| 811 | // NOTE: This function assumes that current tile has non-zero in mx_h[], |
| 812 | // otherwise we get division by zero. |
| 813 | void |
| 814 | PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[]) |
| 815 | { |
| 816 | int idx = r->tl.y * m_widthTiles + r->tl.x; |
| 817 | |
| 818 | // NOTE: Rectangle's maximum width and height are 25 and 18 |
| 819 | // (in tiles, where each tile is usually 32x32 pixels). |
| 820 | int max_w = mx_h[idx]; |
| 821 | if (max_w > 25) |
| 822 | max_w = 25; |
| 823 | int cur_h = 18; |
| 824 | |
| 825 | int best_w = max_w; |
| 826 | int best_area = 1 * best_w; |
| 827 | |
| 828 | for (int i = 0; i < max_w; i++) { |
| 829 | int h = mx_v[idx + i]; |
| 830 | if (h < cur_h) { |
| 831 | cur_h = h; |
| 832 | if (cur_h * max_w <= best_area) |
| 833 | break; |
| 834 | } |
| 835 | if (cur_h * (i + 1) > best_area) { |
| 836 | best_w = i + 1; |
| 837 | best_area = cur_h * best_w; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | r->br.x = r->tl.x + best_w; |
| 842 | r->br.y = r->tl.y + best_area / best_w; |
| 843 | } |
| 844 | |