blob: cdafa933f0b4db06b1b791f2930b4b139550605f [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 Kaplinsky85b5eb92008-01-17 17:41:48 +0000161#define DBG_REPORT_CHANGES(title) printChanges((title), m_changeFlags)
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 Kaplinsky85b5eb92008-01-17 17:41:48 +0000200 bool *pChangeFlags = m_changeFlags;
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 Kaplinsky808db552007-10-09 12:48:26 +0000203 nTilesChanged += checkRow(0, y, m_width, pChangeFlags);
204 pChangeFlags += m_widthTiles;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000205 }
206
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000207 // Do the work related to video area detection, if enabled.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000208 bool haveVideoRect = false;
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000209 if ((int)m_videoPriority != 0) {
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000210 handleVideo(m_changeFlags);
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000211 if (!m_videoRect.is_empty()) {
212 getScreenRect(m_videoRect);
213 haveVideoRect = true;
214 }
215 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000216
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000217 DBG_REPORT_CHANGES("After 1st pass");
218
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000219 // If some changes have been detected:
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000220 if (nTilesChanged) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000221 // Try to find more changes around. Before doing that, mark the
222 // video area as changed, to skip comparisons of its pixels.
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000223 flagVideoArea(m_changeFlags, true);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000224 DBG_REPORT_CHANGES("Before checking neighbors");
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000225 checkNeighbors(m_changeFlags);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000226 DBG_REPORT_CHANGES("After checking neighbors");
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000227
228 // Inform the server about the changes. This time, we mark the
229 // video area as NOT changed, to prevent reading its pixels again.
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000230 flagVideoArea(m_changeFlags, false);
Constantin Kaplinskybf11a2d2008-01-10 15:59:03 +0000231 DBG_REPORT_CHANGES("Before sending");
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000232 nTilesChanged = sendChanges(m_changeFlags);
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000233 }
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000234
Constantin Kaplinsky52f29d32007-12-28 18:30:54 +0000235#ifdef DEBUG_PRINT_NUM_CHANGED_TILES
236 printf("%3d ", nTilesChanged);
237 if (m_pollingStep % 32 == 0) {
238 printf("\n");
239 }
240#endif
241
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000242#ifdef DEBUG
243 if (nTilesChanged != 0) {
244 fprintf(stderr, "#%d# ", nTilesChanged);
245 }
246#endif
247
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000248 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000249}
250
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000251int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000252{
253 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
254 int bytesPerLine = m_image->xim->bytes_per_line;
255
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000256 if (x == 0 && w == m_width) {
257 getFullRow(y); // use more efficient method if possible
258 } else {
259 getRow(x, y, w);
260 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000261
262 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
263 char *ptr_new = m_rowImage->xim->data;
264
265 int nTilesChanged = 0;
266 for (int i = 0; i < (w + 31) / 32; i++) {
267 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
268 int nBytes = tile_w * bytesPerPixel;
269 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000270 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000271 nTilesChanged++;
272 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000273 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000274 ptr_old += nBytes;
275 ptr_new += nBytes;
276 }
277
278 return nTilesChanged;
279}
280
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000281int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags)
282{
283 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
284
285 getColumn(x, y, h);
286
287 int nTilesChanged = 0;
288 for (int nTile = 0; nTile < (h + 31) / 32; nTile++) {
289 if (!*pChangeFlags) {
290 int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32;
291 for (int i = 0; i < tile_h; i++) {
292 // FIXME: Provide an inline function Image::locatePixel(x, y).
293 // FIXME: Do not compute these pointers in the inner cycle.
294 char *ptr_old = (m_image->xim->data +
295 (y + nTile * 32 + i) * m_image->xim->bytes_per_line +
296 x * bytesPerPixel);
297 char *ptr_new = (m_columnImage->xim->data +
298 (nTile * 32 + i) * m_columnImage->xim->bytes_per_line);
299 if (memcmp(ptr_old, ptr_new, bytesPerPixel)) {
300 *pChangeFlags = true;
301 nTilesChanged++;
302 break;
303 }
304 }
305 }
306 pChangeFlags += m_widthTiles;
307 }
308
309 return nTilesChanged;
310}
311
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000312int PollingManager::sendChanges(const bool *pChangeFlags)
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000313{
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000314 int nTilesChanged = 0;
315
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000316 Rect rect;
317 for (int y = 0; y < m_heightTiles; y++) {
318 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000319 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000320 // Count successive tiles marked as changed.
321 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000322 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000323 count++;
324 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000325 nTilesChanged += count;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000326 // Compute the coordinates and the size of this band.
327 rect.setXYWH(x * 32, y * 32, count * 32, 32);
328 if (rect.br.x > m_width)
329 rect.br.x = m_width;
330 if (rect.br.y > m_height)
331 rect.br.y = m_height;
332 // Add to the changed region maintained by the server.
333 getScreenRect(rect);
334 m_server->add_changed(rect);
335 // Skip processed tiles.
336 x += count;
337 }
338 }
339 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000340 return nTilesChanged;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000341}
342
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000343void PollingManager::handleVideo(const bool *pChangeFlags)
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000344{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000345 // Update counters in m_rateMatrix.
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000346 for (int i = 0; i < m_numTiles; i++)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000347 m_rateMatrix[i] += (pChangeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000348
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000349 // Once per eight calls: detect video rectangle by examining
350 // m_rateMatrix[], then reset counters in m_rateMatrix[].
351 if (m_pollingStep % 8 == 0) {
352 detectVideo();
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000353 memset(m_rateMatrix, 0, m_numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000354 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000355}
356
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000357void PollingManager::flagVideoArea(bool *pChangeFlags, bool value)
358{
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000359 if (m_videoRect.is_empty())
360 return;
361
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000362 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
363 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
364
365 for (int y = r.tl.y; y < r.br.y; y++)
366 for (int x = r.tl.x; x < r.br.x; x++)
367 pChangeFlags[y * m_widthTiles + x] = value;
368}
369
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000370void
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000371PollingManager::checkNeighbors(bool *pChangeFlags)
372{
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000373 int x, y;
374
375 // Check neighboring pixels above and below changed tiles.
376 // FIXME: Fast skip to the first changed tile (and to the last, too).
377 // FIXME: Check the full-width line above the first changed tile?
378 for (y = 0; y < m_heightTiles; y++) {
379 bool doneAbove = false;
380 bool doneBelow = false;
381 for (x = 0; x < m_widthTiles; x++) {
382 if (!doneAbove && y > 0 &&
383 pChangeFlags[y * m_widthTiles + x] &&
384 !pChangeFlags[(y - 1) * m_widthTiles + x]) {
385 // FIXME: Check pChangeFlags[] to decrease height of the row.
386 checkRow(x * 32, y * 32 - 1, m_width - x * 32,
387 &pChangeFlags[(y - 1) * m_widthTiles + x]);
388 doneAbove = true;
389 }
390 if (!doneBelow && y < m_heightTiles - 1 &&
391 pChangeFlags[y * m_widthTiles + x] &&
392 !pChangeFlags[(y + 1) * m_widthTiles + x]) {
393 // FIXME: Check pChangeFlags[] to decrease height of the row.
394 checkRow(x * 32, (y + 1) * 32, m_width - x * 32,
395 &pChangeFlags[(y + 1) * m_widthTiles + x]);
396 doneBelow = true;
397 }
398 if (doneBelow && doneAbove)
399 break;
400 }
401 }
402
403 // Check neighboring pixels at the right side of changed tiles.
404 for (x = 0; x < m_widthTiles - 1; x++) {
405 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000406 if (pChangeFlags[y * m_widthTiles + x] &&
407 !pChangeFlags[y * m_widthTiles + x + 1]) {
408 // FIXME: Check pChangeFlags[] to decrease height of the column.
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000409 checkColumn((x + 1) * 32, y * 32, m_height - y * 32,
410 &pChangeFlags[y * m_widthTiles + x + 1]);
411 break;
412 }
413 }
414 }
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000415
416 // Check neighboring pixels at the left side of changed tiles.
417 for (x = m_widthTiles - 1; x > 0; x--) {
418 for (y = 0; y < m_heightTiles; y++) {
419 if (pChangeFlags[y * m_widthTiles + x] &&
420 !pChangeFlags[y * m_widthTiles + x - 1]) {
421 // FIXME: Check pChangeFlags[] to decrease height of the column.
422 checkColumn(x * 32 - 1, y * 32, m_height - y * 32,
423 &pChangeFlags[y * m_widthTiles + x - 1]);
424 break;
425 }
426 }
427 }
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000428}
429
430void
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000431PollingManager::printChanges(const char *header, const bool *pChangeFlags)
432{
433 fprintf(stderr, "%s:", header);
434
435 for (int y = 0; y < m_heightTiles; y++) {
436 for (int x = 0; x < m_widthTiles; x++) {
437 if (*pChangeFlags++) {
438 // Count successive tiles marked as changed.
439 int count = 1;
440 while (x + count < m_widthTiles && *pChangeFlags++) {
441 count++;
442 }
443 // Print.
444 fprintf(stderr, " (%d,%d)*%d", x, y, count);
445 // Skip processed tiles.
446 x += count;
447 }
448 }
449 }
450
451 fprintf(stderr, "\n");
452}
453
454void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000455PollingManager::detectVideo()
456{
457 // Configurable parameters.
458 const int VIDEO_THRESHOLD_0 = 3;
459 const int VIDEO_THRESHOLD_1 = 5;
460
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000461 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
462 // This will guarantee that the size of the video area is always a
463 // multiple of 32 pixels. This is important for hardware JPEG encoders.
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000464 if (m_width % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000465 for (int n = m_widthTiles - 1; n < m_numTiles; n += m_widthTiles)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000466 m_rateMatrix[n] = 0;
467 }
468 if (m_height % 32 != 0) {
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000469 for (int n = m_numTiles - m_widthTiles; n < m_numTiles; n++)
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000470 m_rateMatrix[n] = 0;
471 }
472
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000473 // First, detect candidate region that looks like video. In other
474 // words, find a region that consists of continuously changing
475 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000476 for (int i = 0; i < m_numTiles; i++) {
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000477 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
478 m_videoFlags[i] = 0;
479 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
480 m_videoFlags[i] = 1;
481 }
482 }
483
484 // Now, choose the biggest rectangle from that candidate region.
485 Rect newRect;
486 getVideoAreaRect(&newRect);
487
488 // Does new rectangle differ from the previously detected one?
489 // If it does, save new rectangle and inform the server.
490 if (!newRect.equals(m_videoRect)) {
491 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000492 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000493 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000494 vlog.debug("Detected video %dx%d at (%d,%d)",
495 newRect.width(), newRect.height(),
496 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000497 }
498 m_videoRect = newRect;
499 m_server->set_video_area(newRect);
500 }
501}
502
503void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000504PollingManager::getVideoAreaRect(Rect *result)
505{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000506 int *mx_hlen, *mx_vlen;
507 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000508
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000509 int full_h = m_heightTiles;
510 int full_w = m_widthTiles;
511 int x, y;
512 Rect max_rect(0, 0, 0, 0);
513 Rect local_rect;
514
515 for (y = 0; y < full_h; y++) {
516 for (x = 0; x < full_w; x++) {
517 int max_w = mx_hlen[y * full_w + x];
518 int max_h = mx_vlen[y * full_w + x];
519 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
520 local_rect.tl.x = x;
521 local_rect.tl.y = y;
522 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
523 if (local_rect.area() > max_rect.area()) {
524 max_rect = local_rect;
525 }
526 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000527 }
528 }
529
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000530 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000531
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000532 max_rect.tl.x *= 32;
533 max_rect.tl.y *= 32;
534 max_rect.br.x *= 32;
535 max_rect.br.y *= 32;
536 if (max_rect.br.x > m_width)
537 max_rect.br.x = m_width;
538 if (max_rect.br.y > m_height)
539 max_rect.br.y = m_height;
540 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000541}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000542
543void
544PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
545{
546 // Handy shortcuts.
547 int h = m_heightTiles;
548 int w = m_widthTiles;
549
550 // Allocate memory.
551 int *mx_h = new int[h * w];
552 memset(mx_h, 0, h * w * sizeof(int));
553 int *mx_v = new int[h * w];
554 memset(mx_v, 0, h * w * sizeof(int));
555
556 int x, y, len, i;
557
558 // Fill in horizontal length matrix.
559 for (y = 0; y < h; y++) {
560 for (x = 0; x < w; x++) {
561 len = 0;
562 while (x + len < w && m_videoFlags[y * w + x + len]) {
563 len++;
564 }
565 for (i = 0; i < len; i++) {
566 mx_h[y * w + x + i] = len - i;
567 }
568 x += len;
569 }
570 }
571
572 // Fill in vertical length matrix.
573 for (x = 0; x < w; x++) {
574 for (y = 0; y < h; y++) {
575 len = 0;
576 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
577 len++;
578 }
579 for (i = 0; i < len; i++) {
580 mx_v[(y + i) * w + x] = len - i;
581 }
582 y += len;
583 }
584 }
585
586 *pmx_h = mx_h;
587 *pmx_v = mx_v;
588}
589
590void
591PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
592{
593 delete[] mx_h;
594 delete[] mx_v;
595}
596
597// NOTE: This function assumes that current tile has non-zero in mx_h[],
598// otherwise we get division by zero.
599void
600PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
601{
602 int idx = r->tl.y * m_widthTiles + r->tl.x;
603
604 // NOTE: Rectangle's maximum width and height are 25 and 18
605 // (in tiles, where each tile is usually 32x32 pixels).
606 int max_w = mx_h[idx];
607 if (max_w > 25)
608 max_w = 25;
609 int cur_h = 18;
610
611 int best_w = max_w;
612 int best_area = 1 * best_w;
613
614 for (int i = 0; i < max_w; i++) {
615 int h = mx_v[idx + i];
616 if (h < cur_h) {
617 cur_h = h;
618 if (cur_h * max_w <= best_area)
619 break;
620 }
621 if (cur_h * (i + 1) > best_area) {
622 best_w = i + 1;
623 best_area = cur_h * best_w;
624 }
625 }
626
627 r->br.x = r->tl.x + best_w;
628 r->br.y = r->tl.y + best_area / best_w;
629}
630