blob: 5cd611bd99e452867cd973dd95733fec40968b5f [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
42//
43// Constructor.
44//
45// Note that dpy and image should remain valid during the object
46// lifetime, while factory is used only in the constructor itself.
47//
48
49PollingManager::PollingManager(Display *dpy, Image *image,
50 ImageFactory *factory,
51 int offsetLeft, int offsetTop)
52 : m_dpy(dpy), m_server(0), m_image(image),
53 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000054 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055{
56 // Save width and height of the screen (and the image).
57 m_width = m_image->xim->width;
58 m_height = m_image->xim->height;
59
60 // Compute width and height in 32x32 tiles.
61 m_widthTiles = (m_width + 31) / 32;
62 m_heightTiles = (m_height + 31) / 32;
63
64 // Get initial screen image.
65 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
66
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000067 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000068 // underlying class names are different from the class name of the
69 // primary image.
70 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000071 if (strcmp(m_image->className(), m_rowImage->className()) != 0) {
72 vlog.error("Image types do not match (%s)",
73 m_rowImage->className());
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000074 }
75
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000076 int numTiles = m_widthTiles * m_heightTiles;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000077 m_rateMatrix = new char[numTiles];
78 m_videoFlags = new char[numTiles];
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000079 memset(m_rateMatrix, 0, numTiles);
80 memset(m_videoFlags, 0, numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000081}
82
83PollingManager::~PollingManager()
84{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000085 delete[] m_videoFlags;
86 delete[] m_rateMatrix;
87
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000088 delete m_rowImage;
89}
90
91//
92// Register VNCServer object.
93//
94
95void PollingManager::setVNCServer(VNCServer *s)
96{
97 m_server = s;
98}
99
100//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000101// DEBUG: Measuring time spent in the poll() function,
102// as well as time intervals between poll() calls.
103//
104
105#ifdef DEBUG
106void PollingManager::debugBeforePoll()
107{
108 TimeMillis timeNow;
109 int diff = timeNow.diffFrom(m_timeSaved);
110 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
111 m_timeSaved = timeNow;
112}
113
114void PollingManager::debugAfterPoll()
115{
116 TimeMillis timeNow;
117 int diff = timeNow.diffFrom(m_timeSaved);
118 fprintf(stderr, "[poll%4dms]\n", diff);
119 m_timeSaved = timeNow;
120}
121
122#endif
123
124//
125// Search for changed rectangles on the screen.
126//
127
128void PollingManager::poll()
129{
130#ifdef DEBUG
131 debugBeforePoll();
132#endif
133
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000134 // Perform polling and try update clients if changes were detected.
135 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000136 m_server->tryUpdate();
137
138#ifdef DEBUG
139 debugAfterPoll();
140#endif
141}
142
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000143bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000144{
145 if (!m_server)
146 return false;
147
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000148 // changeFlags[] array will hold boolean values corresponding to
149 // each 32x32 tile. If a value is true, then we've detected a change
150 // in that tile. Initially, we fill in the array with zero values.
151 bool *changeFlags = new bool[m_widthTiles * m_heightTiles];
152 memset(changeFlags, 0, m_widthTiles * m_heightTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000153
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000154 // First pass over the framebuffer. Here we scan 1/32 part of the
155 // framebuffer -- that is, one line in each (32 * m_width) stripe.
156 // We compare the pixels of that line with previous framebuffer
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000157 // contents and raise corresponding member values of changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000158 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000159 bool *pChangeFlags = changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000160 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000161 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000162 nTilesChanged += checkRow(0, y, m_width, pChangeFlags);
163 pChangeFlags += m_widthTiles;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000164 }
165
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000166 // Do the work related to video area detection.
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000167 bool haveVideoRect = handleVideo(changeFlags);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000168
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000169 // Inform the server about the changes.
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000170 // FIXME: It's possible that (nTilesChanged != 0) but changeFlags[]
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000171 // array is empty. That's possible because handleVideo()
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000172 // modifies changeFlags[].
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000173 if (nTilesChanged)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000174 sendChanges(changeFlags);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000175
176 // Cleanup.
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000177 delete[] changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000178
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000179#ifdef DEBUG
180 if (nTilesChanged != 0) {
181 fprintf(stderr, "#%d# ", nTilesChanged);
182 }
183#endif
184
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000185 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000186}
187
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000188int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000189{
190 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
191 int bytesPerLine = m_image->xim->bytes_per_line;
192
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000193 if (x == 0 && w == m_width) {
194 getFullRow(y); // use more efficient method if possible
195 } else {
196 getRow(x, y, w);
197 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000198
199 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
200 char *ptr_new = m_rowImage->xim->data;
201
202 int nTilesChanged = 0;
203 for (int i = 0; i < (w + 31) / 32; i++) {
204 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
205 int nBytes = tile_w * bytesPerPixel;
206 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000207 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000208 nTilesChanged++;
209 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000210 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000211 ptr_old += nBytes;
212 ptr_new += nBytes;
213 }
214
215 return nTilesChanged;
216}
217
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000218void PollingManager::sendChanges(bool *pChangeFlags)
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000219{
220 Rect rect;
221 for (int y = 0; y < m_heightTiles; y++) {
222 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000223 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000224 // Count successive tiles marked as changed.
225 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000226 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000227 count++;
228 }
229 // Compute the coordinates and the size of this band.
230 rect.setXYWH(x * 32, y * 32, count * 32, 32);
231 if (rect.br.x > m_width)
232 rect.br.x = m_width;
233 if (rect.br.y > m_height)
234 rect.br.y = m_height;
235 // Add to the changed region maintained by the server.
236 getScreenRect(rect);
237 m_server->add_changed(rect);
238 // Skip processed tiles.
239 x += count;
240 }
241 }
242 }
243}
244
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000245bool PollingManager::handleVideo(bool *pChangeFlags)
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000246{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000247 // Update counters in m_rateMatrix.
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000248 int numTiles = m_heightTiles * m_widthTiles;
249 for (int i = 0; i < numTiles; i++)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000250 m_rateMatrix[i] += (pChangeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000251
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000252 // Once per eight calls: detect video rectangle by examining
253 // m_rateMatrix[], then reset counters in m_rateMatrix[].
254 if (m_pollingStep % 8 == 0) {
255 detectVideo();
256 memset(m_rateMatrix, 0, numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000257 }
258
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000259 // Grab the pixels of video area. Also, exclude video rectangle from
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000260 // pChangeFlags[], to prevent grabbing the same pixels twice.
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000261 if (!m_videoRect.is_empty()) {
262 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
263 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
264 for (int y = r.tl.y; y < r.br.y; y++) {
265 for (int x = r.tl.x; x < r.br.x; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000266 pChangeFlags[y * m_widthTiles + x] = false;
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000267 }
268 }
269 getScreenRect(m_videoRect);
270 return true; // we've got a video rectangle
271 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000272
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000273 return false; // video rectangle is empty
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000274}
275
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000276void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000277PollingManager::detectVideo()
278{
279 // Configurable parameters.
280 const int VIDEO_THRESHOLD_0 = 3;
281 const int VIDEO_THRESHOLD_1 = 5;
282
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000283 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
284 // This will guarantee that the size of the video area is always a
285 // multiple of 32 pixels. This is important for hardware JPEG encoders.
286 int numTiles = m_heightTiles * m_widthTiles;
287 if (m_width % 32 != 0) {
288 for (int n = m_widthTiles - 1; n < numTiles; n += m_widthTiles)
289 m_rateMatrix[n] = 0;
290 }
291 if (m_height % 32 != 0) {
292 for (int n = numTiles - m_widthTiles; n < numTiles; n++)
293 m_rateMatrix[n] = 0;
294 }
295
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000296 // First, detect candidate region that looks like video. In other
297 // words, find a region that consists of continuously changing
298 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000299 for (int i = 0; i < numTiles; i++) {
300 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
301 m_videoFlags[i] = 0;
302 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
303 m_videoFlags[i] = 1;
304 }
305 }
306
307 // Now, choose the biggest rectangle from that candidate region.
308 Rect newRect;
309 getVideoAreaRect(&newRect);
310
311 // Does new rectangle differ from the previously detected one?
312 // If it does, save new rectangle and inform the server.
313 if (!newRect.equals(m_videoRect)) {
314 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000315 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000316 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000317 vlog.debug("Detected video %dx%d at (%d,%d)",
318 newRect.width(), newRect.height(),
319 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000320 }
321 m_videoRect = newRect;
322 m_server->set_video_area(newRect);
323 }
324}
325
326void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000327PollingManager::getVideoAreaRect(Rect *result)
328{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000329 int *mx_hlen, *mx_vlen;
330 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000331
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000332 int full_h = m_heightTiles;
333 int full_w = m_widthTiles;
334 int x, y;
335 Rect max_rect(0, 0, 0, 0);
336 Rect local_rect;
337
338 for (y = 0; y < full_h; y++) {
339 for (x = 0; x < full_w; x++) {
340 int max_w = mx_hlen[y * full_w + x];
341 int max_h = mx_vlen[y * full_w + x];
342 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
343 local_rect.tl.x = x;
344 local_rect.tl.y = y;
345 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
346 if (local_rect.area() > max_rect.area()) {
347 max_rect = local_rect;
348 }
349 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000350 }
351 }
352
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000353 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000354
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000355 max_rect.tl.x *= 32;
356 max_rect.tl.y *= 32;
357 max_rect.br.x *= 32;
358 max_rect.br.y *= 32;
359 if (max_rect.br.x > m_width)
360 max_rect.br.x = m_width;
361 if (max_rect.br.y > m_height)
362 max_rect.br.y = m_height;
363 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000364}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000365
366void
367PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
368{
369 // Handy shortcuts.
370 int h = m_heightTiles;
371 int w = m_widthTiles;
372
373 // Allocate memory.
374 int *mx_h = new int[h * w];
375 memset(mx_h, 0, h * w * sizeof(int));
376 int *mx_v = new int[h * w];
377 memset(mx_v, 0, h * w * sizeof(int));
378
379 int x, y, len, i;
380
381 // Fill in horizontal length matrix.
382 for (y = 0; y < h; y++) {
383 for (x = 0; x < w; x++) {
384 len = 0;
385 while (x + len < w && m_videoFlags[y * w + x + len]) {
386 len++;
387 }
388 for (i = 0; i < len; i++) {
389 mx_h[y * w + x + i] = len - i;
390 }
391 x += len;
392 }
393 }
394
395 // Fill in vertical length matrix.
396 for (x = 0; x < w; x++) {
397 for (y = 0; y < h; y++) {
398 len = 0;
399 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
400 len++;
401 }
402 for (i = 0; i < len; i++) {
403 mx_v[(y + i) * w + x] = len - i;
404 }
405 y += len;
406 }
407 }
408
409 *pmx_h = mx_h;
410 *pmx_v = mx_v;
411}
412
413void
414PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
415{
416 delete[] mx_h;
417 delete[] mx_v;
418}
419
420// NOTE: This function assumes that current tile has non-zero in mx_h[],
421// otherwise we get division by zero.
422void
423PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
424{
425 int idx = r->tl.y * m_widthTiles + r->tl.x;
426
427 // NOTE: Rectangle's maximum width and height are 25 and 18
428 // (in tiles, where each tile is usually 32x32 pixels).
429 int max_w = mx_h[idx];
430 if (max_w > 25)
431 max_w = 25;
432 int cur_h = 18;
433
434 int best_w = max_w;
435 int best_area = 1 * best_w;
436
437 for (int i = 0; i < max_w; i++) {
438 int h = mx_v[idx + i];
439 if (h < cur_h) {
440 cur_h = h;
441 if (cur_h * max_w <= best_area)
442 break;
443 }
444 if (cur_h * (i + 1) > best_area) {
445 best_w = i + 1;
446 best_area = cur_h * best_w;
447 }
448 }
449
450 r->br.x = r->tl.x + best_w;
451 r->br.y = r->tl.y + best_area / best_w;
452}
453