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->draw_refresh(1);
152 delete bitmap; bitmap = 0;
153 delete output_frame; output_frame = 0;
154 delete capture_bitmap; capture_bitmap = 0;
157 output->get_canvas()->unlock_window();
158 output->unlock_canvas();
166 int VDeviceX11::read_buffer(VFrame *frame)
168 capture_bitmap->bars_reposition(
169 device->input_x, device->input_y,
170 device->in_config->w, device->in_config->h);
172 capture_bitmap->capture_frame(frame,
173 device->input_x, device->input_y, device->do_cursor);
178 int VDeviceX11::get_best_colormodel(Asset *asset)
180 return File::get_best_colormodel(asset, SCREENCAPTURE);
185 int VDeviceX11::get_display_colormodel(int file_colormodel)
189 if( device->out_config->driver == PLAYBACK_X11_GL ) {
190 if( file_colormodel == BC_RGB888 ||
191 file_colormodel == BC_RGBA8888 ||
192 file_colormodel == BC_YUV888 ||
193 file_colormodel == BC_YUVA8888 ||
194 file_colormodel == BC_RGB_FLOAT ||
195 file_colormodel == BC_RGBA_FLOAT ) {
196 return file_colormodel;
202 if( !device->single_frame ) {
203 switch( file_colormodel ) {
207 result = file_colormodel;
213 switch( file_colormodel ) {
220 result = file_colormodel;
224 output->lock_canvas("VDeviceX11::get_display_colormodel");
225 result = output->get_canvas()->get_color_model();
226 output->unlock_canvas();
235 void VDeviceX11::new_output_buffer(VFrame **result, int file_colormodel, EDL *edl)
237 // printf("VDeviceX11::new_output_buffer %d hardware_scaling=%d\n",
238 // __LINE__, bitmap ? bitmap->hardware_scaling() : 0);
239 output->lock_canvas("VDeviceX11::new_output_buffer");
240 output->get_canvas()->lock_window("VDeviceX11::new_output_buffer 1");
242 // Get the best colormodel the display can handle.
243 int display_colormodel = get_display_colormodel(file_colormodel);
245 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
246 // __LINE__, file_colormodel, display_colormodel);
247 // Only create OpenGL Pbuffer and texture.
248 if( device->out_config->driver == PLAYBACK_X11_GL ) {
249 // Create bitmap for initial load into texture.
250 // Not necessary to do through Playback3D.....yet
251 if( !output_frame ) {
252 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
255 window_id = output->get_canvas()->get_id();
256 output_frame->set_opengl_state(VFrame::RAM);
259 output->get_transfers(edl,
260 output_x1, output_y1, output_x2, output_y2,
261 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
262 // Canvas may be a different size than the temporary bitmap for pure software
264 canvas_w = canvas_x2 - canvas_x1;
265 canvas_h = canvas_y2 - canvas_y1;
266 // can the direct frame be used?
267 int direct_supported =
268 device->out_config->use_direct_x11 &&
269 !output->xscroll && !output->yscroll &&
270 output_x1 == 0 && output_x2 == device->out_w &&
271 output_y1 == 0 && output_y2 == device->out_h;
273 // file wants direct frame but we need a temp
274 if( !direct_supported && file_colormodel == BC_BGR8888 )
275 file_colormodel = BC_RGB888;
277 // Conform existing bitmap to new colormodel and output size
279 // printf("VDeviceX11::new_output_buffer %d bitmap=%dx%d canvas=%dx%d canvas=%dx%d\n",
280 // __LINE__, bitmap->get_w(), bitmap->get_h(), canvas_w, canvas_h,);
282 bitmap->get_w() != canvas_w ||
283 bitmap->get_h() != canvas_h );
285 // Restart if output size changed or output colormodel changed.
286 // May have to recreate if transferring between windowed and fullscreen.
287 if( !color_model_selected ||
288 file_colormodel != output_frame->get_color_model() ||
289 (!bitmap->hardware_scaling() && size_change) ) {
290 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d prev "
291 // "file_colormodel=%d bitmap=%p output_frame=%p\n", __LINE__,
292 // file_colormodel, output_frame->get_color_model(), bitmap, output_frame);
293 delete bitmap; bitmap = 0;
294 delete output_frame; output_frame = 0;
295 // Clear borders if size changed
297 //printf("VDeviceX11::new_output_buffer %d w=%d h=%d "
298 // "canvas_x1=%d canvas_y1=%d canvas_x2=%d canvas_y2=%d\n",
299 // __LINE__, // (int)output->w, (int)output->h,
300 // (int)canvas_x1, (int)canvas_y1, (int)canvas_x2, (int)canvas_y2);
301 output->get_canvas()->set_color(BLACK);
303 if( canvas_y1 > 0 ) {
304 output->get_canvas()->draw_box(0, 0, output->w, canvas_y1);
305 output->get_canvas()->flash(0, 0, output->w, canvas_y1);
308 if( canvas_y2 < output->h ) {
309 output->get_canvas()->draw_box(0, canvas_y2, output->w, output->h - canvas_y2);
310 output->get_canvas()->flash(0, canvas_y2, output->w, output->h - canvas_y2);
313 if( canvas_x1 > 0 ) {
314 output->get_canvas()->draw_box(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
315 output->get_canvas()->flash(0, canvas_y1, canvas_x1, canvas_y2 - canvas_y1);
318 if( canvas_x2 < output->w ) {
319 output->get_canvas()->draw_box(canvas_x2, canvas_y1,
320 output->w - canvas_x2, canvas_y2 - canvas_y1);
321 output->get_canvas()->flash(canvas_x2, canvas_y1,
322 output->w - canvas_x2, canvas_y2 - canvas_y1);
331 bitmap_type = BITMAP_TEMP;
332 //printf("VDeviceX11::new_output_buffer %d file_colormodel=%d display_colormodel=%d\n",
333 // __LINE__, file_colormodel, display_colormodel);
335 // Try hardware accelerated
336 switch( display_colormodel ) {
337 // blit from the codec directly to the window, using the standard X11 color model.
338 // Must scale in the codec. No cropping
340 if( direct_supported ) {
341 bitmap_type = BITMAP_PRIMARY;
347 if( device->out_config->driver == PLAYBACK_X11_XV &&
348 output->get_canvas()->accel_available(display_colormodel, 0) &&
349 !output->use_scrollbars )
350 bitmap_type = BITMAP_PRIMARY;
354 if( device->out_config->driver == PLAYBACK_X11_XV &&
355 output->get_canvas()->accel_available(display_colormodel, 0) &&
356 !output->use_scrollbars )
357 bitmap_type = BITMAP_PRIMARY;
358 else if( device->out_config->driver == PLAYBACK_X11_XV &&
359 output->get_canvas()->accel_available(BC_YUV422, 0) ) {
360 bitmap = new BC_Bitmap(output->get_canvas(),
361 device->out_w, device->out_h, BC_YUV422, 1);
366 if( device->out_config->driver == PLAYBACK_X11_XV &&
367 output->get_canvas()->accel_available(display_colormodel, 0) &&
368 !output->use_scrollbars ) {
369 bitmap_type = BITMAP_PRIMARY;
371 else if( device->out_config->driver == PLAYBACK_X11_XV &&
372 output->get_canvas()->accel_available(BC_YUV422P, 0) ) {
373 bitmap = new BC_Bitmap(output->get_canvas(),
374 device->out_w, device->out_h, BC_YUV422P, 1);
378 if( bitmap_type == BITMAP_PRIMARY ) {
379 int bitmap_w = use_direct ? canvas_w : device->out_w;
380 int bitmap_h = use_direct ? canvas_h : device->out_h;
381 bitmap = new BC_Bitmap(output->get_canvas(),
382 bitmap_w, bitmap_h, display_colormodel, -1);
383 output_frame = new VFrame(bitmap,
384 bitmap_w, bitmap_h, display_colormodel, -1);
386 // Make an intermediate frame
388 display_colormodel = output->get_canvas()->get_color_model();
389 // printf("VDeviceX11::new_output_buffer %d creating temp display_colormodel=%d "
390 // "file_colormodel=%d %dx%d %dx%d %dx%d\n", __LINE__,
391 // display_colormodel, file_colormodel, device->out_w, device->out_h,
392 // output->get_canvas()->get_w(), output->get_canvas()->get_h(), canvas_w, canvas_h);
393 bitmap = new BC_Bitmap(output->get_canvas(),
394 canvas_w, canvas_h, display_colormodel, 1);
395 bitmap_type = BITMAP_TEMP;
398 if( bitmap_type == BITMAP_TEMP ) {
399 // Intermediate frame
400 //printf("VDeviceX11::new_output_buffer %d creating output_frame\n", __LINE__);
401 output_frame = new VFrame(device->out_w, device->out_h, file_colormodel);
403 color_model_selected = 1;
405 else if( bitmap_type == BITMAP_PRIMARY ) {
406 output_frame->set_memory(bitmap);
410 *result = output_frame;
411 //printf("VDeviceX11::new_output_buffer 10 %d\n", output->get_canvas()->get_window_lock());
413 output->get_canvas()->unlock_window();
414 output->unlock_canvas();
418 int VDeviceX11::start_playback()
420 // Record window is initialized when its monitor starts.
421 if( !device->single_frame )
422 output->start_video();
426 int VDeviceX11::stop_playback()
428 if( !device->single_frame )
429 output->stop_video();
430 // Record window goes back to monitoring
431 // get the last frame played and store it in the video_out
435 int VDeviceX11::write_buffer(VFrame *output_channels, EDL *edl)
437 output->lock_canvas("VDeviceX11::write_buffer");
438 output->get_canvas()->lock_window("VDeviceX11::write_buffer 1");
439 // if( device->out_config->driver == PLAYBACK_X11_GL &&
440 // output_frame->get_color_model() != BC_RGB888 ) {
441 // this is handled by overlay call in virtualvnode, using flatten alpha
442 // invoked when is_nested = -1 is passed to vdevicex11->overlay(...)
445 // printf("VDeviceX11::write_buffer %d %d bitmap_type=%d\n",
447 // output->get_canvas()->get_video_on(),
450 // int use_bitmap_extents = 0;
451 // canvas_w = -1; canvas_h = -1;
452 // // Canvas may be a different size than the temporary bitmap for pure software
453 // if( bitmap_type == BITMAP_TEMP && !bitmap->hardware_scaling() ) {
454 // canvas_w = bitmap->get_w();
455 // canvas_h = bitmap->get_h();
458 // output->get_transfers(edl,
459 // output_x1, output_y1, output_x2, output_y2,
460 // canvas_x1, canvas_y1, canvas_x2, canvas_y2,
461 // canvas_w, canvas_h);
463 // Convert colormodel
464 if( bitmap_type == BITMAP_TEMP ) {
465 // printf("VDeviceX11::write_buffer 1 %d %d, %d %d %d %d -> %d %d %d %d\n",
466 // output->w, output->h, in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
469 // printf("VDeviceX11::write_buffer %d output_channels=%p\n", __LINE__, output_channels);
470 // printf("VDeviceX11::write_buffer %d input color_model=%d output color_model=%d\n",
471 // __LINE__, output_channels->get_color_model(), bitmap->get_color_model());
472 if( bitmap->hardware_scaling() ) {
473 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
474 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
475 0, 0, output_channels->get_w(), output_channels->get_h(),
476 0, 0, bitmap->get_w(), bitmap->get_h(),
477 output_channels->get_color_model(), bitmap->get_color_model(),
478 -1, output_channels->get_w(), bitmap->get_w());
481 BC_CModels::transfer(bitmap->get_row_pointers(), output_channels->get_rows(), 0, 0, 0,
482 output_channels->get_y(), output_channels->get_u(), output_channels->get_v(),
483 (int)output_x1, (int)output_y1, (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
484 0, 0, (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
485 output_channels->get_color_model(), bitmap->get_color_model(),
486 -1, output_channels->get_w(), bitmap->get_w());
490 //printf("VDeviceX11::write_buffer 4 %p\n", bitmap);
491 //for( i = 0; i < 1000; i += 4 ) bitmap->get_data()[i] = 128;
492 //printf("VDeviceX11::write_buffer 2 %d %d %d\n", bitmap_type,
493 // bitmap->get_color_model(),
494 // output->get_color_model());fflush(stdout);
496 // printf("VDeviceX11::write_buffer %d %dx%d %f %f %f %f -> %f %f %f %f\n",
497 // __LINE__, // output->w, output->h,
498 // output_x1, output_y1, output_x2, output_y2,
499 // canvas_x1, canvas_y1, canvas_x2, canvas_y2);
501 // Cause X server to display it
502 if( device->out_config->driver == PLAYBACK_X11_GL ) {
503 // Output is drawn in close_all if no video.
504 if( output->get_canvas()->get_video_on() ) {
505 canvas_w = -1; canvas_h = -1;
506 // Canvas may be a different size than the temporary bitmap for pure software
507 if( bitmap_type == BITMAP_TEMP &&
508 !bitmap->hardware_scaling() ) {
509 canvas_w = bitmap->get_w();
510 canvas_h = bitmap->get_h();
513 output->get_transfers(edl,
514 output_x1, output_y1, output_x2, output_y2,
515 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
518 //printf("VDeviceX11::write_buffer %d\n", __LINE__);
519 // Draw output frame directly. Not used for compositing.
520 output->get_canvas()->unlock_window();
521 output->unlock_canvas();
522 output->mwindow->playback_3d->write_buffer(output, output_frame,
523 output_x1, output_y1, output_x2, output_y2,
524 canvas_x1, canvas_y1, canvas_x2, canvas_y2,
527 output->lock_canvas("VDeviceX11::write_buffer 2");
528 output->get_canvas()->lock_window("VDeviceX11::write_buffer 2");
532 if( bitmap->hardware_scaling() ) {
533 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
534 (int)canvas_x1, (int)canvas_y1,
535 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
536 (int)output_x1, (int)output_y1,
537 (int)(output_x2 - output_x1), (int)(output_y2 - output_y1),
541 //printf("VDeviceX11::write_buffer %d x=%d y=%d w=%d h=%d\n",
542 // __LINE__, (int)canvas_x1, (int)canvas_y1,
543 // output->get_canvas()->get_w(), output->get_canvas()->get_h());
544 output->get_canvas()->draw_bitmap(bitmap, !device->single_frame,
545 (int)canvas_x1, (int)canvas_y1,
546 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
548 (int)(canvas_x2 - canvas_x1), (int)(canvas_y2 - canvas_y1),
550 //printf("VDeviceX11::write_buffer %d bitmap=%p\n", __LINE__, bitmap);
552 if( !output->get_canvas()->get_video_on() )
553 output->get_canvas()->flash(0);
556 output->get_canvas()->unlock_window();
557 output->unlock_canvas();
562 void VDeviceX11::clear_output()
565 output->mwindow->playback_3d->clear_output(output, 0);
566 output->mwindow->playback_3d->clear_output(output, output_frame);
570 void VDeviceX11::clear_input(VFrame *frame)
572 this->output->mwindow->playback_3d->clear_input(this->output, frame);
575 void VDeviceX11::convert_cmodel(VFrame *output, int dst_cmodel)
577 this->output->mwindow->playback_3d->convert_cmodel(this->output,
582 void VDeviceX11::do_camera(VFrame *output, VFrame *input,
583 float in_x1, float in_y1, float in_x2, float in_y2,
584 float out_x1, float out_y1, float out_x2, float out_y2)
586 this->output->mwindow->playback_3d->do_camera(this->output,
588 in_x1, in_y1, in_x2, in_y2,
589 out_x1, out_y1, out_x2, out_y2);
593 void VDeviceX11::do_fade(VFrame *output_temp, float fade)
595 this->output->mwindow->playback_3d->do_fade(this->output, output_temp, fade);
598 bool VDeviceX11::can_mask(int64_t start_position_project, MaskAutos *keyframe_set)
601 MaskAuto *keyframe = (MaskAuto*)keyframe_set->
602 get_prev_auto(start_position_project, PLAY_FORWARD, current);
603 return keyframe->disable_opengl_masking ? 0 : 1;
606 void VDeviceX11::do_mask(VFrame *output_temp, int64_t start_position_project,
607 MaskAutos *keyframe_set, MaskAuto *keyframe, MaskAuto *default_auto)
609 this->output->mwindow->playback_3d->do_mask(output, output_temp,
610 start_position_project, keyframe_set, keyframe, default_auto);
613 void VDeviceX11::overlay(VFrame *output_frame, VFrame *input,
614 float in_x1, float in_y1, float in_x2, float in_y2,
615 float out_x1, float out_y1, float out_x2, float out_y2,
616 float alpha, int mode, EDL *edl, int is_nested)
618 int interpolation_type = edl->session->interpolation_type;
619 output->mwindow->playback_3d->overlay(output, input,
620 in_x1, in_y1, in_x2, in_y2,
621 out_x1, out_y1, out_x2, out_y2, alpha, // 0 - 1
622 mode, interpolation_type, output_frame, is_nested);
625 void VDeviceX11::run_plugin(PluginClient *client)
627 output->mwindow->playback_3d->run_plugin(output, client);
630 void VDeviceX11::copy_frame(VFrame *dst, VFrame *src)
632 output->mwindow->playback_3d->copy_from(output, dst, src, 1);