RetroArch/gfx/gfx_common.c

217 lines
5.8 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2013-01-01 00:37:37 +00:00
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
* Copyright (C) 2011-2013 - Daniel De Matteis
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-03-26 17:34:58 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-03-26 17:34:58 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-03-26 17:34:58 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(_MSC_VER) && !defined(_XBOX)
2012-11-15 08:40:31 +00:00
#pragma comment(lib, "winmm")
#endif
2011-03-26 17:34:58 +00:00
#include "gfx_common.h"
2011-11-30 16:27:47 +00:00
#include "../general.h"
2011-12-24 23:59:46 +00:00
#ifndef _MSC_VER
2011-03-26 17:34:58 +00:00
#include <sys/time.h>
2011-12-24 23:59:46 +00:00
#else
2012-07-15 17:12:58 +00:00
#ifndef _XBOX
2011-12-24 23:59:46 +00:00
#include <winsock2.h>
#include <mmsystem.h>
2012-07-15 17:12:58 +00:00
#endif
#endif
2012-07-23 23:20:58 +00:00
#if defined(__PSL1GHT__)
#include <sys/time.h>
2012-07-23 23:20:58 +00:00
#elif defined(__CELLOS_LV2__)
2012-07-16 04:09:41 +00:00
#include <sys/sys_time.h>
#endif
2012-08-27 20:09:53 +00:00
#ifdef GEKKO
#include <ogc/lwp_watchdog.h>
#endif
#ifdef __linux__
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#endif
2012-08-27 20:09:53 +00:00
#if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(_MSC_VER) || defined(GEKKO)
static int gettimeofday2(struct timeval *val, void *dummy)
2011-12-24 23:59:46 +00:00
{
(void)dummy;
#if defined(_MSC_VER) && !defined(_XBOX360)
2011-12-24 23:59:46 +00:00
DWORD msec = timeGetTime();
#elif defined(_XBOX360)
DWORD msec = GetTickCount();
2011-12-25 00:54:29 +00:00
#endif
#if defined(__CELLOS_LV2__)
2011-12-25 00:54:29 +00:00
uint64_t usec = sys_time_get_system_time();
2012-08-27 20:09:53 +00:00
#elif defined(GEKKO)
uint64_t usec = ticks_to_microsecs(gettime());
#else
uint64_t usec = msec * 1000;
#endif
2012-07-23 23:20:58 +00:00
val->tv_sec = usec / 1000000;
2011-12-25 00:54:29 +00:00
val->tv_usec = usec % 1000000;
return 0;
2011-12-24 23:59:46 +00:00
}
2012-08-27 20:09:53 +00:00
// GEKKO has gettimeofday, but it's not accurate enough for calculating FPS, so hack around it
#define gettimeofday gettimeofday2
2011-12-24 23:59:46 +00:00
#endif
2011-03-26 17:34:58 +00:00
static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, int frames)
{
2012-07-23 23:20:58 +00:00
float time = new_tv->tv_sec - tv->tv_sec + (new_tv->tv_usec - tv->tv_usec) / 1000000.0;
2011-03-26 17:34:58 +00:00
return frames/time;
}
bool gfx_get_fps(char *buf, size_t size, bool always_write)
2011-03-26 17:34:58 +00:00
{
static struct timeval tv;
2012-11-15 16:30:31 +00:00
static float last_fps;
2011-03-26 17:34:58 +00:00
struct timeval new_tv;
bool ret = false;
if (g_extern.frame_count == 0)
2011-03-26 17:34:58 +00:00
{
gettimeofday(&tv, NULL);
2011-04-08 18:53:11 +00:00
snprintf(buf, size, "%s", g_extern.title_buf);
2011-03-26 17:34:58 +00:00
ret = true;
}
else if ((g_extern.frame_count % 180) == 0)
2011-03-26 17:34:58 +00:00
{
gettimeofday(&new_tv, NULL);
struct timeval tmp_tv = tv;
tv = new_tv;
2012-11-15 16:30:31 +00:00
last_fps = tv_to_fps(&tmp_tv, &new_tv, 180);
2011-03-26 17:34:58 +00:00
#ifdef RARCH_CONSOLE
snprintf(buf, size, "FPS: %6.1f || Frames: %d", last_fps, g_extern.frame_count);
#else
snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, last_fps, g_extern.frame_count);
#endif
2011-03-26 17:34:58 +00:00
ret = true;
}
2012-11-15 16:30:31 +00:00
else if (always_write)
{
#ifdef RARCH_CONSOLE
snprintf(buf, size, "FPS: %6.1f || Frames: %d", last_fps, g_extern.frame_count);
2012-11-15 16:30:31 +00:00
#else
snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, last_fps, g_extern.frame_count);
2012-11-15 16:30:31 +00:00
#endif
}
2011-03-26 17:34:58 +00:00
return ret;
}
void gfx_window_title_reset(void)
{
g_extern.frame_count = 0;
}
2012-07-15 17:12:58 +00:00
#if defined(_WIN32) && !defined(_XBOX)
#include <windows.h>
2011-12-24 23:59:46 +00:00
#include "../dynamic.h"
2011-08-07 20:31:59 +00:00
// We only load this library once, so we let it be unloaded at application shutdown,
// since unloading it early seems to cause issues on some systems.
static dylib_t dwmlib = NULL;
static void gfx_dwm_shutdown(void)
{
if (dwmlib)
dylib_close(dwmlib);
}
2011-08-07 19:15:50 +00:00
void gfx_set_dwm(void)
{
static bool inited = false;
if (inited)
return;
inited = true;
2011-08-07 20:31:59 +00:00
dwmlib = dylib_load("dwmapi.dll");
if (!dwmlib)
{
2012-04-21 21:25:32 +00:00
RARCH_LOG("Did not find dwmapi.dll.\n");
return;
}
2011-08-07 20:31:59 +00:00
atexit(gfx_dwm_shutdown);
2011-08-07 20:31:59 +00:00
HRESULT (WINAPI *mmcss)(BOOL) = (HRESULT (WINAPI*)(BOOL))dylib_proc(dwmlib, "DwmEnableMMCSS");
2011-08-07 19:15:50 +00:00
if (mmcss)
{
2012-04-21 21:25:32 +00:00
RARCH_LOG("Setting multimedia scheduling for DWM.\n");
2011-08-07 19:15:50 +00:00
mmcss(TRUE);
}
if (!g_settings.video.disable_composition)
2011-08-07 20:31:59 +00:00
return;
2011-08-07 19:15:50 +00:00
2011-08-07 20:31:59 +00:00
HRESULT (WINAPI *composition_enable)(UINT) = (HRESULT (WINAPI*)(UINT))dylib_proc(dwmlib, "DwmEnableComposition");
if (!composition_enable)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Did not find DwmEnableComposition ...\n");
return;
}
HRESULT ret = composition_enable(0);
if (FAILED(ret))
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to set composition state ...\n");
}
#endif
2013-01-29 18:28:33 +00:00
void gfx_scale_integer(struct rarch_viewport *vp, unsigned width, unsigned height, float aspect_ratio, bool keep_aspect)
{
// Use system reported sizes as these define the geometry for the "normal" case.
unsigned base_height = g_extern.system.av_info.geometry.base_height;
// Account for non-square pixels.
// This is sort of contradictory with the goal of integer scale,
// but it is desirable in some cases.
// If square pixels are used, base_height will be equal to g_extern.system.av_info.base_height.
unsigned base_width = (unsigned)roundf(base_height * aspect_ratio);
unsigned padding_x = 0;
unsigned padding_y = 0;
// Make sure that we don't get 0x scale ...
if (width >= base_width && height >= base_height)
{
if (keep_aspect) // X/Y scale must be same.
{
unsigned max_scale = min(width / base_width, height / base_height);
padding_x = width - base_width * max_scale;
padding_y = height - base_height * max_scale;
}
else // X/Y can be independent, each scaled as much as possible.
{
padding_x = width % base_width;
padding_y = height % base_height;
}
}
width -= padding_x;
height -= padding_y;
vp->width = width;
vp->height = height;
vp->x = padding_x >> 1;
vp->y = padding_y >> 1;
}