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 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 126 | pollScreen(server); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 127 | |
| 128 | #ifdef DEBUG |
| 129 | debugAfterPoll(); |
| 130 | #endif |
| 131 | } |
| 132 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 133 | #ifdef DEBUG_REPORT_CHANGED_TILES |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 134 | #define DBG_REPORT_CHANGES(title) printChanges((title)) |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 135 | #else |
| 136 | #define DBG_REPORT_CHANGES(title) |
| 137 | #endif |
| 138 | |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 139 | bool PollingManager::pollScreen(VNCServer *server) |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 140 | { |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 141 | if (!server) |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 142 | return false; |
| 143 | |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 144 | // Clear the m_changeFlags[] array, indicating that no changes have |
| 145 | // been detected yet. |
| 146 | memset(m_changeFlags, 0, m_numTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 147 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 148 | // First pass over the framebuffer. Here we scan 1/32 part of the |
| 149 | // framebuffer -- that is, one line in each (32 * m_width) stripe. |
| 150 | // We compare the pixels of that line with previous framebuffer |
Constantin Kaplinsky | 85b5eb9 | 2008-01-17 17:41:48 +0000 | [diff] [blame] | 151 | // contents and raise corresponding elements of m_changeFlags[]. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 152 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 153 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 154 | for (int y = scanOffset; y < m_height; y += 32) { |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 155 | nTilesChanged += checkRow(0, y, m_width); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 158 | DBG_REPORT_CHANGES("After 1st pass"); |
| 159 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 160 | // If some changes have been detected: |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 161 | if (nTilesChanged) { |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 162 | // Try to find more changes around. |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 163 | checkNeighbors(); |
Constantin Kaplinsky | bf11a2d | 2008-01-10 15:59:03 +0000 | [diff] [blame] | 164 | DBG_REPORT_CHANGES("After checking neighbors"); |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 165 | // Inform the server about the changes. |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 166 | nTilesChanged = sendChanges(server); |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 167 | } |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 168 | |
Constantin Kaplinsky | 52f29d3 | 2007-12-28 18:30:54 +0000 | [diff] [blame] | 169 | #ifdef DEBUG_PRINT_NUM_CHANGED_TILES |
| 170 | printf("%3d ", nTilesChanged); |
| 171 | if (m_pollingStep % 32 == 0) { |
| 172 | printf("\n"); |
| 173 | } |
| 174 | #endif |
| 175 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 176 | #ifdef DEBUG |
| 177 | if (nTilesChanged != 0) { |
| 178 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 179 | } |
| 180 | #endif |
| 181 | |
Constantin Kaplinsky | 82f7b01 | 2008-05-30 10:03:30 +0000 | [diff] [blame] | 182 | return (nTilesChanged != 0); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 185 | int PollingManager::checkRow(int x, int y, int w) |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 186 | { |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 187 | // If necessary, expand the row to the left, to the tile border. |
| 188 | // In other words, x must be a multiple of 32. |
| 189 | if (x % 32 != 0) { |
| 190 | int correction = x % 32; |
| 191 | x -= correction; |
| 192 | w += correction; |
| 193 | } |
| 194 | |
Constantin Kaplinsky | 41de242 | 2008-01-19 09:03:47 +0000 | [diff] [blame] | 195 | // Read a row from the screen into m_rowImage. |
| 196 | getRow(x, y, w); |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 197 | |
Constantin Kaplinsky | 04aa520 | 2008-01-18 15:37:15 +0000 | [diff] [blame] | 198 | // Compute a pointer to the initial element of m_changeFlags. |
| 199 | bool *pChangeFlags = &m_changeFlags[getTileIndex(x, y)]; |
| 200 | |
| 201 | // Compute pointers to image data to be compared. |
Constantin Kaplinsky | 801123d | 2008-01-18 15:23:11 +0000 | [diff] [blame] | 202 | char *ptr_old = m_image->locatePixel(x, y); |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 203 | char *ptr_new = m_rowImage->xim->data; |
| 204 | |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 205 | // Compare pixels, raise corresponding elements of m_changeFlags[]. |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 206 | // First, handle full-size (32 pixels wide) tiles. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 207 | int nTilesChanged = 0; |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 208 | int nBytesPerTile = 32 * m_bytesPerPixel; |
| 209 | for (int i = 0; i < w / 32; i++) { |
| 210 | if (memcmp(ptr_old, ptr_new, nBytesPerTile)) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 211 | *pChangeFlags = true; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 212 | nTilesChanged++; |
| 213 | } |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 214 | pChangeFlags++; |
Constantin Kaplinsky | 8650688 | 2008-01-21 10:24:25 +0000 | [diff] [blame] | 215 | ptr_old += nBytesPerTile; |
| 216 | ptr_new += nBytesPerTile; |
| 217 | } |
| 218 | |
| 219 | // Handle the rightmost pixels, if the width is not a multiple of 32. |
| 220 | int nBytesLeft = (w % 32) * m_bytesPerPixel; |
| 221 | if (nBytesLeft != 0) { |
| 222 | if (memcmp(ptr_old, ptr_new, nBytesLeft)) { |
| 223 | *pChangeFlags = true; |
| 224 | nTilesChanged++; |
| 225 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return nTilesChanged; |
| 229 | } |
| 230 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 231 | int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags) |
| 232 | { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 233 | getColumn(x, y, h); |
| 234 | |
| 235 | int nTilesChanged = 0; |
| 236 | for (int nTile = 0; nTile < (h + 31) / 32; nTile++) { |
| 237 | if (!*pChangeFlags) { |
| 238 | int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32; |
| 239 | for (int i = 0; i < tile_h; i++) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 240 | // FIXME: Do not compute these pointers in the inner cycle. |
Constantin Kaplinsky | 801123d | 2008-01-18 15:23:11 +0000 | [diff] [blame] | 241 | char *ptr_old = m_image->locatePixel(x, y + nTile * 32 + i); |
| 242 | char *ptr_new = m_columnImage->locatePixel(0, nTile * 32 + i); |
Constantin Kaplinsky | ec45c48 | 2008-01-18 14:13:16 +0000 | [diff] [blame] | 243 | if (memcmp(ptr_old, ptr_new, m_bytesPerPixel)) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 244 | *pChangeFlags = true; |
| 245 | nTilesChanged++; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | pChangeFlags += m_widthTiles; |
| 251 | } |
| 252 | |
| 253 | return nTilesChanged; |
| 254 | } |
| 255 | |
Constantin Kaplinsky | 93d40aa | 2008-06-04 11:20:09 +0000 | [diff] [blame] | 256 | int PollingManager::sendChanges(VNCServer *server) const |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 257 | { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 258 | const bool *pChangeFlags = m_changeFlags; |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 259 | int nTilesChanged = 0; |
| 260 | |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 261 | Rect rect; |
| 262 | for (int y = 0; y < m_heightTiles; y++) { |
| 263 | for (int x = 0; x < m_widthTiles; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 264 | if (*pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 265 | // Count successive tiles marked as changed. |
| 266 | int count = 1; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 267 | while (x + count < m_widthTiles && *pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 268 | count++; |
| 269 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 270 | nTilesChanged += count; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 271 | // Compute the coordinates and the size of this band. |
| 272 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 273 | if (rect.br.x > m_width) |
| 274 | rect.br.x = m_width; |
| 275 | if (rect.br.y > m_height) |
| 276 | rect.br.y = m_height; |
| 277 | // Add to the changed region maintained by the server. |
Constantin Kaplinsky | 54cfef3 | 2008-06-03 07:04:17 +0000 | [diff] [blame] | 278 | server->add_changed(rect); |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 279 | // Skip processed tiles. |
| 280 | x += count; |
| 281 | } |
| 282 | } |
| 283 | } |
Constantin Kaplinsky | 1a03211 | 2008-01-09 10:22:42 +0000 | [diff] [blame] | 284 | return nTilesChanged; |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 287 | void |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 288 | PollingManager::checkNeighbors() |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 289 | { |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 290 | int x, y; |
| 291 | |
| 292 | // Check neighboring pixels above and below changed tiles. |
| 293 | // FIXME: Fast skip to the first changed tile (and to the last, too). |
| 294 | // FIXME: Check the full-width line above the first changed tile? |
| 295 | for (y = 0; y < m_heightTiles; y++) { |
| 296 | bool doneAbove = false; |
| 297 | bool doneBelow = false; |
| 298 | for (x = 0; x < m_widthTiles; x++) { |
| 299 | if (!doneAbove && y > 0 && |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 300 | m_changeFlags[y * m_widthTiles + x] && |
| 301 | !m_changeFlags[(y - 1) * m_widthTiles + x]) { |
| 302 | // FIXME: Check m_changeFlags[] to decrease height of the row. |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 303 | checkRow(x * 32, y * 32 - 1, m_width - x * 32); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 304 | doneAbove = true; |
| 305 | } |
| 306 | if (!doneBelow && y < m_heightTiles - 1 && |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 307 | m_changeFlags[y * m_widthTiles + x] && |
| 308 | !m_changeFlags[(y + 1) * m_widthTiles + x]) { |
| 309 | // FIXME: Check m_changeFlags[] to decrease height of the row. |
Constantin Kaplinsky | 9d37e5c | 2008-01-18 11:17:26 +0000 | [diff] [blame] | 310 | checkRow(x * 32, (y + 1) * 32, m_width - x * 32); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 311 | doneBelow = true; |
| 312 | } |
| 313 | if (doneBelow && doneAbove) |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Check neighboring pixels at the right side of changed tiles. |
| 319 | for (x = 0; x < m_widthTiles - 1; x++) { |
| 320 | for (y = 0; y < m_heightTiles; y++) { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 321 | if (m_changeFlags[y * m_widthTiles + x] && |
| 322 | !m_changeFlags[y * m_widthTiles + x + 1]) { |
| 323 | // FIXME: Check m_changeFlags[] to decrease height of the column. |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 324 | checkColumn((x + 1) * 32, y * 32, m_height - y * 32, |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 325 | &m_changeFlags[y * m_widthTiles + x + 1]); |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
| 329 | } |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 330 | |
| 331 | // Check neighboring pixels at the left side of changed tiles. |
| 332 | for (x = m_widthTiles - 1; x > 0; x--) { |
| 333 | for (y = 0; y < m_heightTiles; y++) { |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 334 | if (m_changeFlags[y * m_widthTiles + x] && |
| 335 | !m_changeFlags[y * m_widthTiles + x - 1]) { |
| 336 | // FIXME: Check m_changeFlags[] to decrease height of the column. |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 337 | checkColumn(x * 32 - 1, y * 32, m_height - y * 32, |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 338 | &m_changeFlags[y * m_widthTiles + x - 1]); |
Constantin Kaplinsky | 474b12f | 2008-01-09 10:25:34 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 346 | PollingManager::printChanges(const char *header) const |
Constantin Kaplinsky | f50bd7f | 2008-01-10 15:27:42 +0000 | [diff] [blame] | 347 | { |
| 348 | fprintf(stderr, "%s:", header); |
| 349 | |
Constantin Kaplinsky | 850de2b | 2008-01-17 19:14:37 +0000 | [diff] [blame] | 350 | const bool *pChangeFlags = m_changeFlags; |
| 351 | |
Constantin Kaplinsky | f50bd7f | 2008-01-10 15:27:42 +0000 | [diff] [blame] | 352 | for (int y = 0; y < m_heightTiles; y++) { |
| 353 | for (int x = 0; x < m_widthTiles; x++) { |
| 354 | if (*pChangeFlags++) { |
| 355 | // Count successive tiles marked as changed. |
| 356 | int count = 1; |
| 357 | while (x + count < m_widthTiles && *pChangeFlags++) { |
| 358 | count++; |
| 359 | } |
| 360 | // Print. |
| 361 | fprintf(stderr, " (%d,%d)*%d", x, y, count); |
| 362 | // Skip processed tiles. |
| 363 | x += count; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | fprintf(stderr, "\n"); |
| 369 | } |
| 370 | |