4 * Copyright (C) 2008-2017 Adam Williams <broadcast at earthling dot net>
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.
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.
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
24 #include "bccapture.h"
25 #include "bccmodels.h"
26 #include "bcsignals.h"
29 #include "edlsession.h"
32 #include "maskautos.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"
42 #include "videodevice.h"
43 #include "videowindow.h"
44 #include "videowindowgui.h"
49 VDeviceX11::VDeviceX11(VideoDevice *device, Canvas *output)
53 this->output = output;
56 VDeviceX11::~VDeviceX11()
61 int VDeviceX11::reset_parameters()
78 color_model_selected = 0;
81 for( int i = 0; i < SCREENCAP_BORDERS; i++ ) {
82 screencap_border[i] = 0;
87 int VDeviceX11::open_input()
89 //printf("VDeviceX11::open_input 1\n");
90 capture_bitmap = new BC_Capture(device->in_config->w,
92 device->in_config->screencapture_display);
93 //printf("VDeviceX11::open_input 2\n");
96 device->mwindow->gui->lock_window("VDeviceX11::close_all");
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,
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,
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,
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,
114 usleep(500000); // avoids a bug in gnome-shell 2017/10/19
116 for( int i=0; i<SCREENCAP_BORDERS; ++i )
117 screencap_border[i]->show_window(0);
119 device->mwindow->gui->flush();
120 device->mwindow->gui->unlock_window();
125 int VDeviceX11::open_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();
133 output->start_single();
134 output->get_canvas()->unlock_window();
136 // Enable opengl in the first routine that needs it, to reduce the complexity.
137 output->unlock_canvas();
143 int VDeviceX11::output_visible()
145 if( !output ) return 0;
147 output->lock_canvas("VDeviceX11::output_visible");
148 if( output->get_canvas()->get_hidden() ) {
149 output->unlock_canvas();
153 output->unlock_canvas();
159 int VDeviceX11::close_all()
162 output->lock_canvas("VDeviceX11::close_all 1");
163 output->get_canvas()->lock_window("VDeviceX11::close_all 1");
164 int video_on = output->get_canvas()->get_video_on();
165 // Update the status bug
166 if( !device->single_frame ) {
167 output->stop_video();
170 output->stop_single();
173 output->update_refresh(device, output_frame);
174 // if the last frame is good, don't draw over it
175 if( !video_on || output->need_overlays() )
176 output->draw_refresh(1);
180 delete bitmap; bitmap = 0;
181 delete output_frame; output_frame = 0;
182 delete capture_bitmap; capture_bitmap = 0;
185 output->get_canvas()->unlock_window();
186 output->unlock_canvas();
189 if( device->mwindow ) {
190 device->mwindow->gui->lock_window("VDeviceX11::close_all");
191 for( int i=0; i<SCREENCAP_BORDERS; ++i ) {
192 delete screencap_border[i];
193 screencap_border[i] = 0;
195 device->mwindow->gui->unlock_window();
203 int VDeviceX11::read_buffer(VFrame *frame)
205 //printf("VDeviceX11::read_buffer %d colormodel=%d\n", __LINE__, frame->get_color_model());
206 device->mwindow->gui->lock_window("VDeviceX11::close_all");
208 screencap_border[0]->reposition_window(device->input_x - SCREENCAP_PIXELS,
209 device->input_y - SCREENCAP_PIXELS);
210 screencap_border[1]->reposition_window(device->input_x - SCREENCAP_PIXELS,
212 screencap_border[2]->reposition_window(device->input_x - SCREENCAP_PIXELS,
213 device->input_y + device->in_config->h);
214 screencap_border[3]->reposition_window(device->input_x + device->in_config->w,
216 device->mwindow->gui->flush();
217 device->mwindow->gui->unlock_window();
220 capture_bitmap->capture_frame(frame,
221 device->input_x, device->input_y, device->do_cursor);
226 int VDeviceX11::get_best_colormodel(Asset *asset)
228 return File::get_best_colormodel(asset, SCREENCAPTURE);
233 int VDeviceX11::get_display_colormodel(int file_colormodel)
237 if( device->out_config->driver == PLAYBACK_X11_GL ) {
238 if( file_colormodel == BC_RGB888 ||
239 file_colormodel == BC_RGBA8888 ||
240 file_colormodel == BC_YUV888 ||
241 file_colormodel == BC_YUVA8888 ||
242 file_colormodel == BC_RGB_FLOAT ||
243 file_colormodel == BC_RGBA_FLOAT ) {
244 return file_colormodel;
250 if( !device->single_frame ) {
251 switch( file_colormodel ) {
255 result = file_colormodel;
261 switch( file_colormodel ) {
268 result = file_colormodel;
272 output->lock_canvas("VDeviceX11::get_display_colormodel");
273 result = output->get_canvas()->get_color_model();
274 output->unlock_canvas();
283 void VDeviceX11::new_output_buffer(VFrame **result, int file_colormodel, EDL *edl)
285 // printf("VDeviceX11::new_output_buffer %d hardware_scaling=%d\n",
286 // __LINE__, bitmap ? bitmap->hardware_scaling() : 0);
287 output->lock_canvas("VDeviceX11::new_output_buffer");
288 output->get_canvas()->lock_window("VDeviceX11::new_output_buffer 1");
290 // Get the best colormodel the display can handle.
291 int display_colormodel = get_display_colormodel(file_colormodel);
293 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
294 // __LINE__, file_colormodel, display_colormodel);
295 // Only create OpenGL Pbuffer and texture.
296 if( device->out_config->driver == PLAYBACK_X11_GL ) {
297 // Create bitmap for initial load into texture.
298 // Not necessary to do through Playback3D.....yet
299 if( !output_frame ) {
300 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
303 window_id = output->get_canvas()->get_id();
304 output_frame->set_opengl_state(VFrame::RAM);
307 output->get_transfers(edl,
308 output_x1, output_y1, output_x2, output_y2,
309 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
310 // Canvas may be a different size than the temporary bitmap for pure software
312 canvas_w = canvas_x2 - canvas_x1;
313 canvas_h = canvas_y2 - canvas_y1;
314 // can the direct frame be used?
315 int direct_supported =
316 device->out_config->use_direct_x11 &&
317 !output->xscroll && !output->yscroll &&
318 output_x1 == 0 && output_x2 == device->out_w &&
319 output_y1 == 0 && output_y2 == device->out_h;
321 // file wants direct frame but we need a temp
322 if( !direct_supported && file_colormodel == BC_BGR8888 )
323 file_colormodel = BC_RGB888;
325 // Conform existing bitmap to new colormodel and output size
327 // printf("VDeviceX11::new_output_buffer %d bitmap=%dx%d canvas=%dx%d canvas=%dx%d\n",
328 // __LINE__, bitmap->get_w(), bitmap->get_h(), canvas_w, canvas_h,);
330 bitmap->get_w() != canvas_w ||
331 bitmap->get_h() != canvas_h );
333 // Restart if output size changed or output colormodel changed.
334 // May have to recreate if transferring between windowed and fullscreen.
335 if( !color_model_selected ||
336 file_colormodel != output_frame->get_color_model() ||
337 (!bitmap->hardware_scaling() && size_change) ) {
338 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d prev "
339 // "file_colormodel=%d bitmap=%p output_frame=%p\n", __LINE__,
340 // file_colormodel, output_frame->get_color_model(), bitmap, output_frame);
341 delete bitmap; bitmap = 0;
342 delete output_frame; output_frame = 0;
343 // Clear borders if size changed
345 //printf("VDeviceX11::new_output_buffer %d w=%d h=%d "
346 // "canvas_x1=%d canvas_y1=%d canvas_x2=%d canvas_y2=%d\n",
347 // __LINE__, // (int)output->w, (int)output->h,
348 // (int)canvas_x1, (int)canvas_y1, (int)canvas_x2, (int)canvas_y2);
349 output->get_canvas()->set_color(BLACK);
351 if( canvas_y1 > 0 ) {
352 output->get_canvas()->draw_box(0, 0, output->w, canvas_y1);
353 output->get_canvas()->flash(0, 0, output->w, canvas_y1);
356 if( canvas_y2 < output->h ) {
357 output->get_canvas()->draw_box(0, canvas_y2, output->w, output->h - canvas_y2);
358 output->get_canvas()->flash(0, canvas_y2, output->w, output->h - canvas_y2);
361 if( canvas_x1 > 0 ) {
362 output->get_canvas()->draw_box(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
363 output->get_canvas()->flash(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
366 if( canvas_x2 < output->w ) {
367 output->get_canvas()->draw_box(canvas_x2, canvas_y1,
368 output->w - canvas_x2, canvas_y2 - canvas_y1);
369 output->get_canvas()->flash(canvas_x2, canvas_y1,
370 output->w - canvas_x2, canvas_y2 - canvas_y1);
379 bitmap_type = BITMAP_TEMP;
380 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
381 // __LINE__, file_colormodel, display_colormodel);
383 // Try hardware accelerated
384 switch( display_colormodel ) {
385 // blit from the codec directly to the window, using the standard X11 color model.
386 // Must scale in the codec. No cropping
388 if( direct_supported ) {
389 bitmap_type = BITMAP_PRIMARY;
395 if( device->out_config->driver == PLAYBACK_X11_XV &&
396 output->get_canvas()->accel_available(display_colormodel, 0) &&
397 !output->use_scrollbars )
398 bitmap_type = BITMAP_PRIMARY;
402 if( device->out_config->driver == PLAYBACK_X11_XV &&
403 output->get_canvas()->accel_available(display_colormodel, 0) &&
404 !output->use_scrollbars )
405 bitmap_type = BITMAP_PRIMARY;
406 else if( device->out_config->driver == PLAYBACK_X11_XV &&
407 output->get_canvas()->accel_available(BC_YUV422, 0) ) {
408 bitmap = new BC_Bitmap(output->get_canvas(),
409 device->out_w, device->out_h, BC_YUV422, 1);
414 if( device->out_config->driver == PLAYBACK_X11_XV &&
415 output->get_canvas()->accel_available(display_colormodel, 0) &&
416 !output->use_scrollbars ) {
417 bitmap_type = BITMAP_PRIMARY;
419 else if( device->out_config->driver == PLAYBACK_X11_XV &&
420 output->get_canvas()->accel_available(BC_YUV422P, 0) ) {
421 bitmap = new BC_Bitmap(output->get_canvas(),
422 device->out_w, device->out_h, BC_YUV422P, 1);
426 if( bitmap_type == BITMAP_PRIMARY ) {
427 int bitmap_w = use_direct ? canvas_w : device->out_w;
428 int bitmap_h = use_direct ? canvas_h : device->out_h;
429 bitmap = new BC_Bitmap(output->get_canvas(),
430 bitmap_w, bitmap_h, display_colormodel, -1);
431 output_frame = new VFrame(bitmap,
432 bitmap_w, bitmap_h, display_colormodel, -1);
434 // Make an intermediate frame
436 display_colormodel = output->get_canvas()->get_color_model();
437 // printf("VDeviceX11::new_output_buffer %d creating temp display_colormodel=%d "
438 // "file_colormodel=%d %dx%d %dx%d %dx%d\n", __LINE__,
439 // display_colormodel, file_colormodel, device->out_w, device->out_h,
440 // output->get_canvas()->get_w(), output->get_canvas()->get_h(), canvas_w, canvas_h);
441 bitmap = new BC_Bitmap(output->get_canvas(),
442 canvas_w, canvas_h, display_colormodel, 1);
443 bitmap_type = BITMAP_TEMP;
446 if( bitmap_type == BITMAP_TEMP ) {
447 // Intermediate frame
448 //printf("VDeviceX11::new_output_buffer %d creating output_frame\n", __LINE__);
449 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
451 color_model_selected = 1;
453 else if( bitmap_type == BITMAP_PRIMARY ) {
454 output_frame->set_memory(bitmap);
458 *result = output_frame;
459 //printf("VDeviceX11::new_output_buffer 10 %d\n", output->get_canvas()->get_window_lock());
461 output->get_canvas()->unlock_window();
462 output->unlock_canvas();
466 int VDeviceX11::start_playback()
468 // Record window is initialized when its monitor starts.
469 if( !device->single_frame )
470 output->start_video();
474 int VDeviceX11::stop_playback()
476 if( !device->single_frame )
477 output->stop_video();
478 // Record window goes back to monitoring
479 // get the last frame played and store it in the video_out
483 int VDeviceX11::write_buffer(VFrame *output_channels, EDL *edl)
485 output->lock_canvas("VDeviceX11::write_buffer");
486 output->get_canvas()->lock_window("VDeviceX11::write_buffer 1");
487 // if( device->out_config->driver == PLAYBACK_X11_GL &&
488 // output_frame->get_color_model() != BC_RGB888 ) {
489 // this is handled by overlay call in virtualvnode, using flatten alpha
490 // invoked when is_nested = -1 is passed to vdevicex11->overlay(...)
493 // printf("VDeviceX11::write_buffer %d %d bitmap_type=%d\n",
495 // output->get_canvas()->get_video_on(),
498 // int use_bitmap_extents = 0;
499 // canvas_w = -1; canvas_h = -1;
500 // // Canvas may be a different size than the temporary bitmap for pure software
501 // if( bitmap_type == BITMAP_TEMP && !bitmap->hardware_scaling() ) {
502 // canvas_w = bitmap->get_w();
503 // canvas_h = bitmap->get_h();
506 // output->get_transfers(edl,
507 // output_x1, output_y1, output_x2, output_y2,
508 // canvas_x1, canvas_y1, canvas_x2, canvas_y2,
509 // canvas_w, canvas_h);
511 // Convert colormodel
512 if( bitmap_type == BITMAP_TEMP ) {
513 // printf("VDeviceX11::write_buffer 1 %d %d, %d %d %d %d -> %d %d %d %d\n",
514 // output->w, output->h, in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
517 // printf("VDeviceX11::write_buffer %d output_channels=%p\n", __LINE__, output_channels);
518 // printf("VDeviceX11::write_buffer %d input color_model=%d output color_model=%d\n",
519 // __LINE__, output_channels->get_color_model(), bitmap->get_color_model());
520 if( bitmap->hardware_scaling() ) {
521 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
522 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
523 0, 0, output_channels->get_w(), output_channels->get_h(),
524 0, 0, bitmap->get_w(), bitmap->get_h(),
525 output_channels->get_color_model(), bitmap->get_color_model(),
526 -1, output_channels->get_w(), bitmap->get_w());
529 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
530 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
531 (int)output_x1, (int)output_y1, (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
532 0, 0, (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
533 output_channels->get_color_model(), bitmap->get_color_model(),
534 -1, output_channels->get_w(), bitmap->get_w());
538 //printf("VDeviceX11::write_buffer 4 %p\n", bitmap);
539 //for( i = 0; i < 1000; i += 4 ) bitmap->get_data()[i] = 128;
540 //printf("VDeviceX11::write_buffer 2 %d %d %d\n", bitmap_type,
541 // bitmap->get_color_model(),
542 // output->get_color_model());fflush(stdout);
544 // printf("VDeviceX11::write_buffer %d %dx%d %f %f %f %f -> %f %f %f %f\n",
545 // __LINE__, // output->w, output->h,
546 // output_x1, output_y1, output_x2, output_y2,
547 // canvas_x1, canvas_y1, canvas_x2, canvas_y2);
549 // Cause X server to display it
550 if( device->out_config->driver == PLAYBACK_X11_GL ) {
551 // Output is drawn in close_all if no video.
552 if( output->get_canvas()->get_video_on() ) {
553 canvas_w = -1; canvas_h = -1;
554 // Canvas may be a different size than the temporary bitmap for pure software
555 if( bitmap_type == BITMAP_TEMP &&
556 !bitmap->hardware_scaling() ) {
557 canvas_w = bitmap->get_w();
558 canvas_h = bitmap->get_h();
561 output->get_transfers(edl,
562 output_x1, output_y1, output_x2, output_y2,
563 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
566 //printf("VDeviceX11::write_buffer %d\n", __LINE__);
567 // Draw output frame directly. Not used for compositing.
568 output->get_canvas()->unlock_window();
569 output->unlock_canvas();
570 output->mwindow->playback_3d->write_buffer(output, output_frame,
571 output_x1, output_y1, output_x2, output_y2,
572 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
575 output->lock_canvas("VDeviceX11::write_buffer 2");
576 output->get_canvas()->lock_window("VDeviceX11::write_buffer 2");
580 if( bitmap->hardware_scaling() ) {
581 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
582 (int)canvas_x1, (int)canvas_y1,
583 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
584 (int)output_x1, (int)output_y1,
585 (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
589 //printf("VDeviceX11::write_buffer %d x=%d y=%d w=%d h=%d\n",
590 // __LINE__, (int)canvas_x1, (int)canvas_y1,
591 // output->get_canvas()->get_w(), output->get_canvas()->get_h());
592 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
593 (int)canvas_x1, (int)canvas_y1,
594 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
596 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
598 //printf("VDeviceX11::write_buffer %d bitmap=%p\n", __LINE__, bitmap);
600 if( !output->get_canvas()->get_video_on() )
601 output->get_canvas()->flash(0);
604 output->get_canvas()->unlock_window();
605 output->unlock_canvas();
610 void VDeviceX11::clear_output()
613 output->mwindow->playback_3d->clear_output(output, 0);
614 output->mwindow->playback_3d->clear_output(output, output_frame);
618 void VDeviceX11::clear_input(VFrame *frame)
620 this->output->mwindow->playback_3d->clear_input(this->output, frame);
623 void VDeviceX11::convert_cmodel(VFrame *output, int dst_cmodel)
625 this->output->mwindow->playback_3d->convert_cmodel(this->output,
630 void VDeviceX11::do_camera(VFrame *output, VFrame *input,
631 float in_x1, float in_y1, float in_x2, float in_y2,
632 float out_x1, float out_y1, float out_x2, float out_y2)
634 this->output->mwindow->playback_3d->do_camera(this->output,
636 in_x1, in_y1, in_x2, in_y2,
637 out_x1, out_y1, out_x2, out_y2);
641 void VDeviceX11::do_fade(VFrame *output_temp, float fade)
643 this->output->mwindow->playback_3d->do_fade(this->output, output_temp, fade);
646 bool VDeviceX11::can_mask(int64_t start_position_project, MaskAutos *keyframe_set)
649 MaskAuto *keyframe = (MaskAuto*)keyframe_set->
650 get_prev_auto(start_position_project, PLAY_FORWARD, current);
651 return keyframe->disable_opengl_masking ? 0 : 1;
654 void VDeviceX11::do_mask(VFrame *output_temp, int64_t start_position_project,
655 MaskAutos *keyframe_set, MaskAuto *keyframe, MaskAuto *default_auto)
657 this->output->mwindow->playback_3d->do_mask(output, output_temp,
658 start_position_project, keyframe_set, keyframe, default_auto);
661 void VDeviceX11::overlay(VFrame *output_frame, VFrame *input,
662 float in_x1, float in_y1, float in_x2, float in_y2,
663 float out_x1, float out_y1, float out_x2, float out_y2,
664 float alpha, int mode, EDL *edl, int is_nested)
666 int interpolation_type = edl->session->interpolation_type;
667 output->mwindow->playback_3d->overlay(output, input,
668 in_x1, in_y1, in_x2, in_y2,
669 out_x1, out_y1, out_x2, out_y2, alpha, // 0 - 1
670 mode, interpolation_type, output_frame, is_nested);
673 void VDeviceX11::run_plugin(PluginClient *client)
675 output->mwindow->playback_3d->run_plugin(output, client);
678 void VDeviceX11::copy_frame(VFrame *dst, VFrame *src)
680 output->mwindow->playback_3d->copy_from(output, dst, src, 1);