blob: 9ae8d159425993335e482fb6b9f6060c3917935b [file] [log] [blame]
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +00001/* Copyright (C) 2004-2007 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00002 *
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 Kaplinskyb30ae7f2006-05-25 05:04:46 +000022#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
33static LogWriter vlog("PollingMgr");
34
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000035const 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 Kaplinsky04e910b2007-12-25 18:10:35 +000042// FIXME: Check that the parameter's value is in the allowed range.
43// This applies to all other parameters as well.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000044IntParameter PollingManager::m_videoPriority("VideoPriority",
45 "Priority of sending updates for video area (0..8)", 2);
46
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000047//
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 Kaplinsky936c3692007-12-27 08:42:53 +000053// FIXME: Pass XPixelBuffer* instead of Image*.
54//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055
56PollingManager::PollingManager(Display *dpy, Image *image,
57 ImageFactory *factory,
58 int offsetLeft, int offsetTop)
Constantin Kaplinskyec45c482008-01-18 14:13:16 +000059 : 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),
66 m_height(m_image->xim->height),
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000067 m_numVideoPasses(0),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000068 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000069{
70 // Save width and height of the screen (and the image).
71 m_width = m_image->xim->width;
72 m_height = m_image->xim->height;
73
74 // Compute width and height in 32x32 tiles.
75 m_widthTiles = (m_width + 31) / 32;
76 m_heightTiles = (m_height + 31) / 32;
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000077 m_numTiles = m_widthTiles * m_heightTiles;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000078
79 // Get initial screen image.
80 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
81
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000082 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000083 // underlying class names are different from the class name of the
84 // primary image.
85 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +000086 m_columnImage = factory->newImage(m_dpy, 1, m_height);
87 const char *primaryImgClass = m_image->className();
88 const char *rowImgClass = m_rowImage->className();
89 const char *columnImgClass = m_columnImage->className();
90 if (strcmp(rowImgClass, primaryImgClass) != 0 ||
91 strcmp(columnImgClass, primaryImgClass) != 0) {
92 vlog.error("Image types do not match (%s, %s, %s)",
93 primaryImgClass, rowImgClass, columnImgClass);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000094 }
95
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000096 m_changeFlags = new bool[m_numTiles];
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000097 m_rateMatrix = new char[m_numTiles];
98 m_videoFlags = new char[m_numTiles];
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000099 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000100 memset(m_rateMatrix, 0, m_numTiles);
101 memset(m_videoFlags, 0, m_numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000102}
103
104PollingManager::~PollingManager()
105{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000106 delete[] m_videoFlags;
107 delete[] m_rateMatrix;
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000108 delete[] m_changeFlags;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000109
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000110 delete m_rowImage;
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000111 delete m_columnImage;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000112}
113
114//
115// Register VNCServer object.
116//
117
118void PollingManager::setVNCServer(VNCServer *s)
119{
120 m_server = s;
121}
122
123//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000124// DEBUG: Measuring time spent in the poll() function,
125// as well as time intervals between poll() calls.
126//
127
128#ifdef DEBUG
129void PollingManager::debugBeforePoll()
130{
131 TimeMillis timeNow;
132 int diff = timeNow.diffFrom(m_timeSaved);
133 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
134 m_timeSaved = timeNow;
135}
136
137void PollingManager::debugAfterPoll()
138{
139 TimeMillis timeNow;
140 int diff = timeNow.diffFrom(m_timeSaved);
141 fprintf(stderr, "[poll%4dms]\n", diff);
142 m_timeSaved = timeNow;
143}
144
145#endif
146
147//
148// Search for changed rectangles on the screen.
149//
150
151void PollingManager::poll()
152{
153#ifdef DEBUG
154 debugBeforePoll();
155#endif
156
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000157 // Perform polling and try update clients if changes were detected.
158 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000159 m_server->tryUpdate();
160
161#ifdef DEBUG
162 debugAfterPoll();
163#endif
164}
165
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000166#ifdef DEBUG_REPORT_CHANGED_TILES
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000167#define DBG_REPORT_CHANGES(title) printChanges((title))
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000168#else
169#define DBG_REPORT_CHANGES(title)
170#endif
171
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000172bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000173{
174 if (!m_server)
175 return false;
176
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000177 // If video data should have higher priority, and video area was
178 // detected, perform special passes to send video data only. Such
179 // "video passes" will be performed between normal polling passes.
180 // No actual polling is performed in a video pass since we know that
181 // video is changing continuously.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000182 //
183 // FIXME: Should we move this block into a separate function?
184 // FIXME: Giving higher priority to video area lengthens video
185 // detection cycles. Should we do something with that?
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000186 if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) {
187 if (m_numVideoPasses > 0) {
188 m_numVideoPasses--;
189 getScreenRect(m_videoRect);
190 return true; // we've got changes
191 } else {
192 // Normal pass now, but schedule video passes for next calls.
193 m_numVideoPasses = (int)m_videoPriority - 1;
194 }
195 }
196
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000197 // Clear the m_changeFlags[] array, indicating that no changes have
198 // been detected yet.
199 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000200
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000201 // First pass over the framebuffer. Here we scan 1/32 part of the
202 // framebuffer -- that is, one line in each (32 * m_width) stripe.
203 // We compare the pixels of that line with previous framebuffer
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000204 // contents and raise corresponding elements of m_changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000205 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000206 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000207 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000208 nTilesChanged += checkRow(0, y, m_width);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000209 }
210
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000211 // Do the work related to video area detection, if enabled.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000212 bool haveVideoRect = false;
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000213 if ((int)m_videoPriority != 0) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000214 handleVideo();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000215 if (!m_videoRect.is_empty()) {
216 getScreenRect(m_videoRect);
217 haveVideoRect = true;
218 }
219 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000220
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000221 DBG_REPORT_CHANGES("After 1st pass");
222
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000223 // If some changes have been detected:
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000224 if (nTilesChanged) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000225 // Try to find more changes around. Before doing that, mark the
226 // video area as changed, to skip comparisons of its pixels.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000227 flagVideoArea(true);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000228 DBG_REPORT_CHANGES("Before checking neighbors");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000229 checkNeighbors();
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000230 DBG_REPORT_CHANGES("After checking neighbors");
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000231
232 // Inform the server about the changes. This time, we mark the
233 // video area as NOT changed, to prevent reading its pixels again.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000234 flagVideoArea(false);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000235 DBG_REPORT_CHANGES("Before sending");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000236 nTilesChanged = sendChanges();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000237 }
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000238
Constantin Kaplinsky52f29d32007-12-28 18:30:54 +0000239#ifdef DEBUG_PRINT_NUM_CHANGED_TILES
240 printf("%3d ", nTilesChanged);
241 if (m_pollingStep % 32 == 0) {
242 printf("\n");
243 }
244#endif
245
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000246#ifdef DEBUG
247 if (nTilesChanged != 0) {
248 fprintf(stderr, "#%d# ", nTilesChanged);
249 }
250#endif
251
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000252 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000253}
254
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000255int PollingManager::checkRow(int x, int y, int w)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000256{
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000257 int bytesPerLine = m_image->xim->bytes_per_line;
258
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000259 // If necessary, expand the row to the left, to the tile border.
260 // In other words, x must be a multiple of 32.
261 if (x % 32 != 0) {
262 int correction = x % 32;
263 x -= correction;
264 w += correction;
265 }
266
267 // Compute a pointer to the corresponding element of m_changeFlags.
268 // FIXME: Provide an inline function for that?
269 bool *pChangeFlags = &m_changeFlags[(y / 32) * m_widthTiles + (x / 32)];
270
271 // Read a row from the screen. Note that getFullRow() may be more
272 // efficient than getRow() which is more general.
273 // FIXME: Move the logic to getRow()?
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000274 if (x == 0 && w == m_width) {
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000275 getFullRow(y);
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000276 } else {
277 getRow(x, y, w);
278 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000279
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000280 // Compute pointers to images to be compared.
281 // FIXME: Provide an inline function Image::locatePixel(x, y).
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000282 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * m_bytesPerPixel;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000283 char *ptr_new = m_rowImage->xim->data;
284
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000285 // Compare pixels, raise corresponding elements of m_changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000286 int nTilesChanged = 0;
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000287 for (int i = 0; i < w; i += 32) {
288 int tile_w = (w - i >= 32) ? 32 : w - i;
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000289 int nBytes = tile_w * m_bytesPerPixel;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000290 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000291 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000292 nTilesChanged++;
293 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000294 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000295 ptr_old += nBytes;
296 ptr_new += nBytes;
297 }
298
299 return nTilesChanged;
300}
301
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000302int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags)
303{
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000304 getColumn(x, y, h);
305
306 int nTilesChanged = 0;
307 for (int nTile = 0; nTile < (h + 31) / 32; nTile++) {
308 if (!*pChangeFlags) {
309 int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32;
310 for (int i = 0; i < tile_h; i++) {
311 // FIXME: Provide an inline function Image::locatePixel(x, y).
312 // FIXME: Do not compute these pointers in the inner cycle.
313 char *ptr_old = (m_image->xim->data +
314 (y + nTile * 32 + i) * m_image->xim->bytes_per_line +
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000315 x * m_bytesPerPixel);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000316 char *ptr_new = (m_columnImage->xim->data +
317 (nTile * 32 + i) * m_columnImage->xim->bytes_per_line);
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000318 if (memcmp(ptr_old, ptr_new, m_bytesPerPixel)) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000319 *pChangeFlags = true;
320 nTilesChanged++;
321 break;
322 }
323 }
324 }
325 pChangeFlags += m_widthTiles;
326 }
327
328 return nTilesChanged;
329}
330
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000331int PollingManager::sendChanges()
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000332{
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000333 const bool *pChangeFlags = m_changeFlags;
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000334 int nTilesChanged = 0;
335
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000336 Rect rect;
337 for (int y = 0; y < m_heightTiles; y++) {
338 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000339 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000340 // Count successive tiles marked as changed.
341 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000342 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000343 count++;
344 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000345 nTilesChanged += count;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000346 // Compute the coordinates and the size of this band.
347 rect.setXYWH(x * 32, y * 32, count * 32, 32);
348 if (rect.br.x > m_width)
349 rect.br.x = m_width;
350 if (rect.br.y > m_height)
351 rect.br.y = m_height;
352 // Add to the changed region maintained by the server.
353 getScreenRect(rect);
354 m_server->add_changed(rect);
355 // Skip processed tiles.
356 x += count;
357 }
358 }
359 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000360 return nTilesChanged;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000361}
362
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000363void PollingManager::handleVideo()
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000364{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000365 // Update counters in m_rateMatrix.
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000366 for (int i = 0; i < m_numTiles; i++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000367 m_rateMatrix[i] += (m_changeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000368
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000369 // Once per eight calls: detect video rectangle by examining
370 // m_rateMatrix[], then reset counters in m_rateMatrix[].
371 if (m_pollingStep % 8 == 0) {
372 detectVideo();
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000373 memset(m_rateMatrix, 0, m_numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000374 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000375}
376
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000377void PollingManager::flagVideoArea(bool value)
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000378{
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000379 if (m_videoRect.is_empty())
380 return;
381
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000382 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
383 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
384
385 for (int y = r.tl.y; y < r.br.y; y++)
386 for (int x = r.tl.x; x < r.br.x; x++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000387 m_changeFlags[y * m_widthTiles + x] = value;
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000388}
389
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000390void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000391PollingManager::checkNeighbors()
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000392{
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000393 int x, y;
394
395 // Check neighboring pixels above and below changed tiles.
396 // FIXME: Fast skip to the first changed tile (and to the last, too).
397 // FIXME: Check the full-width line above the first changed tile?
398 for (y = 0; y < m_heightTiles; y++) {
399 bool doneAbove = false;
400 bool doneBelow = false;
401 for (x = 0; x < m_widthTiles; x++) {
402 if (!doneAbove && y > 0 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000403 m_changeFlags[y * m_widthTiles + x] &&
404 !m_changeFlags[(y - 1) * m_widthTiles + x]) {
405 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000406 checkRow(x * 32, y * 32 - 1, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000407 doneAbove = true;
408 }
409 if (!doneBelow && y < m_heightTiles - 1 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000410 m_changeFlags[y * m_widthTiles + x] &&
411 !m_changeFlags[(y + 1) * m_widthTiles + x]) {
412 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000413 checkRow(x * 32, (y + 1) * 32, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000414 doneBelow = true;
415 }
416 if (doneBelow && doneAbove)
417 break;
418 }
419 }
420
421 // Check neighboring pixels at the right side of changed tiles.
422 for (x = 0; x < m_widthTiles - 1; x++) {
423 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000424 if (m_changeFlags[y * m_widthTiles + x] &&
425 !m_changeFlags[y * m_widthTiles + x + 1]) {
426 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000427 checkColumn((x + 1) * 32, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000428 &m_changeFlags[y * m_widthTiles + x + 1]);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000429 break;
430 }
431 }
432 }
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000433
434 // Check neighboring pixels at the left side of changed tiles.
435 for (x = m_widthTiles - 1; x > 0; x--) {
436 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000437 if (m_changeFlags[y * m_widthTiles + x] &&
438 !m_changeFlags[y * m_widthTiles + x - 1]) {
439 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000440 checkColumn(x * 32 - 1, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000441 &m_changeFlags[y * m_widthTiles + x - 1]);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000442 break;
443 }
444 }
445 }
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000446}
447
448void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000449PollingManager::printChanges(const char *header) const
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000450{
451 fprintf(stderr, "%s:", header);
452
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000453 const bool *pChangeFlags = m_changeFlags;
454
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000455 for (int y = 0; y < m_heightTiles; y++) {
456 for (int x = 0; x < m_widthTiles; x++) {
457 if (*pChangeFlags++) {
458 // Count successive tiles marked as changed.
459 int count = 1;
460 while (x + count < m_widthTiles && *pChangeFlags++) {
461 count++;
462 }
463 // Print.
464 fprintf(stderr, " (%d,%d)*%d", x, y, count);
465 // Skip processed tiles.
466 x += count;
467 }
468 }
469 }
470
471 fprintf(stderr, "\n");
472}
473
474void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000475PollingManager::detectVideo()
476{
477 // Configurable parameters.
478 const int VIDEO_THRESHOLD_0 = 3;
479 const int VIDEO_THRESHOLD_1 = 5;
480
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000481 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
482 // This will guarantee that the size of the video area is always a
483 // multiple of 32 pixels. This is important for hardware JPEG encoders.
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000484 if (m_width % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000485 for (int n = m_widthTiles - 1; n < m_numTiles; n += m_widthTiles)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000486 m_rateMatrix[n] = 0;
487 }
488 if (m_height % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000489 for (int n = m_numTiles - m_widthTiles; n < m_numTiles; n++)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000490 m_rateMatrix[n] = 0;
491 }
492
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000493 // First, detect candidate region that looks like video. In other
494 // words, find a region that consists of continuously changing
495 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000496 for (int i = 0; i < m_numTiles; i++) {
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000497 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
498 m_videoFlags[i] = 0;
499 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
500 m_videoFlags[i] = 1;
501 }
502 }
503
504 // Now, choose the biggest rectangle from that candidate region.
505 Rect newRect;
506 getVideoAreaRect(&newRect);
507
508 // Does new rectangle differ from the previously detected one?
509 // If it does, save new rectangle and inform the server.
510 if (!newRect.equals(m_videoRect)) {
511 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000512 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000513 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000514 vlog.debug("Detected video %dx%d at (%d,%d)",
515 newRect.width(), newRect.height(),
516 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000517 }
518 m_videoRect = newRect;
519 m_server->set_video_area(newRect);
520 }
521}
522
523void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000524PollingManager::getVideoAreaRect(Rect *result)
525{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000526 int *mx_hlen, *mx_vlen;
527 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000528
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000529 int full_h = m_heightTiles;
530 int full_w = m_widthTiles;
531 int x, y;
532 Rect max_rect(0, 0, 0, 0);
533 Rect local_rect;
534
535 for (y = 0; y < full_h; y++) {
536 for (x = 0; x < full_w; x++) {
537 int max_w = mx_hlen[y * full_w + x];
538 int max_h = mx_vlen[y * full_w + x];
539 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
540 local_rect.tl.x = x;
541 local_rect.tl.y = y;
542 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
543 if (local_rect.area() > max_rect.area()) {
544 max_rect = local_rect;
545 }
546 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000547 }
548 }
549
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000550 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000551
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000552 max_rect.tl.x *= 32;
553 max_rect.tl.y *= 32;
554 max_rect.br.x *= 32;
555 max_rect.br.y *= 32;
556 if (max_rect.br.x > m_width)
557 max_rect.br.x = m_width;
558 if (max_rect.br.y > m_height)
559 max_rect.br.y = m_height;
560 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000561}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000562
563void
564PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
565{
566 // Handy shortcuts.
567 int h = m_heightTiles;
568 int w = m_widthTiles;
569
570 // Allocate memory.
571 int *mx_h = new int[h * w];
572 memset(mx_h, 0, h * w * sizeof(int));
573 int *mx_v = new int[h * w];
574 memset(mx_v, 0, h * w * sizeof(int));
575
576 int x, y, len, i;
577
578 // Fill in horizontal length matrix.
579 for (y = 0; y < h; y++) {
580 for (x = 0; x < w; x++) {
581 len = 0;
582 while (x + len < w && m_videoFlags[y * w + x + len]) {
583 len++;
584 }
585 for (i = 0; i < len; i++) {
586 mx_h[y * w + x + i] = len - i;
587 }
588 x += len;
589 }
590 }
591
592 // Fill in vertical length matrix.
593 for (x = 0; x < w; x++) {
594 for (y = 0; y < h; y++) {
595 len = 0;
596 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
597 len++;
598 }
599 for (i = 0; i < len; i++) {
600 mx_v[(y + i) * w + x] = len - i;
601 }
602 y += len;
603 }
604 }
605
606 *pmx_h = mx_h;
607 *pmx_v = mx_v;
608}
609
610void
611PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
612{
613 delete[] mx_h;
614 delete[] mx_v;
615}
616
617// NOTE: This function assumes that current tile has non-zero in mx_h[],
618// otherwise we get division by zero.
619void
620PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
621{
622 int idx = r->tl.y * m_widthTiles + r->tl.x;
623
624 // NOTE: Rectangle's maximum width and height are 25 and 18
625 // (in tiles, where each tile is usually 32x32 pixels).
626 int max_w = mx_h[idx];
627 if (max_w > 25)
628 max_w = 25;
629 int cur_h = 18;
630
631 int best_w = max_w;
632 int best_area = 1 * best_w;
633
634 for (int i = 0; i < max_w; i++) {
635 int h = mx_v[idx + i];
636 if (h < cur_h) {
637 cur_h = h;
638 if (cur_h * max_w <= best_area)
639 break;
640 }
641 if (cur_h * (i + 1) > best_area) {
642 best_w = i + 1;
643 best_area = cur_h * best_w;
644 }
645 }
646
647 r->br.x = r->tl.x + best_w;
648 r->br.y = r->tl.y + best_area / best_w;
649}
650