add clip folders, icon margins, fix safe regions, update neophyte, fix folder edl...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vdevicex11.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2017 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 "assets.h"
23 #include "autos.h"
24 #include "bccapture.h"
25 #include "bccmodels.h"
26 #include "bcsignals.h"
27 #include "canvas.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "file.h"
31 #include "maskauto.h"
32 #include "maskautos.h"
33 #include "mwindow.h"
34 #include "mwindowgui.h"
35 #include "playback3d.h"
36 #include "playbackconfig.h"
37 #include "preferences.h"
38 #include "recordconfig.h"
39 #include "strategies.inc"
40 #include "vdevicex11.h"
41 #include "vframe.h"
42 #include "videodevice.h"
43 #include "videowindow.h"
44 #include "videowindowgui.h"
45
46 #include <string.h>
47 #include <unistd.h>
48
49 VDeviceX11::VDeviceX11(VideoDevice *device, Canvas *output)
50  : VDeviceBase(device)
51 {
52         reset_parameters();
53         this->output = output;
54 }
55
56 VDeviceX11::~VDeviceX11()
57 {
58         close_all();
59 }
60
61 int VDeviceX11::reset_parameters()
62 {
63         output_frame = 0;
64         window_id = 0;
65         bitmap = 0;
66         bitmap_type = 0;
67         bitmap_w = 0;
68         bitmap_h = 0;
69         output_x1 = 0;
70         output_y1 = 0;
71         output_x2 = 0;
72         output_y2 = 0;
73         canvas_x1 = 0;
74         canvas_y1 = 0;
75         canvas_x2 = 0;
76         canvas_y2 = 0;
77         capture_bitmap = 0;
78         color_model_selected = 0;
79         is_cleared = 0;
80
81         for( int i = 0; i < SCREENCAP_BORDERS; i++ ) {
82                 screencap_border[i] = 0;
83         }
84         return 0;
85 }
86
87 int VDeviceX11::open_input()
88 {
89 //printf("VDeviceX11::open_input 1\n");
90         capture_bitmap = new BC_Capture(device->in_config->w, 
91                 device->in_config->h,
92                 device->in_config->screencapture_display);
93 //printf("VDeviceX11::open_input 2\n");
94
95 // create overlay
96         device->mwindow->gui->lock_window("VDeviceX11::close_all");
97
98         screencap_border[0] = new BC_Popup(device->mwindow->gui,
99                         device->input_x - SCREENCAP_PIXELS, device->input_y - SCREENCAP_PIXELS,
100                         device->in_config->w + SCREENCAP_PIXELS * 2, SCREENCAP_PIXELS,
101                         SCREENCAP_COLOR, 1);
102         screencap_border[1] = new BC_Popup(device->mwindow->gui,
103                         device->input_x - SCREENCAP_PIXELS, device->input_y,
104                         SCREENCAP_PIXELS, device->in_config->h,
105                         SCREENCAP_COLOR, 1);
106         screencap_border[2] = new BC_Popup(device->mwindow->gui,
107                         device->input_x - SCREENCAP_PIXELS, device->input_y + device->in_config->h,
108                         device->in_config->w + SCREENCAP_PIXELS * 2, SCREENCAP_PIXELS,
109                         SCREENCAP_COLOR, 1);
110         screencap_border[3] = new BC_Popup(device->mwindow->gui,
111                         device->input_x + device->in_config->w, device->input_y,
112                         SCREENCAP_PIXELS, device->in_config->h,
113                         SCREENCAP_COLOR, 1);
114 usleep(500000); // avoids a bug in gnome-shell 2017/10/19
115
116         for( int i=0; i<SCREENCAP_BORDERS; ++i )
117                 screencap_border[i]->show_window(0);
118
119         device->mwindow->gui->flush();
120         device->mwindow->gui->unlock_window();
121
122         return 0;
123 }
124
125 int VDeviceX11::open_output()
126 {
127         if( output ) {
128                 output->lock_canvas("VDeviceX11::open_output");
129                 output->get_canvas()->lock_window("VDeviceX11::open_output");
130                 if( !device->single_frame )
131                         output->start_video();
132                 else
133                         output->start_single();
134                 output->get_canvas()->unlock_window();
135
136 // Enable opengl in the first routine that needs it, to reduce the complexity.
137                 output->unlock_canvas();
138         }
139         return 0;
140 }
141
142
143 int VDeviceX11::output_visible()
144 {
145         if( !output ) return 0;
146
147         output->lock_canvas("VDeviceX11::output_visible");
148         if( output->get_canvas()->get_hidden() ) {
149                 output->unlock_canvas();
150                 return 0; 
151         }
152         else {
153                 output->unlock_canvas();
154                 return 1;
155         }
156 }
157
158
159 int VDeviceX11::close_all()
160 {
161         if( output ) {
162                 output->lock_canvas("VDeviceX11::close_all 1");
163                 output->get_canvas()->lock_window("VDeviceX11::close_all 1");
164 // Update the status bug
165                 if( !device->single_frame ) {
166                         output->stop_video();
167                 }
168                 else {
169                         output->stop_single();
170                 }
171         }
172
173         if( output && output_frame ) {
174                 output->update_refresh(device, output_frame);
175                 output->draw_refresh(1);
176         }
177
178         delete bitmap;          bitmap = 0;
179         delete output_frame;    output_frame = 0;
180         delete capture_bitmap;  capture_bitmap = 0;
181
182         if( output ) {
183                 output->get_canvas()->unlock_window();
184                 output->unlock_canvas();
185         }
186
187         if( device->mwindow ) {
188                 device->mwindow->gui->lock_window("VDeviceX11::close_all");
189                 for( int i=0; i<SCREENCAP_BORDERS; ++i ) {
190                         delete screencap_border[i];
191                         screencap_border[i] = 0;
192                 }
193                 device->mwindow->gui->unlock_window();
194         }
195         
196         reset_parameters();
197
198         return 0;
199 }
200
201 int VDeviceX11::read_buffer(VFrame *frame)
202 {
203 //printf("VDeviceX11::read_buffer %d colormodel=%d\n", __LINE__, frame->get_color_model());
204         device->mwindow->gui->lock_window("VDeviceX11::close_all");
205
206         screencap_border[0]->reposition_window(device->input_x - SCREENCAP_PIXELS,
207                         device->input_y - SCREENCAP_PIXELS);
208         screencap_border[1]->reposition_window(device->input_x - SCREENCAP_PIXELS,
209                         device->input_y);
210         screencap_border[2]->reposition_window(device->input_x - SCREENCAP_PIXELS,
211                         device->input_y + device->in_config->h);
212         screencap_border[3]->reposition_window(device->input_x + device->in_config->w,
213                         device->input_y);
214         device->mwindow->gui->flush();
215         device->mwindow->gui->unlock_window();
216
217
218         capture_bitmap->capture_frame(frame, 
219                 device->input_x, device->input_y, device->do_cursor);
220         return 0;
221 }
222
223
224 int VDeviceX11::get_best_colormodel(Asset *asset)
225 {
226         return File::get_best_colormodel(asset, SCREENCAPTURE);
227 //      return BC_RGB888;
228 }
229
230
231 int VDeviceX11::get_display_colormodel(int file_colormodel)
232 {
233         int result = -1;
234
235         if( device->out_config->driver == PLAYBACK_X11_GL ) {
236                 if( file_colormodel == BC_RGB888 ||
237                     file_colormodel == BC_RGBA8888 ||
238                     file_colormodel == BC_YUV888 ||
239                     file_colormodel == BC_YUVA8888 ||
240                     file_colormodel == BC_RGB_FLOAT ||
241                     file_colormodel == BC_RGBA_FLOAT ) {
242                         return file_colormodel;
243                 }
244                 
245                 return BC_RGB888;
246         }
247
248         if( !device->single_frame ) {
249                 switch( file_colormodel ) {
250                 case BC_YUV420P:
251                 case BC_YUV422P:
252                 case BC_YUV422:
253                         result = file_colormodel;
254                         break;
255                 }
256         }
257
258         if( result < 0 ) {
259                 switch( file_colormodel ) {
260                 case BC_RGB888:
261                 case BC_RGBA8888:
262                 case BC_YUV888:
263                 case BC_YUVA8888:
264                 case BC_RGB_FLOAT:
265                 case BC_RGBA_FLOAT:
266                         result = file_colormodel;
267                         break;
268
269                 default:
270                         output->lock_canvas("VDeviceX11::get_display_colormodel");
271                         result = output->get_canvas()->get_color_model();
272                         output->unlock_canvas();
273                         break;
274                 }
275         }
276
277         return result;
278 }
279
280
281 void VDeviceX11::new_output_buffer(VFrame **result, int file_colormodel, EDL *edl)
282 {
283 // printf("VDeviceX11::new_output_buffer %d hardware_scaling=%d\n",
284 // __LINE__, bitmap ? bitmap->hardware_scaling() : 0);
285         output->lock_canvas("VDeviceX11::new_output_buffer");
286         output->get_canvas()->lock_window("VDeviceX11::new_output_buffer 1");
287
288 // Get the best colormodel the display can handle.
289         int display_colormodel = get_display_colormodel(file_colormodel);
290
291 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
292 // __LINE__, file_colormodel, display_colormodel);
293 // Only create OpenGL Pbuffer and texture.
294         if( device->out_config->driver == PLAYBACK_X11_GL ) {
295 // Create bitmap for initial load into texture.
296 // Not necessary to do through Playback3D.....yet
297                 if( !output_frame ) {
298                         output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
299                 }
300
301                 window_id = output->get_canvas()->get_id();
302                 output_frame->set_opengl_state(VFrame::RAM);
303         }
304         else {
305                 output->get_transfers(edl, 
306                         output_x1, output_y1, output_x2, output_y2, 
307                         canvas_x1, canvas_y1, canvas_x2, canvas_y2,
308 // Canvas may be a different size than the temporary bitmap for pure software
309                         -1, -1);
310                 canvas_w = canvas_x2 - canvas_x1;
311                 canvas_h = canvas_y2 - canvas_y1;
312 // can the direct frame be used?
313                 int direct_supported = 
314                         device->out_config->use_direct_x11 &&
315                         !output->xscroll && !output->yscroll &&
316                         output_x1 == 0 && output_x2 == device->out_w &&
317                         output_y1 == 0 && output_y2 == device->out_h;
318
319 // file wants direct frame but we need a temp
320                 if( !direct_supported && file_colormodel == BC_BGR8888 )
321                         file_colormodel = BC_RGB888;
322
323 // Conform existing bitmap to new colormodel and output size
324                 if( bitmap ) {
325 // printf("VDeviceX11::new_output_buffer %d bitmap=%dx%d canvas=%dx%d canvas=%dx%d\n",
326 // __LINE__, bitmap->get_w(), bitmap->get_h(), canvas_w, canvas_h,);
327                         int size_change = (
328                                 bitmap->get_w() != canvas_w ||
329                                 bitmap->get_h() != canvas_h );
330
331 // Restart if output size changed or output colormodel changed.
332 // May have to recreate if transferring between windowed and fullscreen.
333                         if( !color_model_selected ||
334                             file_colormodel != output_frame->get_color_model() ||
335                             (!bitmap->hardware_scaling() && size_change) ) {
336 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d prev "
337 // "file_colormodel=%d bitmap=%p output_frame=%p\n", __LINE__,
338 // file_colormodel, output_frame->get_color_model(), bitmap, output_frame);
339                                 delete bitmap;        bitmap = 0;
340                                 delete output_frame;  output_frame = 0;
341 // Clear borders if size changed
342                                 if( size_change ) {
343 //printf("VDeviceX11::new_output_buffer %d w=%d h=%d "
344 // "canvas_x1=%d canvas_y1=%d canvas_x2=%d canvas_y2=%d\n",
345 // __LINE__, // (int)output->w, (int)output->h,
346 // (int)canvas_x1, (int)canvas_y1, (int)canvas_x2, (int)canvas_y2);
347                                         output->get_canvas()->set_color(BLACK);
348
349                                         if( canvas_y1 > 0 ) {
350                                                 output->get_canvas()->draw_box(0, 0, output->w, canvas_y1);
351                                                 output->get_canvas()->flash(0, 0, output->w, canvas_y1);
352                                         }
353
354                                         if( canvas_y2 < output->h ) {
355                                                 output->get_canvas()->draw_box(0, canvas_y2, output->w, output->h - canvas_y2);
356                                                 output->get_canvas()->flash(0, canvas_y2, output->w, output->h - canvas_y2);
357                                         }
358
359                                         if( canvas_x1 > 0 ) {
360                                                 output->get_canvas()->draw_box(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
361                                                 output->get_canvas()->flash(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
362                                         }
363
364                                         if( canvas_x2 < output->w ) {
365                                                 output->get_canvas()->draw_box(canvas_x2, canvas_y1,
366                                                         output->w - canvas_x2, canvas_y2 - canvas_y1);
367                                                 output->get_canvas()->flash(canvas_x2, canvas_y1,
368                                                         output->w - canvas_x2, canvas_y2 - canvas_y1);
369                                         }
370                                 }
371                         }
372                 }
373
374 // Create new bitmap
375                 if( !bitmap ) {
376                         int use_direct = 0;
377                         bitmap_type = BITMAP_TEMP;
378 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n", 
379 // __LINE__, file_colormodel, display_colormodel);
380
381 // Try hardware accelerated
382                         switch( display_colormodel ) {
383 // blit from the codec directly to the window, using the standard X11 color model.
384 // Must scale in the codec.  No cropping
385                         case BC_BGR8888:
386                                 if( direct_supported ) {
387                                         bitmap_type = BITMAP_PRIMARY;
388                                         use_direct = 1;
389                                 }
390                                 break;
391
392                         case BC_YUV420P:
393                                 if( device->out_config->driver == PLAYBACK_X11_XV &&
394                                     output->get_canvas()->accel_available(display_colormodel, 0) &&
395                                     !output->use_scrollbars )
396                                         bitmap_type = BITMAP_PRIMARY;
397                                 break;
398
399                         case BC_YUV422P:
400                                 if( device->out_config->driver == PLAYBACK_X11_XV &&
401                                     output->get_canvas()->accel_available(display_colormodel, 0) &&
402                                     !output->use_scrollbars )
403                                         bitmap_type = BITMAP_PRIMARY;
404                                 else if( device->out_config->driver == PLAYBACK_X11_XV &&
405                                     output->get_canvas()->accel_available(BC_YUV422, 0) ) {
406                                         bitmap = new BC_Bitmap(output->get_canvas(), 
407                                                 device->out_w, device->out_h, BC_YUV422, 1);
408                                 }
409                                 break;
410
411                         case BC_YUV422:
412                                 if( device->out_config->driver == PLAYBACK_X11_XV &&
413                                     output->get_canvas()->accel_available(display_colormodel, 0) &&
414                                     !output->use_scrollbars ) {
415                                         bitmap_type = BITMAP_PRIMARY;
416                                 }
417                                 else if( device->out_config->driver == PLAYBACK_X11_XV &&
418                                     output->get_canvas()->accel_available(BC_YUV422P, 0) ) {
419                                         bitmap = new BC_Bitmap(output->get_canvas(), 
420                                                 device->out_w, device->out_h, BC_YUV422P, 1);
421                                 }
422                                 break;
423                         }
424                         if( bitmap_type == BITMAP_PRIMARY ) {
425                                 int bitmap_w = use_direct ? canvas_w : device->out_w;
426                                 int bitmap_h = use_direct ? canvas_h : device->out_h;
427                                 bitmap = new BC_Bitmap(output->get_canvas(),
428                                         bitmap_w, bitmap_h,  display_colormodel, -1);
429                                 output_frame = new VFrame(bitmap,
430                                         bitmap_w, bitmap_h, display_colormodel, -1);
431                         }
432 // Make an intermediate frame
433                         if( !bitmap ) {
434                                 display_colormodel = output->get_canvas()->get_color_model();
435 // printf("VDeviceX11::new_output_buffer %d creating temp display_colormodel=%d "
436 // "file_colormodel=%d %dx%d %dx%d %dx%d\n", __LINE__,
437 // display_colormodel, file_colormodel, device->out_w, device->out_h,
438 // output->get_canvas()->get_w(), output->get_canvas()->get_h(), canvas_w, canvas_h);
439                                 bitmap = new BC_Bitmap(output->get_canvas(), 
440                                         canvas_w, canvas_h, display_colormodel, 1);
441                                 bitmap_type = BITMAP_TEMP;
442                         }
443
444                         if( bitmap_type == BITMAP_TEMP ) {
445 // Intermediate frame
446 //printf("VDeviceX11::new_output_buffer %d creating output_frame\n", __LINE__);
447                                 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
448                         }
449                         color_model_selected = 1;
450                 }
451                 else if( bitmap_type == BITMAP_PRIMARY ) {
452                         output_frame->set_memory(bitmap);
453                 }
454         }
455
456         *result = output_frame;
457 //printf("VDeviceX11::new_output_buffer 10 %d\n", output->get_canvas()->get_window_lock());
458
459         output->get_canvas()->unlock_window();
460         output->unlock_canvas();
461 }
462
463
464 int VDeviceX11::start_playback()
465 {
466 // Record window is initialized when its monitor starts.
467         if( !device->single_frame )
468                 output->start_video();
469         return 0;
470 }
471
472 int VDeviceX11::stop_playback()
473 {
474         if( !device->single_frame )
475                 output->stop_video();
476 // Record window goes back to monitoring
477 // get the last frame played and store it in the video_out
478         return 0;
479 }
480
481 int VDeviceX11::write_buffer(VFrame *output_channels, EDL *edl)
482 {
483         output->lock_canvas("VDeviceX11::write_buffer");
484         output->get_canvas()->lock_window("VDeviceX11::write_buffer 1");
485 //      if( device->out_config->driver == PLAYBACK_X11_GL &&
486 //          output_frame->get_color_model() != BC_RGB888 ) {
487 // this is handled by overlay call in virtualvnode, using flatten alpha
488 // invoked when is_nested = -1 is passed to vdevicex11->overlay(...)
489 //      }
490
491 // printf("VDeviceX11::write_buffer %d %d bitmap_type=%d\n", 
492 // __LINE__, 
493 // output->get_canvas()->get_video_on(),
494 // bitmap_type);
495
496 //      int use_bitmap_extents = 0;
497 //      canvas_w = -1;  canvas_h = -1;
498 // // Canvas may be a different size than the temporary bitmap for pure software
499 //      if( bitmap_type == BITMAP_TEMP && !bitmap->hardware_scaling() ) {
500 //              canvas_w = bitmap->get_w();
501 //              canvas_h = bitmap->get_h();
502 //      }
503 // 
504 //      output->get_transfers(edl, 
505 //              output_x1, output_y1, output_x2, output_y2, 
506 //              canvas_x1, canvas_y1, canvas_x2, canvas_y2,
507 //              canvas_w, canvas_h);
508
509 // Convert colormodel
510         if( bitmap_type == BITMAP_TEMP ) {
511 // printf("VDeviceX11::write_buffer 1 %d %d, %d %d %d %d -> %d %d %d %d\n",
512 //   output->w, output->h, in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
513 // fflush(stdout);
514
515 // printf("VDeviceX11::write_buffer %d output_channels=%p\n", __LINE__, output_channels);
516 // printf("VDeviceX11::write_buffer %d input color_model=%d output color_model=%d\n", 
517 // __LINE__, output_channels->get_color_model(), bitmap->get_color_model());
518                 if( bitmap->hardware_scaling() ) {
519                         BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
520                                 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
521                                 0, 0, output_channels->get_w(), output_channels->get_h(),
522                                 0, 0, bitmap->get_w(), bitmap->get_h(),
523                                 output_channels->get_color_model(), bitmap->get_color_model(),
524                                 0, output_channels->get_w(), bitmap->get_w());
525                 }
526                 else {
527                         BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
528                                 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
529                                 (int)output_x1, (int)output_y1, (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
530                                 0, 0, (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
531                                 output_channels->get_color_model(), bitmap->get_color_model(),
532                                 0, output_channels->get_w(), bitmap->get_w());
533                 }
534         }
535
536 //printf("VDeviceX11::write_buffer 4 %p\n", bitmap);
537 //for( i = 0; i < 1000; i += 4 ) bitmap->get_data()[i] = 128;
538 //printf("VDeviceX11::write_buffer 2 %d %d %d\n", bitmap_type, 
539 //      bitmap->get_color_model(), 
540 //      output->get_color_model());fflush(stdout);
541
542 // printf("VDeviceX11::write_buffer %d %dx%d %f %f %f %f -> %f %f %f %f\n",
543 // __LINE__, // output->w, output->h,
544 // output_x1, output_y1, output_x2, output_y2,
545 // canvas_x1, canvas_y1, canvas_x2, canvas_y2);
546
547 // Cause X server to display it
548         if( device->out_config->driver == PLAYBACK_X11_GL ) {
549 // Output is drawn in close_all if no video.
550                 if( output->get_canvas()->get_video_on() ) {
551                         canvas_w = -1;  canvas_h = -1;
552 // Canvas may be a different size than the temporary bitmap for pure software
553                         if( bitmap_type == BITMAP_TEMP && 
554                                 !bitmap->hardware_scaling() ) {
555                                 canvas_w = bitmap->get_w();
556                                 canvas_h = bitmap->get_h();
557                         }
558
559                         output->get_transfers(edl, 
560                                 output_x1, output_y1, output_x2, output_y2, 
561                                 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
562                                 canvas_w, canvas_h);
563
564 //printf("VDeviceX11::write_buffer %d\n", __LINE__);
565 // Draw output frame directly.  Not used for compositing.
566                         output->get_canvas()->unlock_window();
567                         output->unlock_canvas();
568                         output->mwindow->playback_3d->write_buffer(output, output_frame,
569                                 output_x1, output_y1, output_x2, output_y2,
570                                 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
571                                 is_cleared);
572                         is_cleared = 0;
573                         output->lock_canvas("VDeviceX11::write_buffer 2");
574                         output->get_canvas()->lock_window("VDeviceX11::write_buffer 2");
575                 }
576         }
577         else
578         if( bitmap->hardware_scaling() ) {
579                 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
580                         (int)canvas_x1, (int)canvas_y1,
581                         (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
582                         (int)output_x1, (int)output_y1,
583                         (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
584                         0);
585         }
586         else {
587 //printf("VDeviceX11::write_buffer %d x=%d y=%d w=%d h=%d\n", 
588 // __LINE__, (int)canvas_x1, (int)canvas_y1,
589 // output->get_canvas()->get_w(), output->get_canvas()->get_h());
590
591                 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
592                         (int)canvas_x1, (int)canvas_y1,
593                         (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
594                         0, 0, 
595                         (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
596                         0);
597 //printf("VDeviceX11::write_buffer %d bitmap=%p\n", __LINE__, bitmap);
598         }
599
600         output->get_canvas()->unlock_window();
601         output->unlock_canvas();
602         return 0;
603 }
604
605
606 void VDeviceX11::clear_output()
607 {
608         is_cleared = 1;
609
610         output->mwindow->playback_3d->clear_output(output,
611                 output->get_canvas()->get_video_on() ? 0 : output_frame);
612
613 }
614
615
616 void VDeviceX11::clear_input(VFrame *frame)
617 {
618         this->output->mwindow->playback_3d->clear_input(this->output, frame);
619 }
620
621 void VDeviceX11::convert_cmodel(VFrame *output, int dst_cmodel)
622 {
623         this->output->mwindow->playback_3d->convert_cmodel(this->output,
624                 output, 
625                 dst_cmodel);
626 }
627
628 void VDeviceX11::do_camera(VFrame *output, VFrame *input,
629         float in_x1, float in_y1, float in_x2, float in_y2, 
630         float out_x1, float out_y1, float out_x2, float out_y2)
631 {
632         this->output->mwindow->playback_3d->do_camera(this->output, 
633                 output, input,
634                 in_x1, in_y1, in_x2, in_y2, 
635                 out_x1, out_y1, out_x2, out_y2);
636 }
637
638
639 void VDeviceX11::do_fade(VFrame *output_temp, float fade)
640 {
641         this->output->mwindow->playback_3d->do_fade(this->output, output_temp, fade);
642 }
643
644 bool VDeviceX11::can_mask(int64_t start_position_project, MaskAutos *keyframe_set)
645 {
646         Auto *current = 0;
647         MaskAuto *keyframe = (MaskAuto*)keyframe_set->
648                 get_prev_auto(start_position_project, PLAY_FORWARD, current);
649         return keyframe->disable_opengl_masking ? 0 : 1;
650 }
651
652 void VDeviceX11::do_mask(VFrame *output_temp, int64_t start_position_project,
653                 MaskAutos *keyframe_set, MaskAuto *keyframe, MaskAuto *default_auto)
654 {
655         this->output->mwindow->playback_3d->do_mask(output, output_temp,
656                 start_position_project, keyframe_set, keyframe, default_auto);
657 }
658
659 void VDeviceX11::overlay(VFrame *output_frame, VFrame *input, 
660 // This is the transfer from track to output frame
661                 float in_x1, float in_y1, float in_x2, float in_y2, 
662                 float out_x1, float out_y1, float out_x2, float out_y2, 
663                 float alpha,        // 0 - 1
664                 int mode, EDL *edl, int is_nested)
665 {
666         int interpolation_type = edl->session->interpolation_type;
667
668 // printf("VDeviceX11::overlay 1:\n"
669 // "in_x1=%f in_y1=%f in_x2=%f in_y2=%f\n"
670 // "out_x1=%f out_y1=%f out_x2=%f out_y2=%f\n",
671 // in_x1, in_y1, in_x2, in_y2, out_x1, out_y1, out_x2, out_y2);
672 // Convert node coords to canvas coords in here
673
674 // If single frame playback or nested EDL, use full sized PBuffer as output.
675         if( device->single_frame || is_nested > 0 ) {
676                 output->mwindow->playback_3d->overlay(output, input,
677                         in_x1, in_y1, in_x2, in_y2, 
678                         out_x1, out_y1, out_x2, out_y2, alpha, // 0 - 1
679                         mode, interpolation_type, output_frame, is_nested);
680 // printf("VDeviceX11::overlay 1 %p %d %d %d\n", 
681 // output_frame, output_frame->get_w(), output_frame->get_h(),
682 // output_frame->get_opengl_state());
683         }
684         else {
685                 output->lock_canvas("VDeviceX11::overlay");
686                 output->get_canvas()->lock_window("VDeviceX11::overlay");
687
688 // This is the transfer from output frame to canvas
689                 output->get_transfers(edl, 
690                         output_x1, output_y1, output_x2, output_y2, 
691                         canvas_x1, canvas_y1, canvas_x2, canvas_y2,
692                         -1, -1);
693
694                 output->get_canvas()->unlock_window();
695                 output->unlock_canvas();
696
697
698 // Get transfer from track to canvas
699                 float track_xscale = (out_x2 - out_x1) / (in_x2 - in_x1);
700                 float track_yscale = (out_y2 - out_y1) / (in_y2 - in_y1);
701                 float canvas_xscale = (float)(canvas_x2 - canvas_x1) / (output_x2 - output_x1);
702                 float canvas_yscale = (float)(canvas_y2 - canvas_y1) / (output_y2 - output_y1);
703
704
705 // Get coordinates of canvas relative to track frame
706                 float track_x1 = (float)(output_x1 - out_x1) / track_xscale + in_x1;
707                 float track_y1 = (float)(output_y1 - out_y1) / track_yscale + in_y1;
708                 float track_x2 = (float)(output_x2 - out_x2) / track_xscale + in_x2;
709                 float track_y2 = (float)(output_y2 - out_y2) / track_yscale + in_y2;
710
711 // Clamp canvas coords to track boundary
712                 if( track_x1 < 0 ) {
713                         float difference = -track_x1;
714                         track_x1 += difference;
715                         canvas_x1 += difference * track_xscale * canvas_xscale;
716                 }
717                 if( track_y1 < 0 ) {
718                         float difference = -track_y1;
719                         track_y1 += difference;
720                         canvas_y1 += difference * track_yscale * canvas_yscale;
721                 }
722
723                 if( track_x2 > input->get_w() ) {
724                         float difference = track_x2 - input->get_w();
725                         track_x2 -= difference;
726                         canvas_x2 -= difference * track_xscale * canvas_xscale;
727                 }
728                 if( track_y2 > input->get_h() ) {
729                         float difference = track_y2 - input->get_h();
730                         track_y2 -= difference;
731                         canvas_y2 -= difference * track_yscale * canvas_yscale;
732                 }
733
734 // Overlay directly from track buffer to canvas, skipping output buffer
735                 if( track_x2 > track_x1 && track_y2 > track_y1 &&
736                         canvas_x2 > canvas_x1 && canvas_y2 > canvas_y1 ) {
737                         output->mwindow->playback_3d->overlay(output, input,
738                                 track_x1, track_y1, track_x2, track_y2, 
739                                 canvas_x1, canvas_y1, canvas_x2, canvas_y2, alpha, // 0 - 1
740                                 mode, interpolation_type, 0, is_nested);
741                 }
742         }
743 }
744
745 void VDeviceX11::run_plugin(PluginClient *client)
746 {
747         output->mwindow->playback_3d->run_plugin(output, client);
748 }
749
750 void VDeviceX11::copy_frame(VFrame *dst, VFrame *src)
751 {
752         output->mwindow->playback_3d->copy_from(output, dst, src, 1);
753 }
754