Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util_progress.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ff48630a813f9cd0f7c8f10b537d3b6b10b4195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright 2011-2013 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __UTIL_PROGRESS_H__
#define __UTIL_PROGRESS_H__

/* Progress
 *
 * Simple class to communicate progress status messages, timing information,
 * update notifications from a job running in another thread. All methods
 * except for the constructor/destructor are thread safe. */

#include "util_function.h"
#include "util_string.h"
#include "util_time.h"
#include "util_thread.h"

CCL_NAMESPACE_BEGIN

class Progress {
public:
	Progress()
	{
		tile = 0;
		sample = 0;
		start_time = time_dt();
		total_time = 0.0f;
		render_time = 0.0f;
		tile_time = 0.0f;
		status = "Initializing";
		substatus = "";
		sync_status = "";
		sync_substatus = "";
		update_cb = NULL;
		cancel = false;
		cancel_message = "";
		error = false;
		error_message = "";
		cancel_cb = NULL;
	}

	Progress(Progress& progress)
	{
		*this = progress;
	}

	Progress& operator=(Progress& progress)
	{
		thread_scoped_lock lock(progress.progress_mutex);

		progress.get_status(status, substatus);
		progress.get_tile(tile, total_time, render_time, tile_time);

		sample = progress.get_sample();

		return *this;
	}

	void reset()
	{
		tile = 0;
		sample = 0;
		start_time = time_dt();
		render_start_time = time_dt();
		total_time = 0.0f;
		render_time = 0.0f;
		tile_time = 0.0f;
		status = "Initializing";
		substatus = "";
		sync_status = "";
		sync_substatus = "";
		cancel = false;
		cancel_message = "";
		error = false;
		error_message = "";
	}

	/* cancel */
	void set_cancel(const string& cancel_message_)
	{
		thread_scoped_lock lock(progress_mutex);
		cancel_message = cancel_message_;
		cancel = true;
	}

	bool get_cancel()
	{
		if(!cancel && cancel_cb)
			cancel_cb();

		return cancel;
	}

	string get_cancel_message()
	{
		thread_scoped_lock lock(progress_mutex);
		return cancel_message;
	}

	void set_cancel_callback(boost::function<void(void)> function)
	{
		cancel_cb = function;
	}

	/* error */
	void set_error(const string& error_message_)
	{
		thread_scoped_lock lock(progress_mutex);
		error_message = error_message_;
		error = true;
		/* If error happens we also stop rendering. */
		cancel_message = error_message_;
		cancel = true;
	}

	bool get_error()
	{
		return error;
	}

	string get_error_message()
	{
		thread_scoped_lock lock(progress_mutex);
		return error_message;
	}

	/* tile and timing information */

	void set_start_time(double start_time_)
	{
		thread_scoped_lock lock(progress_mutex);

		start_time = start_time_;
	}

	void set_render_start_time(double render_start_time_)
	{
		thread_scoped_lock lock(progress_mutex);

		render_start_time = render_start_time_;
	}

	void set_tile(int tile_, double tile_time_)
	{
		thread_scoped_lock lock(progress_mutex);

		tile = tile_;
		total_time = time_dt() - start_time;
		render_time = time_dt() - render_start_time;
		tile_time = tile_time_;
	}

	void get_tile(int& tile_, double& total_time_, double& render_time_, double& tile_time_)
	{
		thread_scoped_lock lock(progress_mutex);

		tile_ = tile;
		total_time_ = (total_time > 0.0)? total_time: 0.0;
		render_time_ = (render_time > 0.0)? render_time: 0.0;
		tile_time_ = tile_time;
	}

	void reset_sample()
	{
		thread_scoped_lock lock(progress_mutex);

		sample = 0;
	}

	void increment_sample()
	{
		thread_scoped_lock lock(progress_mutex);

		sample++;
	}

	void increment_sample_update()
	{
		increment_sample();
		set_update();
	}

	int get_sample()
	{
		return sample;
	}

	/* status messages */

	void set_status(const string& status_, const string& substatus_ = "")
	{
		{
			thread_scoped_lock lock(progress_mutex);
			status = status_;
			substatus = substatus_;
			total_time = time_dt() - start_time;
			render_time = time_dt() - render_start_time;
		}

		set_update();
	}

	void set_substatus(const string& substatus_)
	{
		{
			thread_scoped_lock lock(progress_mutex);
			substatus = substatus_;
			total_time = time_dt() - start_time;
			render_time = time_dt() - render_start_time;
		}

		set_update();
	}

	void set_sync_status(const string& status_, const string& substatus_ = "")
	{
		{
			thread_scoped_lock lock(progress_mutex);
			sync_status = status_;
			sync_substatus = substatus_;
			total_time = time_dt() - start_time;
			render_time = time_dt() - render_start_time;
		}

		set_update();

	}

	void set_sync_substatus(const string& substatus_)
	{
		{
			thread_scoped_lock lock(progress_mutex);
			sync_substatus = substatus_;
			total_time = time_dt() - start_time;
			render_time = time_dt() - render_start_time;
		}

		set_update();
	}

	void get_status(string& status_, string& substatus_)
	{
		thread_scoped_lock lock(progress_mutex);

		if(sync_status != "") {
			status_ = sync_status;
			substatus_ = sync_substatus;
		}
		else {
			status_ = status;
			substatus_ = substatus;
		}
	}

	/* callback */

	void set_update()
	{
		if(update_cb) {
			thread_scoped_lock lock(update_mutex);
			update_cb();
		}
	}

	void set_update_callback(boost::function<void(void)> function)
	{
		update_cb = function;
	}

protected:
	thread_mutex progress_mutex;
	thread_mutex update_mutex;
	boost::function<void(void)> update_cb;
	boost::function<void(void)> cancel_cb;

	int tile;    /* counter for rendered tiles */
	int sample;  /* counter of rendered samples, global for all tiles */

	double start_time, render_start_time;
	double total_time, render_time;
	double tile_time;

	string status;
	string substatus;

	string sync_status;
	string sync_substatus;

	volatile bool cancel;
	string cancel_message;

	volatile bool error;
	string error_message;
};

CCL_NAMESPACE_END

#endif /* __UTIL_PROGRESS_H__ */