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
|
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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2003-10-01 18:19:29 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-11-01 18:59:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <afxwin.h>
|
2003-09-30 21:17:20 +00:00
|
|
|
extern DWORD WINAPI win_graphics_event_loop( LPVOID duff );
|
2002-11-14 00:49:24 +00:00
|
|
|
HANDLE graphics_threadh=NULL;
|
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"
|
|
|
|
#endif
|
2002-10-31 00:13:50 +00:00
|
|
|
|
2002-11-12 20:59:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
#ifdef HAVE_PTHREAD
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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-10-02 21:16:37 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "graphics_api.h"
|
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
|
|
|
GRAPHICS_INFO gi;
|
|
|
|
bool graphics_inited = false;
|
2002-09-18 21:15:23 +00:00
|
|
|
|
|
|
|
int boinc_init_opengl() {
|
2003-10-01 18:19:29 +00:00
|
|
|
#ifdef HAVE_GL_LIB
|
2003-09-30 21:17:20 +00:00
|
|
|
FILE* f;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
f = fopen(GRAPHICS_DATA_FILE, "r");
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't open graphics data file\n");
|
|
|
|
fprintf(stderr, "Using default graphics settings.\n");
|
|
|
|
gi.refresh_period = 0.1; // 1/10th of a second
|
|
|
|
gi.xsize = 640;
|
|
|
|
gi.ysize = 480;
|
|
|
|
} else {
|
|
|
|
retval = parse_graphics_file(f, &gi);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't parse graphics data file\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2002-09-18 22:15:49 +00:00
|
|
|
#ifdef _WIN32
|
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
|
|
|
//
|
2002-11-14 00:49:24 +00:00
|
|
|
graphics_threadh = CreateThread(
|
2002-10-06 00:43:55 +00:00
|
|
|
NULL, 0, win_graphics_event_loop, &gi, CREATE_SUSPENDED, &threadId
|
|
|
|
);
|
2002-09-18 22:15:49 +00:00
|
|
|
|
2003-05-21 17:59:10 +00:00
|
|
|
// lower priority of worker thread (i.e. current thread)
|
|
|
|
//
|
|
|
|
HANDLE h = GetCurrentThread();
|
|
|
|
SetThreadPriority(h, THREAD_PRIORITY_LOWEST);
|
|
|
|
|
|
|
|
// Raise graphics thread priority
|
|
|
|
//
|
2002-11-14 00:49:24 +00:00
|
|
|
SetThreadPriority(graphics_threadh, THREAD_PRIORITY_HIGHEST);
|
2002-09-18 22:15:49 +00:00
|
|
|
|
|
|
|
// Start the graphics thread
|
2003-05-21 17:59:10 +00:00
|
|
|
//
|
2002-11-14 00:49:24 +00:00
|
|
|
ResumeThread(graphics_threadh);
|
2002-09-18 22:15:49 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-18 21:15:23 +00:00
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
OSErr theErr = noErr;
|
|
|
|
ThreadID graphicsThreadID = 0;
|
|
|
|
ThreadEntryUPP entry_proc;
|
|
|
|
|
|
|
|
entry_proc = NewThreadEntryUPP( mac_graphics_event_loop );
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// In theory we could do customized scheduling or install thread disposal routines here
|
|
|
|
|
|
|
|
// Put the graphics event loop into the ready state
|
2002-10-06 00:43:55 +00:00
|
|
|
SetThreadState(graphicsThreadID, kReadyThreadState, kNoThreadID);
|
2002-09-18 21:15:23 +00:00
|
|
|
|
|
|
|
YieldToAnyThread();
|
|
|
|
#endif
|
2002-11-12 20:59:00 +00:00
|
|
|
|
|
|
|
#ifdef _PTHREAD_H
|
|
|
|
pthread_t graphics_thread;
|
|
|
|
pthread_attr_t graphics_thread_attr;
|
|
|
|
|
|
|
|
pthread_attr_init( &graphics_thread_attr );
|
|
|
|
retval = pthread_create( &graphics_thread, &graphics_thread_attr, p_graphics_loop, &gi );
|
|
|
|
if (retval) return ERR_THREAD;
|
|
|
|
pthread_attr_destroy( &graphics_thread_attr );
|
|
|
|
#endif
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
graphics_inited = true;
|
2003-10-01 18:19:29 +00:00
|
|
|
#else
|
|
|
|
graphics_inited = false;
|
|
|
|
#endif
|
|
|
|
return !graphics_inited;
|
2002-09-18 21:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_finish_opengl() {
|
2003-09-30 21:17:20 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (graphics_inited) {
|
|
|
|
win_loop_done = TRUE;
|
|
|
|
if (hQuitEvent != NULL) {
|
|
|
|
WaitForSingleObject(hQuitEvent, 1000); // Wait up to 1000 ms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-17 23:17:09 +00:00
|
|
|
|
2002-09-18 22:15:49 +00:00
|
|
|
return 0;
|
2002-09-18 21:15:23 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2003-10-02 21:16:37 +00:00
|
|
|
boinc_cpu_time(t0, m);
|
|
|
|
}
|
|
|
|
app_render(x, y, t);
|
2003-10-14 20:37:02 +00:00
|
|
|
if (boinc_max_gfx_cpu_frac) {
|
2003-10-02 21:16:37 +00:00
|
|
|
boinc_cpu_time(t1, m);
|
|
|
|
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;
|
2003-10-02 21:16:37 +00:00
|
|
|
}
|
|
|
|
|
2003-10-01 18:19:29 +00:00
|
|
|
#ifdef HAVE_GL_LIB
|
2003-10-02 18:51:38 +00:00
|
|
|
// Custom GL "Print" Routine
|
|
|
|
GLvoid glPrint(GLuint font, const char *fmt, ...) {
|
2003-09-22 22:22:20 +00:00
|
|
|
/*
|
2002-10-31 00:13:50 +00:00
|
|
|
char text[256]; // Holds Our String
|
|
|
|
va_list ap; // Pointer To List Of Arguments
|
|
|
|
|
|
|
|
if (fmt == NULL) // If There's No Text
|
|
|
|
return; // Do Nothing
|
|
|
|
|
|
|
|
va_start(ap, fmt); // Parses The String For Variables
|
|
|
|
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
|
|
|
|
va_end(ap); // Results Are Stored In Text
|
|
|
|
|
|
|
|
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
|
|
|
|
glListBase(font); // Sets The Base Character
|
|
|
|
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
|
|
|
|
glPopAttrib(); // Pops The Display List Bits
|
2003-09-22 22:22:20 +00:00
|
|
|
*/
|
2002-10-31 00:13:50 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 18:51:38 +00:00
|
|
|
// All Setup For OpenGL Goes Here
|
|
|
|
//
|
|
|
|
GLenum InitGL(GLvoid) {
|
2003-02-14 18:47:26 +00:00
|
|
|
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
|
|
|
glClearDepth(1.0f); // Depth Buffer Setup
|
|
|
|
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
|
|
|
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
|
|
|
|
return GL_NO_ERROR; // Initialization Went OK
|
|
|
|
}
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
// Resize And Initialize The GL Window
|
|
|
|
// TODO: why is 4/3 hardwired here?
|
|
|
|
GLenum ReSizeGLScene(GLsizei width, GLsizei height) {
|
2003-02-14 18:47:26 +00:00
|
|
|
GLenum err;
|
2003-10-10 23:36:19 +00:00
|
|
|
#if 0
|
2003-02-14 18:47:26 +00:00
|
|
|
double aspect_ratio = 4.0/3.0;
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
if (height<=0) height=1;
|
2003-02-14 18:47:26 +00:00
|
|
|
if (width<=0) width=1;
|
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
if (height*aspect_ratio > width) {
|
|
|
|
glViewport(0,0,(int)width,(int)(width/aspect_ratio));
|
|
|
|
} else {
|
|
|
|
glViewport(0,0,(int)(height*aspect_ratio),(height));
|
|
|
|
}
|
2003-10-10 23:36:19 +00:00
|
|
|
#endif
|
|
|
|
glViewport(0,0,(int)width,(int)(height));
|
2003-09-30 21:17:20 +00:00
|
|
|
err = glGetError();
|
|
|
|
if (err) return err;
|
|
|
|
|
2003-09-22 22:22:20 +00:00
|
|
|
app_resize(width,height);
|
2003-02-14 18:47:26 +00:00
|
|
|
|
|
|
|
return GL_NO_ERROR;
|
|
|
|
}
|
2003-10-01 18:19:29 +00:00
|
|
|
#endif
|