2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef __UTIL_PROGRESS_H__
20 #define __UTIL_PROGRESS_H__
24 * Simple class to communicate progress status messages, timing information,
25 * update notifications from a job running in another thread. All methods
26 * except for the constructor/destructor are thread safe. */
28 #include "util_function.h"
29 #include "util_string.h"
30 #include "util_time.h"
31 #include "util_thread.h"
41 start_time = time_dt();
44 status = "Initializing";
52 Progress(Progress& progress)
57 Progress& operator=(Progress& progress)
59 thread_scoped_lock lock(progress.progress_mutex);
61 progress.get_status(status, substatus);
62 progress.get_tile(tile, total_time, tile_time);
64 sample = progress.get_sample();
70 void set_cancel(const string& cancel_message_)
72 thread_scoped_lock lock(progress_mutex);
73 cancel_message = cancel_message_;
79 if(!cancel && cancel_cb)
85 string get_cancel_message()
87 thread_scoped_lock lock(progress_mutex);
88 return cancel_message;
91 void set_cancel_callback(boost::function<void(void)> function)
96 /* tile and timing information */
98 void set_start_time(double start_time_)
100 thread_scoped_lock lock(progress_mutex);
102 start_time = start_time_;
105 void set_tile(int tile_, double tile_time_)
107 thread_scoped_lock lock(progress_mutex);
110 total_time = time_dt() - start_time;
111 tile_time = tile_time_;
114 void get_tile(int& tile_, double& total_time_, double& tile_time_)
116 thread_scoped_lock lock(progress_mutex);
119 total_time_ = (total_time > 0.0)? total_time: 0.0;
120 tile_time_ = tile_time;
125 thread_scoped_lock lock(progress_mutex);
130 void increment_sample()
132 thread_scoped_lock lock(progress_mutex);
142 /* status messages */
144 void set_status(const string& status_, const string& substatus_ = "")
147 thread_scoped_lock lock(progress_mutex);
149 substatus = substatus_;
150 total_time = time_dt() - start_time;
156 void set_substatus(const string& substatus_)
159 thread_scoped_lock lock(progress_mutex);
160 substatus = substatus_;
161 total_time = time_dt() - start_time;
167 void get_status(string& status_, string& substatus_)
169 thread_scoped_lock lock(progress_mutex);
171 substatus_ = substatus;
179 thread_scoped_lock lock(update_mutex);
184 void set_update_callback(boost::function<void(void)> function)
186 update_cb = function;
190 thread_mutex progress_mutex;
191 thread_mutex update_mutex;
192 boost::function<void(void)> update_cb;
193 boost::function<void(void)> cancel_cb;
195 int tile; /* counter for rendered tiles */
196 int sample; /* counter of rendered samples, global for all tiles */
205 volatile bool cancel;
206 string cancel_message;
211 #endif /* __UTIL_PROGRESS_H__ */