blob: 9921fd0e51e06ef1f42b345efdaaf2aa3969ce9c [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
Constantin Kaplinsky5e2f69f2007-10-07 13:08:18 +0000246 // that tile. Initially, we fill in the array with zero values.
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000247 bool *mxChanged = new bool[m_widthTiles * m_heightTiles];
Constantin Kaplinskyf25307a2007-10-08 13:49:44 +0000248 memset(mxChanged, 0, m_widthTiles * m_heightTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000249
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.
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000263 if (nTilesChanged)
264 sendChanges(mxChanged);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000265
266 // Cleanup.
267 delete[] mxChanged;
268
269 return (nTilesChanged != 0);
270}
271
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000272int PollingManager::checkRow(int x, int y, int w, bool *pmxChanged)
273{
274 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
275 int bytesPerLine = m_image->xim->bytes_per_line;
276
277 getRow(x, y, w);
278
279 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
280 char *ptr_new = m_rowImage->xim->data;
281
282 int nTilesChanged = 0;
283 for (int i = 0; i < (w + 31) / 32; i++) {
284 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
285 int nBytes = tile_w * bytesPerPixel;
286 if (memcmp(ptr_old, ptr_new, nBytes)) {
287 *pmxChanged = true;
288 nTilesChanged++;
289 }
290 pmxChanged++;
291 ptr_old += nBytes;
292 ptr_new += nBytes;
293 }
294
295 return nTilesChanged;
296}
297
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000298void PollingManager::sendChanges(bool *pmxChanged)
299{
300 Rect rect;
301 for (int y = 0; y < m_heightTiles; y++) {
302 for (int x = 0; x < m_widthTiles; x++) {
303 if (*pmxChanged++) {
304 // Count successive tiles marked as changed.
305 int count = 1;
306 while (x + count < m_widthTiles && *pmxChanged++) {
307 count++;
308 }
309 // Compute the coordinates and the size of this band.
310 rect.setXYWH(x * 32, y * 32, count * 32, 32);
311 if (rect.br.x > m_width)
312 rect.br.x = m_width;
313 if (rect.br.y > m_height)
314 rect.br.y = m_height;
315 // Add to the changed region maintained by the server.
316 getScreenRect(rect);
317 m_server->add_changed(rect);
318 // Skip processed tiles.
319 x += count;
320 }
321 }
322 }
323}
324
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000325bool PollingManager::poll_DetectVideo()
326{
327 if (!m_server)
328 return false;
329
330 const int GRAND_STEP_DIVISOR = 8;
331 const int VIDEO_THRESHOLD_0 = 3;
332 const int VIDEO_THRESHOLD_1 = 5;
333
334 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
335
336 // FIXME: Save shortcuts in member variables?
337 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
338 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
339 int bytesPerLine = m_image->xim->bytes_per_line;
340
341 Rect rect;
342 int nTilesChanged = 0;
343 int idx = 0;
344
345 for (int y = 0; y * 32 < m_height; y++) {
346 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
347 if (scanLine >= tile_h)
348 break;
349 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000350 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000351 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
352 char *ptr_new = m_rowImage->xim->data;
353 for (int x = 0; x * 32 < m_width; x++) {
354 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
355 int nBytes = tile_w * bytesPerPixel;
356
357 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
358 m_rateMatrix[idx] += wasChanged;
359
360 if (grandStep) {
361 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
362 m_videoFlags[idx] = 0;
363 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
364 m_videoFlags[idx] = 1;
365 }
366 m_rateMatrix[idx] = 0;
367 }
368
369 m_changedFlags[idx] |= wasChanged;
370 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
371 getTile32(x, y, tile_w, tile_h);
372 m_image->updateRect(m_tileImage, x * 32, y * 32);
373 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
374 m_server->add_changed(rect);
375 nTilesChanged++;
376 m_changedFlags[idx] = 0;
377 }
378
379 ptr_old += nBytes;
380 ptr_new += nBytes;
381 idx++;
382 }
383 }
384
385 if (grandStep)
386 adjustVideoArea();
387
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000388 bool videoDetected = false;
389 Rect r;
390 getVideoAreaRect(&r);
391 m_server->set_video_area(r);
392 videoDetected = !r.is_empty();
393 if (videoDetected) {
394 m_image->get(DefaultRootWindow(m_dpy),
395 m_offsetLeft + r.tl.x, m_offsetTop + r.tl.y,
396 r.width(), r.height(), r.tl.x, r.tl.y);
397 }
398
Constantin Kaplinsky344c3fe2007-07-24 08:35:43 +0000399#ifdef DEBUG
400 if (nTilesChanged != 0) {
401 fprintf(stderr, "#%d# ", nTilesChanged);
402 }
403#endif
404
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000405 return (nTilesChanged != 0 || videoDetected);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000406}
407
408bool PollingManager::poll_SkipCycles()
409{
410 if (!m_server)
411 return false;
412
413 enum {
414 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
415 };
416
417 bool grandStep = (m_pollingStep % 8 == 0);
418
419 int nTilesChanged = 0;
420 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
421 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
422 int bytesPerLine = m_image->xim->bytes_per_line;
423 char *pstatus = m_statusMatrix;
424 bool wasChanged;
425 Rect rect;
426
427 for (int y = 0; y * 32 < m_height; y++) {
428 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
429 if (scanLine >= tile_h)
430 scanLine %= tile_h;
431 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000432 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000433 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
434 char *ptr_new = m_rowImage->xim->data;
435 for (int x = 0; x * 32 < m_width; x++) {
436 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
437 int nBytes = tile_w * bytesPerPixel;
438
439 if (grandStep || *pstatus != CHANGED_AGAIN) {
440 wasChanged = (*pstatus == CHANGED_AGAIN) ?
441 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
442 if (wasChanged) {
443 if (grandStep || *pstatus == NOT_CHANGED) {
444 getTile32(x, y, tile_w, tile_h);
445 m_image->updateRect(m_tileImage, x * 32, y * 32);
446 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
447 m_server->add_changed(rect);
448 nTilesChanged++;
449 *pstatus = CHANGED_ONCE;
450 } else {
451 *pstatus = CHANGED_AGAIN;
452 }
453 } else if (grandStep) {
454 *pstatus = NOT_CHANGED;
455 }
456 }
457
458 ptr_old += nBytes;
459 ptr_new += nBytes;
460 pstatus++;
461 }
462 }
463
464 return (nTilesChanged != 0);
465}
466
467bool PollingManager::poll_Traditional()
468{
469 if (!m_server)
470 return false;
471
472 int nTilesChanged = 0;
473 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
474 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
475 int bytesPerLine = m_image->xim->bytes_per_line;
476 Rect rect;
477
478 for (int y = 0; y * 32 < m_height; y++) {
479 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
480 if (scanLine >= tile_h)
481 break;
482 int scan_y = y * 32 + scanLine;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000483 getFullRow(scan_y);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000484 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
485 char *ptr_new = m_rowImage->xim->data;
486 for (int x = 0; x * 32 < m_width; x++) {
487 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
488 int nBytes = tile_w * bytesPerPixel;
489 if (memcmp(ptr_old, ptr_new, nBytes)) {
490 getTile32(x, y, tile_w, tile_h);
491 m_image->updateRect(m_tileImage, x * 32, y * 32);
492 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
493 m_server->add_changed(rect);
494 nTilesChanged++;
495 }
496 ptr_old += nBytes;
497 ptr_new += nBytes;
498 }
499 }
500
501 return (nTilesChanged != 0);
502}
503
504//
505// Simplest polling method, from the original x0vncserver of VNC4.
506//
507
508bool PollingManager::poll_Dumb()
509{
510 if (!m_server)
511 return false;
512
513 getScreen();
514 Rect rect(0, 0, m_width, m_height);
515 m_server->add_changed(rect);
516
517 // Report that some changes have been detected.
518 return true;
519}
520
521//
522// Compute coordinates of the rectangle around the pointer.
523//
524// ASSUMES: (m_pointerPosKnown != false)
525//
526
527void PollingManager::computePointerArea(Rect *r)
528{
529 int x = m_pointerPos.x - 64;
530 int y = m_pointerPos.y - 64;
531 int w = 128;
532 int h = 128;
533 if (x < 0) {
534 w += x; x = 0;
535 }
536 if (x + w > m_width) {
537 w = m_width - x;
538 }
539 if (y < 0) {
540 h += y; y = 0;
541 }
542 if (y + h > m_height) {
543 h = m_height - y;
544 }
545
546 r->setXYWH(x, y, w, h);
547}
548
549//
550// Poll the area under current pointer position. Each pixel of the
551// area should be compared. Using such polling option gives higher
552// priority to screen area under the pointer.
553//
554// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
555//
556
557bool PollingManager::pollPointerArea()
558{
559 Rect r;
560 computePointerArea(&r);
561
562 // Shortcuts for coordinates.
563 int x = r.tl.x, y = r.tl.y;
564 int w = r.width(), h = r.height();
565
566 // Get new pixels.
567 getArea128(x, y, w, h);
568
569 // Now, try to minimize the rectangle by cutting out unchanged
570 // borders (at top and bottom).
571 //
572 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
573 // to produce a region instead of a rectangle. If there would
574 // be just one universal polling algorithm, it could be
575 // better to integrate pointer area polling into that
576 // algorithm, instead of a separate pollPointerArea()
577 // function.
578
579 // Shortcuts.
580 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
581 int oldBytesPerLine = m_image->xim->bytes_per_line;
582 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
583 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
584 char *newPtr = m_areaImage->xim->data;
585
586 // Check and cut out unchanged rows at the top.
587 int ty;
588 for (ty = 0; ty < h; ty++) {
589 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
590 break;
591 oldPtr += oldBytesPerLine;
592 newPtr += newBytesPerLine;
593 }
594 if (ty == h) {
595 return false; // no changes at all
596 }
597 y += ty; h -= ty;
598
599 // Check and cut out unchanged rows at the bottom.
600 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
601 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
602 int by;
603 for (by = 0; by < h - 1; by++) {
604 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
605 break;
606 oldPtr -= oldBytesPerLine;
607 newPtr -= newBytesPerLine;
608 }
609 h -= by;
610
611 // Copy pixels.
612 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
613
614 // Report updates to the server.
615 Rect rect(x, y, x+w, y+h);
616 m_server->add_changed(rect);
617 return true;
618}
619
620//
621// Make video area pattern more regular.
622//
623// FIXME: Replace the above with a normal comment.
624// FIXME: Is the function efficient enough?
625//
626
627void PollingManager::adjustVideoArea()
628{
629 char newFlags[m_widthTiles * m_heightTiles];
630 char *ptr = newFlags;
631 int x, y;
632
633 // DEBUG:
634 // int nVideoTiles = 0;
635
636 for (y = 0; y < m_heightTiles; y++) {
637 for (x = 0; x < m_widthTiles; x++) {
638
639 // DEBUG:
640 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
641
642 int weightedSum = 0, n;
643 if (y > 0 && x > 0) {
644 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
645 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
646 m_videoFlags[(y-1) * m_widthTiles + x ]);
647 if (n == 3) {
648 *ptr++ = 1;
649 continue;
650 }
651 weightedSum += n;
652 }
653 if (y > 0 && x < m_widthTiles - 1) {
654 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
655 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
656 m_videoFlags[(y-1) * m_widthTiles + x ]);
657 if (n == 3) {
658 *ptr++ = 1;
659 continue;
660 }
661 weightedSum += n;
662 }
663 if (y < m_heightTiles - 1 && x > 0) {
664 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
665 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
666 m_videoFlags[(y+1) * m_widthTiles + x ]);
667 if (n == 3) {
668 *ptr++ = 1;
669 continue;
670 }
671 weightedSum += n;
672 }
673 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
674 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
675 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
676 m_videoFlags[(y+1) * m_widthTiles + x ]);
677 if (n == 3) {
678 *ptr++ = 1;
679 continue;
680 }
681 weightedSum += n;
682 }
683 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
684 }
685 }
686
687 /*
688 /// DEBUG: ------------------------------------------------------
689 if (nVideoTiles) {
690 for (y = 0; y < m_heightTiles; y++) {
691 for (x = 0; x < m_widthTiles; x++) {
692 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
693 }
694 printf(" ");
695 for (x = 0; x < m_widthTiles; x++) {
696 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
697 }
698 printf("\n");
699 }
700 printf("\n");
701 }
702 /// -------------------------------------------------------------
703 */
704
705 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
706}
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000707
708void
709PollingManager::getVideoAreaRect(Rect *result)
710{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000711 int *mx_hlen, *mx_vlen;
712 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000713
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000714 int full_h = m_heightTiles;
715 int full_w = m_widthTiles;
716 int x, y;
717 Rect max_rect(0, 0, 0, 0);
718 Rect local_rect;
719
720 for (y = 0; y < full_h; y++) {
721 for (x = 0; x < full_w; x++) {
722 int max_w = mx_hlen[y * full_w + x];
723 int max_h = mx_vlen[y * full_w + x];
724 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
725 local_rect.tl.x = x;
726 local_rect.tl.y = y;
727 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
728 if (local_rect.area() > max_rect.area()) {
729 max_rect = local_rect;
730 }
731 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000732 }
733 }
734
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000735 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000736
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000737 max_rect.tl.x *= 32;
738 max_rect.tl.y *= 32;
739 max_rect.br.x *= 32;
740 max_rect.br.y *= 32;
741 if (max_rect.br.x > m_width)
742 max_rect.br.x = m_width;
743 if (max_rect.br.y > m_height)
744 max_rect.br.y = m_height;
745 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000746
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000747 if (!result->is_empty()) {
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000748 fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n",
749 result->width(), result->height(), result->tl.x, result->tl.y);
750 }
751}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000752
753void
754PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
755{
756 // Handy shortcuts.
757 int h = m_heightTiles;
758 int w = m_widthTiles;
759
760 // Allocate memory.
761 int *mx_h = new int[h * w];
762 memset(mx_h, 0, h * w * sizeof(int));
763 int *mx_v = new int[h * w];
764 memset(mx_v, 0, h * w * sizeof(int));
765
766 int x, y, len, i;
767
768 // Fill in horizontal length matrix.
769 for (y = 0; y < h; y++) {
770 for (x = 0; x < w; x++) {
771 len = 0;
772 while (x + len < w && m_videoFlags[y * w + x + len]) {
773 len++;
774 }
775 for (i = 0; i < len; i++) {
776 mx_h[y * w + x + i] = len - i;
777 }
778 x += len;
779 }
780 }
781
782 // Fill in vertical length matrix.
783 for (x = 0; x < w; x++) {
784 for (y = 0; y < h; y++) {
785 len = 0;
786 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
787 len++;
788 }
789 for (i = 0; i < len; i++) {
790 mx_v[(y + i) * w + x] = len - i;
791 }
792 y += len;
793 }
794 }
795
796 *pmx_h = mx_h;
797 *pmx_v = mx_v;
798}
799
800void
801PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
802{
803 delete[] mx_h;
804 delete[] mx_v;
805}
806
807// NOTE: This function assumes that current tile has non-zero in mx_h[],
808// otherwise we get division by zero.
809void
810PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
811{
812 int idx = r->tl.y * m_widthTiles + r->tl.x;
813
814 // NOTE: Rectangle's maximum width and height are 25 and 18
815 // (in tiles, where each tile is usually 32x32 pixels).
816 int max_w = mx_h[idx];
817 if (max_w > 25)
818 max_w = 25;
819 int cur_h = 18;
820
821 int best_w = max_w;
822 int best_area = 1 * best_w;
823
824 for (int i = 0; i < max_w; i++) {
825 int h = mx_v[idx + i];
826 if (h < cur_h) {
827 cur_h = h;
828 if (cur_h * max_w <= best_area)
829 break;
830 }
831 if (cur_h * (i + 1) > best_area) {
832 best_w = i + 1;
833 best_area = cur_h * best_w;
834 }
835 }
836
837 r->br.x = r->tl.x + best_w;
838 r->br.y = r->tl.y + best_area / best_w;
839}
840