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;
83 int VDeviceX11::open_input()
85 //printf("VDeviceX11::open_input 1\n");
86 capture_bitmap = new BC_Capture(device->in_config->w,
88 device->in_config->screencapture_display);
89 //printf("VDeviceX11::open_input 2\n");
90 capture_bitmap->bars_on(SCREENCAP_PIXELS, SCREENCAP_COLOR,
91 device->input_x, device->input_y,
92 device->in_config->w, device->in_config->h);
97 int VDeviceX11::open_output()
100 output->lock_canvas("VDeviceX11::open_output");
101 output->get_canvas()->lock_window("VDeviceX11::open_output");
102 if( !device->single_frame )
103 output->start_video();
105 output->start_single();
106 output->get_canvas()->unlock_window();
108 // Enable opengl in the first routine that needs it, to reduce the complexity.
109 output->unlock_canvas();
115 int VDeviceX11::output_visible()
117 if( !output ) return 0;
119 output->lock_canvas("VDeviceX11::output_visible");
120 if( output->get_canvas()->get_hidden() ) {
121 output->unlock_canvas();
125 output->unlock_canvas();
131 int VDeviceX11::close_all()
134 output->lock_canvas("VDeviceX11::close_all 1");
135 output->get_canvas()->lock_window("VDeviceX11::close_all 1");
136 int video_on = output->get_canvas()->get_video_on();
137 // Update the status bug
138 if( !device->single_frame ) {
139 output->stop_video();
142 output->stop_single();
145 output->update_refresh(device, output_frame);
146 // if the last frame is good, don't draw over it
147 if( !video_on || output->need_overlays() ) {
148 output->unlock_canvas();
149 output->draw_refresh(1);
150 output->lock_canvas("VDeviceX11::close_all 2");
155 delete bitmap; bitmap = 0;
156 delete output_frame; output_frame = 0;
157 delete capture_bitmap; capture_bitmap = 0;
160 output->get_canvas()->unlock_window();
161 output->unlock_canvas();
169 int VDeviceX11::read_buffer(VFrame *frame)
171 capture_bitmap->bars_reposition(
172 device->input_x, device->input_y,
173 device->in_config->w, device->in_config->h);
175 capture_bitmap->capture_frame(frame,
176 device->input_x, device->input_y, device->do_cursor);
181 int VDeviceX11::get_best_colormodel(Asset *asset)
183 return File::get_best_colormodel(asset, SCREENCAPTURE);
188 int VDeviceX11::get_display_colormodel(int file_colormodel)
192 if( device->out_config->driver == PLAYBACK_X11_GL ) {
193 if( file_colormodel == BC_RGB888 ||
194 file_colormodel == BC_RGBA8888 ||
195 file_colormodel == BC_YUV888 ||
196 file_colormodel == BC_YUVA8888 ||
197 file_colormodel == BC_RGB_FLOAT ||
198 file_colormodel == BC_RGBA_FLOAT ) {
199 return file_colormodel;
205 if( !device->single_frame ) {
206 switch( file_colormodel ) {
210 result = file_colormodel;
216 switch( file_colormodel ) {
223 result = file_colormodel;
227 output->lock_canvas("VDeviceX11::get_display_colormodel");
228 result = output->get_canvas()->get_color_model();
229 output->unlock_canvas();
238 void VDeviceX11::new_output_buffer(VFrame **result, int file_colormodel, EDL *edl)
240 // printf("VDeviceX11::new_output_buffer %d hardware_scaling=%d\n",
241 // __LINE__, bitmap ? bitmap->hardware_scaling() : 0);
242 output->lock_canvas("VDeviceX11::new_output_buffer");
243 output->get_canvas()->lock_window("VDeviceX11::new_output_buffer 1");
245 // Get the best colormodel the display can handle.
246 int display_colormodel = get_display_colormodel(file_colormodel);
248 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
249 // __LINE__, file_colormodel, display_colormodel);
250 // Only create OpenGL Pbuffer and texture.
251 if( device->out_config->driver == PLAYBACK_X11_GL ) {
252 // Create bitmap for initial load into texture.
253 // Not necessary to do through Playback3D.....yet
254 if( !output_frame ) {
255 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
258 window_id = output->get_canvas()->get_id();
259 output_frame->set_opengl_state(VFrame::RAM);
262 output->get_transfers(edl,
263 output_x1, output_y1, output_x2, output_y2,
264 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
265 // Canvas may be a different size than the temporary bitmap for pure software
267 canvas_w = canvas_x2 - canvas_x1;
268 canvas_h = canvas_y2 - canvas_y1;
269 // can the direct frame be used?
270 int direct_supported =
271 device->out_config->use_direct_x11 &&
272 !output->xscroll && !output->yscroll &&
273 output_x1 == 0 && output_x2 == device->out_w &&
274 output_y1 == 0 && output_y2 == device->out_h;
276 // file wants direct frame but we need a temp
277 if( !direct_supported && file_colormodel == BC_BGR8888 )
278 file_colormodel = BC_RGB888;
280 // Conform existing bitmap to new colormodel and output size
282 // printf("VDeviceX11::new_output_buffer %d bitmap=%dx%d canvas=%dx%d canvas=%dx%d\n",
283 // __LINE__, bitmap->get_w(), bitmap->get_h(), canvas_w, canvas_h,);
285 bitmap->get_w() != canvas_w ||
286 bitmap->get_h() != canvas_h );
288 // Restart if output size changed or output colormodel changed.
289 // May have to recreate if transferring between windowed and fullscreen.
290 if( !color_model_selected ||
291 file_colormodel != output_frame->get_color_model() ||
292 (!bitmap->hardware_scaling() && size_change) ) {
293 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d prev "
294 // "file_colormodel=%d bitmap=%p output_frame=%p\n", __LINE__,
295 // file_colormodel, output_frame->get_color_model(), bitmap, output_frame);
296 delete bitmap; bitmap = 0;
297 delete output_frame; output_frame = 0;
298 // Clear borders if size changed
300 //printf("VDeviceX11::new_output_buffer %d w=%d h=%d "
301 // "canvas_x1=%d canvas_y1=%d canvas_x2=%d canvas_y2=%d\n",
302 // __LINE__, // (int)output->w, (int)output->h,
303 // (int)canvas_x1, (int)canvas_y1, (int)canvas_x2, (int)canvas_y2);
304 output->get_canvas()->set_color(BLACK);
306 if( canvas_y1 > 0 ) {
307 output->get_canvas()->draw_box(0, 0, output->w, canvas_y1);
308 output->get_canvas()->flash(0, 0, output->w, canvas_y1);
311 if( canvas_y2 < output->h ) {
312 output->get_canvas()->draw_box(0, canvas_y2, output->w, output->h - canvas_y2);
313 output->get_canvas()->flash(0, canvas_y2, output->w, output->h - canvas_y2);
316 if( canvas_x1 > 0 ) {
317 output->get_canvas()->draw_box(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
318 output->get_canvas()->flash(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
321 if( canvas_x2 < output->w ) {
322 output->get_canvas()->draw_box(canvas_x2, canvas_y1,
323 output->w - canvas_x2, canvas_y2 - canvas_y1);
324 output->get_canvas()->flash(canvas_x2, canvas_y1,
325 output->w - canvas_x2, canvas_y2 - canvas_y1);
334 bitmap_type = BITMAP_TEMP;
335 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
336 // __LINE__, file_colormodel, display_colormodel);
338 // Try hardware accelerated
339 switch( display_colormodel ) {
340 // blit from the codec directly to the window, using the standard X11 color model.
341 // Must scale in the codec. No cropping
343 if( direct_supported ) {
344 bitmap_type = BITMAP_PRIMARY;
350 if( device->out_config->driver == PLAYBACK_X11_XV &&
351 output->get_canvas()->accel_available(display_colormodel, 0) &&
352 !output->use_scrollbars )
353 bitmap_type = BITMAP_PRIMARY;
357 if( device->out_config->driver == PLAYBACK_X11_XV &&
358 output->get_canvas()->accel_available(display_colormodel, 0) &&
359 !output->use_scrollbars )
360 bitmap_type = BITMAP_PRIMARY;
361 else if( device->out_config->driver == PLAYBACK_X11_XV &&
362 output->get_canvas()->accel_available(BC_YUV422, 0) ) {
363 bitmap = new BC_Bitmap(output->get_canvas(),
364 device->out_w, device->out_h, BC_YUV422, 1);
369 if( device->out_config->driver == PLAYBACK_X11_XV &&
370 output->get_canvas()->accel_available(display_colormodel, 0) &&
371 !output->use_scrollbars ) {
372 bitmap_type = BITMAP_PRIMARY;
374 else if( device->out_config->driver == PLAYBACK_X11_XV &&
375 output->get_canvas()->accel_available(BC_YUV422P, 0) ) {
376 bitmap = new BC_Bitmap(output->get_canvas(),
377 device->out_w, device->out_h, BC_YUV422P, 1);
381 if( bitmap_type == BITMAP_PRIMARY ) {
382 int bitmap_w = use_direct ? canvas_w : device->out_w;
383 int bitmap_h = use_direct ? canvas_h : device->out_h;
384 bitmap = new BC_Bitmap(output->get_canvas(),
385 bitmap_w, bitmap_h, display_colormodel, -1);
386 output_frame = new VFrame(bitmap,
387 bitmap_w, bitmap_h, display_colormodel, -1);
389 // Make an intermediate frame
391 display_colormodel = output->get_canvas()->get_color_model();
392 // printf("VDeviceX11::new_output_buffer %d creating temp display_colormodel=%d "
393 // "file_colormodel=%d %dx%d %dx%d %dx%d\n", __LINE__,
394 // display_colormodel, file_colormodel, device->out_w, device->out_h,
395 // output->get_canvas()->get_w(), output->get_canvas()->get_h(), canvas_w, canvas_h);
396 bitmap = new BC_Bitmap(output->get_canvas(),
397 canvas_w, canvas_h, display_colormodel, 1);
398 bitmap_type = BITMAP_TEMP;
401 if( bitmap_type == BITMAP_TEMP ) {
402 // Intermediate frame
403 //printf("VDeviceX11::new_output_buffer %d creating output_frame\n", __LINE__);
404 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
406 color_model_selected = 1;
408 else if( bitmap_type == BITMAP_PRIMARY ) {
409 output_frame->set_memory(bitmap);
413 *result = output_frame;
414 //printf("VDeviceX11::new_output_buffer 10 %d\n", output->get_canvas()->get_window_lock());
416 output->get_canvas()->unlock_window();
417 output->unlock_canvas();
421 int VDeviceX11::start_playback()
423 // Record window is initialized when its monitor starts.
424 if( !device->single_frame )
425 output->start_video();
429 int VDeviceX11::stop_playback()
431 if( !device->single_frame )
432 output->stop_video();
433 // Record window goes back to monitoring
434 // get the last frame played and store it in the video_out
438 int VDeviceX11::write_buffer(VFrame *output_channels, EDL *edl)
440 output->lock_canvas("VDeviceX11::write_buffer");
441 output->get_canvas()->lock_window("VDeviceX11::write_buffer 1");
442 // if( device->out_config->driver == PLAYBACK_X11_GL &&
443 // output_frame->get_color_model() != BC_RGB888 ) {
444 // this is handled by overlay call in virtualvnode, using flatten alpha
445 // invoked when is_nested = -1 is passed to vdevicex11->overlay(...)
448 // printf("VDeviceX11::write_buffer %d %d bitmap_type=%d\n",
450 // output->get_canvas()->get_video_on(),
453 // int use_bitmap_extents = 0;
454 // canvas_w = -1; canvas_h = -1;
455 // // Canvas may be a different size than the temporary bitmap for pure software
456 // if( bitmap_type == BITMAP_TEMP && !bitmap->hardware_scaling() ) {
457 // canvas_w = bitmap->get_w();
458 // canvas_h = bitmap->get_h();
461 // output->get_transfers(edl,
462 // output_x1, output_y1, output_x2, output_y2,
463 // canvas_x1, canvas_y1, canvas_x2, canvas_y2,
464 // canvas_w, canvas_h);
466 // Convert colormodel
467 if( bitmap_type == BITMAP_TEMP ) {
468 // printf("VDeviceX11::write_buffer 1 %d %d, %d %d %d %d -> %d %d %d %d\n",
469 // output->w, output->h, in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
472 // printf("VDeviceX11::write_buffer %d output_channels=%p\n", __LINE__, output_channels);
473 // printf("VDeviceX11::write_buffer %d input color_model=%d output color_model=%d\n",
474 // __LINE__, output_channels->get_color_model(), bitmap->get_color_model());
475 if( bitmap->hardware_scaling() ) {
476 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
477 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
478 0, 0, output_channels->get_w(), output_channels->get_h(),
479 0, 0, bitmap->get_w(), bitmap->get_h(),
480 output_channels->get_color_model(), bitmap->get_color_model(),
481 -1, output_channels->get_w(), bitmap->get_w());
484 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
485 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
486 (int)output_x1, (int)output_y1, (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
487 0, 0, (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
488 output_channels->get_color_model(), bitmap->get_color_model(),
489 -1, output_channels->get_w(), bitmap->get_w());
493 //printf("VDeviceX11::write_buffer 4 %p\n", bitmap);
494 //for( i = 0; i < 1000; i += 4 ) bitmap->get_data()[i] = 128;
495 //printf("VDeviceX11::write_buffer 2 %d %d %d\n", bitmap_type,
496 // bitmap->get_color_model(),
497 // output->get_color_model());fflush(stdout);
499 // printf("VDeviceX11::write_buffer %d %dx%d %f %f %f %f -> %f %f %f %f\n",
500 // __LINE__, // output->w, output->h,
501 // output_x1, output_y1, output_x2, output_y2,
502 // canvas_x1, canvas_y1, canvas_x2, canvas_y2);
504 // Cause X server to display it
505 if( device->out_config->driver == PLAYBACK_X11_GL ) {
506 // Output is drawn in close_all if no video.
507 if( output->get_canvas()->get_video_on() ) {
508 canvas_w = -1; canvas_h = -1;
509 // Canvas may be a different size than the temporary bitmap for pure software
510 if( bitmap_type == BITMAP_TEMP &&
511 !bitmap->hardware_scaling() ) {
512 canvas_w = bitmap->get_w();
513 canvas_h = bitmap->get_h();
516 output->get_transfers(edl,
517 output_x1, output_y1, output_x2, output_y2,
518 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
521 //printf("VDeviceX11::write_buffer %d\n", __LINE__);
522 // Draw output frame directly. Not used for compositing.
523 output->get_canvas()->unlock_window();
524 output->unlock_canvas();
525 output->mwindow->playback_3d->write_buffer(output, output_frame,
526 output_x1, output_y1, output_x2, output_y2,
527 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
530 output->lock_canvas("VDeviceX11::write_buffer 2");
531 output->get_canvas()->lock_window("VDeviceX11::write_buffer 2");
535 if( bitmap->hardware_scaling() ) {
536 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
537 (int)canvas_x1, (int)canvas_y1,
538 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
539 (int)output_x1, (int)output_y1,
540 (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
544 //printf("VDeviceX11::write_buffer %d x=%d y=%d w=%d h=%d\n",
545 // __LINE__, (int)canvas_x1, (int)canvas_y1,
546 // output->get_canvas()->get_w(), output->get_canvas()->get_h());
547 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
548 (int)canvas_x1, (int)canvas_y1,
549 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
551 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
553 //printf("VDeviceX11::write_buffer %d bitmap=%p\n", __LINE__, bitmap);
557 output->get_canvas()->unlock_window();
558 output->unlock_canvas();
563 void VDeviceX11::clear_output()
566 output->mwindow->playback_3d->clear_output(output, 0);
567 output->mwindow->playback_3d->clear_output(output, output_frame);
571 void VDeviceX11::clear_input(VFrame *frame)
573 this->output->mwindow->playback_3d->clear_input(this->output, frame);
576 void VDeviceX11::convert_cmodel(VFrame *output, int dst_cmodel)
578 this->output->mwindow->playback_3d->convert_cmodel(this->output,
583 void VDeviceX11::do_camera(VFrame *output, VFrame *input,
584 float in_x1, float in_y1, float in_x2, float in_y2,
585 float out_x1, float out_y1, float out_x2, float out_y2)
587 this->output->mwindow->playback_3d->do_camera(this->output,
589 in_x1, in_y1, in_x2, in_y2,
590 out_x1, out_y1, out_x2, out_y2);
594 void VDeviceX11::do_fade(VFrame *output_temp, float fade)
596 this->output->mwindow->playback_3d->do_fade(this->output, output_temp, fade);
599 bool VDeviceX11::can_mask(int64_t start_position_project, MaskAutos *keyframe_set)
602 MaskAuto *keyframe = (MaskAuto*)keyframe_set->
603 get_prev_auto(start_position_project, PLAY_FORWARD, current);
604 return keyframe->disable_opengl_masking ? 0 : 1;
607 void VDeviceX11::do_mask(VFrame *output_temp, int64_t start_position_project,
608 MaskAutos *keyframe_set, MaskAuto *keyframe, MaskAuto *default_auto)
610 this->output->mwindow->playback_3d->do_mask(output, output_temp,
611 start_position_project, keyframe_set, keyframe, default_auto);
614 void VDeviceX11::overlay(VFrame *output_frame, VFrame *input,
615 float in_x1, float in_y1, float in_x2, float in_y2,
616 float out_x1, float out_y1, float out_x2, float out_y2,
617 float alpha, int mode, EDL *edl, int is_nested)
619 int interpolation_type = edl->session->interpolation_type;
620 output->mwindow->playback_3d->overlay(output, input,
621 in_x1, in_y1, in_x2, in_y2,
622 out_x1, out_y1, out_x2, out_y2, alpha, // 0 - 1
623 mode, interpolation_type, output_frame, is_nested);
626 void VDeviceX11::run_plugin(PluginClient *client)
628 output->mwindow->playback_3d->run_plugin(output, client);
631 void VDeviceX11::copy_frame(VFrame *dst, VFrame *src)
633 output->mwindow->playback_3d->copy_from(output, dst, src, 1);