1 /* === S Y N F I G ========================================================= */
3 ** \brief Preview implementation file
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
35 #include "audiocontainer.h"
36 #include <gtkmm/stock.h>
37 #include <gtkmm/separator.h>
39 #include <synfig/target_scanline.h>
40 #include <synfig/surface.h>
43 #include "asyncrenderer.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
51 using namespace studio;
53 /* === M A C R O S ========================================================= */
55 /* === G L O B A L S ======================================================= */
57 /* === P R O C E D U R E S ================================================= */
59 /* === M E T H O D S ======================================================= */
61 /* === E N T R Y P O I N T ================================================= */
63 class studio::Preview::Preview_Target : public Target_Scanline
67 sigc::signal<void, const Preview_Target *> signal_frame_done_;
82 nframes = curframe = 0;
85 const RendDesc &get_rend_desc() const { return desc; }
87 virtual bool set_rend_desc(RendDesc *r)
89 if(Target_Scanline::set_rend_desc(r))
91 /*synfig::warning("Succeeded in setting the desc to new one: %d x %d, %.2f fps [%.2f,%.2f]",
92 desc.get_w(),desc.get_h(),desc.get_frame_rate(),
93 (float)desc.get_time_start(),(float)desc.get_time_end());*/
95 surface.set_wh(desc.get_w(),desc.get_h());
98 nframes = (int)floor((desc.get_time_end() - desc.get_time_start())*desc.get_frame_rate());
100 tbegin = desc.get_time_start();
101 tend = tbegin + nframes/desc.get_frame_rate();
108 virtual bool start_frame(ProgressCallback */*cb*/=NULL)
113 virtual void end_frame()
115 //ok... notify our subscribers...
116 signal_frame_done_(this);
118 //synfig::warning("Finished the frame stuff, and changed time to %.3f",t);
121 virtual Color * start_scanline(int scanline)
123 return surface[scanline];
126 virtual bool end_scanline() {return true;}
128 sigc::signal<void, const Preview_Target *> &signal_frame_done() {return signal_frame_done_;}
130 const Surface &get_surface() const {return surface;}
132 float get_time() const
134 double time = ((nframes-curframe)/(double)nframes)*tbegin
135 + ((curframe)/(double)nframes)*tend;
140 studio::Preview::Preview(const studio::CanvasView::LooseHandle &h, float zoom, float f)
141 :canvasview(h),zoom(zoom),fps(f)
147 void studio::Preview::set_canvasview(const studio::CanvasView::LooseHandle &h)
153 //perhaps reset override values...
154 const RendDesc &r = canvasview->get_canvas()->rend_desc();
155 if(r.get_frame_rate())
157 float rate = 1/r.get_frame_rate();
158 overbegin = false; begintime = r.get_time_start() + r.get_frame_start()*rate;
159 overend = false; endtime = r.get_time_start() + r.get_frame_end()*rate;
164 studio::Preview::~Preview()
166 signal_destroyed_(this); //tell anything that attached to us, we're dying
169 void studio::Preview::render()
173 //render using the preview target
174 etl::handle<Preview_Target> target = new Preview_Target;
176 //connect our information to his...
177 //synfig::warning("Connecting to the end frame function...");
178 target->signal_frame_done().connect(sigc::mem_fun(*this,&Preview::frame_finish));
181 //synfig::warning("Setting Canvas");
182 target->set_canvas(get_canvas());
183 target->set_quality(quality);
186 RendDesc desc = get_canvas()->rend_desc();
188 //set the global fps of the preview
189 set_global_fps(desc.get_frame_rate());
193 int neww = (int)floor(desc.get_w()*zoom+0.5),
194 newh = (int)floor(desc.get_h()*zoom+0.5);
197 /*synfig::warning("Setting the render description: %d x %d, %f fps, [%f,%f]",
198 neww,newh,newfps, overbegin?begintime:(float)desc.get_time_start(),
199 overend?endtime:(float)desc.get_time_end());*/
203 desc.set_frame_rate(newfps);
207 desc.set_time_start(std::max(begintime,(float)desc.get_time_start()));
208 //synfig::warning("Set start time to %.2f...",(float)desc.get_time_start());
212 desc.set_time_end(std::min(endtime,(float)desc.get_time_end()));
213 //synfig::warning("Set end time to %.2f...",(float)desc.get_time_end());
216 //setting the description
218 //HACK - add on one extra frame because the renderer can't render the last frame
219 desc.set_time_end(desc.get_time_end() + 1.000001/fps);
221 target->set_rend_desc(&desc);
223 //... first we must clear our current selves of space
226 //now tell it to go... with inherited prog. reporting...
227 //synfig::info("Rendering Asynchronously...");
228 if(renderer) renderer->stop();
229 renderer = new AsyncRenderer(target);
234 static void free_guint8(const guint8 *mem)
239 void studio::Preview::frame_finish(const Preview_Target *targ)
241 //copy image with time to next frame (can just push back)
243 float time = targ->get_time();
244 const Surface &surf = targ->get_surface();
245 const RendDesc& r = targ->get_rend_desc();
247 //synfig::warning("Finished a frame at %f s",time);
250 PixelFormat pf(PF_RGB);
251 const int total_bytes(r.get_w()*r.get_h()*synfig::channels(pf));
253 //synfig::warning("Creating a buffer");
254 unsigned char *buffer((unsigned char*)malloc(total_bytes));
259 //convert all the pixels to the pixbuf... buffer... thing...
260 //synfig::warning("Converting...");
261 convert_color_format(buffer, surf[0], surf.get_w()*surf.get_h(), pf, App::gamma);
265 //uses and manages the memory for the buffer...
266 //synfig::warning("Create a pixmap...");
268 Gdk::Pixbuf::create_from_data(
269 buffer, // pointer to the data
270 Gdk::COLORSPACE_RGB, // the colorspace
271 ((pf&PF_A)==PF_A), // has alpha?
272 8, // bits per sample
273 surf.get_w(), // width
274 surf.get_h(), // height
275 surf.get_w()*synfig::channels(pf), // stride (pitch)
276 sigc::ptr_fun(free_guint8)
279 //add the flipbook element to the list (assume time is correct)
280 //synfig::info("Prev: Adding %f s to the list", time);
281 frames.push_back(fe);
286 #define IMAGIFY_BUTTON(button,stockid,tooltip) \
287 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON)); \
288 button->add(*icon); \
289 tooltips.set_tip(*button,tooltip); \
290 icon->set_padding(0,0);\
293 Widget_Preview::Widget_Preview()
294 :Gtk::Table(5,5,false),
295 adj_time_scrub(0,0,1000,1,10,0),
296 scr_time_scrub(adj_time_scrub),
297 b_loop(/*_("Loop")*/),
304 //connect to expose events
305 //signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Preview::redraw));
307 //manage all the change in values etc...
308 adj_time_scrub.signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Preview::slider_move));
309 scr_time_scrub.signal_event().connect(sigc::mem_fun(*this,&Widget_Preview::scroll_move_event));
310 draw_area.signal_expose_event().connect(sigc::mem_fun(*this,&Widget_Preview::redraw));
312 disp_sound.set_time_adjustment(&adj_sound);
315 //Set up signals to modify time value as it should be...
316 disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
317 disp_sound.signal_scrub().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
320 ---------------------------------
328 ---------------------------------
329 |loop|play|stop | hbox
330 |lastl|lastt|rerender|haltrend | hbox
336 Gtk::Button *button = 0;
337 Gtk::Image *icon = 0;
339 //should set up the dialog using attach etc.
340 attach(draw_area, 0, 1, 0, 1);
341 attach(scr_time_scrub, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
346 hbox = manage(new Gtk::HBox);
349 IMAGIFY_BUTTON(button,Gtk::Stock::REFRESH,"Toggle Looping");
350 hbox->pack_start(b_loop,Gtk::PACK_SHRINK,0);
351 //attach(b_loop,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
353 button = manage(new Gtk::Button(/*_("Play")*/));
354 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::play));
355 IMAGIFY_BUTTON(button,Gtk::Stock::GO_FORWARD,"Play");
356 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
357 //attach(*button,1,2,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
359 button = manage(new Gtk::Button(/*_("Stop")*/));
360 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stop));
361 IMAGIFY_BUTTON(button,Gtk::Stock::NO,"Stop");
362 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
363 //attach(*button,2,3,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
365 //attack the stop render and erase all buttons to same line...
367 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
368 hbox->pack_start(*vsep,Gtk::PACK_SHRINK,0);
371 button = manage(new Gtk::Button(/*_("Halt Render")*/));
372 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stoprender));
373 IMAGIFY_BUTTON(button,Gtk::Stock::STOP,"Halt Render");
374 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
375 //attach(*button,2,3,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
377 button = manage(new Gtk::Button(/*_("Re-Preview")*/));
378 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::repreview));
379 IMAGIFY_BUTTON(button,Gtk::Stock::CONVERT,"Re-Preview");
380 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
381 //attach(*button,0,2,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
383 button = manage(new Gtk::Button(/*_("Erase All")*/));
384 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::eraseall));
385 IMAGIFY_BUTTON(button,Gtk::Stock::DELETE,"Erase All");
386 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
387 //attach(*button,2,3,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
390 attach(*hbox,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
393 hbox = manage(new Gtk::HBox);
395 Gtk::Label *label = manage(new Gtk::Label("Last Rendered: "));
397 hbox->pack_start(*label,Gtk::PACK_SHRINK,10);
398 //attach(*manage(new Gtk::Label("Last Rendered: ")),0,1,3,4,Gtk::SHRINK,Gtk::SHRINK);
401 hbox->pack_start(l_lasttime,Gtk::PACK_SHRINK,0);
403 attach(*hbox,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
404 //attach(l_lasttime,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
407 disp_sound.set_size_request(-1,32);
408 attach(disp_sound,0,1,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
412 //if(draw_area.get_window()) gc_area = Gdk::GC::create(draw_area.get_window());
416 studio::Widget_Preview::~Widget_Preview()
420 void studio::Widget_Preview::update()
422 //the meat goes in this locker...
423 double time = adj_time_scrub.get_value();
425 //find the frame and display it...
428 //synfig::warning("Updating at %.3f s",time);
430 //use time to find closest frame...
431 studio::Preview::FlipBook::const_iterator beg = preview->begin(),end = preview->end();
432 studio::Preview::FlipBook::const_iterator i;
436 //go to current hint if need be...
437 if(currentindex >= 0 && currentindex < (int)preview->numframes())
439 i = beg+currentindex;
442 //we can't have a picture if there are none to get
445 //don't bother with binary search it will just be slower...
447 //synfig::info("Search for time %f",time);
449 //incrementally go in either direction
450 //(bias downward towards beg, because that's what we want)
453 //synfig::info("Look at %f",i->t);
454 if(i->t > time) break;
455 //synfig::info("Go past...");
460 //bias down, so we can't be at end... and it still is valid...
464 //synfig::info("Look at %f",i->t);
465 if(i->t <= time) break;
466 //synfig::info("Go past...");
469 /*i = preview->begin(); end = preview->end();
473 for(;i != end; j = i++)
475 if(i->t > time) break;
478 //we should be at a valid edge since we biased downward
480 //don't get the closest, round down... (if we can)
483 synfig::error("i == end....");
491 currentindex = i-beg;
495 //synfig::warning("Update at: %f seconds (%f s)",time,timedisp);
497 //synfig::warning("success!");
503 if(disp_sound.get_profile() && adj_sound.get_value() != time)
507 //Set the position of the sound (short circuited for sound modifying the time)
509 disp_sound.set_position(time);
510 disp_sound.queue_draw();
513 void studio::Widget_Preview::preview_draw()
515 draw_area.queue_draw();//on_expose_event();
518 bool studio::Widget_Preview::redraw(GdkEventExpose */*heh*/)
520 //And render the drawing area
521 Glib::RefPtr<Gdk::Pixbuf> pxnew, px = currentbuf;
523 if(!px || draw_area.get_height() == 0
524 || px->get_height() == 0 || px->get_width() == 0 /*|| is_visible()*/) //made not need this line
527 //figure out the scaling factors...
531 sx = draw_area.get_width() / (float)px->get_width();
532 sy = draw_area.get_height() / (float)px->get_height();
534 //synfig::info("widget_preview redraw: now to scale the bitmap: %.3f x %.3f",sx,sy);
536 //round to smallest scale (fit entire thing in window without distortion)
540 //scale to a new pixmap and then copy over to the window
541 nw = (int)(px->get_width()*sx);
542 nh = (int)(px->get_height()*sx);
544 if(nw == 0 || nh == 0)return true;
546 pxnew = px->scale_simple(nw,nh,Gdk::INTERP_NEAREST);
548 //synfig::info("Now to draw to the window...");
550 Glib::RefPtr<Gdk::Window> wind = draw_area.get_window();
551 Glib::RefPtr<Gdk::Drawable> surf = Glib::RefPtr<Gdk::Drawable>::cast_static(wind);
552 Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(wind);
555 Gdk::Rectangle r(0,0,draw_area.get_width(),draw_area.get_height());
556 draw_area.get_window()->begin_paint_rect(r);
559 if(!wind) synfig::warning("The destination window is broken...");
560 if(!surf) synfig::warning("The destination is not drawable...");
564 /* Options for drawing...
565 1) store with alpha, then clear and render with alpha every frame
566 - more time consuming
568 2) store with just pixel info
571 + better memory footprint
573 //px->composite(const Glib::RefPtr<Gdk::Pixbuf>& dest, int dest_x, int dest_y, int dest_width, int dest_height, double offset_x, double offset_y, double scale_x, double scale_y, InterpType interp_type, int overall_alpha) const
578 0, 0, // Source X and Y
579 0, 0, // Dest X and Y
580 -1,-1, // Width and Height
581 Gdk::RGB_DITHER_NONE, // RgbDither
582 0, 0 // Dither offset X and Y
587 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
588 Glib::ustring timecode(Time((double)timedisp).round(preview->get_global_fps())
589 .get_string(preview->get_global_fps(),
590 App::get_time_format()));
591 //synfig::info("Time for preview draw is: %s for time %g", timecode.c_str(), adj_time_scrub.get_value());
593 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
594 layout->set_text(timecode);
595 surf->draw_layout(gc,4,4,layout);
599 draw_area.get_window()->end_paint();
601 //synfig::warning("Refresh the draw area");
602 //make sure the widget refreshes
607 bool studio::Widget_Preview::play_update()
609 float diff = timer.pop_time();
610 //synfig::info("Play update: diff = %.2f",diff);
614 //we go to the next one...
615 double time = adj_time_scrub.get_value() + diff;
617 //adjust it to be synced with the audio if it can...
619 double newtime = audiotime;
620 if(audio && audio->is_playing()) audio->get_current_time(newtime);
622 if(newtime != audiotime)
624 //synfig::info("Adjusted time from %.3lf to %.3lf", time,newtime);
625 time = audiotime = newtime;
629 //Looping conditions...
630 if(time >= adj_time_scrub.get_upper())
634 time = adj_time_scrub.get_lower();// + time-adj_time_scrub.get_upper();
638 time = adj_time_scrub.get_upper();
639 adj_time_scrub.set_value(time);
643 //synfig::info("Play Stopped: time set to %f",adj_time_scrub.get_value());
648 //set the new time...
649 adj_time_scrub.set_value(time);
650 adj_time_scrub.value_changed();
652 //update the window to the correct image we might want to do this later...
654 //synfig::warning("Did update pu");
659 void studio::Widget_Preview::slider_move()
664 //synfig::warning("Did update sm");
668 //for other things updating the value changed signal...
669 void studio::Widget_Preview::scrub_updated(double t)
673 //Attempt at being more accurate... the time is adjusted to be exactly where the sound says it is
677 if(!audio->isPaused())
679 audio->get_current_time(t);
683 //synfig::info("Scrubbing to %.3f, setting adj to %.3f",oldt,t);
685 if(adj_time_scrub.get_value() != t)
687 adj_time_scrub.set_value(t);
688 adj_time_scrub.value_changed();
692 void studio::Widget_Preview::disconnect_preview(Preview *prev)
697 prevchanged.disconnect();
701 void studio::Widget_Preview::set_preview(handle<Preview> prev)
705 synfig::info("Setting preview");
707 //stop playing the mini animation...
712 //set the internal values
713 float rate = preview->get_fps();
714 synfig::info(" FPS = %f",rate);
717 float start = preview->get_begintime();
718 float end = preview->get_endtime();
722 adj_time_scrub.set_lower(start);
723 adj_time_scrub.set_upper(end);
724 adj_time_scrub.set_value(start);
725 adj_time_scrub.set_step_increment(rate);
726 adj_time_scrub.set_page_increment(10*rate);
728 //if the begin time and the end time are the same there is only a single frame
729 singleframe = end==start;
732 adj_time_scrub.set_lower(0);
733 adj_time_scrub.set_upper(0);
734 adj_time_scrub.set_value(0);
735 adj_time_scrub.set_step_increment(0);
736 adj_time_scrub.set_page_increment(0);
740 //connect so future information will be found...
741 prevchanged = prev->signal_changed().connect(sigc::mem_fun(*this,&Widget_Preview::whenupdated));
742 prev->signal_destroyed().connect(sigc::mem_fun(*this,&Widget_Preview::disconnect_preview));
744 //synfig::warning("Did update sp");
749 void studio::Widget_Preview::whenupdated()
751 l_lasttime.set_text((Time((double)(--preview->end())->t)
752 .round(preview->get_global_fps())
753 .get_string(preview->get_global_fps(),App::get_time_format())));
757 void studio::Widget_Preview::clear()
760 prevchanged.disconnect();
763 void studio::Widget_Preview::play()
765 if(preview && !playing)
767 //synfig::info("Playing at %lf",adj_time_scrub.get_value());
768 //audiotime = adj_time_scrub.get_value();
771 //adj_time_scrub.set_value(adj_time_scrub.get_lower());
772 update(); //we don't want to call play update because that will try to advance the timer
773 //synfig::warning("Did update p");
775 //approximate length of time in seconds, right?
776 double rate = /*std::min(*/adj_time_scrub.get_step_increment()/*,1/30.0)*/;
777 int timeout = (int)floor(1000*rate);
779 //synfig::info(" rate = %.3lfs = %d ms",rate,timeout);
781 signal_play_(adj_time_scrub.get_value());
784 if(audio) audio->play(adj_time_scrub.get_value());
786 timecon = Glib::signal_timeout().connect(sigc::mem_fun(*this,&Widget_Preview::play_update),timeout);
792 void studio::Widget_Preview::play_stop()
796 if(audio) audio->stop(); //!< stop the audio
797 //synfig::info("Stopping...");
800 void studio::Widget_Preview::stop()
802 //synfig::warning("stopping");
804 timecon.disconnect();
807 bool studio::Widget_Preview::scroll_move_event(GdkEvent *event)
811 case GDK_BUTTON_PRESS:
813 if(event->button.button == 1 || event->button.button == 3)
825 void studio::Widget_Preview::set_audioprofile(etl::handle<AudioProfile> p)
827 disp_sound.set_profile(p);
830 void studio::Widget_Preview::set_audio(etl::handle<AudioContainer> a)
834 //disconnect any previous signals
835 scrstartcon.disconnect(); scrstopcon.disconnect(); scrubcon.disconnect();
837 //connect the new signals
838 scrstartcon = disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::start_scrubbing));
839 scrstopcon = disp_sound.signal_stop_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::stop_scrubbing));
840 scrubcon = disp_sound.signal_scrub().connect(sigc::mem_fun(*a,&AudioContainer::scrub));
843 void studio::Widget_Preview::seek(float t)
846 adj_time_scrub.set_value(t);
849 void studio::Widget_Preview::repreview()
855 preview->get_canvasview()->preview_option();
859 void studio::Widget_Preview::stoprender()
862 if (preview->renderer->updating)
863 preview->renderer->stop();
865 preview->renderer.detach();
868 void studio::Widget_Preview::eraseall()