Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004-2007 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 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 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
| 25 | #include <X11/Xlib.h> |
| 26 | #include <rfb/LogWriter.h> |
| 27 | #include <rfb/VNCServer.h> |
| 28 | #include <rfb/Configuration.h> |
| 29 | #include <rfb/ServerCore.h> |
| 30 | |
| 31 | #include <x0vncserver/PollingManager.h> |
| 32 | |
| 33 | static LogWriter vlog("PollingMgr"); |
| 34 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 35 | const int PollingManager::m_pollingOrder[32] = { |
| 36 | 0, 16, 8, 24, 4, 20, 12, 28, |
| 37 | 10, 26, 18, 2, 22, 6, 30, 14, |
| 38 | 1, 17, 9, 25, 7, 23, 15, 31, |
| 39 | 19, 3, 27, 11, 29, 13, 5, 21 |
| 40 | }; |
| 41 | |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 42 | // FIXME: Check that the parameter's value is in the allowed range. |
| 43 | // This applies to all other parameters as well. |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 44 | IntParameter PollingManager::m_videoPriority("VideoPriority", |
| 45 | "Priority of sending updates for video area (0..8)", 2); |
| 46 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 47 | // |
| 48 | // Constructor. |
| 49 | // |
| 50 | // Note that dpy and image should remain valid during the object |
| 51 | // lifetime, while factory is used only in the constructor itself. |
| 52 | // |
Constantin Kaplinsky | 936c369 | 2007-12-27 08:42:53 +0000 | [diff] [blame] | 53 | // FIXME: Pass XPixelBuffer* instead of Image*. |
| 54 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 55 | |
| 56 | PollingManager::PollingManager(Display *dpy, Image *image, |
| 57 | ImageFactory *factory, |
| 58 | int offsetLeft, int offsetTop) |
| 59 | : m_dpy(dpy), m_server(0), m_image(image), |
| 60 | m_offsetLeft(offsetLeft), m_offsetTop(offsetTop), |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 61 | m_numVideoPasses(0), |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 62 | m_pollingStep(0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 63 | { |
| 64 | // Save width and height of the screen (and the image). |
| 65 | m_width = m_image->xim->width; |
| 66 | m_height = m_image->xim->height; |
| 67 | |
| 68 | // Compute width and height in 32x32 tiles. |
| 69 | m_widthTiles = (m_width + 31) / 32; |
| 70 | m_heightTiles = (m_height + 31) / 32; |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 71 | m_numTiles = m_widthTiles * m_heightTiles; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 72 | |
| 73 | // Get initial screen image. |
| 74 | m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop); |
| 75 | |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 76 | // Create additional images used in polling algorithm, warn if |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 77 | // underlying class names are different from the class name of the |
| 78 | // primary image. |
| 79 | m_rowImage = factory->newImage(m_dpy, m_width, 1); |
Constantin Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 80 | m_columnImage = factory->newImage(m_dpy, 1, m_height); |
| 81 | const char *primaryImgClass = m_image->className(); |
| 82 | const char *rowImgClass = m_rowImage->className(); |
| 83 | const char *columnImgClass = m_columnImage->className(); |
| 84 | if (strcmp(rowImgClass, primaryImgClass) != 0 || |
| 85 | strcmp(columnImgClass, primaryImgClass) != 0) { |
| 86 | vlog.error("Image types do not match (%s, %s, %s)", |
| 87 | primaryImgClass, rowImgClass, columnImgClass); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 90 | m_rateMatrix = new char[m_numTiles]; |
| 91 | m_videoFlags = new char[m_numTiles]; |
| 92 | memset(m_rateMatrix, 0, m_numTiles); |
| 93 | memset(m_videoFlags, 0, m_numTiles); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | PollingManager::~PollingManager() |
| 97 | { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 98 | delete[] m_videoFlags; |
| 99 | delete[] m_rateMatrix; |
| 100 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 101 | delete m_rowImage; |
Constantin Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 102 | delete m_columnImage; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // |
| 106 | // Register VNCServer object. |
| 107 | // |
| 108 | |
| 109 | void PollingManager::setVNCServer(VNCServer *s) |
| 110 | { |
| 111 | m_server = s; |
| 112 | } |
| 113 | |
| 114 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 115 | // DEBUG: Measuring time spent in the poll() function, |
| 116 | // as well as time intervals between poll() calls. |
| 117 | // |
| 118 | |
| 119 | #ifdef DEBUG |
| 120 | void PollingManager::debugBeforePoll() |
| 121 | { |
| 122 | TimeMillis timeNow; |
| 123 | int diff = timeNow.diffFrom(m_timeSaved); |
| 124 | fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32); |
| 125 | m_timeSaved = timeNow; |
| 126 | } |
| 127 | |
| 128 | void PollingManager::debugAfterPoll() |
| 129 | { |
| 130 | TimeMillis timeNow; |
| 131 | int diff = timeNow.diffFrom(m_timeSaved); |
| 132 | fprintf(stderr, "[poll%4dms]\n", diff); |
| 133 | m_timeSaved = timeNow; |
| 134 | } |
| 135 | |
| 136 | #endif |
| 137 | |
| 138 | // |
| 139 | // Search for changed rectangles on the screen. |
| 140 | // |
| 141 | |
| 142 | void PollingManager::poll() |
| 143 | { |
| 144 | #ifdef DEBUG |
| 145 | debugBeforePoll(); |
| 146 | #endif |
| 147 | |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 148 | // Perform polling and try update clients if changes were detected. |
| 149 | if (pollScreen()) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 150 | m_server->tryUpdate(); |
| 151 | |
| 152 | #ifdef DEBUG |
| 153 | debugAfterPoll(); |
| 154 | #endif |
| 155 | } |
| 156 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 157 | #ifdef DEBUG_REPORT_CHANGED_TILES |
| 158 | #define DBG_REPORT_CHANGES(title) printChanges((title), changeFlags) |
| 159 | #else |
| 160 | #define DBG_REPORT_CHANGES(title) |
| 161 | #endif |
| 162 | |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 163 | bool PollingManager::pollScreen() |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 164 | { |
| 165 | if (!m_server) |
| 166 | return false; |
| 167 | |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 168 | // If video data should have higher priority, and video area was |
| 169 | // detected, perform special passes to send video data only. Such |
| 170 | // "video passes" will be performed between normal polling passes. |
| 171 | // No actual polling is performed in a video pass since we know that |
| 172 | // video is changing continuously. |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 173 | // |
| 174 | // FIXME: Should we move this block into a separate function? |
| 175 | // FIXME: Giving higher priority to video area lengthens video |
| 176 | // detection cycles. Should we do something with that? |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 177 | if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) { |
| 178 | if (m_numVideoPasses > 0) { |
| 179 | m_numVideoPasses--; |
| 180 | getScreenRect(m_videoRect); |
| 181 | return true; // we've got changes |
| 182 | } else { |
| 183 | // Normal pass now, but schedule video passes for next calls. |
| 184 | m_numVideoPasses = (int)m_videoPriority - 1; |
| 185 | } |
| 186 | } |
| 187 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 188 | // changeFlags[] array will hold boolean values corresponding to |
| 189 | // each 32x32 tile. If a value is true, then we've detected a change |
| 190 | // in that tile. Initially, we fill in the array with zero values. |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 191 | // |
| 192 | // FIXME: Should we use a member variable in place of changeFlags? |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 193 | bool *changeFlags = new bool[m_numTiles]; |
| 194 | memset(changeFlags, 0, m_numTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 195 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 196 | // First pass over the framebuffer. Here we scan 1/32 part of the |
| 197 | // framebuffer -- that is, one line in each (32 * m_width) stripe. |
| 198 | // We compare the pixels of that line with previous framebuffer |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 199 | // contents and raise corresponding member values of changeFlags[]. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 200 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 201 | bool *pChangeFlags = changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 202 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 203 | for (int y = scanOffset; y < m_height; y += 32) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 204 | nTilesChanged += checkRow(0, y, m_width, pChangeFlags); |
| 205 | pChangeFlags += m_widthTiles; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 208 | // Do the work related to video area detection, if enabled. |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 209 | bool haveVideoRect = false; |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 210 | if ((int)m_videoPriority != 0) { |
| 211 | handleVideo(changeFlags); |
| 212 | if (!m_videoRect.is_empty()) { |
| 213 | getScreenRect(m_videoRect); |
| 214 | haveVideoRect = true; |
| 215 | } |
| 216 | } |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 217 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 218 | DBG_REPORT_CHANGES("After 1st pass"); |
| 219 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 220 | // If some changes have been detected: |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 221 | if (nTilesChanged) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 222 | // Try to find more changes around. Before doing that, mark the |
| 223 | // video area as changed, to skip comparisons of its pixels. |
| 224 | flagVideoArea(changeFlags, true); |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 225 | DBG_REPORT_CHANGES("Before checking neighbors"); |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 226 | checkNeighbors(changeFlags); |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 227 | DBG_REPORT_CHANGES("After checking neighbors"); |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 228 | |
| 229 | // Inform the server about the changes. This time, we mark the |
| 230 | // video area as NOT changed, to prevent reading its pixels again. |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 231 | flagVideoArea(changeFlags, false); |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 232 | DBG_REPORT_CHANGES("Before sending"); |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 233 | nTilesChanged = sendChanges(changeFlags); |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 234 | } |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 235 | |
| 236 | // Cleanup. |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 237 | delete[] changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 238 | |
Constantin Kaplinsky | 52f29d3 | 2007-12-28 18:30:54 +0000 | [diff] [blame] | 239 | #ifdef DEBUG_PRINT_NUM_CHANGED_TILES |
| 240 | printf("%3d ", nTilesChanged); |
| 241 | if (m_pollingStep % 32 == 0) { |
| 242 | printf("\n"); |
| 243 | } |
| 244 | #endif |
| 245 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 246 | #ifdef DEBUG |
| 247 | if (nTilesChanged != 0) { |
| 248 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 249 | } |
| 250 | #endif |
| 251 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 252 | return (nTilesChanged != 0 || haveVideoRect); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 255 | int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags) |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 256 | { |
| 257 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 258 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 259 | |
Constantin Kaplinsky | 29d3205 | 2007-10-08 14:16:02 +0000 | [diff] [blame] | 260 | if (x == 0 && w == m_width) { |
| 261 | getFullRow(y); // use more efficient method if possible |
| 262 | } else { |
| 263 | getRow(x, y, w); |
| 264 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 265 | |
| 266 | char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel; |
| 267 | char *ptr_new = m_rowImage->xim->data; |
| 268 | |
| 269 | int nTilesChanged = 0; |
| 270 | for (int i = 0; i < (w + 31) / 32; i++) { |
| 271 | int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32; |
| 272 | int nBytes = tile_w * bytesPerPixel; |
| 273 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 274 | *pChangeFlags = true; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 275 | nTilesChanged++; |
| 276 | } |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 277 | pChangeFlags++; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 278 | ptr_old += nBytes; |
| 279 | ptr_new += nBytes; |
| 280 | } |
| 281 | |
| 282 | return nTilesChanged; |
| 283 | } |
| 284 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 285 | int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags) |
| 286 | { |
| 287 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 288 | |
| 289 | getColumn(x, y, h); |
| 290 | |
| 291 | int nTilesChanged = 0; |
| 292 | for (int nTile = 0; nTile < (h + 31) / 32; nTile++) { |
| 293 | if (!*pChangeFlags) { |
| 294 | int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32; |
| 295 | for (int i = 0; i < tile_h; i++) { |
| 296 | // FIXME: Provide an inline function Image::locatePixel(x, y). |
| 297 | // FIXME: Do not compute these pointers in the inner cycle. |
| 298 | char *ptr_old = (m_image->xim->data + |
| 299 | (y + nTile * 32 + i) * m_image->xim->bytes_per_line + |
| 300 | x * bytesPerPixel); |
| 301 | char *ptr_new = (m_columnImage->xim->data + |
| 302 | (nTile * 32 + i) * m_columnImage->xim->bytes_per_line); |
| 303 | if (memcmp(ptr_old, ptr_new, bytesPerPixel)) { |
| 304 | *pChangeFlags = true; |
| 305 | nTilesChanged++; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | pChangeFlags += m_widthTiles; |
| 311 | } |
| 312 | |
| 313 | return nTilesChanged; |
| 314 | } |
| 315 | |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 316 | int PollingManager::sendChanges(const bool *pChangeFlags) |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 317 | { |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 318 | int nTilesChanged = 0; |
| 319 | |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 320 | Rect rect; |
| 321 | for (int y = 0; y < m_heightTiles; y++) { |
| 322 | for (int x = 0; x < m_widthTiles; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 323 | if (*pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 324 | // Count successive tiles marked as changed. |
| 325 | int count = 1; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 326 | while (x + count < m_widthTiles && *pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 327 | count++; |
| 328 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 329 | nTilesChanged += count; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 330 | // Compute the coordinates and the size of this band. |
| 331 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 332 | if (rect.br.x > m_width) |
| 333 | rect.br.x = m_width; |
| 334 | if (rect.br.y > m_height) |
| 335 | rect.br.y = m_height; |
| 336 | // Add to the changed region maintained by the server. |
| 337 | getScreenRect(rect); |
| 338 | m_server->add_changed(rect); |
| 339 | // Skip processed tiles. |
| 340 | x += count; |
| 341 | } |
| 342 | } |
| 343 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 344 | return nTilesChanged; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 347 | void PollingManager::handleVideo(const bool *pChangeFlags) |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 348 | { |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 349 | // Update counters in m_rateMatrix. |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 350 | for (int i = 0; i < m_numTiles; i++) |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 351 | m_rateMatrix[i] += (pChangeFlags[i] != false); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 352 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 353 | // Once per eight calls: detect video rectangle by examining |
| 354 | // m_rateMatrix[], then reset counters in m_rateMatrix[]. |
| 355 | if (m_pollingStep % 8 == 0) { |
| 356 | detectVideo(); |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 357 | memset(m_rateMatrix, 0, m_numTiles); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 358 | } |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Constantin Kaplinsky | bd39035 | 2007-12-28 08:44:59 +0000 | [diff] [blame] | 361 | void PollingManager::flagVideoArea(bool *pChangeFlags, bool value) |
| 362 | { |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 363 | if (m_videoRect.is_empty()) |
| 364 | return; |
| 365 | |
Constantin Kaplinsky | bd39035 | 2007-12-28 08:44:59 +0000 | [diff] [blame] | 366 | Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32, |
| 367 | m_videoRect.br.x / 32, m_videoRect.br.y / 32); |
| 368 | |
| 369 | for (int y = r.tl.y; y < r.br.y; y++) |
| 370 | for (int x = r.tl.x; x < r.br.x; x++) |
| 371 | pChangeFlags[y * m_widthTiles + x] = value; |
| 372 | } |
| 373 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 374 | void |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 375 | PollingManager::checkNeighbors(bool *pChangeFlags) |
| 376 | { |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 377 | int x, y; |
| 378 | |
| 379 | // Check neighboring pixels above and below changed tiles. |
| 380 | // FIXME: Fast skip to the first changed tile (and to the last, too). |
| 381 | // FIXME: Check the full-width line above the first changed tile? |
| 382 | for (y = 0; y < m_heightTiles; y++) { |
| 383 | bool doneAbove = false; |
| 384 | bool doneBelow = false; |
| 385 | for (x = 0; x < m_widthTiles; x++) { |
| 386 | if (!doneAbove && y > 0 && |
| 387 | pChangeFlags[y * m_widthTiles + x] && |
| 388 | !pChangeFlags[(y - 1) * m_widthTiles + x]) { |
| 389 | // FIXME: Check pChangeFlags[] to decrease height of the row. |
| 390 | checkRow(x * 32, y * 32 - 1, m_width - x * 32, |
| 391 | &pChangeFlags[(y - 1) * m_widthTiles + x]); |
| 392 | doneAbove = true; |
| 393 | } |
| 394 | if (!doneBelow && y < m_heightTiles - 1 && |
| 395 | pChangeFlags[y * m_widthTiles + x] && |
| 396 | !pChangeFlags[(y + 1) * m_widthTiles + x]) { |
| 397 | // FIXME: Check pChangeFlags[] to decrease height of the row. |
| 398 | checkRow(x * 32, (y + 1) * 32, m_width - x * 32, |
| 399 | &pChangeFlags[(y + 1) * m_widthTiles + x]); |
| 400 | doneBelow = true; |
| 401 | } |
| 402 | if (doneBelow && doneAbove) |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Check neighboring pixels at the right side of changed tiles. |
| 408 | for (x = 0; x < m_widthTiles - 1; x++) { |
| 409 | for (y = 0; y < m_heightTiles; y++) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 410 | if (pChangeFlags[y * m_widthTiles + x] && |
| 411 | !pChangeFlags[y * m_widthTiles + x + 1]) { |
| 412 | // FIXME: Check pChangeFlags[] to decrease height of the column. |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 413 | checkColumn((x + 1) * 32, y * 32, m_height - y * 32, |
| 414 | &pChangeFlags[y * m_widthTiles + x + 1]); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 419 | |
| 420 | // Check neighboring pixels at the left side of changed tiles. |
| 421 | for (x = m_widthTiles - 1; x > 0; x--) { |
| 422 | for (y = 0; y < m_heightTiles; y++) { |
| 423 | if (pChangeFlags[y * m_widthTiles + x] && |
| 424 | !pChangeFlags[y * m_widthTiles + x - 1]) { |
| 425 | // FIXME: Check pChangeFlags[] to decrease height of the column. |
| 426 | checkColumn(x * 32 - 1, y * 32, m_height - y * 32, |
| 427 | &pChangeFlags[y * m_widthTiles + x - 1]); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void |
Constantin Kaplinsky | f50bd7f | 2008-01-10 15:27:42 +0000 | [diff] [blame] | 435 | PollingManager::printChanges(const char *header, const bool *pChangeFlags) |
| 436 | { |
| 437 | fprintf(stderr, "%s:", header); |
| 438 | |
| 439 | for (int y = 0; y < m_heightTiles; y++) { |
| 440 | for (int x = 0; x < m_widthTiles; x++) { |
| 441 | if (*pChangeFlags++) { |
| 442 | // Count successive tiles marked as changed. |
| 443 | int count = 1; |
| 444 | while (x + count < m_widthTiles && *pChangeFlags++) { |
| 445 | count++; |
| 446 | } |
| 447 | // Print. |
| 448 | fprintf(stderr, " (%d,%d)*%d", x, y, count); |
| 449 | // Skip processed tiles. |
| 450 | x += count; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | fprintf(stderr, "\n"); |
| 456 | } |
| 457 | |
| 458 | void |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 459 | PollingManager::detectVideo() |
| 460 | { |
| 461 | // Configurable parameters. |
| 462 | const int VIDEO_THRESHOLD_0 = 3; |
| 463 | const int VIDEO_THRESHOLD_1 = 5; |
| 464 | |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 465 | // In m_rateMatrix, clear counters corresponding to non-32x32 tiles. |
| 466 | // This will guarantee that the size of the video area is always a |
| 467 | // multiple of 32 pixels. This is important for hardware JPEG encoders. |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 468 | if (m_width % 32 != 0) { |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 469 | for (int n = m_widthTiles - 1; n < m_numTiles; n += m_widthTiles) |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 470 | m_rateMatrix[n] = 0; |
| 471 | } |
| 472 | if (m_height % 32 != 0) { |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 473 | for (int n = m_numTiles - m_widthTiles; n < m_numTiles; n++) |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 474 | m_rateMatrix[n] = 0; |
| 475 | } |
| 476 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 477 | // First, detect candidate region that looks like video. In other |
| 478 | // words, find a region that consists of continuously changing |
| 479 | // pixels. Save the result in m_videoFlags[]. |
Constantin Kaplinsky | 20390a2 | 2008-01-17 15:17:36 +0000 | [diff] [blame^] | 480 | for (int i = 0; i < m_numTiles; i++) { |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 481 | if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) { |
| 482 | m_videoFlags[i] = 0; |
| 483 | } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) { |
| 484 | m_videoFlags[i] = 1; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Now, choose the biggest rectangle from that candidate region. |
| 489 | Rect newRect; |
| 490 | getVideoAreaRect(&newRect); |
| 491 | |
| 492 | // Does new rectangle differ from the previously detected one? |
| 493 | // If it does, save new rectangle and inform the server. |
| 494 | if (!newRect.equals(m_videoRect)) { |
| 495 | if (newRect.is_empty()) { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 496 | vlog.debug("No video detected"); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 497 | } else { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 498 | vlog.debug("Detected video %dx%d at (%d,%d)", |
| 499 | newRect.width(), newRect.height(), |
| 500 | newRect.tl.x, newRect.tl.y); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 501 | } |
| 502 | m_videoRect = newRect; |
| 503 | m_server->set_video_area(newRect); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 508 | PollingManager::getVideoAreaRect(Rect *result) |
| 509 | { |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 510 | int *mx_hlen, *mx_vlen; |
| 511 | constructLengthMatrices(&mx_hlen, &mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 512 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 513 | int full_h = m_heightTiles; |
| 514 | int full_w = m_widthTiles; |
| 515 | int x, y; |
| 516 | Rect max_rect(0, 0, 0, 0); |
| 517 | Rect local_rect; |
| 518 | |
| 519 | for (y = 0; y < full_h; y++) { |
| 520 | for (x = 0; x < full_w; x++) { |
| 521 | int max_w = mx_hlen[y * full_w + x]; |
| 522 | int max_h = mx_vlen[y * full_w + x]; |
| 523 | if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) { |
| 524 | local_rect.tl.x = x; |
| 525 | local_rect.tl.y = y; |
| 526 | findMaxLocalRect(&local_rect, mx_hlen, mx_vlen); |
| 527 | if (local_rect.area() > max_rect.area()) { |
| 528 | max_rect = local_rect; |
| 529 | } |
| 530 | } |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 534 | destroyLengthMatrices(mx_hlen, mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 535 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 536 | max_rect.tl.x *= 32; |
| 537 | max_rect.tl.y *= 32; |
| 538 | max_rect.br.x *= 32; |
| 539 | max_rect.br.y *= 32; |
| 540 | if (max_rect.br.x > m_width) |
| 541 | max_rect.br.x = m_width; |
| 542 | if (max_rect.br.y > m_height) |
| 543 | max_rect.br.y = m_height; |
| 544 | *result = max_rect; |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 545 | } |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 546 | |
| 547 | void |
| 548 | PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v) |
| 549 | { |
| 550 | // Handy shortcuts. |
| 551 | int h = m_heightTiles; |
| 552 | int w = m_widthTiles; |
| 553 | |
| 554 | // Allocate memory. |
| 555 | int *mx_h = new int[h * w]; |
| 556 | memset(mx_h, 0, h * w * sizeof(int)); |
| 557 | int *mx_v = new int[h * w]; |
| 558 | memset(mx_v, 0, h * w * sizeof(int)); |
| 559 | |
| 560 | int x, y, len, i; |
| 561 | |
| 562 | // Fill in horizontal length matrix. |
| 563 | for (y = 0; y < h; y++) { |
| 564 | for (x = 0; x < w; x++) { |
| 565 | len = 0; |
| 566 | while (x + len < w && m_videoFlags[y * w + x + len]) { |
| 567 | len++; |
| 568 | } |
| 569 | for (i = 0; i < len; i++) { |
| 570 | mx_h[y * w + x + i] = len - i; |
| 571 | } |
| 572 | x += len; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // Fill in vertical length matrix. |
| 577 | for (x = 0; x < w; x++) { |
| 578 | for (y = 0; y < h; y++) { |
| 579 | len = 0; |
| 580 | while (y + len < h && m_videoFlags[(y + len) * w + x]) { |
| 581 | len++; |
| 582 | } |
| 583 | for (i = 0; i < len; i++) { |
| 584 | mx_v[(y + i) * w + x] = len - i; |
| 585 | } |
| 586 | y += len; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | *pmx_h = mx_h; |
| 591 | *pmx_v = mx_v; |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v) |
| 596 | { |
| 597 | delete[] mx_h; |
| 598 | delete[] mx_v; |
| 599 | } |
| 600 | |
| 601 | // NOTE: This function assumes that current tile has non-zero in mx_h[], |
| 602 | // otherwise we get division by zero. |
| 603 | void |
| 604 | PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[]) |
| 605 | { |
| 606 | int idx = r->tl.y * m_widthTiles + r->tl.x; |
| 607 | |
| 608 | // NOTE: Rectangle's maximum width and height are 25 and 18 |
| 609 | // (in tiles, where each tile is usually 32x32 pixels). |
| 610 | int max_w = mx_h[idx]; |
| 611 | if (max_w > 25) |
| 612 | max_w = 25; |
| 613 | int cur_h = 18; |
| 614 | |
| 615 | int best_w = max_w; |
| 616 | int best_area = 1 * best_w; |
| 617 | |
| 618 | for (int i = 0; i < max_w; i++) { |
| 619 | int h = mx_v[idx + i]; |
| 620 | if (h < cur_h) { |
| 621 | cur_h = h; |
| 622 | if (cur_h * max_w <= best_area) |
| 623 | break; |
| 624 | } |
| 625 | if (cur_h * (i + 1) > best_area) { |
| 626 | best_w = i + 1; |
| 627 | best_area = cur_h * best_w; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | r->br.x = r->tl.x + best_w; |
| 632 | r->br.y = r->tl.y + best_area / best_w; |
| 633 | } |
| 634 | |