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