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