2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-12-10 21:06:42 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2004-12-10 21:06:42 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software 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 Lesser General Public License for more details.
|
2004-12-10 21:06:42 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2004-12-10 21:06:42 +00:00
|
|
|
|
|
|
|
// The part of the BOINC API having to do with graphics.
|
|
|
|
// This is the implementation level,
|
|
|
|
// which doesn't directly refer to boinc_init_options()
|
|
|
|
// and thus can be put in a shared library,
|
|
|
|
// separate from the application.
|
|
|
|
|
2007-08-10 01:51:14 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2004-12-10 21:06:42 +00:00
|
|
|
#include "boinc_win.h"
|
2005-07-14 16:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-12-10 21:06:42 +00:00
|
|
|
extern void win_graphics_event_loop();
|
2005-07-05 10:23:20 +00:00
|
|
|
#else
|
2004-12-10 21:06:42 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
2005-07-05 10:23:20 +00:00
|
|
|
#include <signal.h>
|
2007-05-04 20:06:00 +00:00
|
|
|
#include <sys/resource.h>
|
2004-12-13 01:21:50 +00:00
|
|
|
#include "x_opengl.h"
|
2004-12-10 21:06:42 +00:00
|
|
|
#endif
|
|
|
|
|
2007-01-04 11:33:05 +00:00
|
|
|
#include "boinc_api.h"
|
2006-03-21 12:04:14 +00:00
|
|
|
#include "diagnostics.h"
|
2004-12-10 21:06:42 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "app_ipc.h"
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "graphics_api.h"
|
|
|
|
#include "graphics_impl.h"
|
|
|
|
|
|
|
|
|
|
|
|
double boinc_max_fps = 30.;
|
|
|
|
double boinc_max_gfx_cpu_frac = 0.5;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE hQuitEvent;
|
|
|
|
#endif
|
|
|
|
|
2004-12-17 19:55:08 +00:00
|
|
|
BOINC_MAIN_STATE* g_bmsp;
|
2004-12-13 01:21:50 +00:00
|
|
|
static WORKER_FUNC_PTR worker_main;
|
2004-12-10 21:06:42 +00:00
|
|
|
|
2005-01-08 01:17:51 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
pthread_t worker_thread;
|
|
|
|
pthread_t graphics_thread;
|
|
|
|
#endif
|
|
|
|
|
2004-12-10 21:06:42 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD WINAPI foobar(LPVOID) {
|
|
|
|
#else
|
|
|
|
void* foobar(void*) {
|
|
|
|
#endif
|
2004-12-17 19:55:08 +00:00
|
|
|
g_bmsp->set_worker_timer_hook();
|
2004-12-10 21:06:42 +00:00
|
|
|
worker_main();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-13 01:21:50 +00:00
|
|
|
// the following function can be in a shared library,
|
|
|
|
// so it calls boinc_init_options_general() via a pointer instead of directly
|
|
|
|
//
|
2004-12-13 18:55:42 +00:00
|
|
|
int boinc_init_graphics_impl(WORKER_FUNC_PTR worker, BOINC_MAIN_STATE* bmsp) {
|
2004-12-10 21:06:42 +00:00
|
|
|
BOINC_OPTIONS opt;
|
2004-12-13 19:14:54 +00:00
|
|
|
boinc_options_defaults(opt);
|
2004-12-13 18:55:42 +00:00
|
|
|
return boinc_init_options_graphics_impl(opt, worker, bmsp);
|
2004-12-10 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2007-05-14 19:52:00 +00:00
|
|
|
static int start_worker_thread(
|
|
|
|
WORKER_FUNC_PTR _worker_main, BOINC_OPTIONS& options
|
|
|
|
) {
|
2004-12-10 21:06:42 +00:00
|
|
|
worker_main = _worker_main;
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
// Create the event object used to signal between the
|
|
|
|
// worker and event threads
|
|
|
|
hQuitEvent = CreateEvent(
|
|
|
|
NULL, // no security attributes
|
|
|
|
TRUE, // manual reset event
|
|
|
|
TRUE, // initial state is signaled
|
|
|
|
NULL // object not named
|
|
|
|
);
|
|
|
|
|
|
|
|
DWORD threadId;
|
|
|
|
|
2005-01-28 01:58:11 +00:00
|
|
|
// Create and start worker thread
|
2004-12-10 21:06:42 +00:00
|
|
|
//
|
|
|
|
worker_thread_handle = CreateThread(
|
|
|
|
NULL, 0, foobar, 0, CREATE_SUSPENDED, &threadId
|
|
|
|
);
|
|
|
|
ResumeThread(worker_thread_handle);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
pthread_attr_t worker_thread_attr;
|
|
|
|
sched_param param;
|
|
|
|
int retval, currentpolicy, minpriority;
|
|
|
|
|
|
|
|
// make the worker thread low priority
|
|
|
|
// (current thread, i.e. graphics thread, should remain high priority)
|
|
|
|
//
|
|
|
|
retval = pthread_attr_init(&worker_thread_attr);
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
|
|
|
|
retval = pthread_attr_getschedparam(&worker_thread_attr, ¶m);
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
|
|
|
|
// Note: this sets the scheduling policy for the worker thread to
|
|
|
|
// be the same as the scheduling policy of the main thread.
|
|
|
|
// This may not be a wise choice.
|
|
|
|
//
|
|
|
|
retval = pthread_attr_getschedpolicy(&worker_thread_attr, ¤tpolicy);
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
|
|
|
|
minpriority = sched_get_priority_min(currentpolicy);
|
|
|
|
if (minpriority == -1) return ERR_THREAD;
|
|
|
|
|
|
|
|
param.sched_priority = minpriority;
|
|
|
|
retval = pthread_attr_setschedparam(&worker_thread_attr, ¶m);
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
|
2005-05-22 23:00:35 +00:00
|
|
|
// initialize ID of calling thread (the graphics-thread!)
|
2005-01-08 01:17:51 +00:00
|
|
|
graphics_thread = pthread_self();
|
|
|
|
|
2007-05-14 19:52:00 +00:00
|
|
|
// set worker stack size if specified
|
2007-05-04 20:06:00 +00:00
|
|
|
//
|
2007-05-14 19:52:00 +00:00
|
|
|
if (options.worker_thread_stack_size) {
|
|
|
|
pthread_attr_setstacksize(
|
|
|
|
&worker_thread_attr, options.worker_thread_stack_size
|
|
|
|
);
|
|
|
|
}
|
2007-05-04 20:06:00 +00:00
|
|
|
|
2004-12-10 21:06:42 +00:00
|
|
|
retval = pthread_create(&worker_thread, &worker_thread_attr, foobar, 0);
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
pthread_attr_destroy( &worker_thread_attr );
|
2005-07-05 10:23:20 +00:00
|
|
|
|
2005-08-11 21:26:02 +00:00
|
|
|
block_sigalrm();
|
2004-12-10 21:06:42 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_init_options_graphics_impl(
|
|
|
|
BOINC_OPTIONS& opt,
|
2004-12-13 01:21:50 +00:00
|
|
|
WORKER_FUNC_PTR _worker_main,
|
2004-12-17 19:55:08 +00:00
|
|
|
BOINC_MAIN_STATE* bmsp
|
2004-12-10 21:06:42 +00:00
|
|
|
) {
|
|
|
|
int retval;
|
|
|
|
|
2004-12-17 19:55:08 +00:00
|
|
|
g_bmsp = bmsp;
|
|
|
|
retval = g_bmsp->boinc_init_options_general_hook(opt);
|
2004-12-10 21:06:42 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
if (_worker_main) {
|
2007-05-14 19:52:00 +00:00
|
|
|
retval = start_worker_thread(_worker_main, opt);
|
2004-12-10 21:06:42 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
win_graphics_event_loop();
|
|
|
|
fprintf(stderr, "Graphics event loop returned\n");
|
|
|
|
#else
|
|
|
|
xwin_graphics_event_loop();
|
|
|
|
fprintf(stderr, "Graphics event loop returned\n");
|
|
|
|
pthread_exit(0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// normally we never get here
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
extern "C" {
|
|
|
|
void glut_quit() {
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool 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;
|
2005-09-16 18:16:49 +00:00
|
|
|
bool ok_to_render = true;
|
2004-12-10 21:06:42 +00:00
|
|
|
|
|
|
|
now = dtime();
|
|
|
|
diff = now - last_now;
|
|
|
|
last_now = now;
|
2005-09-16 18:16:49 +00:00
|
|
|
|
|
|
|
// ignore interval if negative or more than 1 second
|
|
|
|
//
|
2005-09-18 19:17:20 +00:00
|
|
|
if ((diff<0) || (diff>1.0)) {
|
2005-09-16 18:16:49 +00:00
|
|
|
diff = 0;
|
|
|
|
}
|
2004-12-10 21:06:42 +00:00
|
|
|
|
|
|
|
// enforce frames/sec restriction
|
|
|
|
//
|
|
|
|
if (boinc_max_fps) {
|
|
|
|
time_until_render -= diff;
|
|
|
|
if (time_until_render < 0) {
|
|
|
|
time_until_render += 1./boinc_max_fps;
|
|
|
|
} else {
|
|
|
|
ok_to_render = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enforce max CPU time restriction
|
|
|
|
//
|
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
|
|
|
elapsed_time += diff;
|
|
|
|
if (elapsed_time) {
|
|
|
|
frac = total_render_time/elapsed_time;
|
|
|
|
if (frac > boinc_max_gfx_cpu_frac) {
|
|
|
|
ok_to_render = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// render if allowed
|
|
|
|
//
|
|
|
|
if (ok_to_render) {
|
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
|
|
|
boinc_calling_thread_cpu_time(t0);
|
|
|
|
}
|
|
|
|
app_graphics_render(x, y, t);
|
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
|
|
|
boinc_calling_thread_cpu_time(t1);
|
|
|
|
total_render_time += t1 - t0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-12 18:40:53 +00:00
|
|
|
void get_window_title(APP_INIT_DATA& aid, char* buf, int len) {
|
|
|
|
if (aid.app_version) {
|
|
|
|
snprintf(buf, len,
|
2005-12-10 12:46:09 +00:00
|
|
|
"%s version %.2f [workunit: %s]",
|
|
|
|
aid.app_name, aid.app_version/100.0, aid.wu_name
|
2005-10-12 18:40:53 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
snprintf(buf, len,
|
|
|
|
"%s [workunit: %s]",
|
|
|
|
aid.app_name, aid.wu_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_6e92742852 = "$Id$";
|