proxy cpus usage, unlock win before render_proxy, snap tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / videowindow.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 "bchash.h"
23 #include "levelwindow.h"
24 #include "mainmenu.h"
25 #include "mwindow.h"
26 #include "mwindowgui.h"
27 #include "mainsession.h"
28 #include "vframe.h"
29 #include "videowindow.h"
30 #include "videowindowgui.h"
31
32
33 VideoWindow::VideoWindow(MWindow *mwindow)
34  : Thread(1, 0, 0)
35 {
36         this->mwindow = mwindow;
37         vbuffer = 0;
38         gui = 0;
39         video_window_w = 320;
40         video_visible = 0;
41         video_cropping = 0;
42 }
43
44 VideoWindow::~VideoWindow()
45 {
46         if(gui && running()) {
47                 gui->set_done(0);
48         }
49         join();
50         delete gui;  gui = 0;
51 }
52
53 int VideoWindow::load_defaults(BC_Hash *defaults)
54 {
55         video_visible = defaults->get("VIDEOVISIBLE", 1);
56         video_window_w = defaults->get("PLAYVIDEOW", video_window_w);
57 }
58
59 int VideoWindow::update_defaults(BC_Hash *defaults)
60 {
61         defaults->update("VIDEOVISIBLE", video_visible);
62         defaults->update("PLAYVIDEOW", video_window_w);
63 }
64
65 void VideoWindow::create_objects()
66 {
67         set_synchronous(1);
68         if(mwindow->gui)
69         {
70                 init_window();
71         }
72 }
73
74 int VideoWindow::init_window()
75 {
76 //      int w = mwindow->session->output_w;
77 //      int h = mwindow->session->output_h;
78 //
79 //      fix_size(w, h, video_window_w, mwindow->get_aspect_ratio());
80 //      gui = new VideoWindowGUI(this, w, h);
81 //      gui->create_objects();
82 }
83
84 int VideoWindow::show_window()
85 {
86         if(gui)
87         {
88                 video_visible = 1;
89                 gui->show_window();
90                 mwindow->gui->mainmenu->set_show_video(1);
91         }
92 }
93
94 int VideoWindow::hide_window()
95 {
96         if(gui)
97         {
98                 video_visible = 0;
99                 gui->hide_window();
100                 mwindow->gui->mainmenu->set_show_video(0);
101         }
102 }
103
104 int VideoWindow::resize_window()
105 {
106         int proper_w = mwindow->session->output_w;
107         int proper_h = mwindow->session->output_h;
108
109 //      fix_size(proper_w, proper_h, video_window_w, mwindow->get_aspect_ratio());
110
111         gui->update_title();
112         if(gui &&
113                 (gui->get_w() != proper_w ||
114                         gui->get_h() != proper_h))
115         {
116                 gui->resize_window(proper_w, proper_h);
117                 gui->canvas->reposition_window(0, 0, proper_w, proper_h);
118                 if(video_cropping) gui->canvas->draw_crop_box();
119                 gui->flash();
120         }
121 }
122
123 int VideoWindow::fix_size(int &w, int &h, int width_given, float aspect_ratio)
124 {
125         w = width_given;
126         h = (int)((float)width_given / aspect_ratio);
127 }
128
129 int VideoWindow::original_size()
130 {
131         int w, h;
132         get_full_sizes(w, h);
133         video_window_w = w;
134         resize_window();
135 }
136
137 int VideoWindow::get_full_sizes(int &w, int &h)
138 {
139 //      if(mwindow->get_aspect_ratio() > (float)mwindow->session->output_w / mwindow->session->output_h)
140 //      {
141 //              w = (int)((float)mwindow->session->output_h * mwindow->get_aspect_ratio() + 0.5);
142 //              h = mwindow->session->output_h;
143 //      }
144 //      else
145 //      {
146 //              w = mwindow->session->output_w;
147 //              h = (int)((float)mwindow->session->output_w / mwindow->get_aspect_ratio() + 0.5);
148 //      }
149 }
150
151
152 void VideoWindow::run()
153 {
154         if(gui) gui->run_window();
155 }
156
157 int VideoWindow::init_video()
158 {
159         if(gui)
160         {
161                 gui->canvas->start_video();
162         }
163 }
164
165 int VideoWindow::stop_video()
166 {
167         if(gui)
168         {
169                 gui->canvas->stop_video();
170         }
171 }
172
173 int VideoWindow::update(BC_Bitmap *frame)
174 {
175         if(gui)
176         {
177                 gui->lock_window();
178 //              gui->canvas->draw_bitmap(frame, 1);
179                 gui->unlock_window();
180         }
181 }
182
183 int VideoWindow::get_w()
184 {
185         if(gui) return gui->get_w();
186         else return 0;
187 }
188
189 int VideoWindow::get_h()
190 {
191         if(gui) return gui->get_h();
192         else return 0;
193 }
194
195 BC_Bitmap* VideoWindow::get_bitmap()
196 {
197         return gui->canvas->new_bitmap(gui->get_w(), gui->get_h());
198 }
199
200 int VideoWindow::reset()
201 {
202 }
203
204 int VideoWindow::start_cropping()
205 {
206         video_cropping = 1;
207         gui->x1 = 0;
208         gui->y1 = 0;
209         gui->x2 = gui->get_w();
210         gui->y2 = gui->get_h();
211         gui->canvas->draw_crop_box();
212         gui->canvas->flash();
213 }
214
215 int VideoWindow::get_aspect_ratio(float &aspect_w, float &aspect_h)
216 {
217         int new_w, new_h;
218         float zoom_factor = (float)video_window_w / mwindow->session->output_w;
219
220 // For new aspect ratio
221         new_w = (int)((float)(gui->x2 - gui->x1) / zoom_factor);
222         new_h = (int)((float)(gui->y2 - gui->y1) / zoom_factor);
223
224         mwindow->create_aspect_ratio(aspect_w, aspect_h, new_w, new_h);
225 }
226
227 int VideoWindow::stop_cropping()
228 {
229 //      float x_zoom, y_zoom;
230 //      int new_w, new_h;
231 //      float zoom_factor;
232 //
233 //      if(mwindow->get_aspect_ratio() < (float)mwindow->session->output_w / mwindow->session->output_h)
234 //      {
235 //              x_zoom = 1;
236 //              y_zoom = (float)mwindow->session->output_w / mwindow->get_aspect_ratio() / mwindow->session->output_h;
237 //              zoom_factor = (float)video_window_w / mwindow->session->output_w;
238 //      }
239 //      else
240 //      {
241 //              x_zoom = (float)mwindow->session->output_h * mwindow->get_aspect_ratio() / mwindow->session->output_w;
242 //              y_zoom = 1;
243 //              zoom_factor = (float)video_window_w / (mwindow->session->output_h * mwindow->get_aspect_ratio());
244 //      }
245 //
246 //
247 //      gui->canvas->draw_crop_box();
248 //      gui->canvas->flash();
249 //      video_window_w = gui->x2 - gui->x1;
250 //
251 //      gui->x1 = (int)(gui->x1 / zoom_factor / x_zoom);
252 //      gui->y1 = (int)(gui->y1 / zoom_factor / y_zoom);
253 //      gui->x2 = (int)(gui->x2 / zoom_factor / x_zoom);
254 //      gui->y2 = (int)(gui->y2 / zoom_factor / y_zoom);
255 //
256 //      video_cropping = 0;
257 }