blob: 4d7221add91d963b7ac4aba069a19523bc05673f [file] [log] [blame]
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +00001/* Copyright (C) 2004-2007 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00002 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// PollingManager.cxx
20//
21
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000022#include <stdio.h>
23#include <string.h>
24#include <time.h>
25#include <X11/Xlib.h>
26#include <rfb/LogWriter.h>
27#include <rfb/VNCServer.h>
28#include <rfb/Configuration.h>
29#include <rfb/ServerCore.h>
30
31#include <x0vncserver/PollingManager.h>
32
33static LogWriter vlog("PollingMgr");
34
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000035const int PollingManager::m_pollingOrder[32] = {
36 0, 16, 8, 24, 4, 20, 12, 28,
37 10, 26, 18, 2, 22, 6, 30, 14,
38 1, 17, 9, 25, 7, 23, 15, 31,
39 19, 3, 27, 11, 29, 13, 5, 21
40};
41
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +000042// FIXME: Check that the parameter's value is in the allowed range.
43// This applies to all other parameters as well.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000044IntParameter PollingManager::m_videoPriority("VideoPriority",
45 "Priority of sending updates for video area (0..8)", 2);
46
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000047//
48// Constructor.
49//
50// Note that dpy and image should remain valid during the object
51// lifetime, while factory is used only in the constructor itself.
52//
Constantin Kaplinsky936c3692007-12-27 08:42:53 +000053// FIXME: Pass XPixelBuffer* instead of Image*.
54//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055
56PollingManager::PollingManager(Display *dpy, Image *image,
57 ImageFactory *factory,
58 int offsetLeft, int offsetTop)
59 : m_dpy(dpy), m_server(0), m_image(image),
60 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000061 m_numVideoPasses(0),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000062 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000063{
64 // Save width and height of the screen (and the image).
65 m_width = m_image->xim->width;
66 m_height = m_image->xim->height;
67
68 // Compute width and height in 32x32 tiles.
69 m_widthTiles = (m_width + 31) / 32;
70 m_heightTiles = (m_height + 31) / 32;
71
72 // Get initial screen image.
73 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
74
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000075 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000076 // underlying class names are different from the class name of the
77 // primary image.
78 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +000079 m_columnImage = factory->newImage(m_dpy, 1, m_height);
80 const char *primaryImgClass = m_image->className();
81 const char *rowImgClass = m_rowImage->className();
82 const char *columnImgClass = m_columnImage->className();
83 if (strcmp(rowImgClass, primaryImgClass) != 0 ||
84 strcmp(columnImgClass, primaryImgClass) != 0) {
85 vlog.error("Image types do not match (%s, %s, %s)",
86 primaryImgClass, rowImgClass, columnImgClass);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000087 }
88
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000089 int numTiles = m_widthTiles * m_heightTiles;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000090 m_rateMatrix = new char[numTiles];
91 m_videoFlags = new char[numTiles];
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000092 memset(m_rateMatrix, 0, numTiles);
93 memset(m_videoFlags, 0, numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000094}
95
96PollingManager::~PollingManager()
97{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000098 delete[] m_videoFlags;
99 delete[] m_rateMatrix;
100
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000101 delete m_rowImage;
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000102 delete m_columnImage;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000103}
104
105//
106// Register VNCServer object.
107//
108
109void PollingManager::setVNCServer(VNCServer *s)
110{
111 m_server = s;
112}
113
114//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000115// DEBUG: Measuring time spent in the poll() function,
116// as well as time intervals between poll() calls.
117//
118
119#ifdef DEBUG
120void PollingManager::debugBeforePoll()
121{
122 TimeMillis timeNow;
123 int diff = timeNow.diffFrom(m_timeSaved);
124 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
125 m_timeSaved = timeNow;
126}
127
128void PollingManager::debugAfterPoll()
129{
130 TimeMillis timeNow;
131 int diff = timeNow.diffFrom(m_timeSaved);
132 fprintf(stderr, "[poll%4dms]\n", diff);
133 m_timeSaved = timeNow;
134}
135
136#endif
137
138//
139// Search for changed rectangles on the screen.
140//
141
142void PollingManager::poll()
143{
144#ifdef DEBUG
145 debugBeforePoll();
146#endif
147
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000148 // Perform polling and try update clients if changes were detected.
149 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000150 m_server->tryUpdate();
151
152#ifdef DEBUG
153 debugAfterPoll();
154#endif
155}
156
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000157bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000158{
159 if (!m_server)
160 return false;
161
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000162 // If video data should have higher priority, and video area was
163 // detected, perform special passes to send video data only. Such
164 // "video passes" will be performed between normal polling passes.
165 // No actual polling is performed in a video pass since we know that
166 // video is changing continuously.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000167 //
168 // FIXME: Should we move this block into a separate function?
169 // FIXME: Giving higher priority to video area lengthens video
170 // detection cycles. Should we do something with that?
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000171 if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) {
172 if (m_numVideoPasses > 0) {
173 m_numVideoPasses--;
174 getScreenRect(m_videoRect);
175 return true; // we've got changes
176 } else {
177 // Normal pass now, but schedule video passes for next calls.
178 m_numVideoPasses = (int)m_videoPriority - 1;
179 }
180 }
181
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000182 // changeFlags[] array will hold boolean values corresponding to
183 // each 32x32 tile. If a value is true, then we've detected a change
184 // in that tile. Initially, we fill in the array with zero values.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000185 //
186 // FIXME: Should we use a member variable in place of changeFlags?
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000187 bool *changeFlags = new bool[m_widthTiles * m_heightTiles];
188 memset(changeFlags, 0, m_widthTiles * m_heightTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000189
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000190 // First pass over the framebuffer. Here we scan 1/32 part of the
191 // framebuffer -- that is, one line in each (32 * m_width) stripe.
192 // We compare the pixels of that line with previous framebuffer
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000193 // contents and raise corresponding member values of changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000194 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000195 bool *pChangeFlags = changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000196 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000197 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000198 nTilesChanged += checkRow(0, y, m_width, pChangeFlags);
199 pChangeFlags += m_widthTiles;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000200 }
201
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000202 // Do the work related to video area detection.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000203 bool haveVideoRect = false;
204 if ((int)m_videoPriority != 0)
205 haveVideoRect = handleVideo(changeFlags);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000206
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000207 // Inform the server about the changes.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000208 //
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000209 // FIXME: It's possible that (nTilesChanged != 0) but changeFlags[]
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000210 // array is empty. That's possible because handleVideo()
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000211 // modifies changeFlags[].
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000212 if (nTilesChanged)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000213 sendChanges(changeFlags);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000214
215 // Cleanup.
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000216 delete[] changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000217
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000218#ifdef DEBUG
219 if (nTilesChanged != 0) {
220 fprintf(stderr, "#%d# ", nTilesChanged);
221 }
222#endif
223
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000224 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000225}
226
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000227int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000228{
229 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
230 int bytesPerLine = m_image->xim->bytes_per_line;
231
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000232 if (x == 0 && w == m_width) {
233 getFullRow(y); // use more efficient method if possible
234 } else {
235 getRow(x, y, w);
236 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000237
238 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
239 char *ptr_new = m_rowImage->xim->data;
240
241 int nTilesChanged = 0;
242 for (int i = 0; i < (w + 31) / 32; i++) {
243 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
244 int nBytes = tile_w * bytesPerPixel;
245 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000246 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000247 nTilesChanged++;
248 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000249 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000250 ptr_old += nBytes;
251 ptr_new += nBytes;
252 }
253
254 return nTilesChanged;
255}
256
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000257void PollingManager::sendChanges(bool *pChangeFlags)
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000258{
259 Rect rect;
260 for (int y = 0; y < m_heightTiles; y++) {
261 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000262 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000263 // Count successive tiles marked as changed.
264 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000265 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000266 count++;
267 }
268 // Compute the coordinates and the size of this band.
269 rect.setXYWH(x * 32, y * 32, count * 32, 32);
270 if (rect.br.x > m_width)
271 rect.br.x = m_width;
272 if (rect.br.y > m_height)
273 rect.br.y = m_height;
274 // Add to the changed region maintained by the server.
275 getScreenRect(rect);
276 m_server->add_changed(rect);
277 // Skip processed tiles.
278 x += count;
279 }
280 }
281 }
282}
283
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000284bool PollingManager::handleVideo(bool *pChangeFlags)
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000285{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000286 // Update counters in m_rateMatrix.
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000287 int numTiles = m_heightTiles * m_widthTiles;
288 for (int i = 0; i < numTiles; i++)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000289 m_rateMatrix[i] += (pChangeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000290
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000291 // Once per eight calls: detect video rectangle by examining
292 // m_rateMatrix[], then reset counters in m_rateMatrix[].
293 if (m_pollingStep % 8 == 0) {
294 detectVideo();
295 memset(m_rateMatrix, 0, numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000296 }
297
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000298 // FIXME: It looks like the code below rather belongs to
299 // pollScreen(). Perhaps handleVideo() should be merged back
300 // to pollScreen(), and then pollScreen() should be split in
301 // some better way, if needed at all.
302
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000303 // Grab the pixels of video area. Also, exclude video rectangle from
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000304 // pChangeFlags[], to prevent grabbing the same pixels twice.
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000305 if (!m_videoRect.is_empty()) {
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000306 flagVideoArea(pChangeFlags, false);
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000307 getScreenRect(m_videoRect);
308 return true; // we've got a video rectangle
309 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000310
Constantin Kaplinsky646998a2007-10-09 09:31:41 +0000311 return false; // video rectangle is empty
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000312}
313
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000314void PollingManager::flagVideoArea(bool *pChangeFlags, bool value)
315{
316 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
317 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
318
319 for (int y = r.tl.y; y < r.br.y; y++)
320 for (int x = r.tl.x; x < r.br.x; x++)
321 pChangeFlags[y * m_widthTiles + x] = value;
322}
323
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000324void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000325PollingManager::detectVideo()
326{
327 // Configurable parameters.
328 const int VIDEO_THRESHOLD_0 = 3;
329 const int VIDEO_THRESHOLD_1 = 5;
330
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000331 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
332 // This will guarantee that the size of the video area is always a
333 // multiple of 32 pixels. This is important for hardware JPEG encoders.
334 int numTiles = m_heightTiles * m_widthTiles;
335 if (m_width % 32 != 0) {
336 for (int n = m_widthTiles - 1; n < numTiles; n += m_widthTiles)
337 m_rateMatrix[n] = 0;
338 }
339 if (m_height % 32 != 0) {
340 for (int n = numTiles - m_widthTiles; n < numTiles; n++)
341 m_rateMatrix[n] = 0;
342 }
343
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000344 // First, detect candidate region that looks like video. In other
345 // words, find a region that consists of continuously changing
346 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000347 for (int i = 0; i < numTiles; i++) {
348 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
349 m_videoFlags[i] = 0;
350 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
351 m_videoFlags[i] = 1;
352 }
353 }
354
355 // Now, choose the biggest rectangle from that candidate region.
356 Rect newRect;
357 getVideoAreaRect(&newRect);
358
359 // Does new rectangle differ from the previously detected one?
360 // If it does, save new rectangle and inform the server.
361 if (!newRect.equals(m_videoRect)) {
362 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000363 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000364 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000365 vlog.debug("Detected video %dx%d at (%d,%d)",
366 newRect.width(), newRect.height(),
367 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000368 }
369 m_videoRect = newRect;
370 m_server->set_video_area(newRect);
371 }
372}
373
374void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000375PollingManager::getVideoAreaRect(Rect *result)
376{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000377 int *mx_hlen, *mx_vlen;
378 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000379
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000380 int full_h = m_heightTiles;
381 int full_w = m_widthTiles;
382 int x, y;
383 Rect max_rect(0, 0, 0, 0);
384 Rect local_rect;
385
386 for (y = 0; y < full_h; y++) {
387 for (x = 0; x < full_w; x++) {
388 int max_w = mx_hlen[y * full_w + x];
389 int max_h = mx_vlen[y * full_w + x];
390 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
391 local_rect.tl.x = x;
392 local_rect.tl.y = y;
393 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
394 if (local_rect.area() > max_rect.area()) {
395 max_rect = local_rect;
396 }
397 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000398 }
399 }
400
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000401 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000402
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000403 max_rect.tl.x *= 32;
404 max_rect.tl.y *= 32;
405 max_rect.br.x *= 32;
406 max_rect.br.y *= 32;
407 if (max_rect.br.x > m_width)
408 max_rect.br.x = m_width;
409 if (max_rect.br.y > m_height)
410 max_rect.br.y = m_height;
411 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000412}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000413
414void
415PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
416{
417 // Handy shortcuts.
418 int h = m_heightTiles;
419 int w = m_widthTiles;
420
421 // Allocate memory.
422 int *mx_h = new int[h * w];
423 memset(mx_h, 0, h * w * sizeof(int));
424 int *mx_v = new int[h * w];
425 memset(mx_v, 0, h * w * sizeof(int));
426
427 int x, y, len, i;
428
429 // Fill in horizontal length matrix.
430 for (y = 0; y < h; y++) {
431 for (x = 0; x < w; x++) {
432 len = 0;
433 while (x + len < w && m_videoFlags[y * w + x + len]) {
434 len++;
435 }
436 for (i = 0; i < len; i++) {
437 mx_h[y * w + x + i] = len - i;
438 }
439 x += len;
440 }
441 }
442
443 // Fill in vertical length matrix.
444 for (x = 0; x < w; x++) {
445 for (y = 0; y < h; y++) {
446 len = 0;
447 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
448 len++;
449 }
450 for (i = 0; i < len; i++) {
451 mx_v[(y + i) * w + x] = len - i;
452 }
453 y += len;
454 }
455 }
456
457 *pmx_h = mx_h;
458 *pmx_v = mx_v;
459}
460
461void
462PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
463{
464 delete[] mx_h;
465 delete[] mx_v;
466}
467
468// NOTE: This function assumes that current tile has non-zero in mx_h[],
469// otherwise we get division by zero.
470void
471PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
472{
473 int idx = r->tl.y * m_widthTiles + r->tl.x;
474
475 // NOTE: Rectangle's maximum width and height are 25 and 18
476 // (in tiles, where each tile is usually 32x32 pixels).
477 int max_w = mx_h[idx];
478 if (max_w > 25)
479 max_w = 25;
480 int cur_h = 18;
481
482 int best_w = max_w;
483 int best_area = 1 * best_w;
484
485 for (int i = 0; i < max_w; i++) {
486 int h = mx_v[idx + i];
487 if (h < cur_h) {
488 cur_h = h;
489 if (cur_h * max_w <= best_area)
490 break;
491 }
492 if (cur_h * (i + 1) > best_area) {
493 best_w = i + 1;
494 best_area = cur_h * best_w;
495 }
496 }
497
498 r->br.x = r->tl.x + best_w;
499 r->br.y = r->tl.y + best_area / best_w;
500}
501