rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.git] / cinelerra-5.1 / guicast / bcsynchronous.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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"
28 #include "mutex.h"
29
30
31 #ifdef HAVE_GL
32 #include <GL/gl.h>
33 #endif
34 #include <unistd.h>
35
36 #include <string.h>
37
38
39 TextureID::TextureID(int window_id, int id, int w, int h, int components)
40 {
41         this->window_id = window_id;
42         this->id = id;
43         this->w = w;
44         this->h = h;
45         this->components = components;
46         in_use = 1;
47 }
48
49 ShaderID::ShaderID(int window_id, unsigned int handle, char *source)
50 {
51         this->window_id = window_id;
52         this->handle = handle;
53         this->source = strdup(source);
54 }
55
56 ShaderID::~ShaderID()
57 {
58         free(source);
59 }
60
61 #ifdef HAVE_GL
62 PBufferID::PBufferID(int window_id, GLXPbuffer glx_pbuffer, GLXContext glx_context, int w, int h)
63 {
64         this->glx_pbuffer = glx_pbuffer;
65         this->glx_context = glx_context;
66         this->window_id = window_id;
67         this->w = w;
68         this->h = h;
69         in_use = 1;
70 }
71 #endif
72
73
74
75 BC_SynchronousCommand::BC_SynchronousCommand()
76 {
77         command = BC_SynchronousCommand::NONE;
78         frame = 0;
79         frame_return = 0;
80         result = 0;
81         command_done = new Condition(0, "BC_SynchronousCommand::command_done", 0);
82 }
83
84 BC_SynchronousCommand::~BC_SynchronousCommand()
85 {
86         delete command_done;
87 }
88
89 void BC_SynchronousCommand::copy_from(BC_SynchronousCommand *command)
90 {
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;
98         this->w =               command->w;
99         this->h =               command->h;
100 }
101
102
103
104
105 BC_Synchronous::BC_Synchronous()
106  : Thread(1, 0, 0)
107 {
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");
112         done = 0;
113         is_started = 0;
114         current_window = 0;
115         BC_WindowBase::get_resources()->set_synchronous(this);
116 }
117
118 BC_Synchronous::~BC_Synchronous()
119 {
120         if( running() ) {
121                 quit();
122         }
123         join();
124         commands.remove_all_objects();
125         delete lock_sync;
126         delete next_command;
127         delete command_lock;
128         delete table_lock;
129 }
130
131 void BC_Synchronous::sync_lock(const char *cp)
132 {
133         lock_sync->lock(cp);
134 }
135
136 void BC_Synchronous::sync_unlock()
137 {
138         lock_sync->unlock();
139 }
140
141 BC_SynchronousCommand* BC_Synchronous::new_command()
142 {
143         return new BC_SynchronousCommand();
144 }
145
146 void BC_Synchronous::create_objects()
147 {
148 }
149
150 void BC_Synchronous::start()
151 {
152         is_started = 1;
153         //run();
154         Thread::start();
155 }
156
157 void BC_Synchronous::quit()
158 {
159         if( !is_started ) return;
160         is_started = 0;
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");
168         delete command;
169 }
170
171 long BC_Synchronous::send_command(BC_SynchronousCommand *command)
172 {
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());
180
181 // Wait for completion
182         command2->command_done->lock("BC_Synchronous::send_command");
183         long result = command2->result;
184         delete command2;
185         return result;
186 }
187
188 void BC_Synchronous::run()
189 {
190         sync_lock("BC_Synchronous::run 0");
191         while(!done) {
192                 command_lock->lock("BC_Synchronous::run");
193                 BC_SynchronousCommand *command = 0;
194                 if(commands.total) {
195                         command = commands.values[0];
196                         commands.remove_number(0);
197                 }
198                 command_lock->unlock();
199                 if( !command ) {
200                         sync_unlock();
201                         next_command->lock("BC_Synchronous::run");
202                         sync_lock("BC_Synchronous::run 1");
203                         continue;
204                 }
205 //printf("BC_Synchronous::run %d\n", command->command);
206                 handle_command_base(command);
207         }
208         sync_unlock();
209 }
210
211 void BC_Synchronous::handle_command_base(BC_SynchronousCommand *command)
212 {
213         switch(command->command) {
214         case BC_SynchronousCommand::QUIT:
215                 done = 1;
216                 command->command_done->unlock();
217                 return;
218
219         case BC_SynchronousCommand::DELETE_WINDOW:
220                 delete_window_sync(command);
221                 break;
222
223         case BC_SynchronousCommand::DELETE_PIXMAP:
224                 delete_pixmap_sync(command);
225                 break;
226
227         case BC_SynchronousCommand::DELETE_DISPLAY:
228                 delete_display_sync(command);
229                 break;
230
231         default:
232                 handle_command(command);
233                 command->command_done->unlock();
234                 return;
235         }
236         delete command;
237 }
238
239 void BC_Synchronous::handle_command(BC_SynchronousCommand *command)
240 {
241 }
242
243 void BC_Synchronous::put_texture(int id, int w, int h, int components)
244 {
245         if(id >= 0)
246         {
247                 table_lock->lock("BC_Resources::put_texture");
248 // Search for duplicate
249                 for(int i = 0; i < texture_ids.total; i++)
250                 {
251                         TextureID *ptr = texture_ids.values[i];
252                         if(ptr->window_id == current_window->get_id() &&
253                                 ptr->id == id)
254                         {
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",
258                                         ptr->window_id,
259                                         ptr->id,
260                                         ptr->w,
261                                         ptr->h,
262                                         current_window->get_id(),
263                                         id,
264                                         w,
265                                         h);
266                                 table_lock->unlock();
267                                 return;
268                         }
269                 }
270
271                 TextureID *new_id = new TextureID(current_window->get_id(),
272                         id,
273                         w,
274                         h,
275                         components);
276                 texture_ids.append(new_id);
277                 table_lock->unlock();
278         }
279 }
280
281 int BC_Synchronous::get_texture(int w, int h, int components)
282 {
283         table_lock->lock("BC_Resources::get_texture");
284         for(int i = 0; i < texture_ids.total; i++)
285         {
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())
291                 {
292                         int result = texture_ids.values[i]->id;
293                         texture_ids.values[i]->in_use = 1;
294                         table_lock->unlock();
295                         return result;
296                 }
297         }
298         table_lock->unlock();
299         return -1;
300 }
301
302 void BC_Synchronous::release_texture(int window_id, int id)
303 {
304         table_lock->lock("BC_Resources::release_texture");
305         for(int i = 0; i < texture_ids.total; i++)
306         {
307                 if(texture_ids.values[i]->id == id &&
308                         texture_ids.values[i]->window_id == window_id)
309                 {
310                         texture_ids.values[i]->in_use = 0;
311                         table_lock->unlock();
312                         return;
313                 }
314         }
315         table_lock->unlock();
316 }
317
318
319
320
321
322 unsigned int BC_Synchronous::get_shader(char *source, int *got_it)
323 {
324         table_lock->lock("BC_Resources::get_shader");
325         for(int i = 0; i < shader_ids.total; i++)
326         {
327                 if(shader_ids.values[i]->window_id == current_window->get_id() &&
328                         !strcmp(shader_ids.values[i]->source, source))
329                 {
330                         unsigned int result = shader_ids.values[i]->handle;
331                         table_lock->unlock();
332                         *got_it = 1;
333                         return result;
334                 }
335         }
336         table_lock->unlock();
337         *got_it = 0;
338         return 0;
339 }
340
341 void BC_Synchronous::put_shader(unsigned int handle,
342         char *source)
343 {
344         table_lock->lock("BC_Resources::put_shader");
345         shader_ids.append(new ShaderID(current_window->get_id(), handle, source));
346         table_lock->unlock();
347 }
348
349 void BC_Synchronous::dump_shader(unsigned int handle)
350 {
351         int got_it = 0;
352         table_lock->lock("BC_Resources::dump_shader");
353         for(int i = 0; i < shader_ids.total; i++)
354         {
355                 if(shader_ids.values[i]->handle == handle)
356                 {
357                         printf("BC_Synchronous::dump_shader\n"
358                                 "%s", shader_ids.values[i]->source);
359                         got_it = 1;
360                         break;
361                 }
362         }
363         table_lock->unlock();
364         if(!got_it) printf("BC_Synchronous::dump_shader couldn't find %d\n", handle);
365 }
366
367 void BC_Synchronous::delete_window(BC_WindowBase *window)
368 {
369 #ifdef HAVE_GL
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;
377
378         send_garbage(command);
379 #endif
380 }
381
382 void BC_Synchronous::delete_window_sync(BC_SynchronousCommand *command)
383 {
384 #ifdef HAVE_GL
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);
391 //int debug = 0;
392
393 // texture ID's are unique to different contexts
394         glXMakeContextCurrent(display, glx_win, glx_win, glx_context);
395
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",
402 // id, window_id);
403                         texture_ids.remove_object_number(i);
404                         i--;
405                 }
406         }
407
408         for(int i = 0; i < shader_ids.total; i++)
409         {
410                 if(shader_ids.values[i]->window_id == window_id)
411                 {
412                         glDeleteShader(shader_ids.values[i]->handle);
413 //if(debug)
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);
417                         i--;
418                 }
419         }
420
421         for(int i = 0; i < pbuffer_ids.total; i++)
422         {
423                 if(pbuffer_ids.values[i]->window_id == window_id)
424                 {
425                         glXDestroyPbuffer(display, pbuffer_ids.values[i]->glx_pbuffer);
426                         glXDestroyContext(display, pbuffer_ids.values[i]->glx_context);
427 //if(debug)
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);
431                         i--;
432                 }
433         }
434
435
436         table_lock->unlock();
437
438         XDestroyWindow(display, win);
439         if( glx_context )
440                 glXDestroyContext(display, glx_context);
441         command->command_done->unlock();
442         XUnlockDisplay(display);
443 #endif
444 }
445
446 void BC_Synchronous::delete_display(BC_WindowBase *window)
447 {
448 #ifdef HAVE_GL
449         BC_SynchronousCommand *command = new_command();
450         command->command = BC_SynchronousCommand::DELETE_DISPLAY;
451         command->display = window->get_display();
452
453         send_garbage(command);
454 #endif
455 }
456
457 void BC_Synchronous::delete_display_sync(BC_SynchronousCommand *command)
458 {
459 #ifdef HAVE_GL
460         Display *display = command->display;
461         XLockDisplay(display);
462         XUnlockDisplay(display);
463         glXMakeContextCurrent(display, None, None, 0);
464         XCloseDisplay(display);
465 #endif
466 }
467
468 #ifdef HAVE_GL
469 void BC_Synchronous::put_pbuffer(int w, int h,
470                 GLXPbuffer glx_pbuffer, GLXContext glx_context)
471 {
472         int exists = 0;
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 ) {
477                         exists = 1;
478                         break;
479                 }
480         }
481
482         if(!exists) {
483                 PBufferID *ptr = new PBufferID(current_window->get_id(),
484                         glx_pbuffer, glx_context, w, h);
485                 pbuffer_ids.append(ptr);
486         }
487         table_lock->unlock();
488 }
489
490 GLXPbuffer BC_Synchronous::get_pbuffer(int w, int h, GLXContext *glx_context)
491 {
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;
499                         ptr->in_use = 1;
500                         table_lock->unlock();
501                         return result;
502                 }
503         }
504         table_lock->unlock();
505         return 0;
506 }
507
508 void BC_Synchronous::release_pbuffer(int window_id, GLXPbuffer pbuffer)
509 {
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 ) {
514                         ptr->in_use = 0;
515                 }
516         }
517         table_lock->unlock();
518 }
519
520 void BC_Synchronous::delete_pixmap(BC_WindowBase *window,
521         GLXPixmap glx_pixmap, GLXContext glx_context)
522 {
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;
531
532         send_garbage(command);
533 }
534 #endif
535
536 void BC_Synchronous::delete_pixmap_sync(BC_SynchronousCommand *command)
537 {
538 #ifdef HAVE_GL
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);
546 #endif
547 }
548
549
550
551 void BC_Synchronous::send_garbage(BC_SynchronousCommand *command)
552 {
553         command_lock->lock("BC_Synchronous::send_garbage");
554         commands.append(command);
555         command_lock->unlock();
556         next_command->unlock();
557 }
558
559 BC_WindowBase* BC_Synchronous::get_window()
560 {
561         return current_window;
562 }
563
564
565
566
567
568
569