4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define GL_GLEXT_PROTOTYPES
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bcsynchronous.h"
26 #include "bcwindowbase.h"
27 #include "condition.h"
39 TextureID::TextureID(int window_id, int id, int w, int h, int components)
41 this->window_id = window_id;
45 this->components = components;
49 ShaderID::ShaderID(int window_id, unsigned int handle, char *source)
51 this->window_id = window_id;
52 this->handle = handle;
53 this->source = strdup(source);
62 PBufferID::PBufferID(int window_id, GLXPbuffer glx_pbuffer, GLXContext glx_context, int w, int h)
64 this->glx_pbuffer = glx_pbuffer;
65 this->glx_context = glx_context;
66 this->window_id = window_id;
75 BC_SynchronousCommand::BC_SynchronousCommand()
77 command = BC_SynchronousCommand::NONE;
81 command_done = new Condition(0, "BC_SynchronousCommand::command_done", 0);
84 BC_SynchronousCommand::~BC_SynchronousCommand()
89 void BC_SynchronousCommand::copy_from(BC_SynchronousCommand *command)
91 this->command = command->command;
92 this->colormodel = command->colormodel;
93 this->window = command->window;
94 this->frame = command->frame;
95 this->window_id = command->window_id;
96 this->frame_return = command->frame_return;
97 this->id = command->id;
105 BC_Synchronous::BC_Synchronous()
108 lock_sync = new Mutex("BC_Synchronous::lock_sync");
109 next_command = new Condition(0, "BC_Synchronous::next_command", 1);
110 command_lock = new Mutex("BC_Synchronous::command_lock");
111 table_lock = new Mutex("BC_Synchronous::table_lock");
115 BC_WindowBase::get_resources()->set_synchronous(this);
118 BC_Synchronous::~BC_Synchronous()
124 commands.remove_all_objects();
131 void BC_Synchronous::sync_lock(const char *cp)
136 void BC_Synchronous::sync_unlock()
141 BC_SynchronousCommand* BC_Synchronous::new_command()
143 return new BC_SynchronousCommand();
146 void BC_Synchronous::create_objects()
150 void BC_Synchronous::start()
157 void BC_Synchronous::quit()
159 if( !is_started ) return;
161 BC_SynchronousCommand *command = new_command();
162 command->command = BC_SynchronousCommand::QUIT;
163 command_lock->lock("BC_Synchronous::quit");
164 commands.append(command);
165 command_lock->unlock();
166 next_command->unlock();
167 command->command_done->lock("BC_Synchronous::quit");
171 long BC_Synchronous::send_command(BC_SynchronousCommand *command)
173 BC_SynchronousCommand *command2 = new_command();
174 command2->copy_from(command);
175 command_lock->lock("BC_Synchronous::send_command");
176 commands.append(command2);
177 command_lock->unlock();
178 next_command->unlock();
179 //printf("BC_Synchronous::send_command 1 %d\n", next_command->get_value());
181 // Wait for completion
182 command2->command_done->lock("BC_Synchronous::send_command");
183 long result = command2->result;
188 void BC_Synchronous::run()
190 sync_lock("BC_Synchronous::run 0");
192 command_lock->lock("BC_Synchronous::run");
193 BC_SynchronousCommand *command = 0;
195 command = commands.values[0];
196 commands.remove_number(0);
198 command_lock->unlock();
201 next_command->lock("BC_Synchronous::run");
202 sync_lock("BC_Synchronous::run 1");
205 //printf("BC_Synchronous::run %d\n", command->command);
206 handle_command_base(command);
211 void BC_Synchronous::handle_command_base(BC_SynchronousCommand *command)
213 switch(command->command) {
214 case BC_SynchronousCommand::QUIT:
216 command->command_done->unlock();
219 case BC_SynchronousCommand::DELETE_WINDOW:
220 delete_window_sync(command);
223 case BC_SynchronousCommand::DELETE_PIXMAP:
224 delete_pixmap_sync(command);
227 case BC_SynchronousCommand::DELETE_DISPLAY:
228 delete_display_sync(command);
232 handle_command(command);
233 command->command_done->unlock();
239 void BC_Synchronous::handle_command(BC_SynchronousCommand *command)
243 void BC_Synchronous::put_texture(int id, int w, int h, int components)
247 table_lock->lock("BC_Resources::put_texture");
248 // Search for duplicate
249 for(int i = 0; i < texture_ids.total; i++)
251 TextureID *ptr = texture_ids.values[i];
252 if(ptr->window_id == current_window->get_id() &&
255 printf("BC_Synchronous::push_texture: texture exists\n"
256 "exists: window=%d id=%d w=%d h=%d\n"
257 "new: window=%d id=%d w=%d h=%d\n",
262 current_window->get_id(),
266 table_lock->unlock();
271 TextureID *new_id = new TextureID(current_window->get_id(),
276 texture_ids.append(new_id);
277 table_lock->unlock();
281 int BC_Synchronous::get_texture(int w, int h, int components)
283 table_lock->lock("BC_Resources::get_texture");
284 for(int i = 0; i < texture_ids.total; i++)
286 if(texture_ids.values[i]->w == w &&
287 texture_ids.values[i]->h == h &&
288 texture_ids.values[i]->components == components &&
289 !texture_ids.values[i]->in_use &&
290 texture_ids.values[i]->window_id == current_window->get_id())
292 int result = texture_ids.values[i]->id;
293 texture_ids.values[i]->in_use = 1;
294 table_lock->unlock();
298 table_lock->unlock();
302 void BC_Synchronous::release_texture(int window_id, int id)
304 table_lock->lock("BC_Resources::release_texture");
305 for(int i = 0; i < texture_ids.total; i++)
307 if(texture_ids.values[i]->id == id &&
308 texture_ids.values[i]->window_id == window_id)
310 texture_ids.values[i]->in_use = 0;
311 table_lock->unlock();
315 table_lock->unlock();
322 unsigned int BC_Synchronous::get_shader(char *source, int *got_it)
324 table_lock->lock("BC_Resources::get_shader");
325 for(int i = 0; i < shader_ids.total; i++)
327 if(shader_ids.values[i]->window_id == current_window->get_id() &&
328 !strcmp(shader_ids.values[i]->source, source))
330 unsigned int result = shader_ids.values[i]->handle;
331 table_lock->unlock();
336 table_lock->unlock();
341 void BC_Synchronous::put_shader(unsigned int handle,
344 table_lock->lock("BC_Resources::put_shader");
345 shader_ids.append(new ShaderID(current_window->get_id(), handle, source));
346 table_lock->unlock();
349 void BC_Synchronous::dump_shader(unsigned int handle)
352 table_lock->lock("BC_Resources::dump_shader");
353 for(int i = 0; i < shader_ids.total; i++)
355 if(shader_ids.values[i]->handle == handle)
357 printf("BC_Synchronous::dump_shader\n"
358 "%s", shader_ids.values[i]->source);
363 table_lock->unlock();
364 if(!got_it) printf("BC_Synchronous::dump_shader couldn't find %d\n", handle);
367 void BC_Synchronous::delete_window(BC_WindowBase *window)
370 BC_SynchronousCommand *command = new_command();
371 command->command = BC_SynchronousCommand::DELETE_WINDOW;
372 command->window_id = window->get_id();
373 command->display = window->get_display();
374 command->win = window->win;
375 command->glx_win = window->glx_win;
376 command->glx_context = window->glx_win_context;
378 send_garbage(command);
382 void BC_Synchronous::delete_window_sync(BC_SynchronousCommand *command)
385 int window_id = command->window_id;
386 Display *display = command->display;
387 Window win = command->win;
388 GLXWindow glx_win = command->glx_win;
389 GLXContext glx_context = command->glx_context;
390 XLockDisplay(display);
393 // texture ID's are unique to different contexts
394 glXMakeContextCurrent(display, glx_win, glx_win, glx_context);
396 table_lock->lock("BC_Resources::release_textures");
397 for(int i = 0; i < texture_ids.total; i++) {
398 if(texture_ids.values[i]->window_id == window_id) {
399 GLuint id = texture_ids.values[i]->id;
400 glDeleteTextures(1, &id);
401 //if(debug) printf("BC_Synchronous::delete_window_sync texture_id=%d window_id=%d\n",
403 texture_ids.remove_object_number(i);
408 for(int i = 0; i < shader_ids.total; i++)
410 if(shader_ids.values[i]->window_id == window_id)
412 glDeleteShader(shader_ids.values[i]->handle);
414 //printf("BC_Synchronous::delete_window_sync shader_id=%d window_id=%d\n",
415 //shader_ids.values[i]->handle, window_id);
416 shader_ids.remove_object_number(i);
421 for(int i = 0; i < pbuffer_ids.total; i++)
423 if(pbuffer_ids.values[i]->window_id == window_id)
425 glXDestroyPbuffer(display, pbuffer_ids.values[i]->glx_pbuffer);
426 glXDestroyContext(display, pbuffer_ids.values[i]->glx_context);
428 //printf("BC_Synchronous::delete_window_sync pbuffer_id=%p window_id=%d\n",
429 // (void*)pbuffer_ids.values[i]->pbuffer, window_id);
430 pbuffer_ids.remove_object_number(i);
436 table_lock->unlock();
438 XDestroyWindow(display, win);
440 glXDestroyContext(display, glx_context);
441 command->command_done->unlock();
442 XUnlockDisplay(display);
446 void BC_Synchronous::delete_display(BC_WindowBase *window)
449 BC_SynchronousCommand *command = new_command();
450 command->command = BC_SynchronousCommand::DELETE_DISPLAY;
451 command->display = window->get_display();
453 send_garbage(command);
457 void BC_Synchronous::delete_display_sync(BC_SynchronousCommand *command)
460 Display *display = command->display;
461 XLockDisplay(display);
462 XUnlockDisplay(display);
463 glXMakeContextCurrent(display, None, None, 0);
464 XCloseDisplay(display);
469 void BC_Synchronous::put_pbuffer(int w, int h,
470 GLXPbuffer glx_pbuffer, GLXContext glx_context)
473 table_lock->lock("BC_Resources::release_textures");
474 for(int i = 0; i < pbuffer_ids.total; i++) {
475 PBufferID *ptr = pbuffer_ids.values[i];
476 if( ptr->w == w && ptr->h == h && ptr->glx_pbuffer == glx_pbuffer ) {
483 PBufferID *ptr = new PBufferID(current_window->get_id(),
484 glx_pbuffer, glx_context, w, h);
485 pbuffer_ids.append(ptr);
487 table_lock->unlock();
490 GLXPbuffer BC_Synchronous::get_pbuffer(int w, int h, GLXContext *glx_context)
492 table_lock->lock("BC_Resources::release_textures");
493 for(int i = 0; i < pbuffer_ids.total; i++) {
494 PBufferID *ptr = pbuffer_ids.values[i];
495 if(ptr->w == w && ptr->h == h && !ptr->in_use &&
496 ptr->window_id == current_window->get_id() ) {
497 GLXPbuffer result = ptr->glx_pbuffer;
498 *glx_context = ptr->glx_context;
500 table_lock->unlock();
504 table_lock->unlock();
508 void BC_Synchronous::release_pbuffer(int window_id, GLXPbuffer pbuffer)
510 table_lock->lock("BC_Resources::release_textures");
511 for(int i = 0; i < pbuffer_ids.total; i++) {
512 PBufferID *ptr = pbuffer_ids.values[i];
513 if( ptr->window_id == window_id ) {
517 table_lock->unlock();
520 void BC_Synchronous::delete_pixmap(BC_WindowBase *window,
521 GLXPixmap glx_pixmap, GLXContext glx_context)
523 BC_SynchronousCommand *command = new_command();
524 command->command = BC_SynchronousCommand::DELETE_PIXMAP;
525 command->window_id = window->get_id();
526 command->display = window->get_display();
527 command->win = window->win;
528 command->glx_win = window->glx_win;
529 command->glx_pixmap = glx_pixmap;
530 command->glx_context = glx_context;
532 send_garbage(command);
536 void BC_Synchronous::delete_pixmap_sync(BC_SynchronousCommand *command)
539 Display *display = command->display;
540 GLXWindow glx_win = command->glx_win;
541 XLockDisplay(display);
542 glXMakeContextCurrent(display, glx_win, glx_win, command->glx_context);
543 glXDestroyContext(display, command->glx_context);
544 glXDestroyGLXPixmap(display, command->glx_pixmap);
545 XUnlockDisplay(display);
551 void BC_Synchronous::send_garbage(BC_SynchronousCommand *command)
553 command_lock->lock("BC_Synchronous::send_garbage");
554 commands.append(command);
555 command_lock->unlock();
556 next_command->unlock();
559 BC_WindowBase* BC_Synchronous::get_window()
561 return current_window;