blob: 914077840a9eac3ff78a407e7072f58079b57fe4 [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),
Constantin Kaplinskyadebffb2008-01-18 14:33:05 +000066 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 Kaplinsky1d378022007-12-25 17:43:09 +000071 m_numVideoPasses(0),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000072 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000073{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000074 // Get initial screen image.
75 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
76
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000077 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000078 // 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 Kaplinskyed3cf5d2007-12-28 17:59:10 +000081 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 Kaplinskyb30ae7f2006-05-25 05:04:46 +000089 }
90
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000091 m_changeFlags = new bool[m_numTiles];
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000092 m_rateMatrix = new char[m_numTiles];
93 m_videoFlags = new char[m_numTiles];
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000094 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000095 memset(m_rateMatrix, 0, m_numTiles);
96 memset(m_videoFlags, 0, m_numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000097}
98
99PollingManager::~PollingManager()
100{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000101 delete[] m_videoFlags;
102 delete[] m_rateMatrix;
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000103 delete[] m_changeFlags;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000104
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000105 delete m_rowImage;
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000106 delete m_columnImage;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000107}
108
109//
110// Register VNCServer object.
111//
112
113void PollingManager::setVNCServer(VNCServer *s)
114{
115 m_server = s;
116}
117
118//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000119// DEBUG: Measuring time spent in the poll() function,
120// as well as time intervals between poll() calls.
121//
122
123#ifdef DEBUG
124void 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
132void 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
146void PollingManager::poll()
147{
148#ifdef DEBUG
149 debugBeforePoll();
150#endif
151
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000152 // Perform polling and try update clients if changes were detected.
153 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000154 m_server->tryUpdate();
155
156#ifdef DEBUG
157 debugAfterPoll();
158#endif
159}
160
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000161#ifdef DEBUG_REPORT_CHANGED_TILES
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000162#define DBG_REPORT_CHANGES(title) printChanges((title))
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000163#else
164#define DBG_REPORT_CHANGES(title)
165#endif
166
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000167bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000168{
169 if (!m_server)
170 return false;
171
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000172 // 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 Kaplinsky04e910b2007-12-25 18:10:35 +0000177 //
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 Kaplinsky1d378022007-12-25 17:43:09 +0000181 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 Kaplinsky85b5eb92008-01-17 17:41:48 +0000192 // Clear the m_changeFlags[] array, indicating that no changes have
193 // been detected yet.
194 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000195
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000196 // 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 Kaplinsky85b5eb92008-01-17 17:41:48 +0000199 // contents and raise corresponding elements of m_changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000200 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000201 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000202 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000203 nTilesChanged += checkRow(0, y, m_width);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000204 }
205
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000206 // Do the work related to video area detection, if enabled.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000207 bool haveVideoRect = false;
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000208 if ((int)m_videoPriority != 0) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000209 handleVideo();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000210 if (!m_videoRect.is_empty()) {
211 getScreenRect(m_videoRect);
212 haveVideoRect = true;
213 }
214 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000215
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000216 DBG_REPORT_CHANGES("After 1st pass");
217
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000218 // If some changes have been detected:
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000219 if (nTilesChanged) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000220 // Try to find more changes around. Before doing that, mark the
221 // video area as changed, to skip comparisons of its pixels.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000222 flagVideoArea(true);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000223 DBG_REPORT_CHANGES("Before checking neighbors");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000224 checkNeighbors();
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000225 DBG_REPORT_CHANGES("After checking neighbors");
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000226
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 Kaplinsky850de2b2008-01-17 19:14:37 +0000229 flagVideoArea(false);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000230 DBG_REPORT_CHANGES("Before sending");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000231 nTilesChanged = sendChanges();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000232 }
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000233
Constantin Kaplinsky52f29d32007-12-28 18:30:54 +0000234#ifdef DEBUG_PRINT_NUM_CHANGED_TILES
235 printf("%3d ", nTilesChanged);
236 if (m_pollingStep % 32 == 0) {
237 printf("\n");
238 }
239#endif
240
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000241#ifdef DEBUG
242 if (nTilesChanged != 0) {
243 fprintf(stderr, "#%d# ", nTilesChanged);
244 }
245#endif
246
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000247 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000248}
249
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000250int PollingManager::checkRow(int x, int y, int w)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000251{
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000252 int bytesPerLine = m_image->xim->bytes_per_line;
253
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000254 // If necessary, expand the row to the left, to the tile border.
255 // In other words, x must be a multiple of 32.
256 if (x % 32 != 0) {
257 int correction = x % 32;
258 x -= correction;
259 w += correction;
260 }
261
262 // Compute a pointer to the corresponding element of m_changeFlags.
263 // FIXME: Provide an inline function for that?
264 bool *pChangeFlags = &m_changeFlags[(y / 32) * m_widthTiles + (x / 32)];
265
266 // Read a row from the screen. Note that getFullRow() may be more
267 // efficient than getRow() which is more general.
268 // FIXME: Move the logic to getRow()?
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000269 if (x == 0 && w == m_width) {
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000270 getFullRow(y);
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000271 } else {
272 getRow(x, y, w);
273 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000274
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000275 // Compute pointers to images to be compared.
276 // FIXME: Provide an inline function Image::locatePixel(x, y).
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000277 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * m_bytesPerPixel;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000278 char *ptr_new = m_rowImage->xim->data;
279
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000280 // Compare pixels, raise corresponding elements of m_changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000281 int nTilesChanged = 0;
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000282 for (int i = 0; i < w; i += 32) {
283 int tile_w = (w - i >= 32) ? 32 : w - i;
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000284 int nBytes = tile_w * m_bytesPerPixel;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000285 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000286 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000287 nTilesChanged++;
288 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000289 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000290 ptr_old += nBytes;
291 ptr_new += nBytes;
292 }
293
294 return nTilesChanged;
295}
296
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000297int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags)
298{
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000299 getColumn(x, y, h);
300
301 int nTilesChanged = 0;
302 for (int nTile = 0; nTile < (h + 31) / 32; nTile++) {
303 if (!*pChangeFlags) {
304 int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32;
305 for (int i = 0; i < tile_h; i++) {
306 // FIXME: Provide an inline function Image::locatePixel(x, y).
307 // FIXME: Do not compute these pointers in the inner cycle.
308 char *ptr_old = (m_image->xim->data +
309 (y + nTile * 32 + i) * m_image->xim->bytes_per_line +
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000310 x * m_bytesPerPixel);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000311 char *ptr_new = (m_columnImage->xim->data +
312 (nTile * 32 + i) * m_columnImage->xim->bytes_per_line);
Constantin Kaplinskyec45c482008-01-18 14:13:16 +0000313 if (memcmp(ptr_old, ptr_new, m_bytesPerPixel)) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000314 *pChangeFlags = true;
315 nTilesChanged++;
316 break;
317 }
318 }
319 }
320 pChangeFlags += m_widthTiles;
321 }
322
323 return nTilesChanged;
324}
325
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000326int PollingManager::sendChanges()
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000327{
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000328 const bool *pChangeFlags = m_changeFlags;
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000329 int nTilesChanged = 0;
330
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000331 Rect rect;
332 for (int y = 0; y < m_heightTiles; y++) {
333 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000334 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000335 // Count successive tiles marked as changed.
336 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000337 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000338 count++;
339 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000340 nTilesChanged += count;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000341 // Compute the coordinates and the size of this band.
342 rect.setXYWH(x * 32, y * 32, count * 32, 32);
343 if (rect.br.x > m_width)
344 rect.br.x = m_width;
345 if (rect.br.y > m_height)
346 rect.br.y = m_height;
347 // Add to the changed region maintained by the server.
348 getScreenRect(rect);
349 m_server->add_changed(rect);
350 // Skip processed tiles.
351 x += count;
352 }
353 }
354 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000355 return nTilesChanged;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000356}
357
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000358void PollingManager::handleVideo()
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000359{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000360 // Update counters in m_rateMatrix.
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000361 for (int i = 0; i < m_numTiles; i++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000362 m_rateMatrix[i] += (m_changeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000363
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000364 // Once per eight calls: detect video rectangle by examining
365 // m_rateMatrix[], then reset counters in m_rateMatrix[].
366 if (m_pollingStep % 8 == 0) {
367 detectVideo();
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000368 memset(m_rateMatrix, 0, m_numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000369 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000370}
371
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000372void PollingManager::flagVideoArea(bool value)
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000373{
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000374 if (m_videoRect.is_empty())
375 return;
376
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000377 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
378 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
379
380 for (int y = r.tl.y; y < r.br.y; y++)
381 for (int x = r.tl.x; x < r.br.x; x++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000382 m_changeFlags[y * m_widthTiles + x] = value;
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000383}
384
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000385void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000386PollingManager::checkNeighbors()
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000387{
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000388 int x, y;
389
390 // Check neighboring pixels above and below changed tiles.
391 // FIXME: Fast skip to the first changed tile (and to the last, too).
392 // FIXME: Check the full-width line above the first changed tile?
393 for (y = 0; y < m_heightTiles; y++) {
394 bool doneAbove = false;
395 bool doneBelow = false;
396 for (x = 0; x < m_widthTiles; x++) {
397 if (!doneAbove && y > 0 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000398 m_changeFlags[y * m_widthTiles + x] &&
399 !m_changeFlags[(y - 1) * m_widthTiles + x]) {
400 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000401 checkRow(x * 32, y * 32 - 1, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000402 doneAbove = true;
403 }
404 if (!doneBelow && y < m_heightTiles - 1 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000405 m_changeFlags[y * m_widthTiles + x] &&
406 !m_changeFlags[(y + 1) * m_widthTiles + x]) {
407 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000408 checkRow(x * 32, (y + 1) * 32, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000409 doneBelow = true;
410 }
411 if (doneBelow && doneAbove)
412 break;
413 }
414 }
415
416 // Check neighboring pixels at the right side of changed tiles.
417 for (x = 0; x < m_widthTiles - 1; x++) {
418 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000419 if (m_changeFlags[y * m_widthTiles + x] &&
420 !m_changeFlags[y * m_widthTiles + x + 1]) {
421 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000422 checkColumn((x + 1) * 32, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000423 &m_changeFlags[y * m_widthTiles + x + 1]);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000424 break;
425 }
426 }
427 }
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000428
429 // Check neighboring pixels at the left side of changed tiles.
430 for (x = m_widthTiles - 1; x > 0; x--) {
431 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000432 if (m_changeFlags[y * m_widthTiles + x] &&
433 !m_changeFlags[y * m_widthTiles + x - 1]) {
434 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000435 checkColumn(x * 32 - 1, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000436 &m_changeFlags[y * m_widthTiles + x - 1]);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000437 break;
438 }
439 }
440 }
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000441}
442
443void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000444PollingManager::printChanges(const char *header) const
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000445{
446 fprintf(stderr, "%s:", header);
447
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000448 const bool *pChangeFlags = m_changeFlags;
449
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000450 for (int y = 0; y < m_heightTiles; y++) {
451 for (int x = 0; x < m_widthTiles; x++) {
452 if (*pChangeFlags++) {
453 // Count successive tiles marked as changed.
454 int count = 1;
455 while (x + count < m_widthTiles && *pChangeFlags++) {
456 count++;
457 }
458 // Print.
459 fprintf(stderr, " (%d,%d)*%d", x, y, count);
460 // Skip processed tiles.
461 x += count;
462 }
463 }
464 }
465
466 fprintf(stderr, "\n");
467}
468
469void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000470PollingManager::detectVideo()
471{
472 // Configurable parameters.
473 const int VIDEO_THRESHOLD_0 = 3;
474 const int VIDEO_THRESHOLD_1 = 5;
475
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000476 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
477 // This will guarantee that the size of the video area is always a
478 // multiple of 32 pixels. This is important for hardware JPEG encoders.
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000479 if (m_width % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000480 for (int n = m_widthTiles - 1; n < m_numTiles; n += m_widthTiles)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000481 m_rateMatrix[n] = 0;
482 }
483 if (m_height % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000484 for (int n = m_numTiles - m_widthTiles; n < m_numTiles; n++)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000485 m_rateMatrix[n] = 0;
486 }
487
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000488 // First, detect candidate region that looks like video. In other
489 // words, find a region that consists of continuously changing
490 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000491 for (int i = 0; i < m_numTiles; i++) {
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000492 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
493 m_videoFlags[i] = 0;
494 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
495 m_videoFlags[i] = 1;
496 }
497 }
498
499 // Now, choose the biggest rectangle from that candidate region.
500 Rect newRect;
501 getVideoAreaRect(&newRect);
502
503 // Does new rectangle differ from the previously detected one?
504 // If it does, save new rectangle and inform the server.
505 if (!newRect.equals(m_videoRect)) {
506 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000507 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000508 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000509 vlog.debug("Detected video %dx%d at (%d,%d)",
510 newRect.width(), newRect.height(),
511 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000512 }
513 m_videoRect = newRect;
514 m_server->set_video_area(newRect);
515 }
516}
517
518void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000519PollingManager::getVideoAreaRect(Rect *result)
520{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000521 int *mx_hlen, *mx_vlen;
522 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000523
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000524 int full_h = m_heightTiles;
525 int full_w = m_widthTiles;
526 int x, y;
527 Rect max_rect(0, 0, 0, 0);
528 Rect local_rect;
529
530 for (y = 0; y < full_h; y++) {
531 for (x = 0; x < full_w; x++) {
532 int max_w = mx_hlen[y * full_w + x];
533 int max_h = mx_vlen[y * full_w + x];
534 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
535 local_rect.tl.x = x;
536 local_rect.tl.y = y;
537 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
538 if (local_rect.area() > max_rect.area()) {
539 max_rect = local_rect;
540 }
541 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000542 }
543 }
544
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000545 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000546
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000547 max_rect.tl.x *= 32;
548 max_rect.tl.y *= 32;
549 max_rect.br.x *= 32;
550 max_rect.br.y *= 32;
551 if (max_rect.br.x > m_width)
552 max_rect.br.x = m_width;
553 if (max_rect.br.y > m_height)
554 max_rect.br.y = m_height;
555 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000556}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000557
558void
559PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
560{
561 // Handy shortcuts.
562 int h = m_heightTiles;
563 int w = m_widthTiles;
564
565 // Allocate memory.
566 int *mx_h = new int[h * w];
567 memset(mx_h, 0, h * w * sizeof(int));
568 int *mx_v = new int[h * w];
569 memset(mx_v, 0, h * w * sizeof(int));
570
571 int x, y, len, i;
572
573 // Fill in horizontal length matrix.
574 for (y = 0; y < h; y++) {
575 for (x = 0; x < w; x++) {
576 len = 0;
577 while (x + len < w && m_videoFlags[y * w + x + len]) {
578 len++;
579 }
580 for (i = 0; i < len; i++) {
581 mx_h[y * w + x + i] = len - i;
582 }
583 x += len;
584 }
585 }
586
587 // Fill in vertical length matrix.
588 for (x = 0; x < w; x++) {
589 for (y = 0; y < h; y++) {
590 len = 0;
591 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
592 len++;
593 }
594 for (i = 0; i < len; i++) {
595 mx_v[(y + i) * w + x] = len - i;
596 }
597 y += len;
598 }
599 }
600
601 *pmx_h = mx_h;
602 *pmx_v = mx_v;
603}
604
605void
606PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
607{
608 delete[] mx_h;
609 delete[] mx_v;
610}
611
612// NOTE: This function assumes that current tile has non-zero in mx_h[],
613// otherwise we get division by zero.
614void
615PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
616{
617 int idx = r->tl.y * m_widthTiles + r->tl.x;
618
619 // NOTE: Rectangle's maximum width and height are 25 and 18
620 // (in tiles, where each tile is usually 32x32 pixels).
621 int max_w = mx_h[idx];
622 if (max_w > 25)
623 max_w = 25;
624 int cur_h = 18;
625
626 int best_w = max_w;
627 int best_area = 1 * best_w;
628
629 for (int i = 0; i < max_w; i++) {
630 int h = mx_v[idx + i];
631 if (h < cur_h) {
632 cur_h = h;
633 if (cur_h * max_w <= best_area)
634 break;
635 }
636 if (cur_h * (i + 1) > best_area) {
637 best_w = i + 1;
638 best_area = cur_h * best_w;
639 }
640 }
641
642 r->br.x = r->tl.x + best_w;
643 r->br.y = r->tl.y + best_area / best_w;
644}
645