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