blob: 2ce75fa8c008a60f36ac49b1ea186920b09a1843 [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)
59 : m_dpy(dpy), m_server(0), m_image(image),
60 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000061 m_numVideoPasses(0),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000062 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000063{
64 // Save width and height of the screen (and the image).
65 m_width = m_image->xim->width;
66 m_height = m_image->xim->height;
67
68 // Compute width and height in 32x32 tiles.
69 m_widthTiles = (m_width + 31) / 32;
70 m_heightTiles = (m_height + 31) / 32;
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000071 m_numTiles = m_widthTiles * m_heightTiles;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000072
73 // Get initial screen image.
74 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
75
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000076 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000077 // underlying class names are different from the class name of the
78 // primary image.
79 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +000080 m_columnImage = factory->newImage(m_dpy, 1, m_height);
81 const char *primaryImgClass = m_image->className();
82 const char *rowImgClass = m_rowImage->className();
83 const char *columnImgClass = m_columnImage->className();
84 if (strcmp(rowImgClass, primaryImgClass) != 0 ||
85 strcmp(columnImgClass, primaryImgClass) != 0) {
86 vlog.error("Image types do not match (%s, %s, %s)",
87 primaryImgClass, rowImgClass, columnImgClass);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000088 }
89
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000090 m_changeFlags = new bool[m_numTiles];
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000091 m_rateMatrix = new char[m_numTiles];
92 m_videoFlags = new char[m_numTiles];
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +000093 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinsky20390a22008-01-17 15:17:36 +000094 memset(m_rateMatrix, 0, m_numTiles);
95 memset(m_videoFlags, 0, m_numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000096}
97
98PollingManager::~PollingManager()
99{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000100 delete[] m_videoFlags;
101 delete[] m_rateMatrix;
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000102 delete[] m_changeFlags;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000103
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000104 delete m_rowImage;
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000105 delete m_columnImage;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000106}
107
108//
109// Register VNCServer object.
110//
111
112void PollingManager::setVNCServer(VNCServer *s)
113{
114 m_server = s;
115}
116
117//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000118// DEBUG: Measuring time spent in the poll() function,
119// as well as time intervals between poll() calls.
120//
121
122#ifdef DEBUG
123void PollingManager::debugBeforePoll()
124{
125 TimeMillis timeNow;
126 int diff = timeNow.diffFrom(m_timeSaved);
127 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
128 m_timeSaved = timeNow;
129}
130
131void PollingManager::debugAfterPoll()
132{
133 TimeMillis timeNow;
134 int diff = timeNow.diffFrom(m_timeSaved);
135 fprintf(stderr, "[poll%4dms]\n", diff);
136 m_timeSaved = timeNow;
137}
138
139#endif
140
141//
142// Search for changed rectangles on the screen.
143//
144
145void PollingManager::poll()
146{
147#ifdef DEBUG
148 debugBeforePoll();
149#endif
150
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000151 // Perform polling and try update clients if changes were detected.
152 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000153 m_server->tryUpdate();
154
155#ifdef DEBUG
156 debugAfterPoll();
157#endif
158}
159
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000160#ifdef DEBUG_REPORT_CHANGED_TILES
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000161#define DBG_REPORT_CHANGES(title) printChanges((title))
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000162#else
163#define DBG_REPORT_CHANGES(title)
164#endif
165
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000166bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000167{
168 if (!m_server)
169 return false;
170
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000171 // If video data should have higher priority, and video area was
172 // detected, perform special passes to send video data only. Such
173 // "video passes" will be performed between normal polling passes.
174 // No actual polling is performed in a video pass since we know that
175 // video is changing continuously.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000176 //
177 // FIXME: Should we move this block into a separate function?
178 // FIXME: Giving higher priority to video area lengthens video
179 // detection cycles. Should we do something with that?
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000180 if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) {
181 if (m_numVideoPasses > 0) {
182 m_numVideoPasses--;
183 getScreenRect(m_videoRect);
184 return true; // we've got changes
185 } else {
186 // Normal pass now, but schedule video passes for next calls.
187 m_numVideoPasses = (int)m_videoPriority - 1;
188 }
189 }
190
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000191 // Clear the m_changeFlags[] array, indicating that no changes have
192 // been detected yet.
193 memset(m_changeFlags, 0, m_numTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000194
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000195 // First pass over the framebuffer. Here we scan 1/32 part of the
196 // framebuffer -- that is, one line in each (32 * m_width) stripe.
197 // We compare the pixels of that line with previous framebuffer
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000198 // contents and raise corresponding elements of m_changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000199 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000200 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000201 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000202 nTilesChanged += checkRow(0, y, m_width);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000203 }
204
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000205 // Do the work related to video area detection, if enabled.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000206 bool haveVideoRect = false;
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000207 if ((int)m_videoPriority != 0) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000208 handleVideo();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000209 if (!m_videoRect.is_empty()) {
210 getScreenRect(m_videoRect);
211 haveVideoRect = true;
212 }
213 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000214
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000215 DBG_REPORT_CHANGES("After 1st pass");
216
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000217 // If some changes have been detected:
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000218 if (nTilesChanged) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000219 // Try to find more changes around. Before doing that, mark the
220 // video area as changed, to skip comparisons of its pixels.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000221 flagVideoArea(true);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000222 DBG_REPORT_CHANGES("Before checking neighbors");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000223 checkNeighbors();
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000224 DBG_REPORT_CHANGES("After checking neighbors");
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000225
226 // Inform the server about the changes. This time, we mark the
227 // video area as NOT changed, to prevent reading its pixels again.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000228 flagVideoArea(false);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000229 DBG_REPORT_CHANGES("Before sending");
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000230 nTilesChanged = sendChanges();
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000231 }
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000232
Constantin Kaplinsky52f29d32007-12-28 18:30:54 +0000233#ifdef DEBUG_PRINT_NUM_CHANGED_TILES
234 printf("%3d ", nTilesChanged);
235 if (m_pollingStep % 32 == 0) {
236 printf("\n");
237 }
238#endif
239
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000240#ifdef DEBUG
241 if (nTilesChanged != 0) {
242 fprintf(stderr, "#%d# ", nTilesChanged);
243 }
244#endif
245
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000246 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000247}
248
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000249int PollingManager::checkRow(int x, int y, int w)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000250{
251 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
252 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 Kaplinskybc6b9e22007-10-04 11:43:41 +0000277 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
278 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 Kaplinskybc6b9e22007-10-04 11:43:41 +0000284 int nBytes = tile_w * bytesPerPixel;
285 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{
299 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
300
301 getColumn(x, y, h);
302
303 int nTilesChanged = 0;
304 for (int nTile = 0; nTile < (h + 31) / 32; nTile++) {
305 if (!*pChangeFlags) {
306 int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32;
307 for (int i = 0; i < tile_h; i++) {
308 // FIXME: Provide an inline function Image::locatePixel(x, y).
309 // FIXME: Do not compute these pointers in the inner cycle.
310 char *ptr_old = (m_image->xim->data +
311 (y + nTile * 32 + i) * m_image->xim->bytes_per_line +
312 x * bytesPerPixel);
313 char *ptr_new = (m_columnImage->xim->data +
314 (nTile * 32 + i) * m_columnImage->xim->bytes_per_line);
315 if (memcmp(ptr_old, ptr_new, bytesPerPixel)) {
316 *pChangeFlags = true;
317 nTilesChanged++;
318 break;
319 }
320 }
321 }
322 pChangeFlags += m_widthTiles;
323 }
324
325 return nTilesChanged;
326}
327
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000328int PollingManager::sendChanges()
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000329{
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000330 const bool *pChangeFlags = m_changeFlags;
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000331 int nTilesChanged = 0;
332
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000333 Rect rect;
334 for (int y = 0; y < m_heightTiles; y++) {
335 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000336 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000337 // Count successive tiles marked as changed.
338 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000339 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000340 count++;
341 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000342 nTilesChanged += count;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000343 // Compute the coordinates and the size of this band.
344 rect.setXYWH(x * 32, y * 32, count * 32, 32);
345 if (rect.br.x > m_width)
346 rect.br.x = m_width;
347 if (rect.br.y > m_height)
348 rect.br.y = m_height;
349 // Add to the changed region maintained by the server.
350 getScreenRect(rect);
351 m_server->add_changed(rect);
352 // Skip processed tiles.
353 x += count;
354 }
355 }
356 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000357 return nTilesChanged;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000358}
359
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000360void PollingManager::handleVideo()
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000361{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000362 // Update counters in m_rateMatrix.
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000363 for (int i = 0; i < m_numTiles; i++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000364 m_rateMatrix[i] += (m_changeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000365
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000366 // Once per eight calls: detect video rectangle by examining
367 // m_rateMatrix[], then reset counters in m_rateMatrix[].
368 if (m_pollingStep % 8 == 0) {
369 detectVideo();
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000370 memset(m_rateMatrix, 0, m_numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000371 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000372}
373
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000374void PollingManager::flagVideoArea(bool value)
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000375{
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000376 if (m_videoRect.is_empty())
377 return;
378
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000379 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
380 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
381
382 for (int y = r.tl.y; y < r.br.y; y++)
383 for (int x = r.tl.x; x < r.br.x; x++)
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000384 m_changeFlags[y * m_widthTiles + x] = value;
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000385}
386
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000387void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000388PollingManager::checkNeighbors()
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000389{
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000390 int x, y;
391
392 // Check neighboring pixels above and below changed tiles.
393 // FIXME: Fast skip to the first changed tile (and to the last, too).
394 // FIXME: Check the full-width line above the first changed tile?
395 for (y = 0; y < m_heightTiles; y++) {
396 bool doneAbove = false;
397 bool doneBelow = false;
398 for (x = 0; x < m_widthTiles; x++) {
399 if (!doneAbove && y > 0 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000400 m_changeFlags[y * m_widthTiles + x] &&
401 !m_changeFlags[(y - 1) * m_widthTiles + x]) {
402 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000403 checkRow(x * 32, y * 32 - 1, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000404 doneAbove = true;
405 }
406 if (!doneBelow && y < m_heightTiles - 1 &&
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000407 m_changeFlags[y * m_widthTiles + x] &&
408 !m_changeFlags[(y + 1) * m_widthTiles + x]) {
409 // FIXME: Check m_changeFlags[] to decrease height of the row.
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000410 checkRow(x * 32, (y + 1) * 32, m_width - x * 32);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000411 doneBelow = true;
412 }
413 if (doneBelow && doneAbove)
414 break;
415 }
416 }
417
418 // Check neighboring pixels at the right side of changed tiles.
419 for (x = 0; x < m_widthTiles - 1; x++) {
420 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000421 if (m_changeFlags[y * m_widthTiles + x] &&
422 !m_changeFlags[y * m_widthTiles + x + 1]) {
423 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000424 checkColumn((x + 1) * 32, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000425 &m_changeFlags[y * m_widthTiles + x + 1]);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000426 break;
427 }
428 }
429 }
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000430
431 // Check neighboring pixels at the left side of changed tiles.
432 for (x = m_widthTiles - 1; x > 0; x--) {
433 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000434 if (m_changeFlags[y * m_widthTiles + x] &&
435 !m_changeFlags[y * m_widthTiles + x - 1]) {
436 // FIXME: Check m_changeFlags[] to decrease height of the column.
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000437 checkColumn(x * 32 - 1, y * 32, m_height - y * 32,
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000438 &m_changeFlags[y * m_widthTiles + x - 1]);
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000439 break;
440 }
441 }
442 }
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000443}
444
445void
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000446PollingManager::printChanges(const char *header) const
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000447{
448 fprintf(stderr, "%s:", header);
449
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000450 const bool *pChangeFlags = m_changeFlags;
451
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000452 for (int y = 0; y < m_heightTiles; y++) {
453 for (int x = 0; x < m_widthTiles; x++) {
454 if (*pChangeFlags++) {
455 // Count successive tiles marked as changed.
456 int count = 1;
457 while (x + count < m_widthTiles && *pChangeFlags++) {
458 count++;
459 }
460 // Print.
461 fprintf(stderr, " (%d,%d)*%d", x, y, count);
462 // Skip processed tiles.
463 x += count;
464 }
465 }
466 }
467
468 fprintf(stderr, "\n");
469}
470
471void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000472PollingManager::detectVideo()
473{
474 // Configurable parameters.
475 const int VIDEO_THRESHOLD_0 = 3;
476 const int VIDEO_THRESHOLD_1 = 5;
477
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000478 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
479 // This will guarantee that the size of the video area is always a
480 // multiple of 32 pixels. This is important for hardware JPEG encoders.
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000481 if (m_width % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000482 for (int n = m_widthTiles - 1; n < m_numTiles; n += m_widthTiles)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000483 m_rateMatrix[n] = 0;
484 }
485 if (m_height % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000486 for (int n = m_numTiles - m_widthTiles; n < m_numTiles; n++)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000487 m_rateMatrix[n] = 0;
488 }
489
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000490 // First, detect candidate region that looks like video. In other
491 // words, find a region that consists of continuously changing
492 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000493 for (int i = 0; i < m_numTiles; i++) {
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000494 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
495 m_videoFlags[i] = 0;
496 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
497 m_videoFlags[i] = 1;
498 }
499 }
500
501 // Now, choose the biggest rectangle from that candidate region.
502 Rect newRect;
503 getVideoAreaRect(&newRect);
504
505 // Does new rectangle differ from the previously detected one?
506 // If it does, save new rectangle and inform the server.
507 if (!newRect.equals(m_videoRect)) {
508 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000509 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000510 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000511 vlog.debug("Detected video %dx%d at (%d,%d)",
512 newRect.width(), newRect.height(),
513 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000514 }
515 m_videoRect = newRect;
516 m_server->set_video_area(newRect);
517 }
518}
519
520void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000521PollingManager::getVideoAreaRect(Rect *result)
522{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000523 int *mx_hlen, *mx_vlen;
524 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000525
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000526 int full_h = m_heightTiles;
527 int full_w = m_widthTiles;
528 int x, y;
529 Rect max_rect(0, 0, 0, 0);
530 Rect local_rect;
531
532 for (y = 0; y < full_h; y++) {
533 for (x = 0; x < full_w; x++) {
534 int max_w = mx_hlen[y * full_w + x];
535 int max_h = mx_vlen[y * full_w + x];
536 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
537 local_rect.tl.x = x;
538 local_rect.tl.y = y;
539 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
540 if (local_rect.area() > max_rect.area()) {
541 max_rect = local_rect;
542 }
543 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000544 }
545 }
546
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000547 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000548
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000549 max_rect.tl.x *= 32;
550 max_rect.tl.y *= 32;
551 max_rect.br.x *= 32;
552 max_rect.br.y *= 32;
553 if (max_rect.br.x > m_width)
554 max_rect.br.x = m_width;
555 if (max_rect.br.y > m_height)
556 max_rect.br.y = m_height;
557 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000558}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000559
560void
561PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
562{
563 // Handy shortcuts.
564 int h = m_heightTiles;
565 int w = m_widthTiles;
566
567 // Allocate memory.
568 int *mx_h = new int[h * w];
569 memset(mx_h, 0, h * w * sizeof(int));
570 int *mx_v = new int[h * w];
571 memset(mx_v, 0, h * w * sizeof(int));
572
573 int x, y, len, i;
574
575 // Fill in horizontal length matrix.
576 for (y = 0; y < h; y++) {
577 for (x = 0; x < w; x++) {
578 len = 0;
579 while (x + len < w && m_videoFlags[y * w + x + len]) {
580 len++;
581 }
582 for (i = 0; i < len; i++) {
583 mx_h[y * w + x + i] = len - i;
584 }
585 x += len;
586 }
587 }
588
589 // Fill in vertical length matrix.
590 for (x = 0; x < w; x++) {
591 for (y = 0; y < h; y++) {
592 len = 0;
593 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
594 len++;
595 }
596 for (i = 0; i < len; i++) {
597 mx_v[(y + i) * w + x] = len - i;
598 }
599 y += len;
600 }
601 }
602
603 *pmx_h = mx_h;
604 *pmx_v = mx_v;
605}
606
607void
608PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
609{
610 delete[] mx_h;
611 delete[] mx_v;
612}
613
614// NOTE: This function assumes that current tile has non-zero in mx_h[],
615// otherwise we get division by zero.
616void
617PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
618{
619 int idx = r->tl.y * m_widthTiles + r->tl.x;
620
621 // NOTE: Rectangle's maximum width and height are 25 and 18
622 // (in tiles, where each tile is usually 32x32 pixels).
623 int max_w = mx_h[idx];
624 if (max_w > 25)
625 max_w = 25;
626 int cur_h = 18;
627
628 int best_w = max_w;
629 int best_area = 1 * best_w;
630
631 for (int i = 0; i < max_w; i++) {
632 int h = mx_v[idx + i];
633 if (h < cur_h) {
634 cur_h = h;
635 if (cur_h * max_w <= best_area)
636 break;
637 }
638 if (cur_h * (i + 1) > best_area) {
639 best_w = i + 1;
640 best_area = cur_h * best_w;
641 }
642 }
643
644 r->br.x = r->tl.x + best_w;
645 r->br.y = r->tl.y + best_area / best_w;
646}
647