GLx4 w/h mult of 4 is not req, cwdw crop input range checks, fix cropp.png dir, chang...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcpbuffer.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 #include "bcpbuffer.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bcsynchronous.h"
26 #include "bcwindowbase.h"
27
28
29
30
31 BC_PBuffer::BC_PBuffer(int w, int h)
32 {
33         reset();
34         this->w = w;
35         this->h = h;
36         create_pbuffer(w, h);
37 }
38
39 BC_PBuffer::~BC_PBuffer()
40 {
41 #ifdef HAVE_GL
42         BC_WindowBase::get_synchronous()->release_pbuffer(window_id, pbuffer);
43 #endif
44 }
45
46
47 void BC_PBuffer::reset()
48 {
49 #ifdef HAVE_GL
50         pbuffer = 0;
51         window_id = -1;
52 #endif
53 }
54
55 #ifdef HAVE_GL
56
57 GLXPbuffer BC_PBuffer::get_pbuffer()
58 {
59         return pbuffer;
60 }
61 #endif
62
63
64 void BC_PBuffer::create_pbuffer(int w, int h)
65 {
66 #ifdef HAVE_GL
67 #ifdef GLx4
68         int ww = (w + 3) & ~3, hh = (h + 3) & ~3;
69 #else
70         int ww = w, hh = h;
71 #endif
72         BC_WindowBase *current_window = BC_WindowBase::get_synchronous()->current_window;
73         window_id = current_window->get_id();
74
75         pbuffer = BC_WindowBase::get_synchronous()->get_pbuffer(ww, hh, &glx_context);
76         if( pbuffer ) return;
77
78         int pb_attrs[] = {
79                 GLX_PBUFFER_WIDTH, ww,
80                 GLX_PBUFFER_HEIGHT, hh,
81                 GLX_LARGEST_PBUFFER, False,
82                 GLX_PRESERVED_CONTENTS, True,
83                 None
84         };
85
86         Display *dpy = current_window->get_display();
87         GLXFBConfig *fb_cfgs = current_window->glx_pbuffer_fb_configs();
88         int nfb_cfgs = current_window->n_fbcfgs_pbuffer;
89         XVisualInfo *visinfo = 0;
90
91         for( int i=0; !pbuffer && i<nfb_cfgs; ++i ) {
92                 if( visinfo ) { XFree(visinfo);  visinfo = 0; }
93                 BC_Resources::error = 0;
94                 visinfo = glXGetVisualFromFBConfig(dpy, fb_cfgs[i]);
95                 if( !visinfo || BC_Resources::error ) continue;
96                 pbuffer = glXCreatePbuffer(dpy, fb_cfgs[i], pb_attrs);
97                 if( BC_Resources::error ) pbuffer = 0;
98         }
99
100 // printf("BC_PBuffer::create_pbuffer %d current_config=%d visinfo=%p error=%d pbuffer=%p\n",
101 // __LINE__, current_config, visinfo, BC_Resources::error, pbuffer);
102
103         if( pbuffer ) {
104                 glx_context = glXCreateContext(dpy, visinfo, current_window->glx_win_context, 1);
105                 BC_WindowBase::get_synchronous()->put_pbuffer(ww, hh, pbuffer, glx_context);
106         }
107         else
108                 printf("BC_PBuffer::create_pbuffer: failed\n");
109
110         if( visinfo ) XFree(visinfo);
111 #endif
112 }
113
114
115 int bcgl_enable_ret; // debug
116
117 void BC_PBuffer::enable_opengl()
118 {
119 #ifdef HAVE_GL
120         BC_WindowBase *current_window = BC_WindowBase::get_synchronous()->current_window;
121         bcgl_enable_ret = current_window->glx_make_current(pbuffer);
122         BC_WindowBase::get_synchronous()->is_pbuffer = 1;
123 #endif
124 }
125