blob: d10dde7413533c90c5e010236e027649f38f3ddb [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2004-2005 Constantin Kaplinsky. All Rights Reserved.
2 *
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
22// FIXME: Don't compare pixels already marked as changed.
23// FIXME: Use Image::copyPixels() instead of Image::updateRect()?
24// In that case, note the fact that arguments are not checked.
25
26#include <stdio.h>
27#include <string.h>
28#include <time.h>
29#include <X11/Xlib.h>
30#include <rfb/LogWriter.h>
31#include <rfb/VNCServer.h>
32#include <rfb/Configuration.h>
33#include <rfb/ServerCore.h>
34
35#include <x0vncserver/PollingManager.h>
36
37static LogWriter vlog("PollingMgr");
38
39BoolParameter PollingManager::pollPointer
40("PollPointer",
41 "DEBUG: Poll area under the pointer with higher priority",
Constantin Kaplinsky344c3fe2007-07-24 08:35:43 +000042 false);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000043
44IntParameter PollingManager::pollingType
45("PollingType",
Constantin Kaplinskya119b482007-10-04 06:02:02 +000046 "DEBUG: Select particular polling algorithm (0..4)",
47 4);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000048
49const int PollingManager::m_pollingOrder[32] = {
50 0, 16, 8, 24, 4, 20, 12, 28,
51 10, 26, 18, 2, 22, 6, 30, 14,
52 1, 17, 9, 25, 7, 23, 15, 31,
53 19, 3, 27, 11, 29, 13, 5, 21
54};
55
56//
57// Constructor.
58//
59// Note that dpy and image should remain valid during the object
60// lifetime, while factory is used only in the constructor itself.
61//
62
63PollingManager::PollingManager(Display *dpy, Image *image,
64 ImageFactory *factory,
65 int offsetLeft, int offsetTop)
66 : m_dpy(dpy), m_server(0), m_image(image),
67 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
68 m_pointerPosKnown(false), m_pollingStep(0)
69{
70 // Save width and height of the screen (and the image).
71 m_width = m_image->xim->width;
72 m_height = m_image->xim->height;
73
74 // Compute width and height in 32x32 tiles.
75 m_widthTiles = (m_width + 31) / 32;
76 m_heightTiles = (m_height + 31) / 32;
77
78 // Get initial screen image.
79 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
80
81 // Create additional images used in polling algorithms, warn if
82 // underlying class names are different from the class name of the
83 // primary image.
84 m_rowImage = factory->newImage(m_dpy, m_width, 1);
85 m_tileImage = factory->newImage(m_dpy, 32, 32);
86 m_areaImage = factory->newImage(m_dpy, 128, 128);
87 if (strcmp(m_image->className(), m_rowImage->className()) != 0 ||
88 strcmp(m_image->className(), m_tileImage->className()) != 0 ||
89 strcmp(m_image->className(), m_areaImage->className()) != 0) {
90 vlog.error("Image types do not match (%s, %s, %s)",
91 m_rowImage->className(),
92 m_tileImage->className(),
93 m_areaImage->className());
94 }
95
96 // FIXME: Extend the comment.
97 // Create a matrix with one byte per each 32x32 tile. It will be
98 // used to limit the rate of updates on continuously-changed screen
99 // areas (like video).
100 int numTiles = m_widthTiles * m_heightTiles;
101 m_statusMatrix = new char[numTiles];
102 memset(m_statusMatrix, 0, numTiles);
103
104 // FIXME: Extend the comment.
105 // Create a matrix with one byte per each 32x32 tile. It will be
106 // used to limit the rate of updates on continuously-changed screen
107 // areas (like video).
108 m_rateMatrix = new char[numTiles];
109 m_videoFlags = new char[numTiles];
110 m_changedFlags = new char[numTiles];
111 memset(m_rateMatrix, 0, numTiles);
112 memset(m_videoFlags, 0, numTiles);
113 memset(m_changedFlags, 0, numTiles);
114}
115
116PollingManager::~PollingManager()
117{
118 delete[] m_changedFlags;
119 delete[] m_videoFlags;
120 delete[] m_rateMatrix;
121
122 delete[] m_statusMatrix;
123
124 delete m_areaImage;
125 delete m_tileImage;
126 delete m_rowImage;
127}
128
129//
130// Register VNCServer object.
131//
132
133void PollingManager::setVNCServer(VNCServer *s)
134{
135 m_server = s;
136}
137
138//
139// Update current pointer position which may be used as a hint for
140// polling algorithms.
141//
142
143void PollingManager::setPointerPos(const Point &pos)
144{
145 m_pointerPosTime = time(NULL);
146 m_pointerPos = pos;
147 m_pointerPosKnown = true;
148}
149
150//
151// Indicate that current pointer position is unknown.
152//
153
154void PollingManager::unsetPointerPos()
155{
156 m_pointerPosKnown = false;
157}
158
159//
160// DEBUG: Measuring time spent in the poll() function,
161// as well as time intervals between poll() calls.
162//
163
164#ifdef DEBUG
165void PollingManager::debugBeforePoll()
166{
167 TimeMillis timeNow;
168 int diff = timeNow.diffFrom(m_timeSaved);
169 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
170 m_timeSaved = timeNow;
171}
172
173void PollingManager::debugAfterPoll()
174{
175 TimeMillis timeNow;
176 int diff = timeNow.diffFrom(m_timeSaved);
177 fprintf(stderr, "[poll%4dms]\n", diff);
178 m_timeSaved = timeNow;
179}
180
181#endif
182
183//
184// Search for changed rectangles on the screen.
185//
186
187void PollingManager::poll()
188{
189#ifdef DEBUG
190 debugBeforePoll();
191#endif
192
193 // First step: full-screen polling.
194
195 bool changes1 = false;
196
197 switch((int)pollingType) {
198 case 0:
199 changes1 = poll_Dumb();
200 break;
201 case 1:
202 changes1 = poll_Traditional();
203 break;
204 case 2:
205 changes1 = poll_SkipCycles();
206 break;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000207 case 3:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000208 changes1 = poll_DetectVideo();
209 break;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000210//case 4:
211 default:
212 changes1 = poll_New();
213 break;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000214 }
215
216 // Second step: optional thorough polling of the area around the pointer.
217 // We do that only if the pointer position is known and was set recently.
218
219 bool changes2 = false;
220 if (pollPointer) {
221 if (m_pointerPosKnown && time(NULL) - m_pointerPosTime >= 5) {
222 unsetPointerPos();
223 }
224 if (m_pointerPosKnown) {
225 changes2 = pollPointerArea();
226 }
227 }
228
229 // Update if needed.
230
231 if (changes1 || changes2)
232 m_server->tryUpdate();
233
234#ifdef DEBUG
235 debugAfterPoll();
236#endif
237}
238
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000239bool PollingManager::poll_New()
240{
241 if (!m_server)
242 return false;
243
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000244 // mxChanged[] array will hold boolean values corresponding to each
245 // 32x32 tile. If a value is true, then we've detected a change in
246 // that tile.
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000247 bool *mxChanged = new bool[m_widthTiles * m_heightTiles];
248 memset(mxChanged, 0, m_widthTiles * m_heightTiles);
249
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000250 // First pass over the framebuffer. Here we scan 1/32 part of the
251 // framebuffer -- that is, one line in each (32 * m_width) stripe.
252 // We compare the pixels of that line with previous framebuffer
253 // contents and raise corresponding member values of mxChanged[].
254 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
255 bool *pmxChanged = mxChanged;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000256 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000257 for (int y = scanOffset; y < m_height; y += 32) {
258 nTilesChanged += checkRow(0, y, m_width, pmxChanged);
259 pmxChanged += m_widthTiles;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000260 }
261
262 // Inform the server about the changes.
263 if (nTilesChanged) {
264 bool *pmxChanged = mxChanged;
265 Rect rect;
266 for (int y = 0; y < m_heightTiles; y++) {
267 for (int x = 0; x < m_widthTiles; x++) {
268 if (*pmxChanged++) {
269 // Count successive tiles marked as changed.
270 int count = 1;
271 while (x + count < m_widthTiles && *pmxChanged++) {
272 count++;
273 }
274 // Compute the coordinates and the size of this band.
275 rect.setXYWH(x * 32, y * 32, count * 32, 32);
276 if (rect.br.x > m_width)
277 rect.br.x = m_width;
278 if (rect.br.y > m_height)
279 rect.br.y = m_height;
280 // Add to the changed region maintained by the server.
281 getScreenRect(rect);
282 m_server->add_changed(rect);
283 // Skip processed tiles.
284 x += count;
285 }
286 }
287 }
288 }
289
290 // Cleanup.
291 delete[] mxChanged;
292
293 return (nTilesChanged != 0);
294}
295
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000296int PollingManager::checkRow(int x, int y, int w, bool *pmxChanged)
297{
298 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
299 int bytesPerLine = m_image->xim->bytes_per_line;
300
301 getRow(x, y, w);
302
303 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
304 char *ptr_new = m_rowImage->xim->data;
305
306 int nTilesChanged = 0;
307 for (int i = 0; i < (w + 31) / 32; i++) {
308 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
309 int nBytes = tile_w * bytesPerPixel;
310 if (memcmp(ptr_old, ptr_new, nBytes)) {
311 *pmxChanged = true;
312 nTilesChanged++;
313 }
314 pmxChanged++;
315 ptr_old += nBytes;
316 ptr_new += nBytes;
317 }
318
319 return nTilesChanged;
320}
321
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000322bool PollingManager::poll_DetectVideo()
323{
324 if (!m_server)
325 return false;
326
327 const int GRAND_STEP_DIVISOR = 8;
328 const int VIDEO_THRESHOLD_0 = 3;
329 const int VIDEO_THRESHOLD_1 = 5;
330
331 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
332
333 // FIXME: Save shortcuts in member variables?
334 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
335 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
336 int bytesPerLine = m_image->xim->bytes_per_line;
337
338 Rect rect;
339 int nTilesChanged = 0;
340 int idx = 0;
341
342 for (int y = 0; y * 32 < m_height; y++) {
343 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
344 if (scanLine >= tile_h)
345 break;
346 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000347 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000348 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
349 char *ptr_new = m_rowImage->xim->data;
350 for (int x = 0; x * 32 < m_width; x++) {
351 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
352 int nBytes = tile_w * bytesPerPixel;
353
354 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
355 m_rateMatrix[idx] += wasChanged;
356
357 if (grandStep) {
358 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
359 m_videoFlags[idx] = 0;
360 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
361 m_videoFlags[idx] = 1;
362 }
363 m_rateMatrix[idx] = 0;
364 }
365
366 m_changedFlags[idx] |= wasChanged;
367 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
368 getTile32(x, y, tile_w, tile_h);
369 m_image->updateRect(m_tileImage, x * 32, y * 32);
370 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
371 m_server->add_changed(rect);
372 nTilesChanged++;
373 m_changedFlags[idx] = 0;
374 }
375
376 ptr_old += nBytes;
377 ptr_new += nBytes;
378 idx++;
379 }
380 }
381
382 if (grandStep)
383 adjustVideoArea();
384
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000385 bool videoDetected = false;
386 Rect r;
387 getVideoAreaRect(&r);
388 m_server->set_video_area(r);
389 videoDetected = !r.is_empty();
390 if (videoDetected) {
391 m_image->get(DefaultRootWindow(m_dpy),
392 m_offsetLeft + r.tl.x, m_offsetTop + r.tl.y,
393 r.width(), r.height(), r.tl.x, r.tl.y);
394 }
395
Constantin Kaplinsky344c3fe2007-07-24 08:35:43 +0000396#ifdef DEBUG
397 if (nTilesChanged != 0) {
398 fprintf(stderr, "#%d# ", nTilesChanged);
399 }
400#endif
401
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000402 return (nTilesChanged != 0 || videoDetected);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000403}
404
405bool PollingManager::poll_SkipCycles()
406{
407 if (!m_server)
408 return false;
409
410 enum {
411 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
412 };
413
414 bool grandStep = (m_pollingStep % 8 == 0);
415
416 int nTilesChanged = 0;
417 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
418 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
419 int bytesPerLine = m_image->xim->bytes_per_line;
420 char *pstatus = m_statusMatrix;
421 bool wasChanged;
422 Rect rect;
423
424 for (int y = 0; y * 32 < m_height; y++) {
425 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
426 if (scanLine >= tile_h)
427 scanLine %= tile_h;
428 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000429 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000430 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
431 char *ptr_new = m_rowImage->xim->data;
432 for (int x = 0; x * 32 < m_width; x++) {
433 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
434 int nBytes = tile_w * bytesPerPixel;
435
436 if (grandStep || *pstatus != CHANGED_AGAIN) {
437 wasChanged = (*pstatus == CHANGED_AGAIN) ?
438 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
439 if (wasChanged) {
440 if (grandStep || *pstatus == NOT_CHANGED) {
441 getTile32(x, y, tile_w, tile_h);
442 m_image->updateRect(m_tileImage, x * 32, y * 32);
443 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
444 m_server->add_changed(rect);
445 nTilesChanged++;
446 *pstatus = CHANGED_ONCE;
447 } else {
448 *pstatus = CHANGED_AGAIN;
449 }
450 } else if (grandStep) {
451 *pstatus = NOT_CHANGED;
452 }
453 }
454
455 ptr_old += nBytes;
456 ptr_new += nBytes;
457 pstatus++;
458 }
459 }
460
461 return (nTilesChanged != 0);
462}
463
464bool PollingManager::poll_Traditional()
465{
466 if (!m_server)
467 return false;
468
469 int nTilesChanged = 0;
470 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
471 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
472 int bytesPerLine = m_image->xim->bytes_per_line;
473 Rect rect;
474
475 for (int y = 0; y * 32 < m_height; y++) {
476 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
477 if (scanLine >= tile_h)
478 break;
479 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000480 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000481 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
482 char *ptr_new = m_rowImage->xim->data;
483 for (int x = 0; x * 32 < m_width; x++) {
484 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
485 int nBytes = tile_w * bytesPerPixel;
486 if (memcmp(ptr_old, ptr_new, nBytes)) {
487 getTile32(x, y, tile_w, tile_h);
488 m_image->updateRect(m_tileImage, x * 32, y * 32);
489 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
490 m_server->add_changed(rect);
491 nTilesChanged++;
492 }
493 ptr_old += nBytes;
494 ptr_new += nBytes;
495 }
496 }
497
498 return (nTilesChanged != 0);
499}
500
501//
502// Simplest polling method, from the original x0vncserver of VNC4.
503//
504
505bool PollingManager::poll_Dumb()
506{
507 if (!m_server)
508 return false;
509
510 getScreen();
511 Rect rect(0, 0, m_width, m_height);
512 m_server->add_changed(rect);
513
514 // Report that some changes have been detected.
515 return true;
516}
517
518//
519// Compute coordinates of the rectangle around the pointer.
520//
521// ASSUMES: (m_pointerPosKnown != false)
522//
523
524void PollingManager::computePointerArea(Rect *r)
525{
526 int x = m_pointerPos.x - 64;
527 int y = m_pointerPos.y - 64;
528 int w = 128;
529 int h = 128;
530 if (x < 0) {
531 w += x; x = 0;
532 }
533 if (x + w > m_width) {
534 w = m_width - x;
535 }
536 if (y < 0) {
537 h += y; y = 0;
538 }
539 if (y + h > m_height) {
540 h = m_height - y;
541 }
542
543 r->setXYWH(x, y, w, h);
544}
545
546//
547// Poll the area under current pointer position. Each pixel of the
548// area should be compared. Using such polling option gives higher
549// priority to screen area under the pointer.
550//
551// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
552//
553
554bool PollingManager::pollPointerArea()
555{
556 Rect r;
557 computePointerArea(&r);
558
559 // Shortcuts for coordinates.
560 int x = r.tl.x, y = r.tl.y;
561 int w = r.width(), h = r.height();
562
563 // Get new pixels.
564 getArea128(x, y, w, h);
565
566 // Now, try to minimize the rectangle by cutting out unchanged
567 // borders (at top and bottom).
568 //
569 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
570 // to produce a region instead of a rectangle. If there would
571 // be just one universal polling algorithm, it could be
572 // better to integrate pointer area polling into that
573 // algorithm, instead of a separate pollPointerArea()
574 // function.
575
576 // Shortcuts.
577 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
578 int oldBytesPerLine = m_image->xim->bytes_per_line;
579 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
580 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
581 char *newPtr = m_areaImage->xim->data;
582
583 // Check and cut out unchanged rows at the top.
584 int ty;
585 for (ty = 0; ty < h; ty++) {
586 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
587 break;
588 oldPtr += oldBytesPerLine;
589 newPtr += newBytesPerLine;
590 }
591 if (ty == h) {
592 return false; // no changes at all
593 }
594 y += ty; h -= ty;
595
596 // Check and cut out unchanged rows at the bottom.
597 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
598 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
599 int by;
600 for (by = 0; by < h - 1; by++) {
601 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
602 break;
603 oldPtr -= oldBytesPerLine;
604 newPtr -= newBytesPerLine;
605 }
606 h -= by;
607
608 // Copy pixels.
609 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
610
611 // Report updates to the server.
612 Rect rect(x, y, x+w, y+h);
613 m_server->add_changed(rect);
614 return true;
615}
616
617//
618// Make video area pattern more regular.
619//
620// FIXME: Replace the above with a normal comment.
621// FIXME: Is the function efficient enough?
622//
623
624void PollingManager::adjustVideoArea()
625{
626 char newFlags[m_widthTiles * m_heightTiles];
627 char *ptr = newFlags;
628 int x, y;
629
630 // DEBUG:
631 // int nVideoTiles = 0;
632
633 for (y = 0; y < m_heightTiles; y++) {
634 for (x = 0; x < m_widthTiles; x++) {
635
636 // DEBUG:
637 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
638
639 int weightedSum = 0, n;
640 if (y > 0 && x > 0) {
641 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
642 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
643 m_videoFlags[(y-1) * m_widthTiles + x ]);
644 if (n == 3) {
645 *ptr++ = 1;
646 continue;
647 }
648 weightedSum += n;
649 }
650 if (y > 0 && x < m_widthTiles - 1) {
651 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
652 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
653 m_videoFlags[(y-1) * m_widthTiles + x ]);
654 if (n == 3) {
655 *ptr++ = 1;
656 continue;
657 }
658 weightedSum += n;
659 }
660 if (y < m_heightTiles - 1 && x > 0) {
661 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
662 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
663 m_videoFlags[(y+1) * m_widthTiles + x ]);
664 if (n == 3) {
665 *ptr++ = 1;
666 continue;
667 }
668 weightedSum += n;
669 }
670 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
671 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
672 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
673 m_videoFlags[(y+1) * m_widthTiles + x ]);
674 if (n == 3) {
675 *ptr++ = 1;
676 continue;
677 }
678 weightedSum += n;
679 }
680 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
681 }
682 }
683
684 /*
685 /// DEBUG: ------------------------------------------------------
686 if (nVideoTiles) {
687 for (y = 0; y < m_heightTiles; y++) {
688 for (x = 0; x < m_widthTiles; x++) {
689 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
690 }
691 printf(" ");
692 for (x = 0; x < m_widthTiles; x++) {
693 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
694 }
695 printf("\n");
696 }
697 printf("\n");
698 }
699 /// -------------------------------------------------------------
700 */
701
702 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
703}
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000704
705void
706PollingManager::getVideoAreaRect(Rect *result)
707{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000708 int *mx_hlen, *mx_vlen;
709 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000710
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000711 int full_h = m_heightTiles;
712 int full_w = m_widthTiles;
713 int x, y;
714 Rect max_rect(0, 0, 0, 0);
715 Rect local_rect;
716
717 for (y = 0; y < full_h; y++) {
718 for (x = 0; x < full_w; x++) {
719 int max_w = mx_hlen[y * full_w + x];
720 int max_h = mx_vlen[y * full_w + x];
721 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
722 local_rect.tl.x = x;
723 local_rect.tl.y = y;
724 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
725 if (local_rect.area() > max_rect.area()) {
726 max_rect = local_rect;
727 }
728 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000729 }
730 }
731
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000732 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000733
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000734 max_rect.tl.x *= 32;
735 max_rect.tl.y *= 32;
736 max_rect.br.x *= 32;
737 max_rect.br.y *= 32;
738 if (max_rect.br.x > m_width)
739 max_rect.br.x = m_width;
740 if (max_rect.br.y > m_height)
741 max_rect.br.y = m_height;
742 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000743
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000744 if (!result->is_empty()) {
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000745 fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n",
746 result->width(), result->height(), result->tl.x, result->tl.y);
747 }
748}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000749
750void
751PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
752{
753 // Handy shortcuts.
754 int h = m_heightTiles;
755 int w = m_widthTiles;
756
757 // Allocate memory.
758 int *mx_h = new int[h * w];
759 memset(mx_h, 0, h * w * sizeof(int));
760 int *mx_v = new int[h * w];
761 memset(mx_v, 0, h * w * sizeof(int));
762
763 int x, y, len, i;
764
765 // Fill in horizontal length matrix.
766 for (y = 0; y < h; y++) {
767 for (x = 0; x < w; x++) {
768 len = 0;
769 while (x + len < w && m_videoFlags[y * w + x + len]) {
770 len++;
771 }
772 for (i = 0; i < len; i++) {
773 mx_h[y * w + x + i] = len - i;
774 }
775 x += len;
776 }
777 }
778
779 // Fill in vertical length matrix.
780 for (x = 0; x < w; x++) {
781 for (y = 0; y < h; y++) {
782 len = 0;
783 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
784 len++;
785 }
786 for (i = 0; i < len; i++) {
787 mx_v[(y + i) * w + x] = len - i;
788 }
789 y += len;
790 }
791 }
792
793 *pmx_h = mx_h;
794 *pmx_v = mx_v;
795}
796
797void
798PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
799{
800 delete[] mx_h;
801 delete[] mx_v;
802}
803
804// NOTE: This function assumes that current tile has non-zero in mx_h[],
805// otherwise we get division by zero.
806void
807PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
808{
809 int idx = r->tl.y * m_widthTiles + r->tl.x;
810
811 // NOTE: Rectangle's maximum width and height are 25 and 18
812 // (in tiles, where each tile is usually 32x32 pixels).
813 int max_w = mx_h[idx];
814 if (max_w > 25)
815 max_w = 25;
816 int cur_h = 18;
817
818 int best_w = max_w;
819 int best_area = 1 * best_w;
820
821 for (int i = 0; i < max_w; i++) {
822 int h = mx_v[idx + i];
823 if (h < cur_h) {
824 cur_h = h;
825 if (cur_h * max_w <= best_area)
826 break;
827 }
828 if (cur_h * (i + 1) > best_area) {
829 best_w = i + 1;
830 best_area = cur_h * best_w;
831 }
832 }
833
834 r->br.x = r->tl.x + best_w;
835 r->br.y = r->tl.y + best_area / best_w;
836}
837