summaryrefslogtreecommitdiff
path: root/source/XMP_ProgressTracker.cpp
blob: 0a66d5583d604b583b226196684eb7dfd3eea7b9 (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
// =================================================================================================
// Copyright Adobe
// Copyright 2012 Adobe
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"

#include "source/XMP_LibUtils.hpp"
#include "source/XMP_ProgressTracker.hpp"

// =================================================================================================
// XMP_ProgressTracker::XMP_ProgressTracker
// ========================================

XMP_ProgressTracker::XMP_ProgressTracker ( const CallbackInfo & _cbInfo )
{

	this->Clear();
	if ( _cbInfo.clientProc == 0 ) return;
	XMP_Assert ( _cbInfo.wrapperProc != 0 );
	
	this->cbInfo = _cbInfo;
	if ( this->cbInfo.interval < 0.0 ) this->cbInfo.interval = 1.0;

}	// XMP_ProgressTracker::XMP_ProgressTracker

// =================================================================================================
// XMP_ProgressTracker::BeginWork
// ==============================

void XMP_ProgressTracker::BeginWork ( float _totalWork )
{

	if ( _totalWork < 0.0 ) _totalWork = 0.0;
	this->totalWork = _totalWork;
	this->workDone = 0.0;
	this->workInProgress = true;

	this->startTime = this->prevTime = PerfUtils::NoteThisMoment();
	if ( this->cbInfo.sendStartStop ) this->NotifyClient ( true );

}	// XMP_ProgressTracker::BeginWork

// =================================================================================================
// XMP_ProgressTracker::AddTotalWork
// =================================

void XMP_ProgressTracker::AddTotalWork ( float workIncrement )
{

	if ( workIncrement < 0.0 ) workIncrement = 0.0;
	this->totalWork += workIncrement;
	
}	// XMP_ProgressTracker::AddTotalWork

// =================================================================================================
// XMP_ProgressTracker::AddWorkDone
// ================================

void XMP_ProgressTracker::AddWorkDone ( float workIncrement )
{

	if ( workIncrement < 0.0 ) workIncrement = 0.0;
	this->workDone += workIncrement;
	this->NotifyClient();
	
}	// XMP_ProgressTracker::AddWorkDone

// =================================================================================================
// XMP_ProgressTracker::WorkComplete
// =================================

void XMP_ProgressTracker::WorkComplete ()
{

	if ( this->totalWork == 0.0 ) this->totalWork = 1.0;	// Force non-zero fraction done.
	this->workDone = this->totalWork;
	XMP_Assert ( this->workDone > 0.0 );	// Needed in NotifyClient for start/stop case.

	this->NotifyClient ( this->cbInfo.sendStartStop );
	this->workInProgress = false;

}	// XMP_ProgressTracker::WorkComplete

// =================================================================================================
// XMP_ProgressTracker::Clear
// ==========================

void XMP_ProgressTracker::Clear ()
{

	this->cbInfo.Clear();
	this->workInProgress = false;
	this->totalWork = 0.0;
	this->workDone = 0.0;
	this->startTime = this->prevTime = PerfUtils::kZeroMoment;
	// ! There is no standard zero value for PerfUtils::MomentValue.

}	// XMP_ProgressTracker::Clear

// =================================================================================================
// XMP_ProgressTracker::NotifyClient
// =================================
//
// The math for the time remaining is easy but not immediately obvious. We know the elapsed time and
// fraction of work done:
//
//		elapsedTime = totalTime * fractionDone		or		totalTime = (elapsedTime / fractionDone)
//		remainingTime = totalTime * (1 - fractionDone)
// so:
//		remainingTime = (elapsedTime / fractionDone) * (1 - fractionDone)

void XMP_ProgressTracker::NotifyClient ( bool isStartStop )
{
	XMP_Bool ok = !kXMP_Bool_False;
	float fractionDone = 0.0;
	
	if ( this->cbInfo.clientProc == 0 ) return;
	XMP_Assert ( this->cbInfo.wrapperProc != 0 );
	XMP_Assert ( (this->totalWork >= 0.0) && (this->workDone >= 0.0) && (this->cbInfo.interval >= 0.0) );
	// ! Note that totalWork might be unknown or understimated, and workDone greater than totalWork.
	
	if ( isStartStop ) {

		float totalTime = 0.0;
		if ( this->workDone > 0.0 ) {
			fractionDone = 1.0;	// This is the stop call.
			totalTime = (float)PerfUtils::GetElapsedSeconds ( this->startTime, PerfUtils::NoteThisMoment() );
		}
		ok = (*this->cbInfo.wrapperProc ) ( this->cbInfo.clientProc, this->cbInfo.context,
											totalTime, fractionDone, 0.0 );

	} else {

		PerfUtils::MomentValue currentTime = PerfUtils::NoteThisMoment();
		float elapsedTime =(float) PerfUtils::GetElapsedSeconds ( this->prevTime, currentTime );
		if ( elapsedTime < this->cbInfo.interval ) return;

		float remainingTime = 0.0;
		if ( (this->totalWork > 0.0) && (this->workDone > 0.0) ) {
			fractionDone = this->workDone / this->totalWork;
			if ( fractionDone > 1.0 ) fractionDone = 1.0;
			elapsedTime =(float) PerfUtils::GetElapsedSeconds ( this->startTime, currentTime );
			remainingTime = (float)((elapsedTime / fractionDone) * (1.0F - fractionDone));
		}

		this->prevTime = currentTime;
		ok = (*this->cbInfo.wrapperProc ) ( this->cbInfo.clientProc, this->cbInfo.context,
											elapsedTime, fractionDone, remainingTime );

	}

	if ( ok == kXMP_Bool_False ) XMP_Throw ( "Abort signaled by progress reporting callback", kXMPErr_ProgressAbort );
		
}	// XMP_ProgressTracker::NotifyClient

// =================================================================================================