Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / guicast / bcpbuffer.C
blob28c955b1dfec0670b56f5a6e0841fc9fc728355d
1 #include "bcpbuffer.h"
2 #include "bcresources.h"
3 #include "bcsignals.h"
4 #include "bcsynchronous.h"
5 #include "bcwindowbase.h"
10 BC_PBuffer::BC_PBuffer(int w, int h)
12         reset();
13         this->w = w;
14         this->h = h;
17         new_pbuffer(w, h);
20 BC_PBuffer::~BC_PBuffer()
22 #ifdef HAVE_GL
23         BC_WindowBase::get_synchronous()->release_pbuffer(window_id, pbuffer);
24 #endif
28 void BC_PBuffer::reset()
30 #ifdef HAVE_GL
31         pbuffer = 0;
32         window_id = -1;
33 #endif
36 #ifdef HAVE_GL
38 GLXPbuffer BC_PBuffer::get_pbuffer()
40         return pbuffer;
42 #endif
45 void BC_PBuffer::new_pbuffer(int w, int h)
47 #ifdef HAVE_GL
49         if(!pbuffer)
50         {
51                 BC_WindowBase *current_window = BC_WindowBase::get_synchronous()->current_window;
52 // Try previously created PBuffers
53                 pbuffer = BC_WindowBase::get_synchronous()->get_pbuffer(w, 
54                         h, 
55                         &window_id,
56                         &gl_context);
58                 if(pbuffer)
59                 {
60                         return;
61                 }
67 // You're supposed to try different configurations of decreasing overhead 
68 // until one works.
69 // In reality, only a very specific configuration works at all.
70 #define TOTAL_CONFIGS 1
71                 static int framebuffer_attributes[] = 
72                 {
73                 GLX_RENDER_TYPE, GLX_RGBA_BIT,
74                         GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
75                 GLX_DOUBLEBUFFER, False,
76                 GLX_DEPTH_SIZE, 1,
77                         GLX_ACCUM_RED_SIZE, 1,
78                         GLX_ACCUM_GREEN_SIZE, 1,
79                         GLX_ACCUM_BLUE_SIZE, 1,
80                         GLX_ACCUM_ALPHA_SIZE, 1,
81                 GLX_RED_SIZE, 8,
82                 GLX_GREEN_SIZE, 8,
83                 GLX_BLUE_SIZE, 8,
84                         GLX_ALPHA_SIZE, 8,
85                         None
86                 };
88                 static int pbuffer_attributes[] = 
89                 {
90                         GLX_PBUFFER_WIDTH, 0,
91                         GLX_PBUFFER_HEIGHT, 0,
92                 GLX_LARGEST_PBUFFER, False,
93                 GLX_PRESERVED_CONTENTS, True,
94                 None
95                 };
97                 pbuffer_attributes[1] = w;
98                 pbuffer_attributes[3] = h;
99                 if(w % 4) pbuffer_attributes[1] += 4 - (w % 4);
100                 if(h % 4) pbuffer_attributes[3] += 4 - (h % 4);
102                 GLXFBConfig *config_result;
103                 XVisualInfo *visinfo;
104                 int config_result_count = 0;
105                 config_result = glXChooseFBConfig(current_window->get_display(), 
106                         current_window->get_screen(), 
107                         framebuffer_attributes, 
108                         &config_result_count);
109 // printf("BC_PBuffer::new_pbuffer 1 config_result=%p config_result_count=%d\n", 
110 // config_result, 
111 // config_result_count);
113                 if(!config_result || !config_result_count)
114                 {
115                         printf("BC_PBuffer::new_pbuffer: glXChooseFBConfig failed\n");
116                         return;
117                 }
120 static int current_config = 0;
122                 BC_Resources::error = 0;
123                 pbuffer = glXCreatePbuffer(current_window->get_display(), 
124                         config_result[current_config], 
125                         pbuffer_attributes);
126         visinfo = glXGetVisualFromFBConfig(current_window->get_display(), 
127                         config_result[current_config]);
128 // printf("BC_PBuffer::new_pbuffer 2 visual=%d current_config=%d error=%d pbuffer=%p\n",
129 // visinfo->visual,
130 // current_config,
131 // BC_Resources::error,
132 // pbuffer);
133 ///current_config++;
135 // Got it
136                 if(!BC_Resources::error && pbuffer && visinfo)
137                 {
138                         window_id = current_window->get_id();
139                         gl_context = glXCreateContext(current_window->get_display(),
140                                 visinfo,
141                                 current_window->gl_win_context,
142                                 1);
143                         BC_WindowBase::get_synchronous()->put_pbuffer(w, 
144                                 h, 
145                                 pbuffer, 
146                                 gl_context);
147 // printf("BC_PBuffer::new_pbuffer gl_context=%p window_id=%d\n",
148 // gl_context,
149 // current_window->get_id());
150                 }
152                 if(config_result) XFree(config_result);
153         if(visinfo) XFree(visinfo);
154         }
157         if(!pbuffer) printf("BC_PBuffer::new_pbuffer: failed\n");
158 #endif
162 void BC_PBuffer::enable_opengl()
164 #ifdef HAVE_GL
165         BC_WindowBase *current_window = BC_WindowBase::get_synchronous()->current_window;
166         int result = glXMakeCurrent(current_window->get_display(),
167                 pbuffer,
168                 gl_context);
169         BC_WindowBase::get_synchronous()->is_pbuffer = 1;
170 #endif