blob: 45b0fef30e834655cee72b6ff72de55b0d0c78e4 [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",
46 "DEBUG: Select particular polling algorithm (0..3)",
47 3);
48
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;
207//case 3:
208 default:
209 changes1 = poll_DetectVideo();
210 break;
211 }
212
213 // Second step: optional thorough polling of the area around the pointer.
214 // We do that only if the pointer position is known and was set recently.
215
216 bool changes2 = false;
217 if (pollPointer) {
218 if (m_pointerPosKnown && time(NULL) - m_pointerPosTime >= 5) {
219 unsetPointerPos();
220 }
221 if (m_pointerPosKnown) {
222 changes2 = pollPointerArea();
223 }
224 }
225
226 // Update if needed.
227
228 if (changes1 || changes2)
229 m_server->tryUpdate();
230
231#ifdef DEBUG
232 debugAfterPoll();
233#endif
234}
235
236bool PollingManager::poll_DetectVideo()
237{
238 if (!m_server)
239 return false;
240
241 const int GRAND_STEP_DIVISOR = 8;
242 const int VIDEO_THRESHOLD_0 = 3;
243 const int VIDEO_THRESHOLD_1 = 5;
244
245 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
246
247 // FIXME: Save shortcuts in member variables?
248 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
249 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
250 int bytesPerLine = m_image->xim->bytes_per_line;
251
252 Rect rect;
253 int nTilesChanged = 0;
254 int idx = 0;
255
256 for (int y = 0; y * 32 < m_height; y++) {
257 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
258 if (scanLine >= tile_h)
259 break;
260 int scan_y = y * 32 + scanLine;
261 getRow(scan_y);
262 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
263 char *ptr_new = m_rowImage->xim->data;
264 for (int x = 0; x * 32 < m_width; x++) {
265 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
266 int nBytes = tile_w * bytesPerPixel;
267
268 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
269 m_rateMatrix[idx] += wasChanged;
270
271 if (grandStep) {
272 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
273 m_videoFlags[idx] = 0;
274 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
275 m_videoFlags[idx] = 1;
276 }
277 m_rateMatrix[idx] = 0;
278 }
279
280 m_changedFlags[idx] |= wasChanged;
281 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
282 getTile32(x, y, tile_w, tile_h);
283 m_image->updateRect(m_tileImage, x * 32, y * 32);
284 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
285 m_server->add_changed(rect);
286 nTilesChanged++;
287 m_changedFlags[idx] = 0;
288 }
289
290 ptr_old += nBytes;
291 ptr_new += nBytes;
292 idx++;
293 }
294 }
295
296 if (grandStep)
297 adjustVideoArea();
298
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000299 bool videoDetected = false;
300 Rect r;
301 getVideoAreaRect(&r);
302 m_server->set_video_area(r);
303 videoDetected = !r.is_empty();
304 if (videoDetected) {
305 m_image->get(DefaultRootWindow(m_dpy),
306 m_offsetLeft + r.tl.x, m_offsetTop + r.tl.y,
307 r.width(), r.height(), r.tl.x, r.tl.y);
308 }
309
Constantin Kaplinsky344c3fe2007-07-24 08:35:43 +0000310#ifdef DEBUG
311 if (nTilesChanged != 0) {
312 fprintf(stderr, "#%d# ", nTilesChanged);
313 }
314#endif
315
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000316 return (nTilesChanged != 0 || videoDetected);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000317}
318
319bool PollingManager::poll_SkipCycles()
320{
321 if (!m_server)
322 return false;
323
324 enum {
325 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
326 };
327
328 bool grandStep = (m_pollingStep % 8 == 0);
329
330 int nTilesChanged = 0;
331 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
332 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
333 int bytesPerLine = m_image->xim->bytes_per_line;
334 char *pstatus = m_statusMatrix;
335 bool wasChanged;
336 Rect rect;
337
338 for (int y = 0; y * 32 < m_height; y++) {
339 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
340 if (scanLine >= tile_h)
341 scanLine %= tile_h;
342 int scan_y = y * 32 + scanLine;
343 getRow(scan_y);
344 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
345 char *ptr_new = m_rowImage->xim->data;
346 for (int x = 0; x * 32 < m_width; x++) {
347 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
348 int nBytes = tile_w * bytesPerPixel;
349
350 if (grandStep || *pstatus != CHANGED_AGAIN) {
351 wasChanged = (*pstatus == CHANGED_AGAIN) ?
352 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
353 if (wasChanged) {
354 if (grandStep || *pstatus == NOT_CHANGED) {
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 *pstatus = CHANGED_ONCE;
361 } else {
362 *pstatus = CHANGED_AGAIN;
363 }
364 } else if (grandStep) {
365 *pstatus = NOT_CHANGED;
366 }
367 }
368
369 ptr_old += nBytes;
370 ptr_new += nBytes;
371 pstatus++;
372 }
373 }
374
375 return (nTilesChanged != 0);
376}
377
378bool PollingManager::poll_Traditional()
379{
380 if (!m_server)
381 return false;
382
383 int nTilesChanged = 0;
384 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
385 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
386 int bytesPerLine = m_image->xim->bytes_per_line;
387 Rect rect;
388
389 for (int y = 0; y * 32 < m_height; y++) {
390 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
391 if (scanLine >= tile_h)
392 break;
393 int scan_y = y * 32 + scanLine;
394 getRow(scan_y);
395 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
396 char *ptr_new = m_rowImage->xim->data;
397 for (int x = 0; x * 32 < m_width; x++) {
398 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
399 int nBytes = tile_w * bytesPerPixel;
400 if (memcmp(ptr_old, ptr_new, nBytes)) {
401 getTile32(x, y, tile_w, tile_h);
402 m_image->updateRect(m_tileImage, x * 32, y * 32);
403 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
404 m_server->add_changed(rect);
405 nTilesChanged++;
406 }
407 ptr_old += nBytes;
408 ptr_new += nBytes;
409 }
410 }
411
412 return (nTilesChanged != 0);
413}
414
415//
416// Simplest polling method, from the original x0vncserver of VNC4.
417//
418
419bool PollingManager::poll_Dumb()
420{
421 if (!m_server)
422 return false;
423
424 getScreen();
425 Rect rect(0, 0, m_width, m_height);
426 m_server->add_changed(rect);
427
428 // Report that some changes have been detected.
429 return true;
430}
431
432//
433// Compute coordinates of the rectangle around the pointer.
434//
435// ASSUMES: (m_pointerPosKnown != false)
436//
437
438void PollingManager::computePointerArea(Rect *r)
439{
440 int x = m_pointerPos.x - 64;
441 int y = m_pointerPos.y - 64;
442 int w = 128;
443 int h = 128;
444 if (x < 0) {
445 w += x; x = 0;
446 }
447 if (x + w > m_width) {
448 w = m_width - x;
449 }
450 if (y < 0) {
451 h += y; y = 0;
452 }
453 if (y + h > m_height) {
454 h = m_height - y;
455 }
456
457 r->setXYWH(x, y, w, h);
458}
459
460//
461// Poll the area under current pointer position. Each pixel of the
462// area should be compared. Using such polling option gives higher
463// priority to screen area under the pointer.
464//
465// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
466//
467
468bool PollingManager::pollPointerArea()
469{
470 Rect r;
471 computePointerArea(&r);
472
473 // Shortcuts for coordinates.
474 int x = r.tl.x, y = r.tl.y;
475 int w = r.width(), h = r.height();
476
477 // Get new pixels.
478 getArea128(x, y, w, h);
479
480 // Now, try to minimize the rectangle by cutting out unchanged
481 // borders (at top and bottom).
482 //
483 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
484 // to produce a region instead of a rectangle. If there would
485 // be just one universal polling algorithm, it could be
486 // better to integrate pointer area polling into that
487 // algorithm, instead of a separate pollPointerArea()
488 // function.
489
490 // Shortcuts.
491 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
492 int oldBytesPerLine = m_image->xim->bytes_per_line;
493 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
494 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
495 char *newPtr = m_areaImage->xim->data;
496
497 // Check and cut out unchanged rows at the top.
498 int ty;
499 for (ty = 0; ty < h; ty++) {
500 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
501 break;
502 oldPtr += oldBytesPerLine;
503 newPtr += newBytesPerLine;
504 }
505 if (ty == h) {
506 return false; // no changes at all
507 }
508 y += ty; h -= ty;
509
510 // Check and cut out unchanged rows at the bottom.
511 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
512 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
513 int by;
514 for (by = 0; by < h - 1; by++) {
515 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
516 break;
517 oldPtr -= oldBytesPerLine;
518 newPtr -= newBytesPerLine;
519 }
520 h -= by;
521
522 // Copy pixels.
523 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
524
525 // Report updates to the server.
526 Rect rect(x, y, x+w, y+h);
527 m_server->add_changed(rect);
528 return true;
529}
530
531//
532// Make video area pattern more regular.
533//
534// FIXME: Replace the above with a normal comment.
535// FIXME: Is the function efficient enough?
536//
537
538void PollingManager::adjustVideoArea()
539{
540 char newFlags[m_widthTiles * m_heightTiles];
541 char *ptr = newFlags;
542 int x, y;
543
544 // DEBUG:
545 // int nVideoTiles = 0;
546
547 for (y = 0; y < m_heightTiles; y++) {
548 for (x = 0; x < m_widthTiles; x++) {
549
550 // DEBUG:
551 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
552
553 int weightedSum = 0, n;
554 if (y > 0 && x > 0) {
555 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
556 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
557 m_videoFlags[(y-1) * m_widthTiles + x ]);
558 if (n == 3) {
559 *ptr++ = 1;
560 continue;
561 }
562 weightedSum += n;
563 }
564 if (y > 0 && x < m_widthTiles - 1) {
565 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
566 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
567 m_videoFlags[(y-1) * m_widthTiles + x ]);
568 if (n == 3) {
569 *ptr++ = 1;
570 continue;
571 }
572 weightedSum += n;
573 }
574 if (y < m_heightTiles - 1 && x > 0) {
575 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
576 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
577 m_videoFlags[(y+1) * m_widthTiles + x ]);
578 if (n == 3) {
579 *ptr++ = 1;
580 continue;
581 }
582 weightedSum += n;
583 }
584 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
585 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
586 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
587 m_videoFlags[(y+1) * m_widthTiles + x ]);
588 if (n == 3) {
589 *ptr++ = 1;
590 continue;
591 }
592 weightedSum += n;
593 }
594 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
595 }
596 }
597
598 /*
599 /// DEBUG: ------------------------------------------------------
600 if (nVideoTiles) {
601 for (y = 0; y < m_heightTiles; y++) {
602 for (x = 0; x < m_widthTiles; x++) {
603 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
604 }
605 printf(" ");
606 for (x = 0; x < m_widthTiles; x++) {
607 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
608 }
609 printf("\n");
610 }
611 printf("\n");
612 }
613 /// -------------------------------------------------------------
614 */
615
616 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
617}
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000618
619void
620PollingManager::getVideoAreaRect(Rect *result)
621{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000622 int *mx_hlen, *mx_vlen;
623 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000624
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000625 int full_h = m_heightTiles;
626 int full_w = m_widthTiles;
627 int x, y;
628 Rect max_rect(0, 0, 0, 0);
629 Rect local_rect;
630
631 for (y = 0; y < full_h; y++) {
632 for (x = 0; x < full_w; x++) {
633 int max_w = mx_hlen[y * full_w + x];
634 int max_h = mx_vlen[y * full_w + x];
635 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
636 local_rect.tl.x = x;
637 local_rect.tl.y = y;
638 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
639 if (local_rect.area() > max_rect.area()) {
640 max_rect = local_rect;
641 }
642 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000643 }
644 }
645
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000646 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000647
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000648 max_rect.tl.x *= 32;
649 max_rect.tl.y *= 32;
650 max_rect.br.x *= 32;
651 max_rect.br.y *= 32;
652 if (max_rect.br.x > m_width)
653 max_rect.br.x = m_width;
654 if (max_rect.br.y > m_height)
655 max_rect.br.y = m_height;
656 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000657
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000658 if (!result->is_empty()) {
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000659 fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n",
660 result->width(), result->height(), result->tl.x, result->tl.y);
661 }
662}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000663
664void
665PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
666{
667 // Handy shortcuts.
668 int h = m_heightTiles;
669 int w = m_widthTiles;
670
671 // Allocate memory.
672 int *mx_h = new int[h * w];
673 memset(mx_h, 0, h * w * sizeof(int));
674 int *mx_v = new int[h * w];
675 memset(mx_v, 0, h * w * sizeof(int));
676
677 int x, y, len, i;
678
679 // Fill in horizontal length matrix.
680 for (y = 0; y < h; y++) {
681 for (x = 0; x < w; x++) {
682 len = 0;
683 while (x + len < w && m_videoFlags[y * w + x + len]) {
684 len++;
685 }
686 for (i = 0; i < len; i++) {
687 mx_h[y * w + x + i] = len - i;
688 }
689 x += len;
690 }
691 }
692
693 // Fill in vertical length matrix.
694 for (x = 0; x < w; x++) {
695 for (y = 0; y < h; y++) {
696 len = 0;
697 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
698 len++;
699 }
700 for (i = 0; i < len; i++) {
701 mx_v[(y + i) * w + x] = len - i;
702 }
703 y += len;
704 }
705 }
706
707 *pmx_h = mx_h;
708 *pmx_v = mx_v;
709}
710
711void
712PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
713{
714 delete[] mx_h;
715 delete[] mx_v;
716}
717
718// NOTE: This function assumes that current tile has non-zero in mx_h[],
719// otherwise we get division by zero.
720void
721PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
722{
723 int idx = r->tl.y * m_widthTiles + r->tl.x;
724
725 // NOTE: Rectangle's maximum width and height are 25 and 18
726 // (in tiles, where each tile is usually 32x32 pixels).
727 int max_w = mx_h[idx];
728 if (max_w > 25)
729 max_w = 25;
730 int cur_h = 18;
731
732 int best_w = max_w;
733 int best_area = 1 * best_w;
734
735 for (int i = 0; i < max_w; i++) {
736 int h = mx_v[idx + i];
737 if (h < cur_h) {
738 cur_h = h;
739 if (cur_h * max_w <= best_area)
740 break;
741 }
742 if (cur_h * (i + 1) > best_area) {
743 best_w = i + 1;
744 best_area = cur_h * best_w;
745 }
746 }
747
748 r->br.x = r->tl.x + best_w;
749 r->br.y = r->tl.y + best_area / best_w;
750}
751