1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/*******************************************************************************
* disp_sdl.cpp
*
* Written by Christoph Hormann <chris_hormann@gmx.de>
*
* SDL (Simple direct media layer) based render display system
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/povray/smp/unix/disp_sdl.cpp $
* $Revision: #23 $
* $Change: 6132 $
* $DateTime: 2013/11/25 14:23:41 $
* $Author: clipka $
*******************************************************************************/
#include "config.h"
#ifdef HAVE_LIBSDL
#include "disp_sdl.h"
#include <algorithm>
// this must be the last file included
#include "syspovdebug.h"
namespace pov_frontend
{
using namespace vfe;
using namespace vfePlatform;
extern shared_ptr<Display> gDisplay;
const UnixOptionsProcessor::Option_Info UnixSDLDisplay::Options[] =
{
// command line/povray.conf/environment options of this display mode can be added here
// section name, option name, default, has_param, command line parameter, environment variable name, help text
UnixOptionsProcessor::Option_Info("display", "scaled", "on", false, "", "POV_DISPLAY_SCALED", "scale render view to fit screen"),
UnixOptionsProcessor::Option_Info("", "", "", false, "", "", "") // has to be last
};
bool UnixSDLDisplay::Register(vfeUnixSession *session)
{
session->GetUnixOptions()->Register(Options);
// TODO: correct display detection
return true;
}
UnixSDLDisplay::UnixSDLDisplay(unsigned int w, unsigned int h, GammaCurvePtr gamma, vfeSession *session, bool visible) :
UnixDisplay(w, h, gamma, session, visible)
{
m_valid = false;
m_display_scaled = false;
m_display_scale = 1.;
m_screen = NULL;
m_display = NULL;
}
UnixSDLDisplay::~UnixSDLDisplay()
{
Close();
}
void UnixSDLDisplay::Initialise()
{
if (m_VisibleOnCreation)
Show();
}
void UnixSDLDisplay::Hide()
{
}
bool UnixSDLDisplay::TakeOver(UnixDisplay *display)
{
UnixSDLDisplay *p = dynamic_cast<UnixSDLDisplay *>(display);
if (p == NULL)
return false;
if ((GetWidth() != p->GetWidth()) || (GetHeight() != p->GetHeight()))
return false;
m_valid = p->m_valid;
m_display_scaled = p->m_display_scaled;
m_display_scale = p->m_display_scale;
m_screen = p->m_screen;
m_display = p->m_display;
if (m_display_scaled)
{
int width = GetWidth();
int height = GetHeight();
// allocate a new pixel counters, dropping influence of previous picture
m_PxCount.clear(); // not useful, vector was created empty, just to be sure
m_PxCount.reserve(width*height); // we need that, and the loop!
for(vector<unsigned char>::iterator iter = m_PxCount.begin(); iter != m_PxCount.end(); iter++)
(*iter) = 0;
}
return true;
}
void UnixSDLDisplay::Close()
{
if (!m_valid)
return;
// FIXME: should handle this correctly for the last frame
// SDL_FreeSurface(m_display);
// SDL_Quit();
m_PxCount.clear();
m_valid = false;
}
void UnixSDLDisplay::SetCaption(bool paused)
{
if (!m_valid)
return;
boost::format f;
if (m_display_scaled)
f = boost::format(PACKAGE_NAME " " VERSION_BASE " SDL display (scaled at %.0f%%)%s")
% (m_display_scale*100)
% (paused ? " [paused]" : "");
else
f = boost::format(PACKAGE_NAME " " VERSION_BASE " SDL display%s")
% (paused ? " [paused]" : "");
// FIXME: SDL_WM_SetCaption() causes locks on some distros, see http://bugs.povray.org/23
// FIXME: SDL_WM_SetCaption(f.str().c_str(), PACKAGE_NAME);
}
void UnixSDLDisplay::Show()
{
if (gDisplay.get() != this)
gDisplay = m_Session->GetDisplay();
if (!m_valid)
{
// Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s.\n", SDL_GetError());
return;
}
int desired_bpp = 0;
Uint32 video_flags = 0;
int width = GetWidth();
int height = GetHeight();
vfeUnixSession *UxSession = dynamic_cast<vfeUnixSession *>(m_Session);
if (UxSession->GetUnixOptions()->isOptionSet("display", "scaled"))
// determine maximum display area (wrong and ugly)
{
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
// [JG] about testing vs ...(-1), have a look at SDL_ListModes API (the return is very ugly).
if ((modes != NULL)&&(reinterpret_cast<SDL_Rect**>(-1) != modes))
{
width = min(modes[0]->w - 10, width);
height = min(modes[0]->h - 80, height);
}
}
// calculate display area
float AspectRatio = float(width)/float(height);
float AspectRatio_Full = float(GetWidth())/float(GetHeight());
if (AspectRatio > AspectRatio_Full)
width = int(AspectRatio_Full*float(height));
else if (AspectRatio != AspectRatio_Full)
height = int(float(width)/AspectRatio_Full);
// Initialize the display
m_screen = SDL_SetVideoMode(width, height, desired_bpp, video_flags);
if ( m_screen == NULL )
{
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", width, height, desired_bpp, SDL_GetError());
return;
}
SDL_Surface *temp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
if ( temp == NULL )
{
fprintf(stderr, "Couldn't create render display surface: %s\n", SDL_GetError());
return;
}
m_display = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
if ( m_display == NULL )
{
fprintf(stderr, "Couldn't convert bar surface: %s\n", SDL_GetError());
return;
}
m_PxCount.clear();
m_PxCount.reserve(width*height);
for(vector<unsigned char>::iterator iter = m_PxCount.begin(); iter != m_PxCount.end(); iter++)
(*iter) = 0;
m_update_rect.x = 0;
m_update_rect.y = 0;
m_update_rect.w = width;
m_update_rect.h = height;
m_screen_rect.x = 0;
m_screen_rect.y = 0;
m_screen_rect.w = width;
m_screen_rect.h = height;
m_valid = true;
m_PxCnt = UpdateInterval;
if ((width == GetWidth()) && (height == GetHeight()))
{
m_display_scaled = false;
m_display_scale = 1.;
}
else
{
m_display_scaled = true;
m_display_scale = float(width) / GetWidth();
}
SetCaption(false);
}
}
inline void UnixSDLDisplay::SetPixel(unsigned int x, unsigned int y, const RGBA8& colour)
{
Uint8 *p = (Uint8 *) m_display->pixels + y * m_display->pitch + x * m_display->format->BytesPerPixel;
Uint32 sdl_col = SDL_MapRGBA(m_display->format, colour.red, colour.green, colour.blue, colour.alpha);
switch (m_display->format->BytesPerPixel)
{
case 1:
*p = sdl_col;
break;
case 2:
*(Uint16 *) p = sdl_col;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (sdl_col >> 16) & 0xFF;
p[1] = (sdl_col >> 8) & 0xFF;
p[2] = sdl_col & 0xFF;
}
else
{
p[0] = sdl_col & 0xFF;
p[1] = (sdl_col >> 8) & 0xFF;
p[2] = (sdl_col >> 16) & 0xFF;
}
break;
case 4:
*(Uint32 *) p = sdl_col;
break;
}
}
inline void UnixSDLDisplay::SetPixelScaled(unsigned int x, unsigned int y, const RGBA8& colour)
{
unsigned int ix = x * m_display_scale;
unsigned int iy = y * m_display_scale;
Uint8 *p = (Uint8 *) m_display->pixels + iy * m_display->pitch + ix * m_display->format->BytesPerPixel;
Uint8 r, g, b, a;
Uint32 old = *(Uint32 *) p;
SDL_GetRGBA(old, m_display->format, &r, &g, &b, &a);
unsigned int ofs = ix + iy * m_display->w;
r = (r*m_PxCount[ofs] + colour.red ) / (m_PxCount[ofs]+1);
g = (g*m_PxCount[ofs] + colour.green) / (m_PxCount[ofs]+1);
b = (b*m_PxCount[ofs] + colour.blue ) / (m_PxCount[ofs]+1);
a = (a*m_PxCount[ofs] + colour.alpha) / (m_PxCount[ofs]+1);
Uint32 sdl_col = SDL_MapRGBA(m_display->format, r, g, b, a);
switch (m_display->format->BytesPerPixel)
{
case 1:
*p = sdl_col;
break;
case 2:
*(Uint16 *) p = sdl_col;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (sdl_col >> 16) & 0xFF;
p[1] = (sdl_col >> 8) & 0xFF;
p[2] = sdl_col & 0xFF;
}
else
{
p[0] = sdl_col & 0xFF;
p[1] = (sdl_col >> 8) & 0xFF;
p[2] = (sdl_col >> 16) & 0xFF;
}
break;
case 4:
*(Uint32 *) p = sdl_col;
break;
}
++m_PxCount[ofs];
}
void UnixSDLDisplay::UpdateCoord(unsigned int x, unsigned int y)
{
unsigned int rx2 = m_update_rect.x + m_update_rect.w;
unsigned int ry2 = m_update_rect.y + m_update_rect.h;
m_update_rect.x = min((unsigned int)m_update_rect.x, x);
m_update_rect.y = min((unsigned int)m_update_rect.y, y);
rx2 = max(rx2, x);
ry2 = max(ry2, y);
m_update_rect.w = rx2 - m_update_rect.x;
m_update_rect.h = ry2 - m_update_rect.y;
}
void UnixSDLDisplay::UpdateCoord(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
unsigned int rx2 = m_update_rect.x + m_update_rect.w;
unsigned int ry2 = m_update_rect.y + m_update_rect.h;
m_update_rect.x = min((unsigned int)m_update_rect.x, x1);
m_update_rect.y = min((unsigned int)m_update_rect.y, y1);
rx2 = max(rx2, x2);
ry2 = max(ry2, y2);
m_update_rect.w = rx2 - m_update_rect.x;
m_update_rect.h = ry2 - m_update_rect.y;
}
void UnixSDLDisplay::UpdateCoordScaled(unsigned int x, unsigned int y)
{
UpdateCoord(static_cast<unsigned int>(x * m_display_scale), static_cast<unsigned int>(y * m_display_scale));
}
void UnixSDLDisplay::UpdateCoordScaled(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
UpdateCoord(static_cast<unsigned int>(x1 * m_display_scale), static_cast<unsigned int>(y1 * m_display_scale),
static_cast<unsigned int>(x2 * m_display_scale), static_cast<unsigned int>(y2 * m_display_scale));
}
void UnixSDLDisplay::DrawPixel(unsigned int x, unsigned int y, const RGBA8& colour)
{
if (!m_valid || x >= GetWidth() || y >= GetHeight())
return;
if (SDL_MUSTLOCK(m_display) && SDL_LockSurface(m_display) < 0)
return;
if (m_display_scaled)
{
SetPixelScaled(x, y, colour);
UpdateCoordScaled(x, y);
}
else
{
SetPixel(x, y, colour);
UpdateCoord(x, y);
}
m_PxCnt++;
if (SDL_MUSTLOCK(m_display))
SDL_UnlockSurface(m_display);
}
void UnixSDLDisplay::DrawRectangleFrame(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const RGBA8& colour)
{
if (!m_valid)
return;
int ix1 = min(x1, GetWidth()-1);
int ix2 = min(x2, GetWidth()-1);
int iy1 = min(y1, GetHeight()-1);
int iy2 = min(y2, GetHeight()-1);
if (SDL_MUSTLOCK(m_display) && SDL_LockSurface(m_display) < 0)
return;
if (m_display_scaled)
{
for(unsigned int x = ix1; x <= ix2; x++)
{
SetPixelScaled(x, iy1, colour);
SetPixelScaled(x, iy2, colour);
}
for(unsigned int y = iy1; y <= iy2; y++)
{
SetPixelScaled(ix1, y, colour);
SetPixelScaled(ix2, y, colour);
}
UpdateCoordScaled(ix1, iy1, ix2, iy2);
}
else
{
for(unsigned int x = ix1; x <= ix2; x++)
{
SetPixel(x, iy1, colour);
SetPixel(x, iy2, colour);
}
for(unsigned int y = iy1; y <= iy2; y++)
{
SetPixel(ix1, y, colour);
SetPixel(ix2, y, colour);
}
UpdateCoord(ix1, iy1, ix2, iy2);
}
if (SDL_MUSTLOCK(m_display))
SDL_UnlockSurface(m_display);
m_PxCnt = UpdateInterval;
}
void UnixSDLDisplay::DrawFilledRectangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const RGBA8& colour)
{
if (!m_valid)
return;
unsigned int ix1 = min(x1, GetWidth()-1);
unsigned int ix2 = min(x2, GetWidth()-1);
unsigned int iy1 = min(y1, GetHeight()-1);
unsigned int iy2 = min(y2, GetHeight()-1);
if (m_display_scaled)
{
ix1 *= m_display_scale;
iy1 *= m_display_scale;
ix2 *= m_display_scale;
iy2 *= m_display_scale;
}
UpdateCoord(ix1, iy1, ix2, iy2);
Uint32 sdl_col = SDL_MapRGBA(m_display->format, colour.red, colour.green, colour.blue, colour.alpha);
SDL_Rect tempRect;
tempRect.x = ix1;
tempRect.y = iy1;
tempRect.w = ix2 - ix1 + 1;
tempRect.h = iy2 - iy1 + 1;
SDL_FillRect(m_display, &tempRect, sdl_col);
m_PxCnt = UpdateInterval;
}
void UnixSDLDisplay::DrawPixelBlock(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, const RGBA8 *colour)
{
if (!m_valid)
return;
unsigned int ix1 = min(x1, GetWidth()-1);
unsigned int ix2 = min(x2, GetWidth()-1);
unsigned int iy1 = min(y1, GetHeight()-1);
unsigned int iy2 = min(y2, GetHeight()-1);
if (SDL_MUSTLOCK(m_display) && SDL_LockSurface(m_display) < 0)
return;
if (m_display_scaled)
{
for(unsigned int y = iy1, i = 0; y <= iy2; y++)
for(unsigned int x = ix1; x <= ix2; x++, i++)
SetPixelScaled(x, y, colour[i]);
UpdateCoordScaled(ix1, iy1, ix2, iy2);
}
else
{
for(unsigned int y = y1, i = 0; y <= iy2; y++)
for(unsigned int x = ix1; x <= ix2; x++, i++)
SetPixel(x, y, colour[i]);
UpdateCoord(ix1, iy1, ix2, iy2);
}
if (SDL_MUSTLOCK(m_display))
SDL_UnlockSurface(m_display);
m_PxCnt = UpdateInterval;
}
void UnixSDLDisplay::Clear()
{
for(vector<unsigned char>::iterator iter = m_PxCount.begin(); iter != m_PxCount.end(); iter++)
(*iter) = 0;
m_update_rect.x = 0;
m_update_rect.y = 0;
m_update_rect.w = m_display->w;
m_update_rect.h = m_display->h;
SDL_FillRect(m_display, &m_update_rect, (Uint32)0);
m_PxCnt = UpdateInterval;
}
void UnixSDLDisplay::UpdateScreen(bool Force = false)
{
if (!m_valid)
return;
if (Force || m_PxCnt >= UpdateInterval)
{
SDL_BlitSurface(m_display, &m_update_rect, m_screen, &m_update_rect);
SDL_UpdateRect(m_screen, m_update_rect.x, m_update_rect.y, m_update_rect.w, m_update_rect.h);
m_PxCnt = 0;
}
}
void UnixSDLDisplay::PauseWhenDoneNotifyStart()
{
if (!m_valid)
return;
fprintf(stderr, "Press a key or click the display to continue...");
SetCaption(true);
}
void UnixSDLDisplay::PauseWhenDoneNotifyEnd()
{
if (!m_valid)
return;
SetCaption(false);
fprintf(stderr, "\n\n");
}
bool UnixSDLDisplay::PauseWhenDoneResumeIsRequested()
{
if (!m_valid)
return true;
SDL_Event event;
bool do_quit = false;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if ( event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER )
do_quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
do_quit = true;
break;
}
}
return do_quit;
}
bool UnixSDLDisplay::HandleEvents()
{
if (!m_valid)
return false;
SDL_Event event;
bool do_quit = false;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if ( event.key.keysym.sym == SDLK_q )
do_quit = true;
else if ( event.key.keysym.sym == SDLK_p )
{
if (!m_Session->IsPausable())
break;
if (m_Session->Paused())
{
if (m_Session->Resume())
SetCaption(false);
}
else
{
if (m_Session->Pause())
SetCaption(true);
}
}
break;
case SDL_QUIT:
do_quit = true;
break;
}
if (do_quit)
break;
}
return do_quit;
}
}
#endif /* HAVE_LIBSDL */
|