frame rate throttling

svn path=/trunk/boinc/; revision=2392
This commit is contained in:
David Anderson 2003-10-02 21:16:37 +00:00
parent be95b0e165
commit 938a303ef8
3 changed files with 88 additions and 31 deletions

View File

@ -28,6 +28,9 @@ extern DWORD WINAPI win_graphics_event_loop( LPVOID duff );
HANDLE graphics_threadh=NULL;
#endif
#ifdef __APPLE_CC__
#include "mac_app_opengl.h"
#endif
#include <string.h>
#include <stdarg.h>
@ -37,13 +40,12 @@ HANDLE graphics_threadh=NULL;
#endif
#include "parse.h"
#include "util.h"
#include "app_ipc.h"
#include "graphics_api.h"
#include "error_numbers.h"
#include "boinc_api.h"
#include "graphics_api.h"
#ifdef __APPLE_CC__
#include "mac_app_opengl.h"
#endif
#ifdef _WIN32
HANDLE hQuitEvent;
@ -158,6 +160,61 @@ int boinc_finish_opengl() {
return 0;
}
void throttled_app_render(int x, int y, double t) {
static double total_render_time = 0;
static double time_until_render = 0;
static double last_now = 0;
static double elapsed_time = 0;
double now, t0, t1, diff, frac, m;
bool ok_to_render;
// the following should be passed in via prefs
double max_fps = 1;
double max_gfx_cpu_frac = 0.2;
ok_to_render = true;
now = dtime();
diff = now - last_now;
last_now = now;
if (diff > 1) diff = 0; // handle initial case
// enforce frames/sec restriction
//
if (max_fps) {
time_until_render -= diff;
if (time_until_render < 0) {
time_until_render += 1./max_fps;
} else {
ok_to_render = false;
}
}
// enforce max CPU time restriction
//
if (max_gfx_cpu_frac) {
elapsed_time += diff;
if (elapsed_time) {
frac = total_render_time/elapsed_time;
if (frac > max_gfx_cpu_frac) {
ok_to_render = false;
}
}
}
// render if allowed
//
if (ok_to_render) {
if (max_gfx_cpu_frac) {
boinc_cpu_time(t0, m);
}
app_render(x, y, t);
if (max_gfx_cpu_frac) {
boinc_cpu_time(t1, m);
total_render_time += t1 - t0;
}
}
}
#ifdef HAVE_GL_LIB
// Custom GL "Print" Routine
GLvoid glPrint(GLuint font, const char *fmt, ...) {

View File

@ -8,9 +8,11 @@
#ifndef HAVE_OPENGL_GL_H
#define HAVE_OPENGL_GL_H
#endif
#include <Carbon/Carbon.h>
#endif
#ifdef _WIN32
#include <windows.h>
#ifndef HAVE_GL_LIB
#define HAVE_GL_LIB 1
#endif
@ -68,15 +70,10 @@
#include "x_opengl.h"
#endif
#ifdef __APPLE_CC__
#include <Carbon/Carbon.h>
#endif
// The API (functions called by the app)
extern int boinc_init_opengl();
extern int boinc_finish_opengl();
extern GLvoid glPrint(GLuint font, const char *fmt, ...);
extern GLenum ReSizeGLScene(GLsizei width, GLsizei height);
// Functions that must be supplied by the app
//
@ -88,6 +85,7 @@ extern void app_resize(int width, int height);
//
extern GLenum InitGL(GLvoid);
extern GLenum ReSizeGLScene(GLsizei width, GLsizei height);
extern void throttled_app_render(int, int, double);
#ifdef _WIN32
extern HANDLE hQuitEvent;

View File

@ -1,5 +1,9 @@
// Event loop and support functions for BOINC applications w/ graphics.
// Is any of this related to or dependent on OpenGL??
// Event loop and support functions for Windows versions
// of BOINC applications w/ graphics.
// Platform-independent code should NOT be here.
//
// TODO: Is any of this related to or dependent on OpenGL??
// Why not make it independent of OpenGL?
//
/* This Code Was Created By Jeff Molofee 2000
@ -137,9 +141,8 @@ void SetMode(int mode) {
hWnd = CreateWindowEx(dwExStyle, "BOINC_OpenGL", aid.app_name,
dwStyle|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, WindowRect.left, WindowRect.top,
WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,
NULL, NULL, hInstance, NULL);
NULL, NULL, hInstance, NULL
);
SetForegroundWindow(hWnd);
@ -241,8 +244,8 @@ LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
}
if (current_graphics_mode == MODE_HIDE_GRAPHICS) return 0;
if(dtime()-starttime>1./fps)
{
// TODO: remove width, height from API
//
GetClientRect(hWnd, &rt);
width = rt.right-rt.left;
height = rt.bottom-rt.top;
@ -257,11 +260,10 @@ LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
}
vis_render(width,height,dtime(),data,data_size);
#else
app_render(width, height, dtime());
throttled_app_render(width, height, dtime());
#endif
starttime=dtime();
SwapBuffers(hDC);
}
return 0;
}