2003-06-11 23:36:48 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-09-26 18:11:06 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-06-11 23:36:48 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2004-07-13 13:54:09 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2004-07-13 13:54:09 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-05-07 23:42:17 +00:00
|
|
|
// The part of the BOINC app lib having to do with graphics.
|
|
|
|
// This code is NOT linked into the core client.
|
|
|
|
|
2004-03-06 09:45:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-11-01 18:59:18 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-17 00:05:18 +00:00
|
|
|
#include "boinc_win.h"
|
2004-10-11 23:57:07 +00:00
|
|
|
extern void win_graphics_event_loop();
|
2002-11-01 18:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2003-10-02 21:16:37 +00:00
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
#include "mac_app_opengl.h"
|
2004-07-15 20:49:27 +00:00
|
|
|
#endif
|
2002-10-31 00:13:50 +00:00
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
2004-07-15 21:54:48 +00:00
|
|
|
#include "x_opengl.h"
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
#ifdef HAVE_PTHREAD
|
|
|
|
#include <pthread.h>
|
2004-10-20 22:11:12 +00:00
|
|
|
#include <sched.h>
|
2003-09-30 21:17:20 +00:00
|
|
|
#endif
|
2004-03-06 09:45:25 +00:00
|
|
|
#endif
|
2003-09-30 21:17:20 +00:00
|
|
|
|
|
|
|
#include "parse.h"
|
2003-10-02 21:16:37 +00:00
|
|
|
#include "util.h"
|
2003-09-30 21:17:20 +00:00
|
|
|
#include "app_ipc.h"
|
|
|
|
#include "error_numbers.h"
|
2003-12-24 06:33:28 +00:00
|
|
|
#include "filesys.h"
|
2003-10-02 21:16:37 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "graphics_api.h"
|
2004-07-15 21:54:48 +00:00
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
|
2003-10-14 20:37:02 +00:00
|
|
|
double boinc_max_fps = 30.;
|
|
|
|
double boinc_max_gfx_cpu_frac = 0.5;
|
2002-09-18 21:15:23 +00:00
|
|
|
|
2003-09-30 23:12:27 +00:00
|
|
|
#ifdef _WIN32
|
2003-09-30 23:09:44 +00:00
|
|
|
HANDLE hQuitEvent;
|
2003-09-30 23:12:27 +00:00
|
|
|
#endif
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
bool graphics_inited = false;
|
2002-09-18 21:15:23 +00:00
|
|
|
|
2004-10-13 22:52:37 +00:00
|
|
|
static void (*worker_main)();
|
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// glue routine for Windows
|
2004-10-13 22:52:37 +00:00
|
|
|
DWORD WINAPI foobar(LPVOID) {
|
|
|
|
worker_main();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
void* foobar(void*) {
|
2004-10-26 20:33:16 +00:00
|
|
|
set_timer();
|
2004-10-13 22:52:37 +00:00
|
|
|
worker_main();
|
2004-10-11 23:57:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-30 21:17:20 +00:00
|
|
|
|
2004-10-13 22:52:37 +00:00
|
|
|
int boinc_init_graphics(void (*_worker_main)()) {
|
|
|
|
worker_main = _worker_main;
|
2002-09-18 22:15:49 +00:00
|
|
|
#ifdef _WIN32
|
2004-03-23 01:20:36 +00:00
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
// 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
|
|
|
|
);
|
|
|
|
|
2002-09-18 22:15:49 +00:00
|
|
|
DWORD threadId;
|
|
|
|
|
|
|
|
// Create the graphics thread, passing it the graphics info
|
2002-11-12 20:59:00 +00:00
|
|
|
// TODO: is it better to use _beginthreadex here?
|
2003-05-21 17:59:10 +00:00
|
|
|
//
|
2004-10-25 20:16:30 +00:00
|
|
|
worker_thread_handle = CreateThread(
|
2004-10-13 22:52:37 +00:00
|
|
|
NULL, 0, foobar, 0, CREATE_SUSPENDED, &threadId
|
2002-10-06 00:43:55 +00:00
|
|
|
);
|
2002-09-18 22:15:49 +00:00
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
// raise priority of graphics thread (i.e. current thread)
|
2003-05-21 17:59:10 +00:00
|
|
|
//
|
|
|
|
HANDLE h = GetCurrentThread();
|
2004-10-11 23:57:07 +00:00
|
|
|
SetThreadPriority(h, THREAD_PRIORITY_HIGHEST);
|
2003-05-21 17:59:10 +00:00
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
// lower worker thread priority
|
2003-05-21 17:59:10 +00:00
|
|
|
//
|
2004-10-25 20:16:30 +00:00
|
|
|
SetThreadPriority(worker_thread_handle, THREAD_PRIORITY_LOWEST);
|
2002-09-18 22:15:49 +00:00
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
// Start the worker thread
|
2003-05-21 17:59:10 +00:00
|
|
|
//
|
2004-10-25 20:16:30 +00:00
|
|
|
ResumeThread(worker_thread_handle);
|
2004-10-11 23:57:07 +00:00
|
|
|
|
|
|
|
graphics_inited = true;
|
|
|
|
win_graphics_event_loop();
|
2002-09-18 22:15:49 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
OSErr theErr = noErr;
|
2004-10-11 23:57:07 +00:00
|
|
|
ThreadID workerThreadID = 0;
|
2002-09-18 21:15:23 +00:00
|
|
|
ThreadEntryUPP entry_proc;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
entry_proc = NewThreadEntryUPP( worker_main );
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
// Create the thread in a suspended state
|
2003-10-02 18:51:38 +00:00
|
|
|
theErr = NewThread ( kCooperativeThread, entry_proc,
|
|
|
|
(void *)(&gi), 0, kNewSuspend | kCreateIfNeeded, NULL, &graphicsThreadID
|
|
|
|
);
|
2002-09-18 21:15:23 +00:00
|
|
|
if (theErr != noErr) return ERR_THREAD;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
// In theory we could do customized scheduling or install thread disposal routines here
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
// Put the graphics event loop into the ready state
|
2004-10-11 23:57:07 +00:00
|
|
|
SetThreadState(workerThreadID, kReadyThreadState, kNoThreadID);
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
YieldToAnyThread();
|
2004-10-11 23:57:07 +00:00
|
|
|
graphics_inited = true;
|
2004-10-19 20:27:09 +00:00
|
|
|
mac_graphics_event_loop();
|
2002-09-18 21:15:23 +00:00
|
|
|
#endif
|
2002-11-12 20:59:00 +00:00
|
|
|
|
|
|
|
#ifdef _PTHREAD_H
|
2004-10-11 23:57:07 +00:00
|
|
|
pthread_t worker_thread;
|
|
|
|
pthread_attr_t worker_thread_attr;
|
2004-10-20 22:11:12 +00:00
|
|
|
sched_param param;
|
2004-10-21 05:30:53 +00:00
|
|
|
int retval, currentpolicy, minpriority;
|
2002-11-12 20:59:00 +00:00
|
|
|
|
2004-10-20 22:11:12 +00:00
|
|
|
// make the worker thread low priority
|
|
|
|
// (current thread, i.e. graphics thread, should remain high priority)
|
|
|
|
//
|
2004-10-21 05:30:53 +00:00
|
|
|
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;
|
2004-10-20 22:11:12 +00:00
|
|
|
|
2004-10-21 05:30:53 +00:00
|
|
|
retval = pthread_create(&worker_thread, &worker_thread_attr, foobar, 0);
|
2002-11-12 20:59:00 +00:00
|
|
|
if (retval) return ERR_THREAD;
|
2004-10-11 23:57:07 +00:00
|
|
|
pthread_attr_destroy( &worker_thread_attr );
|
2003-09-30 21:17:20 +00:00
|
|
|
graphics_inited = true;
|
2004-10-11 23:57:07 +00:00
|
|
|
xwin_graphics_event_loop();
|
2004-10-19 20:27:09 +00:00
|
|
|
pthread_exit(0);
|
2004-10-11 23:57:07 +00:00
|
|
|
#endif
|
2004-05-27 20:44:33 +00:00
|
|
|
|
2004-10-11 23:57:07 +00:00
|
|
|
// normally we never get here
|
2004-05-27 20:44:33 +00:00
|
|
|
return 0;
|
2002-09-18 21:15:23 +00:00
|
|
|
}
|
|
|
|
|
2004-10-16 20:20:37 +00:00
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
extern "C" {
|
|
|
|
void glut_quit() {
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-10-05 05:26:59 +00:00
|
|
|
bool throttled_app_render(int x, int y, double t) {
|
2003-10-02 21:16:37 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
ok_to_render = true;
|
|
|
|
now = dtime();
|
|
|
|
diff = now - last_now;
|
|
|
|
last_now = now;
|
2003-10-14 20:37:02 +00:00
|
|
|
if (diff > 1000) diff = 0; // handle initial case
|
2003-10-02 21:16:37 +00:00
|
|
|
|
|
|
|
// enforce frames/sec restriction
|
|
|
|
//
|
2003-10-14 20:37:02 +00:00
|
|
|
if (boinc_max_fps) {
|
2003-10-02 21:16:37 +00:00
|
|
|
time_until_render -= diff;
|
|
|
|
if (time_until_render < 0) {
|
2003-10-14 20:37:02 +00:00
|
|
|
time_until_render += 1./boinc_max_fps;
|
2003-10-02 21:16:37 +00:00
|
|
|
} else {
|
|
|
|
ok_to_render = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enforce max CPU time restriction
|
|
|
|
//
|
2003-10-14 20:37:02 +00:00
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
2003-10-02 21:16:37 +00:00
|
|
|
elapsed_time += diff;
|
|
|
|
if (elapsed_time) {
|
|
|
|
frac = total_render_time/elapsed_time;
|
2003-10-14 20:37:02 +00:00
|
|
|
if (frac > boinc_max_gfx_cpu_frac) {
|
2003-10-02 21:16:37 +00:00
|
|
|
ok_to_render = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// render if allowed
|
|
|
|
//
|
|
|
|
if (ok_to_render) {
|
2003-10-14 20:37:02 +00:00
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
2004-05-25 05:35:59 +00:00
|
|
|
boinc_calling_thread_cpu_time(t0, m);
|
2003-10-02 21:16:37 +00:00
|
|
|
}
|
2003-10-31 22:37:46 +00:00
|
|
|
app_graphics_render(x, y, t);
|
2003-10-14 20:37:02 +00:00
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
2004-05-25 05:35:59 +00:00
|
|
|
boinc_calling_thread_cpu_time(t1, m);
|
2003-10-02 21:16:37 +00:00
|
|
|
total_render_time += t1 - t0;
|
|
|
|
}
|
2003-10-05 05:26:59 +00:00
|
|
|
return true;
|
2003-10-02 21:16:37 +00:00
|
|
|
}
|
2003-10-05 05:26:59 +00:00
|
|
|
return false;
|
2004-07-13 13:54:09 +00:00
|
|
|
}
|