Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004-2008 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 | */ |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 18 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 19 | // |
| 20 | // PollingManager.cxx |
| 21 | // |
| 22 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | #include <X11/Xlib.h> |
| 27 | #include <rfb/LogWriter.h> |
| 28 | #include <rfb/VNCServer.h> |
| 29 | #include <rfb/Configuration.h> |
| 30 | #include <rfb/ServerCore.h> |
| 31 | |
| 32 | #include <x0vncserver/PollingManager.h> |
| 33 | |
| 34 | static LogWriter vlog("PollingMgr"); |
| 35 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 36 | const int PollingManager::m_pollingOrder[32] = { |
| 37 | 0, 16, 8, 24, 4, 20, 12, 28, |
| 38 | 10, 26, 18, 2, 22, 6, 30, 14, |
| 39 | 1, 17, 9, 25, 7, 23, 15, 31, |
| 40 | 19, 3, 27, 11, 29, 13, 5, 21 |
| 41 | }; |
| 42 | |
| 43 | // |
| 44 | // Constructor. |
| 45 | // |
Constantin Kaplinsky | 8a85c49 | 2008-06-04 05:36:40 +0000 | [diff] [blame] | 46 | // Note that dpy and image should remain valid during the object |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 47 | // lifetime, while factory is used only in the constructor itself. |
| 48 | // |
| 49 | |
Constantin Kaplinsky | 8a85c49 | 2008-06-04 05:36:40 +0000 | [diff] [blame] | 50 | PollingManager::PollingManager(Display *dpy, const Image *image, |
Constantin Kaplinsky | 303433a | 2008-06-04 05:57:06 +0000 | [diff] [blame] | 51 | ImageFactory &factory, |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 52 | int offsetLeft, int offsetTop) |
Constantin Kaplinsky | ec45c48 | 2008-01-18 14:13:16 +0000 | [diff] [blame] | 53 | : m_dpy(dpy), |
Constantin Kaplinsky | 8a85c49 | 2008-06-04 05:36:40 +0000 | [diff] [blame] | 54 | m_image(image), |
| 55 | m_bytesPerPixel(image->xim->bits_per_pixel / 8), |
Constantin Kaplinsky | ec45c48 | 2008-01-18 14:13:16 +0000 | [diff] [blame] | 56 | m_offsetLeft(offsetLeft), |
| 57 | m_offsetTop(offsetTop), |
Constantin Kaplinsky | 8a85c49 | 2008-06-04 05:36:40 +0000 | [diff] [blame] | 58 | m_width(image->xim->width), |
| 59 | m_height(image->xim->height), |
| 60 | m_widthTiles((image->xim->width + 31) / 32), |
| 61 | m_heightTiles((image->xim->height + 31) / 32), |
| 62 | m_numTiles(((image->xim->width + 31) / 32) * |
| 63 | ((image->xim->height + 31) / 32)), |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 64 | m_pollingStep(0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 65 | { |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 66 | // Create additional images used in polling algorithm, warn if |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 67 | // underlying class names are different from the class name of the |
| 68 | // primary image. |
Constantin Kaplinsky | 303433a | 2008-06-04 05:57:06 +0000 | [diff] [blame] | 69 | m_rowImage = factory.newImage(m_dpy, m_width, 1); |
| 70 | m_columnImage = factory.newImage(m_dpy, 1, m_height); |
Constantin Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 71 | const char *primaryImgClass = m_image->className(); |
| 72 | const char *rowImgClass = m_rowImage->className(); |
| 73 | const char *columnImgClass = m_columnImage->className(); |
| 74 | if (strcmp(rowImgClass, primaryImgClass) != 0 || |
| 75 | strcmp(columnImgClass, primaryImgClass) != 0) { |
| 76 | vlog.error("Image types do not match (%s, %s, %s)", |
| 77 | primaryImgClass, rowImgClass, columnImgClass); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 80 | m_changeFlags = new bool[m_numTiles]; |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 81 | memset(m_changeFlags, 0, m_numTiles * sizeof(bool)); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | PollingManager::~PollingManager() |
| 85 | { |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 86 | delete[] m_changeFlags; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 87 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 88 | delete m_rowImage; |
Constantin Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 89 | delete m_columnImage; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 93 | // DEBUG: Measuring time spent in the poll() function, |
| 94 | // as well as time intervals between poll() calls. |
| 95 | // |
| 96 | |
| 97 | #ifdef DEBUG |
| 98 | void PollingManager::debugBeforePoll() |
| 99 | { |
| 100 | TimeMillis timeNow; |
| 101 | int diff = timeNow.diffFrom(m_timeSaved); |
| 102 | fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32); |
| 103 | m_timeSaved = timeNow; |
| 104 | } |
| 105 | |
| 106 | void PollingManager::debugAfterPoll() |
| 107 | { |
| 108 | TimeMillis timeNow; |
| 109 | int diff = timeNow.diffFrom(m_timeSaved); |
| 110 | fprintf(stderr, "[poll%4dms]\n", diff); |
| 111 | m_timeSaved = timeNow; |
| 112 | } |
| 113 | |
| 114 | #endif |
| 115 | |
| 116 | // |
| 117 | // Search for changed rectangles on the screen. |
| 118 | // |
| 119 | |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 120 | void PollingManager::poll(VNCServer *server) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 121 | { |
| 122 | #ifdef DEBUG |
| 123 | debugBeforePoll(); |
| 124 | #endif |
| 125 | |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 126 | // Perform polling and try update clients if changes were detected. |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 127 | if (pollScreen(server)) |
| 128 | server->tryUpdate(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 129 | |
| 130 | #ifdef DEBUG |
| 131 | debugAfterPoll(); |
| 132 | #endif |
| 133 | } |
| 134 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 135 | #ifdef DEBUG_REPORT_CHANGED_TILES |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 136 | #define DBG_REPORT_CHANGES(title) printChanges((title)) |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 137 | #else |
| 138 | #define DBG_REPORT_CHANGES(title) |
| 139 | #endif |
| 140 | |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 141 | bool PollingManager::pollScreen(VNCServer *server) |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 142 | { |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 143 | if (!server) |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 144 | return false; |
| 145 | |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 146 | // Clear the m_changeFlags[] array, indicating that no changes have |
| 147 | // been detected yet. |
| 148 | memset(m_changeFlags, 0, m_numTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 149 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 150 | // First pass over the framebuffer. Here we scan 1/32 part of the |
| 151 | // framebuffer -- that is, one line in each (32 * m_width) stripe. |
| 152 | // We compare the pixels of that line with previous framebuffer |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 153 | // contents and raise corresponding elements of m_changeFlags[]. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 154 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 155 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 156 | for (int y = scanOffset; y < m_height; y += 32) { |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 157 | nTilesChanged += checkRow(0, y, m_width); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 160 | DBG_REPORT_CHANGES("After 1st pass"); |
| 161 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 162 | // If some changes have been detected: |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 163 | if (nTilesChanged) { |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 164 | // Try to find more changes around. |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 165 | checkNeighbors(); |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 166 | DBG_REPORT_CHANGES("After checking neighbors"); |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 167 | // Inform the server about the changes. |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 168 | nTilesChanged = sendChanges(server); |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 169 | } |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 170 | |
Constantin Kaplinsky | 52f29d3 | 2007-12-28 18:30:54 +0000 | [diff] [blame] | 171 | #ifdef DEBUG_PRINT_NUM_CHANGED_TILES |
| 172 | printf("%3d ", nTilesChanged); |
| 173 | if (m_pollingStep % 32 == 0) { |
| 174 | printf("\n"); |
| 175 | } |
| 176 | #endif |
| 177 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 178 | #ifdef DEBUG |
| 179 | if (nTilesChanged != 0) { |
| 180 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 181 | } |
| 182 | #endif |
| 183 | |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 184 | return (nTilesChanged != 0); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 187 | int PollingManager::checkRow(int x, int y, int w) |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 188 | { |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 189 | // If necessary, expand the row to the left, to the tile border. |
| 190 | // In other words, x must be a multiple of 32. |
| 191 | if (x % 32 != 0) { |
| 192 | int correction = x % 32; |
| 193 | x -= correction; |
| 194 | w += correction; |
| 195 | } |
| 196 | |
Constantin Kaplinsky | 41de242 | 2008-01-19 09:03:47 +0000 | [diff] [blame] | 197 | // Read a row from the screen into m_rowImage. |
| 198 | getRow(x, y, w); |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 199 | |
Constantin Kaplinsky | 04aa520 | 2008-01-18 15:37:15 +0000 | [diff] [blame] | 200 | // Compute a pointer to the initial element of m_changeFlags. |
| 201 | bool *pChangeFlags = &m_changeFlags[getTileIndex(x, y)]; |
| 202 | |
| 203 | // Compute pointers to image data to be compared. |
Constantin Kaplinsky | 801123d | 2008-01-18 15:23:11 +0000 | [diff] [blame] | 204 | char *ptr_old = m_image->locatePixel(x, y); |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 205 | char *ptr_new = m_rowImage->xim->data; |
| 206 | |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 207 | // Compare pixels, raise corresponding elements of m_changeFlags[]. |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 208 | // First, handle full-size (32 pixels wide) tiles. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 209 | int nTilesChanged = 0; |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 210 | int nBytesPerTile = 32 * m_bytesPerPixel; |
| 211 | for (int i = 0; i < w / 32; i++) { |
| 212 | if (memcmp(ptr_old, ptr_new, nBytesPerTile)) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 213 | *pChangeFlags = true; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 214 | nTilesChanged++; |
| 215 | } |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 216 | pChangeFlags++; |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 217 | ptr_old += nBytesPerTile; |
| 218 | ptr_new += nBytesPerTile; |
| 219 | } |
| 220 | |
| 221 | // Handle the rightmost pixels, if the width is not a multiple of 32. |
| 222 | int nBytesLeft = (w % 32) * m_bytesPerPixel; |
| 223 | if (nBytesLeft != 0) { |
| 224 | if (memcmp(ptr_old, ptr_new, nBytesLeft)) { |
| 225 | *pChangeFlags = true; |
| 226 | nTilesChanged++; |
| 227 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return nTilesChanged; |
| 231 | } |
| 232 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 233 | int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags) |
| 234 | { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 235 | getColumn(x, y, h); |
| 236 | |
| 237 | int nTilesChanged = 0; |
| 238 | for (int nTile = 0; nTile < (h + 31) / 32; nTile++) { |
| 239 | if (!*pChangeFlags) { |
| 240 | int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32; |
| 241 | for (int i = 0; i < tile_h; i++) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 242 | // FIXME: Do not compute these pointers in the inner cycle. |
Constantin Kaplinsky | 801123d | 2008-01-18 15:23:11 +0000 | [diff] [blame] | 243 | char *ptr_old = m_image->locatePixel(x, y + nTile * 32 + i); |
| 244 | char *ptr_new = m_columnImage->locatePixel(0, nTile * 32 + i); |
Constantin Kaplinsky | ec45c48 | 2008-01-18 14:13:16 +0000 | [diff] [blame] | 245 | if (memcmp(ptr_old, ptr_new, m_bytesPerPixel)) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 246 | *pChangeFlags = true; |
| 247 | nTilesChanged++; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | pChangeFlags += m_widthTiles; |
| 253 | } |
| 254 | |
| 255 | return nTilesChanged; |
| 256 | } |
| 257 | |
Constantin Kaplinsky | 93d40aa | 2008-06-04 11:20:09 +0000 | [diff] [blame] | 258 | int PollingManager::sendChanges(VNCServer *server) const |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 259 | { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 260 | const bool *pChangeFlags = m_changeFlags; |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 261 | int nTilesChanged = 0; |
| 262 | |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 263 | Rect rect; |
| 264 | for (int y = 0; y < m_heightTiles; y++) { |
| 265 | for (int x = 0; x < m_widthTiles; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 266 | if (*pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 267 | // Count successive tiles marked as changed. |
| 268 | int count = 1; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 269 | while (x + count < m_widthTiles && *pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 270 | count++; |
| 271 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 272 | nTilesChanged += count; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 273 | // Compute the coordinates and the size of this band. |
| 274 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 275 | if (rect.br.x > m_width) |
| 276 | rect.br.x = m_width; |
| 277 | if (rect.br.y > m_height) |
| 278 | rect.br.y = m_height; |
| 279 | // Add to the changed region maintained by the server. |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 280 | server->add_changed(rect); |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 281 | // Skip processed tiles. |
| 282 | x += count; |
| 283 | } |
| 284 | } |
| 285 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 286 | return nTilesChanged; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 289 | void |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 290 | PollingManager::checkNeighbors() |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 291 | { |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 292 | int x, y; |
| 293 | |
| 294 | // Check neighboring pixels above and below changed tiles. |
| 295 | // FIXME: Fast skip to the first changed tile (and to the last, too). |
| 296 | // FIXME: Check the full-width line above the first changed tile? |
| 297 | for (y = 0; y < m_heightTiles; y++) { |
| 298 | bool doneAbove = false; |
| 299 | bool doneBelow = false; |
| 300 | for (x = 0; x < m_widthTiles; x++) { |
| 301 | if (!doneAbove && y > 0 && |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 302 | m_changeFlags[y * m_widthTiles + x] && |
| 303 | !m_changeFlags[(y - 1) * m_widthTiles + x]) { |
| 304 | // FIXME: Check m_changeFlags[] to decrease height of the row. |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 305 | checkRow(x * 32, y * 32 - 1, m_width - x * 32); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 306 | doneAbove = true; |
| 307 | } |
| 308 | if (!doneBelow && y < m_heightTiles - 1 && |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 309 | m_changeFlags[y * m_widthTiles + x] && |
| 310 | !m_changeFlags[(y + 1) * m_widthTiles + x]) { |
| 311 | // FIXME: Check m_changeFlags[] to decrease height of the row. |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 312 | checkRow(x * 32, (y + 1) * 32, m_width - x * 32); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 313 | doneBelow = true; |
| 314 | } |
| 315 | if (doneBelow && doneAbove) |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Check neighboring pixels at the right side of changed tiles. |
| 321 | for (x = 0; x < m_widthTiles - 1; x++) { |
| 322 | for (y = 0; y < m_heightTiles; y++) { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 323 | if (m_changeFlags[y * m_widthTiles + x] && |
| 324 | !m_changeFlags[y * m_widthTiles + x + 1]) { |
| 325 | // FIXME: Check m_changeFlags[] to decrease height of the column. |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 326 | checkColumn((x + 1) * 32, y * 32, m_height - y * 32, |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 327 | &m_changeFlags[y * m_widthTiles + x + 1]); |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 332 | |
| 333 | // Check neighboring pixels at the left side of changed tiles. |
| 334 | for (x = m_widthTiles - 1; x > 0; x--) { |
| 335 | for (y = 0; y < m_heightTiles; y++) { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 336 | if (m_changeFlags[y * m_widthTiles + x] && |
| 337 | !m_changeFlags[y * m_widthTiles + x - 1]) { |
| 338 | // FIXME: Check m_changeFlags[] to decrease height of the column. |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 339 | checkColumn(x * 32 - 1, y * 32, m_height - y * 32, |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 340 | &m_changeFlags[y * m_widthTiles + x - 1]); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | } |
| 344 | } |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 348 | PollingManager::printChanges(const char *header) const |
Constantin Kaplinsky | f50bd7f | 2008-01-10 15:27:42 +0000 | [diff] [blame] | 349 | { |
| 350 | fprintf(stderr, "%s:", header); |
| 351 | |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 352 | const bool *pChangeFlags = m_changeFlags; |
| 353 | |
Constantin Kaplinsky | f50bd7f | 2008-01-10 15:27:42 +0000 | [diff] [blame] | 354 | for (int y = 0; y < m_heightTiles; y++) { |
| 355 | for (int x = 0; x < m_widthTiles; x++) { |
| 356 | if (*pChangeFlags++) { |
| 357 | // Count successive tiles marked as changed. |
| 358 | int count = 1; |
| 359 | while (x + count < m_widthTiles && *pChangeFlags++) { |
| 360 | count++; |
| 361 | } |
| 362 | // Print. |
| 363 | fprintf(stderr, " (%d,%d)*%d", x, y, count); |
| 364 | // Skip processed tiles. |
| 365 | x += count; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | fprintf(stderr, "\n"); |
| 371 | } |
| 372 | |