Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_canvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_canvas.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "renderer_canvas.h"
34 #include "workarea.h"
35 #include <ETL/misc>
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 Renderer_Canvas::~Renderer_Canvas()
55 {
56 }
57 std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >&
58 Renderer_Canvas::get_tile_book()
59 {
60         return get_work_area()->get_tile_book();
61 }
62
63 bool
64 Renderer_Canvas::get_full_frame()const
65 {
66         return get_work_area()->get_full_frame();
67 }
68
69 int Renderer_Canvas::get_refreshes()const
70 {
71         return get_work_area()->get_refreshes();
72 }
73
74 bool
75 Renderer_Canvas::get_canceled()const
76 {
77         return get_work_area()->get_canceled();
78 }
79
80 bool
81 Renderer_Canvas::get_queued()const
82 {
83         return get_work_area()->get_queued();
84 }
85
86 bool
87 Renderer_Canvas::get_rendering()const
88 {
89         return get_work_area()->get_rendering();
90 }
91
92 void
93 Renderer_Canvas::render_vfunc(
94         const Glib::RefPtr<Gdk::Drawable>& drawable,
95         const Gdk::Rectangle& /*expose_area*/
96 )
97 {
98         assert(get_work_area());
99         if(!get_work_area())
100                 return;
101
102 //      const synfig::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
103
104         const synfig::Vector focus_point(get_work_area()->get_focus_point());
105
106         std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
107
108         int drawable_w,drawable_h;
109         drawable->get_size(drawable_w,drawable_h);
110
111         // Calculate the window coordinates of the top-left
112         // corner of the canvas.
113         const synfig::Vector::value_type
114                 x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
115                 y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
116
117         /*const synfig::Vector::value_type window_startx(window_tl[0]);
118         const synfig::Vector::value_type window_endx(window_br[0]);
119         const synfig::Vector::value_type window_starty(window_tl[1]);
120         const synfig::Vector::value_type window_endy(window_br[1]);
121         */
122         const int
123                 tile_w(get_work_area()->get_tile_w()),
124                 tile_h(get_work_area()->get_tile_h());
125
126         const int
127                 w(get_w()),
128                 h(get_h());
129
130         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
131
132         if(!tile_book.empty())
133         if(get_full_frame())
134         {
135                 if(tile_book[0].first)
136                 {
137                         drawable->draw_pixbuf(
138                                 gc, //GC
139                                 tile_book[0].first, //pixbuf
140                                 0, 0,   // Source X and Y
141                                 round_to_int(x),round_to_int(y),        // Dest X and Y
142                                 -1,-1,  // Width and Height
143                                 Gdk::RGB_DITHER_MAX,            // RgbDither
144                                 2, 2 // Dither offset X and Y
145                         );
146                 }
147                 if(tile_book[0].second!=get_refreshes() && get_canceled()==false && get_rendering()==false && get_queued()==false)
148                         get_work_area()->async_update_preview();
149         }
150         else
151         {
152                 const int width_in_tiles(w/tile_w+(w%tile_w?1:0));
153                 const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
154
155                 int u(0),v(0),tx,ty;
156                 int u1(0),v1(0),u2(width_in_tiles), v2(height_in_tiles);
157
158                 bool needs_refresh(false);
159
160                 u1=int(-x/tile_w);
161                 v1=int(-y/tile_h);
162                 u2=int((-x+drawable_w)/tile_w+1);
163                 v2=int((-y+drawable_h)/tile_h+1);
164                 if(u2>width_in_tiles)u2=width_in_tiles;
165                 if(v2>height_in_tiles)v2=height_in_tiles;
166                 if(u1<0)u1=0;
167                 if(v1<0)v1=0;
168
169                 for(v=v1;v<v2;v++)
170                 {
171                         for(u=u1;u<u2;u++)
172                         {
173                                 int index=v*width_in_tiles+u;
174                                 if(int(tile_book.size())>index && tile_book[index].first)
175                                 {
176                                         tx=u*tile_w;
177                                         ty=v*tile_w;
178
179                                         drawable->draw_pixbuf(
180                                                 gc, //GC
181                                                 tile_book[index].first, //pixbuf
182                                                 0, 0,   // Source X and Y
183                                                 round_to_int(x)+tx,round_to_int(y)+ty,  // Dest X and Y
184                                                 -1,-1,  // Width and Height
185                                                 Gdk::RGB_DITHER_MAX,            // RgbDither
186                                                 2, 2 // Dither offset X and Y
187                                         );
188                                 }
189                                 if(tile_book[index].second!=get_refreshes())
190                                         needs_refresh=true;
191                         }
192                 }
193                 if(needs_refresh==true && get_canceled()==false && get_rendering()==false && get_queued()==false)
194                 {
195                         //queue_render_preview();
196                         get_work_area()->async_update_preview();
197                         //update_preview();
198                         //return true;
199                 }
200
201         }
202
203         // Draw the border around the rendered region
204         {
205                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
206                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
207                 drawable->draw_rectangle(
208                         gc,
209                         false,  // Fill?
210                         round_to_int(x),round_to_int(y),        // x,y
211                         w,h     //w,h
212                 );
213         }
214 }